blob: 1c2bd61aa0baa3207f0429c44a53ebe8bb55d5c4 [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);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static 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 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100502#ifdef FEAT_CHANNEL
503static void f_ch_open(typval_T *argvars, typval_T *rettv);
504static void f_ch_close(typval_T *argvars, typval_T *rettv);
505static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
507#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_changenr(typval_T *argvars, typval_T *rettv);
509static void f_char2nr(typval_T *argvars, typval_T *rettv);
510static void f_cindent(typval_T *argvars, typval_T *rettv);
511static void f_clearmatches(typval_T *argvars, typval_T *rettv);
512static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_complete(typval_T *argvars, typval_T *rettv);
515static void f_complete_add(typval_T *argvars, typval_T *rettv);
516static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000517#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100518static void f_confirm(typval_T *argvars, typval_T *rettv);
519static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_cos(typval_T *argvars, typval_T *rettv);
522static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_count(typval_T *argvars, typval_T *rettv);
525static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
526static void f_cursor(typval_T *argsvars, typval_T *rettv);
527static void f_deepcopy(typval_T *argvars, typval_T *rettv);
528static void f_delete(typval_T *argvars, typval_T *rettv);
529static void f_did_filetype(typval_T *argvars, typval_T *rettv);
530static void f_diff_filler(typval_T *argvars, typval_T *rettv);
531static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
706static void f_server2client(typval_T *argvars, typval_T *rettv);
707static void f_serverlist(typval_T *argvars, typval_T *rettv);
708static void f_setbufvar(typval_T *argvars, typval_T *rettv);
709static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
710static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
711static void f_setline(typval_T *argvars, typval_T *rettv);
712static void f_setloclist(typval_T *argvars, typval_T *rettv);
713static void f_setmatches(typval_T *argvars, typval_T *rettv);
714static void f_setpos(typval_T *argvars, typval_T *rettv);
715static void f_setqflist(typval_T *argvars, typval_T *rettv);
716static void f_setreg(typval_T *argvars, typval_T *rettv);
717static void f_settabvar(typval_T *argvars, typval_T *rettv);
718static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
719static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100720#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100722#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_shellescape(typval_T *argvars, typval_T *rettv);
724static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
725static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_sin(typval_T *argvars, typval_T *rettv);
728static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_sort(typval_T *argvars, typval_T *rettv);
731static void f_soundfold(typval_T *argvars, typval_T *rettv);
732static void f_spellbadword(typval_T *argvars, typval_T *rettv);
733static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
734static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_sqrt(typval_T *argvars, typval_T *rettv);
737static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_str2nr(typval_T *argvars, typval_T *rettv);
740static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000741#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_stridx(typval_T *argvars, typval_T *rettv);
745static void f_string(typval_T *argvars, typval_T *rettv);
746static void f_strlen(typval_T *argvars, typval_T *rettv);
747static void f_strpart(typval_T *argvars, typval_T *rettv);
748static void f_strridx(typval_T *argvars, typval_T *rettv);
749static void f_strtrans(typval_T *argvars, typval_T *rettv);
750static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
751static void f_strwidth(typval_T *argvars, typval_T *rettv);
752static void f_submatch(typval_T *argvars, typval_T *rettv);
753static void f_substitute(typval_T *argvars, typval_T *rettv);
754static void f_synID(typval_T *argvars, typval_T *rettv);
755static void f_synIDattr(typval_T *argvars, typval_T *rettv);
756static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
757static void f_synstack(typval_T *argvars, typval_T *rettv);
758static void f_synconcealed(typval_T *argvars, typval_T *rettv);
759static void f_system(typval_T *argvars, typval_T *rettv);
760static void f_systemlist(typval_T *argvars, typval_T *rettv);
761static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
762static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
763static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
764static void f_taglist(typval_T *argvars, typval_T *rettv);
765static void f_tagfiles(typval_T *argvars, typval_T *rettv);
766static void f_tempname(typval_T *argvars, typval_T *rettv);
767static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_tan(typval_T *argvars, typval_T *rettv);
770static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_tolower(typval_T *argvars, typval_T *rettv);
773static void f_toupper(typval_T *argvars, typval_T *rettv);
774static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_type(typval_T *argvars, typval_T *rettv);
779static void f_undofile(typval_T *argvars, typval_T *rettv);
780static void f_undotree(typval_T *argvars, typval_T *rettv);
781static void f_uniq(typval_T *argvars, typval_T *rettv);
782static void f_values(typval_T *argvars, typval_T *rettv);
783static void f_virtcol(typval_T *argvars, typval_T *rettv);
784static void f_visualmode(typval_T *argvars, typval_T *rettv);
785static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
786static void f_winbufnr(typval_T *argvars, typval_T *rettv);
787static void f_wincol(typval_T *argvars, typval_T *rettv);
788static void f_winheight(typval_T *argvars, typval_T *rettv);
789static void f_winline(typval_T *argvars, typval_T *rettv);
790static void f_winnr(typval_T *argvars, typval_T *rettv);
791static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
792static void f_winrestview(typval_T *argvars, typval_T *rettv);
793static void f_winsaveview(typval_T *argvars, typval_T *rettv);
794static void f_winwidth(typval_T *argvars, typval_T *rettv);
795static void f_writefile(typval_T *argvars, typval_T *rettv);
796static void f_wordcount(typval_T *argvars, typval_T *rettv);
797static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000798
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
800static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
801static int get_env_len(char_u **arg);
802static int get_id_len(char_u **arg);
803static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
804static 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 +0000805#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
806#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
807 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
809static int eval_isnamec(int c);
810static int eval_isnamec1(int c);
811static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
812static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static typval_T *alloc_string_tv(char_u *string);
814static void init_tv(typval_T *varp);
815static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100816#ifdef FEAT_FLOAT
817static float_T get_tv_float(typval_T *varp);
818#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static linenr_T get_tv_lnum(typval_T *argvars);
820static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
821static char_u *get_tv_string(typval_T *varp);
822static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
823static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
824static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
825static hashtab_T *find_var_ht(char_u *name, char_u **varname);
826static funccall_T *get_funccal(void);
827static void vars_clear_ext(hashtab_T *ht, int free_val);
828static void delete_var(hashtab_T *ht, hashitem_T *hi);
829static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
830static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
831static void set_var(char_u *name, typval_T *varp, int copy);
832static int var_check_ro(int flags, char_u *name, int use_gettext);
833static int var_check_fixed(int flags, char_u *name, int use_gettext);
834static int var_check_func_name(char_u *name, int new_var);
835static int valid_varname(char_u *varname);
836static int tv_check_lock(int lock, char_u *name, int use_gettext);
837static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
838static char_u *find_option_end(char_u **arg, int *opt_flags);
839static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
840static int eval_fname_script(char_u *p);
841static int eval_fname_sid(char_u *p);
842static void list_func_head(ufunc_T *fp, int indent);
843static ufunc_T *find_func(char_u *name);
844static int function_exists(char_u *name);
845static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void func_do_profile(ufunc_T *fp);
848static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
849static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000850static int
851# ifdef __BORLANDC__
852 _RTLENTRYF
853# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static int script_autoload(char_u *name, int reload);
862static char_u *autoload_name(char_u *name);
863static void cat_func_name(char_u *buf, ufunc_T *fp);
864static void func_free(ufunc_T *fp);
865static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
866static int can_free_funccal(funccall_T *fc, int copyID) ;
867static void free_funccal(funccall_T *fc, int free_val);
868static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
869static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
870static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
871static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
872static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
873static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
874static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
875static int write_list(FILE *fd, list_T *list, int binary);
876static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static int compare_func_name(const void *s1, const void *s2);
881static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882#endif
883
Bram Moolenaar33570922005-01-25 22:26:29 +0000884/*
885 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000886 */
887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000889{
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 int i;
891 struct vimvar *p;
892
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200893 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
894 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200895 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000896 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 STRCPY(p->vv_di.di_key, p->vv_name);
903 if (p->vv_flags & VV_RO)
904 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
905 else if (p->vv_flags & VV_RO_SBX)
906 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
907 else
908 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000909
910 /* add to v: scope dict, unless the value is not always available */
911 if (p->vv_type != VAR_UNKNOWN)
912 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000914 /* add to compat scope dict */
915 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100917 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
918
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000919 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100920 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200921 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100922 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100923
924 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
925 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
926 set_vim_var_nr(VV_NONE, VVAL_NONE);
927 set_vim_var_nr(VV_NULL, VVAL_NULL);
928
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200929 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930
931#ifdef EBCDIC
932 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100933 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200934 */
935 sortFunctions();
936#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000937}
938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939#if defined(EXITFREE) || defined(PROTO)
940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942{
943 int i;
944 struct vimvar *p;
945
946 for (i = 0; i < VV_LEN; ++i)
947 {
948 p = &vimvars[i];
949 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000951 vim_free(p->vv_str);
952 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000953 }
954 else if (p->vv_di.di_tv.v_type == VAR_LIST)
955 {
956 list_unref(p->vv_list);
957 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959 }
960 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000961 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 hash_clear(&compat_hashtab);
963
Bram Moolenaard9fba312005-06-26 22:34:35 +0000964 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100965# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200966 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100967# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968
969 /* global variables */
970 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000972 /* autoloaded script names */
973 ga_clear_strings(&ga_loaded);
974
Bram Moolenaarcca74132013-09-25 21:00:28 +0200975 /* Script-local variables. First clear all the variables and in a second
976 * loop free the scriptvar_T, because a variable in one script might hold
977 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200978 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200979 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200980 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 ga_clear(&ga_scripts);
983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000984 /* unreferenced lists and dicts */
985 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986
987 /* functions */
988 free_all_functions();
989 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990}
991#endif
992
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Return the name of the executed function.
995 */
996 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100997func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000}
1001
1002/*
1003 * Return the address holding the next breakpoint line for a funccall cookie.
1004 */
1005 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
1012 * Return the address holding the debug tick for a funccall cookie.
1013 */
1014 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001015func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar33570922005-01-25 22:26:29 +00001017 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
1021 * Return the nesting level for a funccall cookie.
1022 */
1023 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar33570922005-01-25 22:26:29 +00001026 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001030funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001032/* pointer to list of previously used funccal, still around because some
1033 * item in it is still being used. */
1034funccall_T *previous_funccal = NULL;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036/*
1037 * Return TRUE when a function was ended by a ":return" command.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 return current_funccal->returned;
1043}
1044
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Set an internal variable to a string value. Creates the variable if it does
1048 * not already exist.
1049 */
1050 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001053 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 val = vim_strsave(value);
1057 if (val != NULL)
1058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001060 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001062 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 }
1066}
1067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001069static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070static char_u *redir_endp = NULL;
1071static char_u *redir_varname = NULL;
1072
1073/*
1074 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 * Returns OK if successfully completed the setup. FAIL otherwise.
1077 */
1078 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080{
1081 int save_emsg;
1082 int err;
1083 typval_T tv;
1084
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 {
1088 EMSG(_(e_invarg));
1089 return FAIL;
1090 }
1091
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 redir_varname = vim_strsave(name);
1094 if (redir_varname == NULL)
1095 return FAIL;
1096
1097 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1098 if (redir_lval == NULL)
1099 {
1100 var_redir_stop();
1101 return FAIL;
1102 }
1103
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 /* The output is stored in growarray "redir_ga" until redirection ends. */
1105 ga_init2(&redir_ga, (int)sizeof(char), 500);
1106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001108 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001109 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1111 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001112 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp != NULL && *redir_endp != NUL)
1114 /* Trailing characters are present after the variable name */
1115 EMSG(_(e_trailing));
1116 else
1117 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
1120 return FAIL;
1121 }
1122
1123 /* check if we can write to the variable: set it to or append an empty
1124 * string */
1125 save_emsg = did_emsg;
1126 did_emsg = FALSE;
1127 tv.v_type = VAR_STRING;
1128 tv.vval.v_string = (char_u *)"";
1129 if (append)
1130 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1131 else
1132 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001133 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001135 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (err)
1137 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 * Append "value[value_len]" to the variable set by var_redir_start().
1148 * The actual appending is postponed until redirection ends, because the value
1149 * appended may in fact be the string we write to, changing it may cause freed
1150 * memory to be used:
1151 * :redir => foo
1152 * :let foo
1153 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 */
1155 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001156var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 if (redir_lval == NULL)
1161 return;
1162
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001164 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 {
1170 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 }
1173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175}
1176
1177/*
1178 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001179 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 typval_T tv;
1185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_lval != NULL)
1187 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* If there was no error: assign the text to the variable. */
1189 if (redir_endp != NULL)
1190 {
1191 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1192 tv.v_type = VAR_STRING;
1193 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001194 /* Call get_lval() again, if it's inside a Dict or List it may
1195 * have changed. */
1196 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001197 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1199 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1200 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 /* free the collected output */
1204 vim_free(redir_ga.ga_data);
1205 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 vim_free(redir_lval);
1208 redir_lval = NULL;
1209 }
1210 vim_free(redir_varname);
1211 redir_varname = NULL;
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# if defined(FEAT_MBYTE) || defined(PROTO)
1215 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001216eval_charconvert(
1217 char_u *enc_from,
1218 char_u *enc_to,
1219 char_u *fname_from,
1220 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
1222 int err = FALSE;
1223
1224 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1225 set_vim_var_string(VV_CC_TO, enc_to, -1);
1226 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1227 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1228 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_CC_FROM, NULL, -1);
1231 set_vim_var_string(VV_CC_TO, NULL, -1);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234
1235 if (err)
1236 return FAIL;
1237 return OK;
1238}
1239# endif
1240
1241# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int err = FALSE;
1246
1247 set_vim_var_string(VV_FNAME_IN, fname, -1);
1248 set_vim_var_string(VV_CMDARG, args, -1);
1249 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1250 err = TRUE;
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_CMDARG, NULL, -1);
1253
1254 if (err)
1255 {
1256 mch_remove(fname);
1257 return FAIL;
1258 }
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_DIFF) || defined(PROTO)
1264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_diff(
1266 char_u *origfile,
1267 char_u *newfile,
1268 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1273 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1274 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1275 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1278 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1279}
1280
1281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282eval_patch(
1283 char_u *origfile,
1284 char_u *difffile,
1285 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err;
1288
1289 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1290 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1291 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1292 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296}
1297# endif
1298
1299/*
1300 * Top level evaluation function, returning a boolean.
1301 * Sets "error" to TRUE if there was an error.
1302 * Return TRUE or FALSE.
1303 */
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_to_bool(
1306 char_u *arg,
1307 int *error,
1308 char_u **nextcmd,
1309 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int retval = FALSE;
1313
1314 if (skip)
1315 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
1319 {
1320 *error = FALSE;
1321 if (!skip)
1322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 if (skip)
1328 --emsg_skip;
1329
1330 return retval;
1331}
1332
1333/*
1334 * Top level evaluation function, returning a string. If "skip" is TRUE,
1335 * only parsing to "nextcmd" is done, without reporting errors. Return
1336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1337 */
1338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339eval_to_string_skip(
1340 char_u *arg,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 retval = vim_strsave(get_tv_string(&tv));
1354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 if (skip)
1357 --emsg_skip;
1358
1359 return retval;
1360}
1361
1362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363 * Skip over an expression at "*pp".
1364 * Return FAIL for an error, OK otherwise.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001370
1371 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 * When "convert" is TRUE convert a List into a sequence of lines and convert
1378 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Return pointer to allocated memory, or NULL for failure.
1380 */
1381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382eval_to_string(
1383 char_u *arg,
1384 char_u **nextcmd,
1385 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 retval = NULL;
1396 else
1397 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 {
1400 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001403 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001404 if (tv.vval.v_list->lv_len > 0)
1405 ga_append(&ga, NL);
1406 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 ga_append(&ga, NUL);
1408 retval = (char_u *)ga.ga_data;
1409 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410#ifdef FEAT_FLOAT
1411 else if (convert && tv.v_type == VAR_FLOAT)
1412 {
1413 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1414 retval = vim_strsave(numbuf);
1415 }
1416#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 else
1418 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 * Call eval_to_string() without using current local variables and using
1427 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430eval_to_string_safe(
1431 char_u *arg,
1432 char_u **nextcmd,
1433 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 char_u *retval;
1436 void *save_funccalp;
1437
1438 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 if (use_sandbox)
1440 ++sandbox;
1441 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 if (use_sandbox)
1444 --sandbox;
1445 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 restore_funccal(save_funccalp);
1447 return retval;
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
1451 * Top level evaluation function, returning a number.
1452 * Evaluates "expr" silently.
1453 * Returns -1 for an error.
1454 */
1455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar33570922005-01-25 22:26:29 +00001458 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001460 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 ++emsg_off;
1463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 retval = -1;
1466 else
1467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001468 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 --emsg_off;
1472
1473 return retval;
1474}
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476/*
1477 * Prepare v: variable "idx" to be used.
1478 * Save the current typeval in "save_tv".
1479 * When not used yet add the variable to the v: hashtable.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483{
1484 *save_tv = vimvars[idx].vv_tv;
1485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1487}
1488
1489/*
1490 * Restore v: variable "idx" to typeval "save_tv".
1491 * When no longer defined, remove the variable from the v: hashtable.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001495{
1496 hashitem_T *hi;
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498 vimvars[idx].vv_tv = *save_tv;
1499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1500 {
1501 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1502 if (HASHITEM_EMPTY(hi))
1503 EMSG2(_(e_intern2), "restore_vimvar()");
1504 else
1505 hash_remove(&vimvarht, hi);
1506 }
1507}
1508
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001509#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510/*
1511 * Evaluate an expression to a list with suggestions.
1512 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001513 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 */
1515 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517{
1518 typval_T save_val;
1519 typval_T rettv;
1520 list_T *list = NULL;
1521 char_u *p = skipwhite(expr);
1522
1523 /* Set "v:val" to the bad word. */
1524 prepare_vimvar(VV_VAL, &save_val);
1525 vimvars[VV_VAL].vv_type = VAR_STRING;
1526 vimvars[VV_VAL].vv_str = badword;
1527 if (p_verbose == 0)
1528 ++emsg_off;
1529
1530 if (eval1(&p, &rettv, TRUE) == OK)
1531 {
1532 if (rettv.v_type != VAR_LIST)
1533 clear_tv(&rettv);
1534 else
1535 list = rettv.vval.v_list;
1536 }
1537
1538 if (p_verbose == 0)
1539 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 restore_vimvar(VV_VAL, &save_val);
1541
1542 return list;
1543}
1544
1545/*
1546 * "list" is supposed to contain two items: a word and a number. Return the
1547 * word in "pp" and the number as the return value.
1548 * Return -1 if anything isn't right.
1549 * Used to get the good word and score from the eval_spell_expr() result.
1550 */
1551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553{
1554 listitem_T *li;
1555
1556 li = list->lv_first;
1557 if (li == NULL)
1558 return -1;
1559 *pp = get_tv_string(&li->li_tv);
1560
1561 li = li->li_next;
1562 if (li == NULL)
1563 return -1;
1564 return get_tv_number(&li->li_tv);
1565}
1566#endif
1567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001569 * Top level evaluation function.
1570 * Returns an allocated typval_T with the result.
1571 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572 */
1573 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575{
1576 typval_T *tv;
1577
1578 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 {
1581 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 }
1584
1585 return tv;
1586}
1587
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 * Uses argv[argc] for the function arguments. Only Number and String
1592 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596call_vim_function(
1597 char_u *func,
1598 int argc,
1599 char_u **argv,
1600 int safe, /* use the sandbox */
1601 int str_arg_only, /* all arguments are strings */
1602 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 long n;
1606 int len;
1607 int i;
1608 int doesrange;
1609 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001612 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 for (i = 0; i < argc; i++)
1617 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001618 /* Pass a NULL or empty argument as an empty string */
1619 if (argv[i] == NULL || *argv[i] == NUL)
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 continue;
1624 }
1625
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001626 if (str_arg_only)
1627 len = 0;
1628 else
1629 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001630 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (len != 0 && len == (int)STRLEN(argv[i]))
1632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001633 argvars[i].v_type = VAR_NUMBER;
1634 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 else
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 }
1642
1643 if (safe)
1644 {
1645 save_funccalp = save_funccal();
1646 ++sandbox;
1647 }
1648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1650 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (safe)
1654 {
1655 --sandbox;
1656 restore_funccal(save_funccalp);
1657 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 vim_free(argvars);
1659
1660 if (ret == FAIL)
1661 clear_tv(rettv);
1662
1663 return ret;
1664}
1665
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666/*
1667 * Call vimL function "func" and return the result as a number.
1668 * Returns -1 when calling the function fails.
1669 * Uses argv[argc] for the function arguments.
1670 */
1671 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672call_func_retnr(
1673 char_u *func,
1674 int argc,
1675 char_u **argv,
1676 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001677{
1678 typval_T rettv;
1679 long retval;
1680
1681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1683 return -1;
1684
1685 retval = get_tv_number_chk(&rettv, NULL);
1686 clear_tv(&rettv);
1687 return retval;
1688}
1689
1690#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1691 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1692
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 * Call vimL function "func" and return the result as a string.
1696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
1698 */
1699 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retstr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705{
1706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return retval;
1716}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retlist(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
1732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 if (rettv.v_type != VAR_LIST)
1738 {
1739 clear_tv(&rettv);
1740 return NULL;
1741 }
1742
1743 return rettv.vval.v_list;
1744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746
1747/*
1748 * Save the current function call pointer, and set it to NULL.
1749 * Used when executing autocommands and for ":source".
1750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 current_funccal = NULL;
1757 return (void *)fc;
1758}
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775prof_child_enter(
1776 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790prof_child_exit(
1791 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 int var_count = 0;
1872 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 argend = skip_var_list(arg, &var_count, &semicolon);
1878 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001880 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1881 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001882 expr = skipwhite(argend);
1883 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1884 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001886 /*
1887 * ":let" without "=": list variables
1888 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 if (*arg == '[')
1890 EMSG(_(e_invarg));
1891 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 list_glob_vars(&first);
1898 list_buf_vars(&first);
1899 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_script_vars(&first);
1904 list_func_vars(&first);
1905 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 eap->nextcmd = check_nextcmd(arg);
1908 }
1909 else
1910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 op[0] = '=';
1912 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1916 op[0] = *expr; /* +=, -= or .= */
1917 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 else
1920 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 {
1927 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 --emsg_skip;
1930 }
1931 else if (i != FAIL)
1932 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 }
1938}
1939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940/*
1941 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1942 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 * When "nextchars" is not NULL it points to a string with characters that
1944 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1945 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 * Returns OK or FAIL;
1947 */
1948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001949ex_let_vars(
1950 char_u *arg_start,
1951 typval_T *tv,
1952 int copy, /* copy values from "tv", don't move */
1953 int semicolon, /* from skip_var_list() */
1954 int var_count, /* from skip_var_list() */
1955 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956{
1957 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 listitem_T *item;
1961 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962
1963 if (*arg != '[')
1964 {
1965 /*
1966 * ":let var = expr" or ":for var in list"
1967 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 return OK;
1971 }
1972
1973 /*
1974 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1975 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001976 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 EMSG(_(e_listreq));
1979 return FAIL;
1980 }
1981
1982 i = list_len(l);
1983 if (semicolon == 0 && var_count < i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988 if (var_count - semicolon > i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993
1994 item = l->lv_first;
1995 while (*arg != ']')
1996 {
1997 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 item = item->li_next;
2000 if (arg == NULL)
2001 return FAIL;
2002
2003 arg = skipwhite(arg);
2004 if (*arg == ';')
2005 {
2006 /* Put the rest of the list (may be empty) in the var after ';'.
2007 * Create a new list for this. */
2008 l = list_alloc();
2009 if (l == NULL)
2010 return FAIL;
2011 while (item != NULL)
2012 {
2013 list_append_tv(l, &item->li_tv);
2014 item = item->li_next;
2015 }
2016
2017 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002018 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 ltv.vval.v_list = l;
2020 l->lv_refcount = 1;
2021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2023 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 clear_tv(&ltv);
2025 if (arg == NULL)
2026 return FAIL;
2027 break;
2028 }
2029 else if (*arg != ',' && *arg != ']')
2030 {
2031 EMSG2(_(e_intern2), "ex_let_vars()");
2032 return FAIL;
2033 }
2034 }
2035
2036 return OK;
2037}
2038
2039/*
2040 * Skip over assignable variable "var" or list of variables "[var, var]".
2041 * Used for ":let varvar = expr" and ":for varvar in expr".
2042 * For "[var, var]" increment "*var_count" for each variable.
2043 * for "[var, var; var]" set "semicolon".
2044 * Return NULL for an error.
2045 */
2046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002047skip_var_list(
2048 char_u *arg,
2049 int *var_count,
2050 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051{
2052 char_u *p, *s;
2053
2054 if (*arg == '[')
2055 {
2056 /* "[var, var]": find the matching ']'. */
2057 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002058 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 {
2060 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2061 s = skip_var_one(p);
2062 if (s == p)
2063 {
2064 EMSG2(_(e_invarg2), p);
2065 return NULL;
2066 }
2067 ++*var_count;
2068
2069 p = skipwhite(s);
2070 if (*p == ']')
2071 break;
2072 else if (*p == ';')
2073 {
2074 if (*semicolon == 1)
2075 {
2076 EMSG(_("Double ; in list of variables"));
2077 return NULL;
2078 }
2079 *semicolon = 1;
2080 }
2081 else if (*p != ',')
2082 {
2083 EMSG2(_(e_invarg2), p);
2084 return NULL;
2085 }
2086 }
2087 return p + 1;
2088 }
2089 else
2090 return skip_var_one(arg);
2091}
2092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002094 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002095 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 if (*arg == '@' && arg[1] != NUL)
2101 return arg + 2;
2102 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2103 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104}
2105
Bram Moolenaara7043832005-01-21 11:56:39 +00002106/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 * List variables for hashtab "ht" with prefix "prefix".
2108 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111list_hashtable_vars(
2112 hashtab_T *ht,
2113 char_u *prefix,
2114 int empty,
2115 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashitem_T *hi;
2118 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 int todo;
2120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2123 {
2124 if (!HASHITEM_EMPTY(hi))
2125 {
2126 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 di = HI2DI(hi);
2128 if (empty || di->di_tv.v_type != VAR_STRING
2129 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 }
2132 }
2133}
2134
2135/*
2136 * List global variables.
2137 */
2138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
2144/*
2145 * List buffer variables.
2146 */
2147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170#ifdef FEAT_WINDOWS
2171/*
2172 * List tab page variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189}
2190
2191/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192 * List script-local variables, if there is a script.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196{
2197 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2199 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200}
2201
2202/*
2203 * List function variables, if there is a function.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_funccal != NULL)
2209 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * List variables in "arg".
2215 */
2216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002325ex_let_one(
2326 char_u *arg, /* points to variable name */
2327 typval_T *tv, /* value to assign to variable */
2328 int copy, /* copy value from "tv" */
2329 char_u *endchars, /* valid chars after variable name or NULL */
2330 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548get_lval(
2549 char_u *name,
2550 typval_T *rettv,
2551 lval_T *lp,
2552 int unlet,
2553 int skip,
2554 int flags, /* GLV_ values */
2555 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002810 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894{
2895 vim_free(lp->ll_exp_name);
2896 vim_free(lp->ll_newkey);
2897}
2898
2899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 */
2904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905set_var_lval(
2906 lval_T *lp,
2907 char_u *endp,
2908 typval_T *rettv,
2909 int copy,
2910 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *ri;
2914 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915
2916 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *endp;
2921 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002927 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 if ((di == NULL
2932 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2933 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2934 FALSE)))
2935 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 set_var(lp->ll_name, &tv, FALSE);
2937 clear_tv(&tv);
2938 }
2939 }
2940 else
2941 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 else if (tv_check_lock(lp->ll_newkey == NULL
2946 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002947 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 else if (lp->ll_range)
2950 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002951 listitem_T *ll_li = lp->ll_li;
2952 int ll_n1 = lp->ll_n1;
2953
2954 /*
2955 * Check whether any of the list items is locked
2956 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002957 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002958 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002959 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 return;
2961 ri = ri->li_next;
2962 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2963 break;
2964 ll_li = ll_li->li_next;
2965 ++ll_n1;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /*
2969 * Assign the List values to the list items.
2970 */
2971 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2975 else
2976 {
2977 clear_tv(&lp->ll_li->li_tv);
2978 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 ri = ri->li_next;
2981 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2982 break;
2983 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002986 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = NULL;
2989 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 lp->ll_li = lp->ll_li->li_next;
2993 ++lp->ll_n1;
2994 }
2995 if (ri != NULL)
2996 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 else if (lp->ll_empty2
2998 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 : lp->ll_n1 != lp->ll_n2)
3000 EMSG(_("E711: List value has not enough items"));
3001 }
3002 else
3003 {
3004 /*
3005 * Assign to a List or Dictionary item.
3006 */
3007 if (lp->ll_newkey != NULL)
3008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 if (op != NULL && *op != '=')
3010 {
3011 EMSG2(_(e_letwrong), op);
3012 return;
3013 }
3014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 if (di == NULL)
3018 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3020 {
3021 vim_free(di);
3022 return;
3023 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 else if (op != NULL && *op != '=')
3027 {
3028 tv_op(lp->ll_tv, rettv, op);
3029 return;
3030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003031 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /*
3035 * Assign the value to the variable or list item.
3036 */
3037 if (copy)
3038 copy_tv(rettv, lp->ll_tv);
3039 else
3040 {
3041 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003042 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 }
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046}
3047
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3050 * Returns OK or FAIL.
3051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003059 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3061 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 {
3063 switch (tv1->v_type)
3064 {
3065 case VAR_DICT:
3066 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003067 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003068 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 break;
3070
3071 case VAR_LIST:
3072 if (*op != '+' || tv2->v_type != VAR_LIST)
3073 break;
3074 /* List += List */
3075 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3076 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3077 return OK;
3078
3079 case VAR_NUMBER:
3080 case VAR_STRING:
3081 if (tv2->v_type == VAR_LIST)
3082 break;
3083 if (*op == '+' || *op == '-')
3084 {
3085 /* nr += nr or nr -= nr*/
3086 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087#ifdef FEAT_FLOAT
3088 if (tv2->v_type == VAR_FLOAT)
3089 {
3090 float_T f = n;
3091
3092 if (*op == '+')
3093 f += tv2->vval.v_float;
3094 else
3095 f -= tv2->vval.v_float;
3096 clear_tv(tv1);
3097 tv1->v_type = VAR_FLOAT;
3098 tv1->vval.v_float = f;
3099 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#endif
3102 {
3103 if (*op == '+')
3104 n += get_tv_number(tv2);
3105 else
3106 n -= get_tv_number(tv2);
3107 clear_tv(tv1);
3108 tv1->v_type = VAR_NUMBER;
3109 tv1->vval.v_number = n;
3110 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 }
3112 else
3113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114 if (tv2->v_type == VAR_FLOAT)
3115 break;
3116
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 /* str .= str */
3118 s = get_tv_string(tv1);
3119 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3120 clear_tv(tv1);
3121 tv1->v_type = VAR_STRING;
3122 tv1->vval.v_string = s;
3123 }
3124 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125
3126#ifdef FEAT_FLOAT
3127 case VAR_FLOAT:
3128 {
3129 float_T f;
3130
3131 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3132 && tv2->v_type != VAR_NUMBER
3133 && tv2->v_type != VAR_STRING))
3134 break;
3135 if (tv2->v_type == VAR_FLOAT)
3136 f = tv2->vval.v_float;
3137 else
3138 f = get_tv_number(tv2);
3139 if (*op == '+')
3140 tv1->vval.v_float += f;
3141 else
3142 tv1->vval.v_float -= f;
3143 }
3144 return OK;
3145#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 }
3147 }
3148
3149 EMSG2(_(e_letwrong), op);
3150 return FAIL;
3151}
3152
3153/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154 * Add a watcher to a list.
3155 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003157list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158{
3159 lw->lw_next = l->lv_watch;
3160 l->lv_watch = lw;
3161}
3162
3163/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003164 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 * No warning when it isn't found...
3166 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003168list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 lwp = &l->lv_watch;
3173 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3174 {
3175 if (lw == lwrem)
3176 {
3177 *lwp = lw->lw_next;
3178 break;
3179 }
3180 lwp = &lw->lw_next;
3181 }
3182}
3183
3184/*
3185 * Just before removing an item from a list: advance watchers to the next
3186 * item.
3187 */
3188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003189list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192
3193 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3194 if (lw->lw_item == item)
3195 lw->lw_item = item->li_next;
3196}
3197
3198/*
3199 * Evaluate the expression used in a ":for var in expr" command.
3200 * "arg" points to "var".
3201 * Set "*errp" to TRUE for an error, FALSE otherwise;
3202 * Return a pointer that holds the info. Null when there is an error.
3203 */
3204 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205eval_for_line(
3206 char_u *arg,
3207 int *errp,
3208 char_u **nextcmdp,
3209 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 typval_T tv;
3214 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 *errp = TRUE; /* default: there is an error */
3217
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 if (fi == NULL)
3220 return NULL;
3221
3222 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3223 if (expr == NULL)
3224 return fi;
3225
3226 expr = skipwhite(expr);
3227 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3228 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003229 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 return fi;
3231 }
3232
3233 if (skip)
3234 ++emsg_skip;
3235 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3236 {
3237 *errp = FALSE;
3238 if (!skip)
3239 {
3240 l = tv.vval.v_list;
3241 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003242 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 clear_tv(&tv);
3245 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 else
3247 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003248 /* No need to increment the refcount, it's already set for the
3249 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 fi->fi_list = l;
3251 list_add_watch(l, &fi->fi_lw);
3252 fi->fi_lw.lw_item = l->lv_first;
3253 }
3254 }
3255 }
3256 if (skip)
3257 --emsg_skip;
3258
3259 return fi;
3260}
3261
3262/*
3263 * Use the first item in a ":for" list. Advance to the next.
3264 * Assign the values to the variable (list). "arg" points to the first one.
3265 * Return TRUE when a valid item was found, FALSE when at end of list or
3266 * something wrong.
3267 */
3268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003271 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274
3275 item = fi->fi_lw.lw_item;
3276 if (item == NULL)
3277 result = FALSE;
3278 else
3279 {
3280 fi->fi_lw.lw_item = item->li_next;
3281 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3282 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3283 }
3284 return result;
3285}
3286
3287/*
3288 * Free the structure used to store info used by ":for".
3289 */
3290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003295 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 list_unref(fi->fi_list);
3299 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 vim_free(fi);
3301}
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3304
3305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306set_context_for_expression(
3307 expand_T *xp,
3308 char_u *arg,
3309 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int got_eq = FALSE;
3312 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 if (cmdidx == CMD_let)
3316 {
3317 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003318 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 {
3320 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003321 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 {
3323 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 if (vim_iswhite(*p))
3326 break;
3327 }
3328 return;
3329 }
3330 }
3331 else
3332 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3333 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 while ((xp->xp_pattern = vim_strpbrk(arg,
3335 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3336 {
3337 c = *xp->xp_pattern;
3338 if (c == '&')
3339 {
3340 c = xp->xp_pattern[1];
3341 if (c == '&')
3342 {
3343 ++xp->xp_pattern;
3344 xp->xp_context = cmdidx != CMD_let || got_eq
3345 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3346 }
3347 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003350 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3351 xp->xp_pattern += 2;
3352
3353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
3355 else if (c == '$')
3356 {
3357 /* environment variable */
3358 xp->xp_context = EXPAND_ENV_VARS;
3359 }
3360 else if (c == '=')
3361 {
3362 got_eq = TRUE;
3363 xp->xp_context = EXPAND_EXPRESSION;
3364 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003365 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 && xp->xp_context == EXPAND_FUNCTIONS
3367 && vim_strchr(xp->xp_pattern, '(') == NULL)
3368 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003369 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 break;
3371 }
3372 else if (cmdidx != CMD_let || got_eq)
3373 {
3374 if (c == '"') /* string */
3375 {
3376 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3377 if (c == '\\' && xp->xp_pattern[1] != NUL)
3378 ++xp->xp_pattern;
3379 xp->xp_context = EXPAND_NOTHING;
3380 }
3381 else if (c == '\'') /* literal string */
3382 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3385 /* skip */ ;
3386 xp->xp_context = EXPAND_NOTHING;
3387 }
3388 else if (c == '|')
3389 {
3390 if (xp->xp_pattern[1] == '|')
3391 {
3392 ++xp->xp_pattern;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
3395 else
3396 xp->xp_context = EXPAND_COMMANDS;
3397 }
3398 else
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
3401 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 /* Doesn't look like something valid, expand as an expression
3403 * anyway. */
3404 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 arg = xp->xp_pattern;
3406 if (*arg != NUL)
3407 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3408 /* skip */ ;
3409 }
3410 xp->xp_pattern = arg;
3411}
3412
3413#endif /* FEAT_CMDL_COMPL */
3414
3415/*
3416 * ":1,25call func(arg1, arg2)" function call.
3417 */
3418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003419ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 char_u *arg = eap->arg;
3422 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 linenr_T lnum;
3428 int doesrange;
3429 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003430 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003432 if (eap->skip)
3433 {
3434 /* trans_function_name() doesn't work well when skipping, use eval0()
3435 * instead to skip to any following command, e.g. for:
3436 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003437 ++emsg_skip;
3438 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3439 clear_tv(&rettv);
3440 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003441 return;
3442 }
3443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003444 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003445 if (fudi.fd_newkey != NULL)
3446 {
3447 /* Still need to give an error message for missing key. */
3448 EMSG2(_(e_dictkey), fudi.fd_newkey);
3449 vim_free(fudi.fd_newkey);
3450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003451 if (tofree == NULL)
3452 return;
3453
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003454 /* Increase refcount on dictionary, it could get deleted when evaluating
3455 * the arguments. */
3456 if (fudi.fd_dict != NULL)
3457 ++fudi.fd_dict->dv_refcount;
3458
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003459 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003461 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar532c7802005-01-27 14:44:31 +00003463 /* Skip white space to allow ":call func ()". Not good, but required for
3464 * backward compatibility. */
3465 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 if (*startarg != '(')
3469 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003470 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 goto end;
3472 }
3473
3474 /*
3475 * When skipping, evaluate the function once, to find the end of the
3476 * arguments.
3477 * When the function takes a range, this is discovered after the first
3478 * call, and the loop is broken.
3479 */
3480 if (eap->skip)
3481 {
3482 ++emsg_skip;
3483 lnum = eap->line2; /* do it once, also with an invalid range */
3484 }
3485 else
3486 lnum = eap->line1;
3487 for ( ; lnum <= eap->line2; ++lnum)
3488 {
3489 if (!eap->skip && eap->addr_count > 0)
3490 {
3491 curwin->w_cursor.lnum = lnum;
3492 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003493#ifdef FEAT_VIRTUALEDIT
3494 curwin->w_cursor.coladd = 0;
3495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003499 eap->line1, eap->line2, &doesrange,
3500 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 failed = TRUE;
3503 break;
3504 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003505
3506 /* Handle a function returning a Funcref, Dictionary or List. */
3507 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3508 {
3509 failed = TRUE;
3510 break;
3511 }
3512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (doesrange || eap->skip)
3515 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003518 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (aborting())
3522 break;
3523 }
3524 if (eap->skip)
3525 --emsg_skip;
3526
3527 if (!failed)
3528 {
3529 /* Check for trailing illegal characters and a following command. */
3530 if (!ends_excmd(*arg))
3531 {
3532 emsg_severe = TRUE;
3533 EMSG(_(e_trailing));
3534 }
3535 else
3536 eap->nextcmd = check_nextcmd(arg);
3537 }
3538
3539end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003540 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003541 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
3543
3544/*
3545 * ":unlet[!] var1 ... " command.
3546 */
3547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 ex_unletlock(eap, eap->arg, 0);
3551}
3552
3553/*
3554 * ":lockvar" and ":unlockvar" commands
3555 */
3556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560 int deep = 2;
3561
3562 if (eap->forceit)
3563 deep = -1;
3564 else if (vim_isdigit(*arg))
3565 {
3566 deep = getdigits(&arg);
3567 arg = skipwhite(arg);
3568 }
3569
3570 ex_unletlock(eap, arg, deep);
3571}
3572
3573/*
3574 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3575 */
3576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_unletlock(
3578 exarg_T *eap,
3579 char_u *argstart,
3580 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581{
3582 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 do
3588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003590 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003591 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 if (lv.ll_name == NULL)
3593 error = TRUE; /* error but continue parsing */
3594 if (name_end == NULL || (!vim_iswhite(*name_end)
3595 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (name_end != NULL)
3598 {
3599 emsg_severe = TRUE;
3600 EMSG(_(e_trailing));
3601 }
3602 if (!(eap->skip || error))
3603 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 break;
3605 }
3606
3607 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608 {
3609 if (eap->cmdidx == CMD_unlet)
3610 {
3611 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3612 error = TRUE;
3613 }
3614 else
3615 {
3616 if (do_lock_var(&lv, name_end, deep,
3617 eap->cmdidx == CMD_lockvar) == FAIL)
3618 error = TRUE;
3619 }
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (!eap->skip)
3623 clear_lval(&lv);
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 arg = skipwhite(name_end);
3626 } while (!ends_excmd(*arg));
3627
3628 eap->nextcmd = check_nextcmd(arg);
3629}
3630
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632do_unlet_var(
3633 lval_T *lp,
3634 char_u *name_end,
3635 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 int ret = OK;
3638 int cc;
3639
3640 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 cc = *name_end;
3643 *name_end = NUL;
3644
3645 /* Normal name or expanded name. */
3646 if (check_changedtick(lp->ll_name))
3647 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003652 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003653 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003654 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003655 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else if (lp->ll_range)
3658 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003660 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003661 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003662
3663 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3664 {
3665 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003666 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003667 return FAIL;
3668 ll_li = li;
3669 ++ll_n1;
3670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671
3672 /* Delete a range of List items. */
3673 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3674 {
3675 li = lp->ll_li->li_next;
3676 listitem_remove(lp->ll_list, lp->ll_li);
3677 lp->ll_li = li;
3678 ++lp->ll_n1;
3679 }
3680 }
3681 else
3682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 /* unlet a List item. */
3685 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003688 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 }
3690
3691 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692}
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 */
3698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003699do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
Bram Moolenaar33570922005-01-25 22:26:29 +00003701 hashtab_T *ht;
3702 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003703 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003704 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003705 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar33570922005-01-25 22:26:29 +00003707 ht = find_var_ht(name, &varname);
3708 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003710 if (ht == &globvarht)
3711 d = &globvardict;
3712 else if (current_funccal != NULL
3713 && ht == &current_funccal->l_vars.dv_hashtab)
3714 d = &current_funccal->l_vars;
3715 else if (ht == &compat_hashtab)
3716 d = &vimvardict;
3717 else
3718 {
3719 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3720 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3721 }
3722 if (d == NULL)
3723 {
3724 EMSG2(_(e_intern2), "do_unlet()");
3725 return FAIL;
3726 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003727 hi = hash_find(ht, varname);
3728 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003729 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003730 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003732 || var_check_ro(di->di_flags, name, FALSE)
3733 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003734 return FAIL;
3735
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 delete_var(ht, hi);
3737 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003740 if (forceit)
3741 return OK;
3742 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 return FAIL;
3744}
3745
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003746/*
3747 * Lock or unlock variable indicated by "lp".
3748 * "deep" is the levels to go (-1 for unlimited);
3749 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3750 */
3751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003752do_lock_var(
3753 lval_T *lp,
3754 char_u *name_end,
3755 int deep,
3756 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757{
3758 int ret = OK;
3759 int cc;
3760 dictitem_T *di;
3761
3762 if (deep == 0) /* nothing to do */
3763 return OK;
3764
3765 if (lp->ll_tv == NULL)
3766 {
3767 cc = *name_end;
3768 *name_end = NUL;
3769
3770 /* Normal name or expanded name. */
3771 if (check_changedtick(lp->ll_name))
3772 ret = FAIL;
3773 else
3774 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003775 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776 if (di == NULL)
3777 ret = FAIL;
3778 else
3779 {
3780 if (lock)
3781 di->di_flags |= DI_FLAGS_LOCK;
3782 else
3783 di->di_flags &= ~DI_FLAGS_LOCK;
3784 item_lock(&di->di_tv, deep, lock);
3785 }
3786 }
3787 *name_end = cc;
3788 }
3789 else if (lp->ll_range)
3790 {
3791 listitem_T *li = lp->ll_li;
3792
3793 /* (un)lock a range of List items. */
3794 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3795 {
3796 item_lock(&li->li_tv, deep, lock);
3797 li = li->li_next;
3798 ++lp->ll_n1;
3799 }
3800 }
3801 else if (lp->ll_list != NULL)
3802 /* (un)lock a List item. */
3803 item_lock(&lp->ll_li->li_tv, deep, lock);
3804 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003805 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 item_lock(&lp->ll_di->di_tv, deep, lock);
3807
3808 return ret;
3809}
3810
3811/*
3812 * Lock or unlock an item. "deep" is nr of levels to go.
3813 */
3814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003816{
3817 static int recurse = 0;
3818 list_T *l;
3819 listitem_T *li;
3820 dict_T *d;
3821 hashitem_T *hi;
3822 int todo;
3823
3824 if (recurse >= DICT_MAXNEST)
3825 {
3826 EMSG(_("E743: variable nested too deep for (un)lock"));
3827 return;
3828 }
3829 if (deep == 0)
3830 return;
3831 ++recurse;
3832
3833 /* lock/unlock the item itself */
3834 if (lock)
3835 tv->v_lock |= VAR_LOCKED;
3836 else
3837 tv->v_lock &= ~VAR_LOCKED;
3838
3839 switch (tv->v_type)
3840 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003841 case VAR_UNKNOWN:
3842 case VAR_NUMBER:
3843 case VAR_STRING:
3844 case VAR_FUNC:
3845 case VAR_FLOAT:
3846 case VAR_SPECIAL:
3847 break;
3848
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003849 case VAR_LIST:
3850 if ((l = tv->vval.v_list) != NULL)
3851 {
3852 if (lock)
3853 l->lv_lock |= VAR_LOCKED;
3854 else
3855 l->lv_lock &= ~VAR_LOCKED;
3856 if (deep < 0 || deep > 1)
3857 /* recursive: lock/unlock the items the List contains */
3858 for (li = l->lv_first; li != NULL; li = li->li_next)
3859 item_lock(&li->li_tv, deep - 1, lock);
3860 }
3861 break;
3862 case VAR_DICT:
3863 if ((d = tv->vval.v_dict) != NULL)
3864 {
3865 if (lock)
3866 d->dv_lock |= VAR_LOCKED;
3867 else
3868 d->dv_lock &= ~VAR_LOCKED;
3869 if (deep < 0 || deep > 1)
3870 {
3871 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003872 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3874 {
3875 if (!HASHITEM_EMPTY(hi))
3876 {
3877 --todo;
3878 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3879 }
3880 }
3881 }
3882 }
3883 }
3884 --recurse;
3885}
3886
Bram Moolenaara40058a2005-07-11 22:42:07 +00003887/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003888 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3889 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003890 */
3891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003892tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003893{
3894 return (tv->v_lock & VAR_LOCKED)
3895 || (tv->v_type == VAR_LIST
3896 && tv->vval.v_list != NULL
3897 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3898 || (tv->v_type == VAR_DICT
3899 && tv->vval.v_dict != NULL
3900 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3901}
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3904/*
3905 * Delete all "menutrans_" variables.
3906 */
3907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003911 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar33570922005-01-25 22:26:29 +00003913 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003914 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 {
3917 if (!HASHITEM_EMPTY(hi))
3918 {
3919 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3921 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003922 }
3923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925}
3926#endif
3927
3928#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3929
3930/*
3931 * Local string buffer for the next two functions to store a variable name
3932 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3933 * get_user_var_name().
3934 */
3935
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003936static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938static char_u *varnamebuf = NULL;
3939static int varnamebuflen = 0;
3940
3941/*
3942 * Function to concatenate a prefix and a variable name.
3943 */
3944 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003945cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
3947 int len;
3948
3949 len = (int)STRLEN(name) + 3;
3950 if (len > varnamebuflen)
3951 {
3952 vim_free(varnamebuf);
3953 len += 10; /* some additional space */
3954 varnamebuf = alloc(len);
3955 if (varnamebuf == NULL)
3956 {
3957 varnamebuflen = 0;
3958 return NULL;
3959 }
3960 varnamebuflen = len;
3961 }
3962 *varnamebuf = prefix;
3963 varnamebuf[1] = ':';
3964 STRCPY(varnamebuf + 2, name);
3965 return varnamebuf;
3966}
3967
3968/*
3969 * Function given to ExpandGeneric() to obtain the list of user defined
3970 * (global/buffer/window/built-in) variable names.
3971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 static long_u gdone;
3976 static long_u bdone;
3977 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003978#ifdef FEAT_WINDOWS
3979 static long_u tdone;
3980#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003981 static int vidx;
3982 static hashitem_T *hi;
3983 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003986 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988#ifdef FEAT_WINDOWS
3989 tdone = 0;
3990#endif
3991 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003992
3993 /* Global variables */
3994 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003996 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003998 else
3999 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4003 return cat_prefix_varname('g', hi->hi_key);
4004 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006
4007 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004008 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 hi = ht->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 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return (char_u *)"b:changedtick";
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004026 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004029 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 else
4032 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 while (HASHITEM_EMPTY(hi))
4034 ++hi;
4035 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038#ifdef FEAT_WINDOWS
4039 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004041 if (tdone < ht->ht_used)
4042 {
4043 if (tdone++ == 0)
4044 hi = ht->ht_array;
4045 else
4046 ++hi;
4047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('t', hi->hi_key);
4050 }
4051#endif
4052
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 /* v: variables */
4054 if (vidx < VV_LEN)
4055 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 vim_free(varnamebuf);
4058 varnamebuf = NULL;
4059 varnamebuflen = 0;
4060 return NULL;
4061}
4062
4063#endif /* FEAT_CMDL_COMPL */
4064
4065/*
4066 * types for expressions.
4067 */
4068typedef enum
4069{
4070 TYPE_UNKNOWN = 0
4071 , TYPE_EQUAL /* == */
4072 , TYPE_NEQUAL /* != */
4073 , TYPE_GREATER /* > */
4074 , TYPE_GEQUAL /* >= */
4075 , TYPE_SMALLER /* < */
4076 , TYPE_SEQUAL /* <= */
4077 , TYPE_MATCH /* =~ */
4078 , TYPE_NOMATCH /* !~ */
4079} exptype_T;
4080
4081/*
4082 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4085 */
4086
4087/*
4088 * Handle zero level expression.
4089 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004091 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * Return OK or FAIL.
4093 */
4094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004095eval0(
4096 char_u *arg,
4097 typval_T *rettv,
4098 char_u **nextcmd,
4099 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100{
4101 int ret;
4102 char_u *p;
4103
4104 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (ret == FAIL || !ends_excmd(*p))
4107 {
4108 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 /*
4111 * Report the invalid expression unless the expression evaluation has
4112 * been cancelled due to an aborting error, an interrupt, or an
4113 * exception.
4114 */
4115 if (!aborting())
4116 EMSG2(_(e_invexpr2), arg);
4117 ret = FAIL;
4118 }
4119 if (nextcmd != NULL)
4120 *nextcmd = check_nextcmd(p);
4121
4122 return ret;
4123}
4124
4125/*
4126 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004127 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004132 * Note: "rettv.v_lock" is not set.
4133 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /*
4143 * Get the first variable.
4144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return FAIL;
4147
4148 if ((*arg)[0] == '?')
4149 {
4150 result = FALSE;
4151 if (evaluate)
4152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 int error = FALSE;
4154
4155 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 if (error)
4159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161
4162 /*
4163 * Get the second variable.
4164 */
4165 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168
4169 /*
4170 * Check for the ":".
4171 */
4172 if ((*arg)[0] != ':')
4173 {
4174 EMSG(_("E109: Missing ':' after '?'"));
4175 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178 }
4179
4180 /*
4181 * Get the third variable.
4182 */
4183 *arg = skipwhite(*arg + 1);
4184 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4185 {
4186 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189 }
4190 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193
4194 return OK;
4195}
4196
4197/*
4198 * Handle first level expression:
4199 * expr2 || expr2 || expr2 logical OR
4200 *
4201 * "arg" must point to the first non-white of the expression.
4202 * "arg" is advanced to the next non-white after the recognized expression.
4203 *
4204 * Return OK or FAIL.
4205 */
4206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004207eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
Bram Moolenaar33570922005-01-25 22:26:29 +00004209 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 long result;
4211 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 /*
4215 * Get the first variable.
4216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return FAIL;
4219
4220 /*
4221 * Repeat until there is no following "||".
4222 */
4223 first = TRUE;
4224 result = FALSE;
4225 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4226 {
4227 if (evaluate && first)
4228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 if (error)
4233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 first = FALSE;
4235 }
4236
4237 /*
4238 * Get the second variable.
4239 */
4240 *arg = skipwhite(*arg + 2);
4241 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4242 return FAIL;
4243
4244 /*
4245 * Compute the result.
4246 */
4247 if (evaluate && !result)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 if (evaluate)
4256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 rettv->v_type = VAR_NUMBER;
4258 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 }
4261
4262 return OK;
4263}
4264
4265/*
4266 * Handle second level expression:
4267 * expr3 && expr3 && expr3 logical AND
4268 *
4269 * "arg" must point to the first non-white of the expression.
4270 * "arg" is advanced to the next non-white after the recognized expression.
4271 *
4272 * Return OK or FAIL.
4273 */
4274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004275eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 long result;
4279 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 /*
4283 * Get the first variable.
4284 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 return FAIL;
4287
4288 /*
4289 * Repeat until there is no following "&&".
4290 */
4291 first = TRUE;
4292 result = TRUE;
4293 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4294 {
4295 if (evaluate && first)
4296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 if (error)
4301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 first = FALSE;
4303 }
4304
4305 /*
4306 * Get the second variable.
4307 */
4308 *arg = skipwhite(*arg + 2);
4309 if (eval4(arg, &var2, evaluate && result) == FAIL)
4310 return FAIL;
4311
4312 /*
4313 * Compute the result.
4314 */
4315 if (evaluate && result)
4316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 if (evaluate)
4324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 rettv->v_type = VAR_NUMBER;
4326 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 }
4329
4330 return OK;
4331}
4332
4333/*
4334 * Handle third level expression:
4335 * var1 == var2
4336 * var1 =~ var2
4337 * var1 != var2
4338 * var1 !~ var2
4339 * var1 > var2
4340 * var1 >= var2
4341 * var1 < var2
4342 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 * var1 is var2
4344 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 *
4346 * "arg" must point to the first non-white of the expression.
4347 * "arg" is advanced to the next non-white after the recognized expression.
4348 *
4349 * Return OK or FAIL.
4350 */
4351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004352eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
Bram Moolenaar33570922005-01-25 22:26:29 +00004354 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 char_u *p;
4356 int i;
4357 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 int len = 2;
4360 long n1, n2;
4361 char_u *s1, *s2;
4362 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4363 regmatch_T regmatch;
4364 int ic;
4365 char_u *save_cpo;
4366
4367 /*
4368 * Get the first variable.
4369 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return FAIL;
4372
4373 p = *arg;
4374 switch (p[0])
4375 {
4376 case '=': if (p[1] == '=')
4377 type = TYPE_EQUAL;
4378 else if (p[1] == '~')
4379 type = TYPE_MATCH;
4380 break;
4381 case '!': if (p[1] == '=')
4382 type = TYPE_NEQUAL;
4383 else if (p[1] == '~')
4384 type = TYPE_NOMATCH;
4385 break;
4386 case '>': if (p[1] != '=')
4387 {
4388 type = TYPE_GREATER;
4389 len = 1;
4390 }
4391 else
4392 type = TYPE_GEQUAL;
4393 break;
4394 case '<': if (p[1] != '=')
4395 {
4396 type = TYPE_SMALLER;
4397 len = 1;
4398 }
4399 else
4400 type = TYPE_SEQUAL;
4401 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 case 'i': if (p[1] == 's')
4403 {
4404 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4405 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004406 i = p[len];
4407 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004408 {
4409 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4410 type_is = TRUE;
4411 }
4412 }
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
4416 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004417 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 */
4419 if (type != TYPE_UNKNOWN)
4420 {
4421 /* extra question mark appended: ignore case */
4422 if (p[len] == '?')
4423 {
4424 ic = TRUE;
4425 ++len;
4426 }
4427 /* extra '#' appended: match case */
4428 else if (p[len] == '#')
4429 {
4430 ic = FALSE;
4431 ++len;
4432 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else
4435 ic = p_ic;
4436
4437 /*
4438 * Get the second variable.
4439 */
4440 *arg = skipwhite(p + len);
4441 if (eval5(arg, &var2, evaluate) == FAIL)
4442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return FAIL;
4445 }
4446
4447 if (evaluate)
4448 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 if (type_is && rettv->v_type != var2.v_type)
4450 {
4451 /* For "is" a different type always means FALSE, for "notis"
4452 * it means TRUE. */
4453 n1 = (type == TYPE_NEQUAL);
4454 }
4455 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4456 {
4457 if (type_is)
4458 {
4459 n1 = (rettv->v_type == var2.v_type
4460 && rettv->vval.v_list == var2.vval.v_list);
4461 if (type == TYPE_NEQUAL)
4462 n1 = !n1;
4463 }
4464 else if (rettv->v_type != var2.v_type
4465 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4466 {
4467 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004468 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004470 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 clear_tv(rettv);
4472 clear_tv(&var2);
4473 return FAIL;
4474 }
4475 else
4476 {
4477 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004478 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4479 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004485 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4486 {
4487 if (type_is)
4488 {
4489 n1 = (rettv->v_type == var2.v_type
4490 && rettv->vval.v_dict == var2.vval.v_dict);
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 else if (rettv->v_type != var2.v_type
4495 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4496 {
4497 if (rettv->v_type != var2.v_type)
4498 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4499 else
4500 EMSG(_("E736: Invalid operation for Dictionary"));
4501 clear_tv(rettv);
4502 clear_tv(&var2);
4503 return FAIL;
4504 }
4505 else
4506 {
4507 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004508 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4509 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004510 if (type == TYPE_NEQUAL)
4511 n1 = !n1;
4512 }
4513 }
4514
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4516 {
4517 if (rettv->v_type != var2.v_type
4518 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4519 {
4520 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004521 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004522 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004523 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 clear_tv(rettv);
4525 clear_tv(&var2);
4526 return FAIL;
4527 }
4528 else
4529 {
4530 /* Compare two Funcrefs for being equal or unequal. */
4531 if (rettv->vval.v_string == NULL
4532 || var2.vval.v_string == NULL)
4533 n1 = FALSE;
4534 else
4535 n1 = STRCMP(rettv->vval.v_string,
4536 var2.vval.v_string) == 0;
4537 if (type == TYPE_NEQUAL)
4538 n1 = !n1;
4539 }
4540 }
4541
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004542#ifdef FEAT_FLOAT
4543 /*
4544 * If one of the two variables is a float, compare as a float.
4545 * When using "=~" or "!~", always compare as string.
4546 */
4547 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4548 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4549 {
4550 float_T f1, f2;
4551
4552 if (rettv->v_type == VAR_FLOAT)
4553 f1 = rettv->vval.v_float;
4554 else
4555 f1 = get_tv_number(rettv);
4556 if (var2.v_type == VAR_FLOAT)
4557 f2 = var2.vval.v_float;
4558 else
4559 f2 = get_tv_number(&var2);
4560 n1 = FALSE;
4561 switch (type)
4562 {
4563 case TYPE_EQUAL: n1 = (f1 == f2); break;
4564 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4565 case TYPE_GREATER: n1 = (f1 > f2); break;
4566 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4567 case TYPE_SMALLER: n1 = (f1 < f2); break;
4568 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4569 case TYPE_UNKNOWN:
4570 case TYPE_MATCH:
4571 case TYPE_NOMATCH: break; /* avoid gcc warning */
4572 }
4573 }
4574#endif
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 /*
4577 * If one of the two variables is a number, compare as a number.
4578 * When using "=~" or "!~", always compare as string.
4579 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004580 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 n1 = get_tv_number(rettv);
4584 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 switch (type)
4586 {
4587 case TYPE_EQUAL: n1 = (n1 == n2); break;
4588 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4589 case TYPE_GREATER: n1 = (n1 > n2); break;
4590 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4591 case TYPE_SMALLER: n1 = (n1 < n2); break;
4592 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4593 case TYPE_UNKNOWN:
4594 case TYPE_MATCH:
4595 case TYPE_NOMATCH: break; /* avoid gcc warning */
4596 }
4597 }
4598 else
4599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 s1 = get_tv_string_buf(rettv, buf1);
4601 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4603 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4604 else
4605 i = 0;
4606 n1 = FALSE;
4607 switch (type)
4608 {
4609 case TYPE_EQUAL: n1 = (i == 0); break;
4610 case TYPE_NEQUAL: n1 = (i != 0); break;
4611 case TYPE_GREATER: n1 = (i > 0); break;
4612 case TYPE_GEQUAL: n1 = (i >= 0); break;
4613 case TYPE_SMALLER: n1 = (i < 0); break;
4614 case TYPE_SEQUAL: n1 = (i <= 0); break;
4615
4616 case TYPE_MATCH:
4617 case TYPE_NOMATCH:
4618 /* avoid 'l' flag in 'cpoptions' */
4619 save_cpo = p_cpo;
4620 p_cpo = (char_u *)"";
4621 regmatch.regprog = vim_regcomp(s2,
4622 RE_MAGIC + RE_STRING);
4623 regmatch.rm_ic = ic;
4624 if (regmatch.regprog != NULL)
4625 {
4626 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004627 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (type == TYPE_NOMATCH)
4629 n1 = !n1;
4630 }
4631 p_cpo = save_cpo;
4632 break;
4633
4634 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4635 }
4636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 clear_tv(rettv);
4638 clear_tv(&var2);
4639 rettv->v_type = VAR_NUMBER;
4640 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 }
4643
4644 return OK;
4645}
4646
4647/*
4648 * Handle fourth level expression:
4649 * + number addition
4650 * - number subtraction
4651 * . string concatenation
4652 *
4653 * "arg" must point to the first non-white of the expression.
4654 * "arg" is advanced to the next non-white after the recognized expression.
4655 *
4656 * Return OK or FAIL.
4657 */
4658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004659eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T var2;
4662 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 int op;
4664 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004665#ifdef FEAT_FLOAT
4666 float_T f1 = 0, f2 = 0;
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 char_u *s1, *s2;
4669 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4670 char_u *p;
4671
4672 /*
4673 * Get the first variable.
4674 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004675 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return FAIL;
4677
4678 /*
4679 * Repeat computing, until no '+', '-' or '.' is following.
4680 */
4681 for (;;)
4682 {
4683 op = **arg;
4684 if (op != '+' && op != '-' && op != '.')
4685 break;
4686
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687 if ((op != '+' || rettv->v_type != VAR_LIST)
4688#ifdef FEAT_FLOAT
4689 && (op == '.' || rettv->v_type != VAR_FLOAT)
4690#endif
4691 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 {
4693 /* For "list + ...", an illegal use of the first operand as
4694 * a number cannot be determined before evaluating the 2nd
4695 * operand: if this is also a list, all is ok.
4696 * For "something . ...", "something - ..." or "non-list + ...",
4697 * we know that the first operand needs to be a string or number
4698 * without evaluating the 2nd operand. So check before to avoid
4699 * side effects after an error. */
4700 if (evaluate && get_tv_string_chk(rettv) == NULL)
4701 {
4702 clear_tv(rettv);
4703 return FAIL;
4704 }
4705 }
4706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 /*
4708 * Get the second variable.
4709 */
4710 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 return FAIL;
4715 }
4716
4717 if (evaluate)
4718 {
4719 /*
4720 * Compute the result.
4721 */
4722 if (op == '.')
4723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4725 s2 = get_tv_string_buf_chk(&var2, buf2);
4726 if (s2 == NULL) /* type error ? */
4727 {
4728 clear_tv(rettv);
4729 clear_tv(&var2);
4730 return FAIL;
4731 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004732 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 clear_tv(rettv);
4734 rettv->v_type = VAR_STRING;
4735 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004737 else if (op == '+' && rettv->v_type == VAR_LIST
4738 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004739 {
4740 /* concatenate Lists */
4741 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4742 &var3) == FAIL)
4743 {
4744 clear_tv(rettv);
4745 clear_tv(&var2);
4746 return FAIL;
4747 }
4748 clear_tv(rettv);
4749 *rettv = var3;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
4752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 int error = FALSE;
4754
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755#ifdef FEAT_FLOAT
4756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758 f1 = rettv->vval.v_float;
4759 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004761 else
4762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764 n1 = get_tv_number_chk(rettv, &error);
4765 if (error)
4766 {
4767 /* This can only happen for "list + non-list". For
4768 * "non-list + ..." or "something - ...", we returned
4769 * before evaluating the 2nd operand. */
4770 clear_tv(rettv);
4771 return FAIL;
4772 }
4773#ifdef FEAT_FLOAT
4774 if (var2.v_type == VAR_FLOAT)
4775 f1 = n1;
4776#endif
4777 }
4778#ifdef FEAT_FLOAT
4779 if (var2.v_type == VAR_FLOAT)
4780 {
4781 f2 = var2.vval.v_float;
4782 n2 = 0;
4783 }
4784 else
4785#endif
4786 {
4787 n2 = get_tv_number_chk(&var2, &error);
4788 if (error)
4789 {
4790 clear_tv(rettv);
4791 clear_tv(&var2);
4792 return FAIL;
4793 }
4794#ifdef FEAT_FLOAT
4795 if (rettv->v_type == VAR_FLOAT)
4796 f2 = n2;
4797#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800
4801#ifdef FEAT_FLOAT
4802 /* If there is a float on either side the result is a float. */
4803 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4804 {
4805 if (op == '+')
4806 f1 = f1 + f2;
4807 else
4808 f1 = f1 - f2;
4809 rettv->v_type = VAR_FLOAT;
4810 rettv->vval.v_float = f1;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813#endif
4814 {
4815 if (op == '+')
4816 n1 = n1 + n2;
4817 else
4818 n1 = n1 - n2;
4819 rettv->v_type = VAR_NUMBER;
4820 rettv->vval.v_number = n1;
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 }
4826 return OK;
4827}
4828
4829/*
4830 * Handle fifth level expression:
4831 * * number multiplication
4832 * / number division
4833 * % number modulo
4834 *
4835 * "arg" must point to the first non-white of the expression.
4836 * "arg" is advanced to the next non-white after the recognized expression.
4837 *
4838 * Return OK or FAIL.
4839 */
4840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004841eval6(
4842 char_u **arg,
4843 typval_T *rettv,
4844 int evaluate,
4845 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaar33570922005-01-25 22:26:29 +00004847 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 int op;
4849 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#ifdef FEAT_FLOAT
4851 int use_float = FALSE;
4852 float_T f1 = 0, f2;
4853#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 /*
4857 * Get the first variable.
4858 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004859 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return FAIL;
4861
4862 /*
4863 * Repeat computing, until no '*', '/' or '%' is following.
4864 */
4865 for (;;)
4866 {
4867 op = **arg;
4868 if (op != '*' && op != '/' && op != '%')
4869 break;
4870
4871 if (evaluate)
4872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (rettv->v_type == VAR_FLOAT)
4875 {
4876 f1 = rettv->vval.v_float;
4877 use_float = TRUE;
4878 n1 = 0;
4879 }
4880 else
4881#endif
4882 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 if (error)
4885 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 else
4888 n1 = 0;
4889
4890 /*
4891 * Get the second variable.
4892 */
4893 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004894 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 return FAIL;
4896
4897 if (evaluate)
4898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899#ifdef FEAT_FLOAT
4900 if (var2.v_type == VAR_FLOAT)
4901 {
4902 if (!use_float)
4903 {
4904 f1 = n1;
4905 use_float = TRUE;
4906 }
4907 f2 = var2.vval.v_float;
4908 n2 = 0;
4909 }
4910 else
4911#endif
4912 {
4913 n2 = get_tv_number_chk(&var2, &error);
4914 clear_tv(&var2);
4915 if (error)
4916 return FAIL;
4917#ifdef FEAT_FLOAT
4918 if (use_float)
4919 f2 = n2;
4920#endif
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /*
4924 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930 if (op == '*')
4931 f1 = f1 * f2;
4932 else if (op == '/')
4933 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004934# ifdef VMS
4935 /* VMS crashes on divide by zero, work around it */
4936 if (f2 == 0.0)
4937 {
4938 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004939 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004940 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004941 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004943 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004944 }
4945 else
4946 f1 = f1 / f2;
4947# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 /* We rely on the floating point library to handle divide
4949 * by zero to result in "inf" and not a crash. */
4950 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004951# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004955 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 return FAIL;
4957 }
4958 rettv->v_type = VAR_FLOAT;
4959 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964 if (op == '*')
4965 n1 = n1 * n2;
4966 else if (op == '/')
4967 {
4968 if (n2 == 0) /* give an error message? */
4969 {
4970 if (n1 == 0)
4971 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4972 else if (n1 < 0)
4973 n1 = -0x7fffffffL;
4974 else
4975 n1 = 0x7fffffffL;
4976 }
4977 else
4978 n1 = n1 / n2;
4979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 {
4982 if (n2 == 0) /* give an error message? */
4983 n1 = 0;
4984 else
4985 n1 = n1 % n2;
4986 }
4987 rettv->v_type = VAR_NUMBER;
4988 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 }
4992
4993 return OK;
4994}
4995
4996/*
4997 * Handle sixth level expression:
4998 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004999 * "string" string constant
5000 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 * &option-name option value
5002 * @r register contents
5003 * identifier variable value
5004 * function() function call
5005 * $VAR environment variable
5006 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005007 * [expr, expr] List
5008 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 *
5010 * Also handle:
5011 * ! in front logical NOT
5012 * - in front unary minus
5013 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 * trailing [] subscript in String or List
5015 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 *
5017 * "arg" must point to the first non-white of the expression.
5018 * "arg" is advanced to the next non-white after the recognized expression.
5019 *
5020 * Return OK or FAIL.
5021 */
5022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023eval7(
5024 char_u **arg,
5025 typval_T *rettv,
5026 int evaluate,
5027 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 long n;
5030 int len;
5031 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 char_u *start_leader, *end_leader;
5033 int ret = OK;
5034 char_u *alias;
5035
5036 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 /*
5043 * Skip '!' and '-' characters. They are handled later.
5044 */
5045 start_leader = *arg;
5046 while (**arg == '!' || **arg == '-' || **arg == '+')
5047 *arg = skipwhite(*arg + 1);
5048 end_leader = *arg;
5049
5050 switch (**arg)
5051 {
5052 /*
5053 * Number constant.
5054 */
5055 case '0':
5056 case '1':
5057 case '2':
5058 case '3':
5059 case '4':
5060 case '5':
5061 case '6':
5062 case '7':
5063 case '8':
5064 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065 {
5066#ifdef FEAT_FLOAT
5067 char_u *p = skipdigits(*arg + 1);
5068 int get_float = FALSE;
5069
5070 /* We accept a float when the format matches
5071 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005072 * strict to avoid backwards compatibility problems.
5073 * Don't look for a float after the "." operator, so that
5074 * ":let vers = 1.2.3" doesn't fail. */
5075 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077 get_float = TRUE;
5078 p = skipdigits(p + 2);
5079 if (*p == 'e' || *p == 'E')
5080 {
5081 ++p;
5082 if (*p == '-' || *p == '+')
5083 ++p;
5084 if (!vim_isdigit(*p))
5085 get_float = FALSE;
5086 else
5087 p = skipdigits(p + 1);
5088 }
5089 if (ASCII_ISALPHA(*p) || *p == '.')
5090 get_float = FALSE;
5091 }
5092 if (get_float)
5093 {
5094 float_T f;
5095
5096 *arg += string2float(*arg, &f);
5097 if (evaluate)
5098 {
5099 rettv->v_type = VAR_FLOAT;
5100 rettv->vval.v_float = f;
5101 }
5102 }
5103 else
5104#endif
5105 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005106 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 *arg += len;
5108 if (evaluate)
5109 {
5110 rettv->v_type = VAR_NUMBER;
5111 rettv->vval.v_number = n;
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
5117 /*
5118 * String constant: "string".
5119 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122
5123 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 break;
5128
5129 /*
5130 * List: [expr, expr]
5131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
5134
5135 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 * Dictionary: {key: val, key: val}
5137 */
5138 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5139 break;
5140
5141 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005142 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
5148 * Environment variable: $VAR.
5149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /*
5154 * Register contents: @r.
5155 */
5156 case '@': ++*arg;
5157 if (evaluate)
5158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005160 rettv->vval.v_string = get_reg_contents(**arg,
5161 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 if (**arg != NUL)
5164 ++*arg;
5165 break;
5166
5167 /*
5168 * nested expression: (expression).
5169 */
5170 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (**arg == ')')
5173 ++*arg;
5174 else if (ret == OK)
5175 {
5176 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 ret = FAIL;
5179 }
5180 break;
5181
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185
5186 if (ret == NOTDONE)
5187 {
5188 /*
5189 * Must be a variable or function name.
5190 * Can also be a curly-braces kind of name: {expr}.
5191 */
5192 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005193 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 if (alias != NULL)
5195 s = alias;
5196
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 ret = FAIL;
5199 else
5200 {
5201 if (**arg == '(') /* recursive! */
5202 {
5203 /* If "s" is the name of a variable of type VAR_FUNC
5204 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005205 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206
5207 /* Invoke the function. */
5208 ret = get_func_tv(s, len, rettv, arg,
5209 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005210 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005211
5212 /* If evaluate is FALSE rettv->v_type was not set in
5213 * get_func_tv, but it's needed in handle_subscript() to parse
5214 * what follows. So set it here. */
5215 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5216 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005217 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005218 rettv->v_type = VAR_FUNC;
5219 }
5220
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 /* Stop the expression evaluation when immediately
5222 * aborting on error, or when an interrupt occurred or
5223 * an exception was thrown but not caught. */
5224 if (aborting())
5225 {
5226 if (ret == OK)
5227 clear_tv(rettv);
5228 ret = FAIL;
5229 }
5230 }
5231 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005232 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005233 else
5234 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005236 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 }
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 *arg = skipwhite(*arg);
5240
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005241 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5242 * expr(expr). */
5243 if (ret == OK)
5244 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 /*
5247 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5248 */
5249 if (ret == OK && evaluate && end_leader > start_leader)
5250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005252 int val = 0;
5253#ifdef FEAT_FLOAT
5254 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005256 if (rettv->v_type == VAR_FLOAT)
5257 f = rettv->vval.v_float;
5258 else
5259#endif
5260 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005261 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 clear_tv(rettv);
5264 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 else
5267 {
5268 while (end_leader > start_leader)
5269 {
5270 --end_leader;
5271 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 {
5273#ifdef FEAT_FLOAT
5274 if (rettv->v_type == VAR_FLOAT)
5275 f = !f;
5276 else
5277#endif
5278 val = !val;
5279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281 {
5282#ifdef FEAT_FLOAT
5283 if (rettv->v_type == VAR_FLOAT)
5284 f = -f;
5285 else
5286#endif
5287 val = -val;
5288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005290#ifdef FEAT_FLOAT
5291 if (rettv->v_type == VAR_FLOAT)
5292 {
5293 clear_tv(rettv);
5294 rettv->vval.v_float = f;
5295 }
5296 else
5297#endif
5298 {
5299 clear_tv(rettv);
5300 rettv->v_type = VAR_NUMBER;
5301 rettv->vval.v_number = val;
5302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305
5306 return ret;
5307}
5308
5309/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005310 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5311 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5313 */
5314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005315eval_index(
5316 char_u **arg,
5317 typval_T *rettv,
5318 int evaluate,
5319 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320{
5321 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005322 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 long len = -1;
5325 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328
Bram Moolenaara03f2332016-02-06 18:09:59 +01005329 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005331 case VAR_FUNC:
5332 if (verbose)
5333 EMSG(_("E695: Cannot index a Funcref"));
5334 return FAIL;
5335 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005336#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005337 if (verbose)
5338 EMSG(_(e_float_as_string));
5339 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005340#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 case VAR_SPECIAL:
5342 if (verbose)
5343 EMSG(_("E909: Cannot index a special variable"));
5344 return FAIL;
5345 case VAR_UNKNOWN:
5346 if (evaluate)
5347 return FAIL;
5348 /* FALLTHROUGH */
5349
5350 case VAR_STRING:
5351 case VAR_NUMBER:
5352 case VAR_LIST:
5353 case VAR_DICT:
5354 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005355 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005357 init_tv(&var1);
5358 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 /*
5362 * dict.name
5363 */
5364 key = *arg + 1;
5365 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5366 ;
5367 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 }
5371 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 /*
5374 * something[idx]
5375 *
5376 * Get the (first) variable from inside the [].
5377 */
5378 *arg = skipwhite(*arg + 1);
5379 if (**arg == ':')
5380 empty1 = TRUE;
5381 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5382 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5384 {
5385 /* not a number or string */
5386 clear_tv(&var1);
5387 return FAIL;
5388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389
5390 /*
5391 * Get the second variable from inside the [:].
5392 */
5393 if (**arg == ':')
5394 {
5395 range = TRUE;
5396 *arg = skipwhite(*arg + 1);
5397 if (**arg == ']')
5398 empty2 = TRUE;
5399 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005401 if (!empty1)
5402 clear_tv(&var1);
5403 return FAIL;
5404 }
5405 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5406 {
5407 /* not a number or string */
5408 if (!empty1)
5409 clear_tv(&var1);
5410 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 return FAIL;
5412 }
5413 }
5414
5415 /* Check for the ']'. */
5416 if (**arg != ']')
5417 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005418 if (verbose)
5419 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 clear_tv(&var1);
5421 if (range)
5422 clear_tv(&var2);
5423 return FAIL;
5424 }
5425 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427
5428 if (evaluate)
5429 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 n1 = 0;
5431 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 n1 = get_tv_number(&var1);
5434 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 }
5436 if (range)
5437 {
5438 if (empty2)
5439 n2 = -1;
5440 else
5441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442 n2 = get_tv_number(&var2);
5443 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 }
5446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005449 case VAR_SPECIAL:
5450 case VAR_FUNC:
5451 case VAR_FLOAT:
5452 case VAR_UNKNOWN:
5453 break; /* not evaluating, skipping over subscript */
5454
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 case VAR_NUMBER:
5456 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 len = (long)STRLEN(s);
5459 if (range)
5460 {
5461 /* The resulting variable is a substring. If the indexes
5462 * are out of range the result is empty. */
5463 if (n1 < 0)
5464 {
5465 n1 = len + n1;
5466 if (n1 < 0)
5467 n1 = 0;
5468 }
5469 if (n2 < 0)
5470 n2 = len + n2;
5471 else if (n2 >= len)
5472 n2 = len;
5473 if (n1 >= len || n2 < 0 || n1 > n2)
5474 s = NULL;
5475 else
5476 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5477 }
5478 else
5479 {
5480 /* The resulting variable is a string of a single
5481 * character. If the index is too big or negative the
5482 * result is empty. */
5483 if (n1 >= len || n1 < 0)
5484 s = NULL;
5485 else
5486 s = vim_strnsave(s + n1, 1);
5487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 clear_tv(rettv);
5489 rettv->v_type = VAR_STRING;
5490 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 break;
5492
5493 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 if (n1 < 0)
5496 n1 = len + n1;
5497 if (!empty1 && (n1 < 0 || n1 >= len))
5498 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005499 /* For a range we allow invalid values and return an empty
5500 * list. A list index out of range is an error. */
5501 if (!range)
5502 {
5503 if (verbose)
5504 EMSGN(_(e_listidx), n1);
5505 return FAIL;
5506 }
5507 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
5509 if (range)
5510 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005511 list_T *l;
5512 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513
5514 if (n2 < 0)
5515 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005516 else if (n2 >= len)
5517 n2 = len - 1;
5518 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005519 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 l = list_alloc();
5521 if (l == NULL)
5522 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 n1 <= n2; ++n1)
5525 {
5526 if (list_append_tv(l, &item->li_tv) == FAIL)
5527 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005528 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 return FAIL;
5530 }
5531 item = item->li_next;
5532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 clear_tv(rettv);
5534 rettv->v_type = VAR_LIST;
5535 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005536 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 }
5538 else
5539 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 clear_tv(rettv);
5542 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545
5546 case VAR_DICT:
5547 if (range)
5548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005549 if (verbose)
5550 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 if (len == -1)
5552 clear_tv(&var1);
5553 return FAIL;
5554 }
5555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557
5558 if (len == -1)
5559 {
5560 key = get_tv_string(&var1);
5561 if (*key == NUL)
5562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005563 if (verbose)
5564 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 clear_tv(&var1);
5566 return FAIL;
5567 }
5568 }
5569
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005570 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005572 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005573 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 if (len == -1)
5575 clear_tv(&var1);
5576 if (item == NULL)
5577 return FAIL;
5578
5579 copy_tv(&item->di_tv, &var1);
5580 clear_tv(rettv);
5581 *rettv = var1;
5582 }
5583 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 }
5585 }
5586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 return OK;
5588}
5589
5590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * Get an option value.
5592 * "arg" points to the '&' or '+' before the option name.
5593 * "arg" is advanced to character after the option name.
5594 * Return OK or FAIL.
5595 */
5596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005597get_option_tv(
5598 char_u **arg,
5599 typval_T *rettv, /* when NULL, only check if option exists */
5600 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 char_u *option_end;
5603 long numval;
5604 char_u *stringval;
5605 int opt_type;
5606 int c;
5607 int working = (**arg == '+'); /* has("+option") */
5608 int ret = OK;
5609 int opt_flags;
5610
5611 /*
5612 * Isolate the option name and find its value.
5613 */
5614 option_end = find_option_end(arg, &opt_flags);
5615 if (option_end == NULL)
5616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 EMSG2(_("E112: Option name missing: %s"), *arg);
5619 return FAIL;
5620 }
5621
5622 if (!evaluate)
5623 {
5624 *arg = option_end;
5625 return OK;
5626 }
5627
5628 c = *option_end;
5629 *option_end = NUL;
5630 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 if (opt_type == -3) /* invalid name */
5634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 EMSG2(_("E113: Unknown option: %s"), *arg);
5637 ret = FAIL;
5638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 if (opt_type == -2) /* hidden string option */
5642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 rettv->v_type = VAR_STRING;
5644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646 else if (opt_type == -1) /* hidden number option */
5647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 rettv->v_type = VAR_NUMBER;
5649 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 else if (opt_type == 1) /* number option */
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 rettv->v_type = VAR_NUMBER;
5654 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656 else /* string option */
5657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 rettv->v_type = VAR_STRING;
5659 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 }
5662 else if (working && (opt_type == -2 || opt_type == -1))
5663 ret = FAIL;
5664
5665 *option_end = c; /* put back for error messages */
5666 *arg = option_end;
5667
5668 return ret;
5669}
5670
5671/*
5672 * Allocate a variable for a string constant.
5673 * Return OK or FAIL.
5674 */
5675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 char_u *p;
5679 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int extra = 0;
5681
5682 /*
5683 * Find the end of the string, skipping backslashed characters.
5684 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005685 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {
5687 if (*p == '\\' && p[1] != NUL)
5688 {
5689 ++p;
5690 /* A "\<x>" form occupies at least 4 characters, and produces up
5691 * to 6 characters: reserve space for 2 extra */
5692 if (*p == '<')
5693 extra += 2;
5694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696
5697 if (*p != '"')
5698 {
5699 EMSG2(_("E114: Missing quote: %s"), *arg);
5700 return FAIL;
5701 }
5702
5703 /* If only parsing, set *arg and return here */
5704 if (!evaluate)
5705 {
5706 *arg = p + 1;
5707 return OK;
5708 }
5709
5710 /*
5711 * Copy the string into allocated memory, handling backslashed
5712 * characters.
5713 */
5714 name = alloc((unsigned)(p - *arg + extra));
5715 if (name == NULL)
5716 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 rettv->v_type = VAR_STRING;
5718 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (*p == '\\')
5723 {
5724 switch (*++p)
5725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 case 'b': *name++ = BS; ++p; break;
5727 case 'e': *name++ = ESC; ++p; break;
5728 case 'f': *name++ = FF; ++p; break;
5729 case 'n': *name++ = NL; ++p; break;
5730 case 'r': *name++ = CAR; ++p; break;
5731 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733 case 'X': /* hex: "\x1", "\x12" */
5734 case 'x':
5735 case 'u': /* Unicode: "\u0023" */
5736 case 'U':
5737 if (vim_isxdigit(p[1]))
5738 {
5739 int n, nr;
5740 int c = toupper(*p);
5741
5742 if (c == 'X')
5743 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005744 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005746 else
5747 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 nr = 0;
5749 while (--n >= 0 && vim_isxdigit(p[1]))
5750 {
5751 ++p;
5752 nr = (nr << 4) + hex2nr(*p);
5753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#ifdef FEAT_MBYTE
5756 /* For "\u" store the number according to
5757 * 'encoding'. */
5758 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 else
5761#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 break;
5765
5766 /* octal: "\1", "\12", "\123" */
5767 case '0':
5768 case '1':
5769 case '2':
5770 case '3':
5771 case '4':
5772 case '5':
5773 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 case '7': *name = *p++ - '0';
5775 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name = (*name << 3) + *p++ - '0';
5778 if (*p >= '0' && *p <= '7')
5779 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 break;
5783
5784 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (extra != 0)
5787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 break;
5790 }
5791 /* FALLTHROUGH */
5792
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795 }
5796 }
5797 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 *arg = p + 1;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 return OK;
5805}
5806
5807/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 * Return OK or FAIL.
5810 */
5811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005812get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 int reduce = 0;
5817
5818 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 */
5821 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 break;
5827 ++reduce;
5828 ++p;
5829 }
5830 }
5831
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 return FAIL;
5836 }
5837
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 if (!evaluate)
5840 {
5841 *arg = p + 1;
5842 return OK;
5843 }
5844
5845 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 */
5848 str = alloc((unsigned)((p - *arg) - reduce));
5849 if (str == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++p;
5861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 *arg = p + 1;
5866
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return OK;
5868}
5869
5870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 * Allocate a variable for a List and fill it from "*arg".
5872 * Return OK or FAIL.
5873 */
5874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005875get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876{
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l = NULL;
5878 typval_T tv;
5879 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880
5881 if (evaluate)
5882 {
5883 l = list_alloc();
5884 if (l == NULL)
5885 return FAIL;
5886 }
5887
5888 *arg = skipwhite(*arg + 1);
5889 while (**arg != ']' && **arg != NUL)
5890 {
5891 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5892 goto failret;
5893 if (evaluate)
5894 {
5895 item = listitem_alloc();
5896 if (item != NULL)
5897 {
5898 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005899 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 list_append(l, item);
5901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005902 else
5903 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 }
5905
5906 if (**arg == ']')
5907 break;
5908 if (**arg != ',')
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 goto failret;
5912 }
5913 *arg = skipwhite(*arg + 1);
5914 }
5915
5916 if (**arg != ']')
5917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919failret:
5920 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005921 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 if (evaluate)
5927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 rettv->v_type = VAR_LIST;
5929 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 ++l->lv_refcount;
5931 }
5932
5933 return OK;
5934}
5935
5936/*
5937 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005938 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005940 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 list_T *l;
5944
5945 l = (list_T *)alloc_clear(sizeof(list_T));
5946 if (l != NULL)
5947 {
5948 /* Prepend the list to the list of lists for garbage collection. */
5949 if (first_list != NULL)
5950 first_list->lv_used_prev = l;
5951 l->lv_used_prev = NULL;
5952 l->lv_used_next = first_list;
5953 first_list = l;
5954 }
5955 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956}
5957
5958/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005959 * Allocate an empty list for a return value.
5960 * Returns OK or FAIL.
5961 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005963rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005964{
5965 list_T *l = list_alloc();
5966
5967 if (l == NULL)
5968 return FAIL;
5969
5970 rettv->vval.v_list = l;
5971 rettv->v_type = VAR_LIST;
5972 ++l->lv_refcount;
5973 return OK;
5974}
5975
5976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 * Unreference a list: decrement the reference count and free it when it
5978 * becomes zero.
5979 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005983 if (l != NULL && --l->lv_refcount <= 0)
5984 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985}
5986
5987/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005988 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 * Ignores the reference count.
5990 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005992list_free(
5993 list_T *l,
5994 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995{
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 /* Remove the list from the list of lists for garbage collection. */
5999 if (l->lv_used_prev == NULL)
6000 first_list = l->lv_used_next;
6001 else
6002 l->lv_used_prev->lv_used_next = l->lv_used_next;
6003 if (l->lv_used_next != NULL)
6004 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6005
Bram Moolenaard9fba312005-06-26 22:34:35 +00006006 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006008 /* Remove the item before deleting it. */
6009 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006010 if (recurse || (item->li_tv.v_type != VAR_LIST
6011 && item->li_tv.v_type != VAR_DICT))
6012 clear_tv(&item->li_tv);
6013 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 }
6015 vim_free(l);
6016}
6017
6018/*
6019 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006020 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006022 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006023listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006029 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006032listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006034 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 vim_free(item);
6036}
6037
6038/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006039 * Remove a list item from a List and free it. Also clears the value.
6040 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006043{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006044 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006045 listitem_free(item);
6046}
6047
6048/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 * Get the number of items in a list.
6050 */
6051 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 if (l == NULL)
6055 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006056 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057}
6058
6059/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006060 * Return TRUE when two lists have exactly the same values.
6061 */
6062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006063list_equal(
6064 list_T *l1,
6065 list_T *l2,
6066 int ic, /* ignore case for strings */
6067 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068{
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (l1 == NULL || l2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (l1 == l2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (list_len(l1) != list_len(l2))
6076 return FALSE;
6077
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 for (item1 = l1->lv_first, item2 = l2->lv_first;
6079 item1 != NULL && item2 != NULL;
6080 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 return FALSE;
6083 return item1 == NULL && item2 == NULL;
6084}
6085
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006086/*
6087 * Return the dictitem that an entry in a hashtable points to.
6088 */
6089 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006091{
6092 return HI2DI(hi);
6093}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006094
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096 * Return TRUE when two dictionaries have exactly the same key/values.
6097 */
6098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099dict_equal(
6100 dict_T *d1,
6101 dict_T *d2,
6102 int ic, /* ignore case for strings */
6103 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 hashitem_T *hi;
6106 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006107 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006109 if (d1 == NULL || d2 == NULL)
6110 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006111 if (d1 == d2)
6112 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113 if (dict_len(d1) != dict_len(d2))
6114 return FALSE;
6115
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006116 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006119 if (!HASHITEM_EMPTY(hi))
6120 {
6121 item2 = dict_find(d2, hi->hi_key, -1);
6122 if (item2 == NULL)
6123 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006125 return FALSE;
6126 --todo;
6127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 }
6129 return TRUE;
6130}
6131
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006132static int tv_equal_recurse_limit;
6133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006137 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138 */
6139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140tv_equal(
6141 typval_T *tv1,
6142 typval_T *tv2,
6143 int ic, /* ignore case */
6144 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145{
6146 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006147 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006148 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006149 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006150
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006151 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006154 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006155 * recursiveness to a limit. We guess they are equal then.
6156 * A fixed limit has the problem of still taking an awful long time.
6157 * Reduce the limit every time running into it. That should work fine for
6158 * deeply linked structures that are not recursively linked and catch
6159 * recursiveness quickly. */
6160 if (!recursive)
6161 tv_equal_recurse_limit = 1000;
6162 if (recursive_cnt >= tv_equal_recurse_limit)
6163 {
6164 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006165 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167
6168 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006169 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006170 case VAR_UNKNOWN:
6171 break;
6172
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 ++recursive_cnt;
6175 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6176 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178
6179 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 ++recursive_cnt;
6181 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6182 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006183 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184
6185 case VAR_FUNC:
6186 return (tv1->vval.v_string != NULL
6187 && tv2->vval.v_string != NULL
6188 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6189
6190 case VAR_NUMBER:
6191 return tv1->vval.v_number == tv2->vval.v_number;
6192
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006193#ifdef FEAT_FLOAT
6194 case VAR_FLOAT:
6195 return tv1->vval.v_float == tv2->vval.v_float;
6196#endif
6197
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_STRING:
6199 s1 = get_tv_string_buf(tv1, buf1);
6200 s2 = get_tv_string_buf(tv2, buf2);
6201 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006202
6203 case VAR_SPECIAL:
6204 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206
Bram Moolenaara03f2332016-02-06 18:09:59 +01006207 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6208 * does not equal anything, not even itself. */
6209 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210}
6211
6212/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 * Locate item with index "n" in list "l" and return it.
6214 * A negative index is counted from the end; -1 is the last item.
6215 * Returns NULL when "n" is out of range.
6216 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006217 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006218list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219{
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 long idx;
6222
6223 if (l == NULL)
6224 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006225
6226 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228 n = l->lv_len + n;
6229
6230 /* Check for index out of range. */
6231 if (n < 0 || n >= l->lv_len)
6232 return NULL;
6233
6234 /* When there is a cached index may start search from there. */
6235 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006237 if (n < l->lv_idx / 2)
6238 {
6239 /* closest to the start of the list */
6240 item = l->lv_first;
6241 idx = 0;
6242 }
6243 else if (n > (l->lv_idx + l->lv_len) / 2)
6244 {
6245 /* closest to the end of the list */
6246 item = l->lv_last;
6247 idx = l->lv_len - 1;
6248 }
6249 else
6250 {
6251 /* closest to the cached index */
6252 item = l->lv_idx_item;
6253 idx = l->lv_idx;
6254 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 }
6256 else
6257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 if (n < l->lv_len / 2)
6259 {
6260 /* closest to the start of the list */
6261 item = l->lv_first;
6262 idx = 0;
6263 }
6264 else
6265 {
6266 /* closest to the end of the list */
6267 item = l->lv_last;
6268 idx = l->lv_len - 1;
6269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271
6272 while (n > idx)
6273 {
6274 /* search forward */
6275 item = item->li_next;
6276 ++idx;
6277 }
6278 while (n < idx)
6279 {
6280 /* search backward */
6281 item = item->li_prev;
6282 --idx;
6283 }
6284
6285 /* cache the used index */
6286 l->lv_idx = idx;
6287 l->lv_idx_item = item;
6288
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 return item;
6290}
6291
6292/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006293 * Get list item "l[idx]" as a number.
6294 */
6295 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006296list_find_nr(
6297 list_T *l,
6298 long idx,
6299 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006300{
6301 listitem_T *li;
6302
6303 li = list_find(l, idx);
6304 if (li == NULL)
6305 {
6306 if (errorp != NULL)
6307 *errorp = TRUE;
6308 return -1L;
6309 }
6310 return get_tv_number_chk(&li->li_tv, errorp);
6311}
6312
6313/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006314 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6315 */
6316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006318{
6319 listitem_T *li;
6320
6321 li = list_find(l, idx - 1);
6322 if (li == NULL)
6323 {
6324 EMSGN(_(e_listidx), idx);
6325 return NULL;
6326 }
6327 return get_tv_string(&li->li_tv);
6328}
6329
6330/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006331 * Locate "item" list "l" and return its index.
6332 * Returns -1 when "item" is not in the list.
6333 */
6334 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006336{
6337 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006339
6340 if (l == NULL)
6341 return -1;
6342 idx = 0;
6343 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6344 ++idx;
6345 if (li == NULL)
6346 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006347 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006348}
6349
6350/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 * Append item "item" to the end of list "l".
6352 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355{
6356 if (l->lv_last == NULL)
6357 {
6358 /* empty list */
6359 l->lv_first = item;
6360 l->lv_last = item;
6361 item->li_prev = NULL;
6362 }
6363 else
6364 {
6365 l->lv_last->li_next = item;
6366 item->li_prev = l->lv_last;
6367 l->lv_last = item;
6368 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 item->li_next = NULL;
6371}
6372
6373/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006380 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
Bram Moolenaar05159a02005-02-26 23:04:13 +00006382 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006384 copy_tv(tv, &li->li_tv);
6385 list_append(l, li);
6386 return OK;
6387}
6388
6389/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006390 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006391 * Return FAIL when out of memory.
6392 */
6393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006394list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006395{
6396 listitem_T *li = listitem_alloc();
6397
6398 if (li == NULL)
6399 return FAIL;
6400 li->li_tv.v_type = VAR_DICT;
6401 li->li_tv.v_lock = 0;
6402 li->li_tv.vval.v_dict = dict;
6403 list_append(list, li);
6404 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 return OK;
6406}
6407
6408/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006409 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006410 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006411 * Returns FAIL when out of memory.
6412 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006415{
6416 listitem_T *li = listitem_alloc();
6417
6418 if (li == NULL)
6419 return FAIL;
6420 list_append(l, li);
6421 li->li_tv.v_type = VAR_STRING;
6422 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006423 if (str == NULL)
6424 li->li_tv.vval.v_string = NULL;
6425 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006426 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006427 return FAIL;
6428 return OK;
6429}
6430
6431/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006432 * Append "n" to list "l".
6433 * Returns FAIL when out of memory.
6434 */
6435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006437{
6438 listitem_T *li;
6439
6440 li = listitem_alloc();
6441 if (li == NULL)
6442 return FAIL;
6443 li->li_tv.v_type = VAR_NUMBER;
6444 li->li_tv.v_lock = 0;
6445 li->li_tv.vval.v_number = n;
6446 list_append(l, li);
6447 return OK;
6448}
6449
6450/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 * If "item" is NULL append at the end.
6453 * Return FAIL when out of memory.
6454 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459
6460 if (ni == NULL)
6461 return FAIL;
6462 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006463 list_insert(l, ni, item);
6464 return OK;
6465}
6466
6467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006468list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006469{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 if (item == NULL)
6471 /* Append new item at end of list. */
6472 list_append(l, ni);
6473 else
6474 {
6475 /* Insert new item before existing item. */
6476 ni->li_prev = item->li_prev;
6477 ni->li_next = item;
6478 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006479 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006481 ++l->lv_idx;
6482 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006484 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006486 l->lv_idx_item = NULL;
6487 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006489 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491}
6492
6493/*
6494 * Extend "l1" with "l2".
6495 * If "bef" is NULL append at the end, otherwise insert before this item.
6496 * Returns FAIL when out of memory.
6497 */
6498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006499list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006502 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006504 /* We also quit the loop when we have inserted the original item count of
6505 * the list, avoid a hang when we extend a list with itself. */
6506 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6508 return FAIL;
6509 return OK;
6510}
6511
6512/*
6513 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6514 * Return FAIL when out of memory.
6515 */
6516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006521 if (l1 == NULL || l2 == NULL)
6522 return FAIL;
6523
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 if (l == NULL)
6527 return FAIL;
6528 tv->v_type = VAR_LIST;
6529 tv->vval.v_list = l;
6530
6531 /* append all items from the second list */
6532 return list_extend(l, l2, NULL);
6533}
6534
6535/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 * Returns NULL when out of memory.
6540 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543{
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 list_T *copy;
6545 listitem_T *item;
6546 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547
6548 if (orig == NULL)
6549 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550
6551 copy = list_alloc();
6552 if (copy != NULL)
6553 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 if (copyID != 0)
6555 {
6556 /* Do this before adding the items, because one of the items may
6557 * refer back to this list. */
6558 orig->lv_copyID = copyID;
6559 orig->lv_copylist = copy;
6560 }
6561 for (item = orig->lv_first; item != NULL && !got_int;
6562 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 {
6564 ni = listitem_alloc();
6565 if (ni == NULL)
6566 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 {
6569 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6570 {
6571 vim_free(ni);
6572 break;
6573 }
6574 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 list_append(copy, ni);
6578 }
6579 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 if (item != NULL)
6581 {
6582 list_unref(copy);
6583 copy = NULL;
6584 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 }
6586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 return copy;
6588}
6589
6590/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006592 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006593 * This used to be called list_remove, but that conflicts with a Sun header
6594 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601 /* notify watchers */
6602 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006604 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006605 list_fix_watch(l, ip);
6606 if (ip == item2)
6607 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006609
6610 if (item2->li_next == NULL)
6611 l->lv_last = item->li_prev;
6612 else
6613 item2->li_next->li_prev = item->li_prev;
6614 if (item->li_prev == NULL)
6615 l->lv_first = item2->li_next;
6616 else
6617 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006618 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619}
6620
6621/*
6622 * Return an allocated string with the string representation of a list.
6623 * May return NULL.
6624 */
6625 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627{
6628 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629
6630 if (tv->vval.v_list == NULL)
6631 return NULL;
6632 ga_init2(&ga, (int)sizeof(char), 80);
6633 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006634 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006635 {
6636 vim_free(ga.ga_data);
6637 return NULL;
6638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 ga_append(&ga, ']');
6640 ga_append(&ga, NUL);
6641 return (char_u *)ga.ga_data;
6642}
6643
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006644typedef struct join_S {
6645 char_u *s;
6646 char_u *tofree;
6647} join_T;
6648
6649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650list_join_inner(
6651 garray_T *gap, /* to store the result in */
6652 list_T *l,
6653 char_u *sep,
6654 int echo_style,
6655 int copyID,
6656 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006657{
6658 int i;
6659 join_T *p;
6660 int len;
6661 int sumlen = 0;
6662 int first = TRUE;
6663 char_u *tofree;
6664 char_u numbuf[NUMBUFLEN];
6665 listitem_T *item;
6666 char_u *s;
6667
6668 /* Stringify each item in the list. */
6669 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6670 {
6671 if (echo_style)
6672 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6673 else
6674 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6675 if (s == NULL)
6676 return FAIL;
6677
6678 len = (int)STRLEN(s);
6679 sumlen += len;
6680
Bram Moolenaarcde88542015-08-11 19:14:00 +02006681 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006682 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6683 if (tofree != NULL || s != numbuf)
6684 {
6685 p->s = s;
6686 p->tofree = tofree;
6687 }
6688 else
6689 {
6690 p->s = vim_strnsave(s, len);
6691 p->tofree = p->s;
6692 }
6693
6694 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006695 if (did_echo_string_emsg) /* recursion error, bail out */
6696 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 }
6698
6699 /* Allocate result buffer with its total size, avoid re-allocation and
6700 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6701 if (join_gap->ga_len >= 2)
6702 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6703 if (ga_grow(gap, sumlen + 2) == FAIL)
6704 return FAIL;
6705
6706 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6707 {
6708 if (first)
6709 first = FALSE;
6710 else
6711 ga_concat(gap, sep);
6712 p = ((join_T *)join_gap->ga_data) + i;
6713
6714 if (p->s != NULL)
6715 ga_concat(gap, p->s);
6716 line_breakcheck();
6717 }
6718
6719 return OK;
6720}
6721
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006722/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006724 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006725 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006726 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006728list_join(
6729 garray_T *gap,
6730 list_T *l,
6731 char_u *sep,
6732 int echo_style,
6733 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006735 garray_T join_ga;
6736 int retval;
6737 join_T *p;
6738 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739
Bram Moolenaard39a7512015-04-16 22:51:22 +02006740 if (l->lv_len < 1)
6741 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006742 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6743 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6744
6745 /* Dispose each item in join_ga. */
6746 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006747 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006748 p = (join_T *)join_ga.ga_data;
6749 for (i = 0; i < join_ga.ga_len; ++i)
6750 {
6751 vim_free(p->tofree);
6752 ++p;
6753 }
6754 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006755 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006756
6757 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758}
6759
6760/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006761 * Return the next (unique) copy ID.
6762 * Used for serializing nested structures.
6763 */
6764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006766{
6767 current_copyID += COPYID_INC;
6768 return current_copyID;
6769}
6770
6771/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 * Garbage collection for lists and dictionaries.
6773 *
6774 * We use reference counts to be able to free most items right away when they
6775 * are no longer used. But for composite items it's possible that it becomes
6776 * unused while the reference count is > 0: When there is a recursive
6777 * reference. Example:
6778 * :let l = [1, 2, 3]
6779 * :let d = {9: l}
6780 * :let l[1] = d
6781 *
6782 * Since this is quite unusual we handle this with garbage collection: every
6783 * once in a while find out which lists and dicts are not referenced from any
6784 * variable.
6785 *
6786 * Here is a good reference text about garbage collection (refers to Python
6787 * but it applies to all reference-counting mechanisms):
6788 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006789 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006790
6791/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 * Do garbage collection for lists and dicts.
6793 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006796garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006797{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006799 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006800 buf_T *buf;
6801 win_T *wp;
6802 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006803 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006804 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006805 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006806#ifdef FEAT_WINDOWS
6807 tabpage_T *tp;
6808#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006810 /* Only do this once. */
6811 want_garbage_collect = FALSE;
6812 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006813 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006814
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006815 /* We advance by two because we add one for items referenced through
6816 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006817 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 /*
6820 * 1. Go through all accessible variables and mark all lists and dicts
6821 * with copyID.
6822 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006823
6824 /* Don't free variables in the previous_funccal list unless they are only
6825 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006826 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6828 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006829 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6830 NULL);
6831 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6832 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 }
6834
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 /* script-local variables */
6836 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006837 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838
6839 /* buffer-local variables */
6840 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6842 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843
6844 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006845 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006846 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6847 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006848#ifdef FEAT_AUTOCMD
6849 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006850 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6851 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006852#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006854#ifdef FEAT_WINDOWS
6855 /* tabpage-local variables */
6856 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006857 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6858 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006859#endif
6860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
6864 /* function-local variables */
6865 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6866 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006867 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6868 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 }
6870
Bram Moolenaard812df62008-11-09 12:46:09 +00006871 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006873
Bram Moolenaar1dced572012-04-05 16:54:08 +02006874#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006876#endif
6877
Bram Moolenaardb913952012-06-29 12:54:53 +02006878#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006880#endif
6881
6882#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006884#endif
6885
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006886#ifdef FEAT_CHANNEL
6887 abort = abort || set_ref_in_channel(copyID);
6888#endif
6889
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 /*
6893 * 2. Free lists and dictionaries that are not referenced.
6894 */
6895 did_free = free_unref_items(copyID);
6896
6897 /*
6898 * 3. Check if any funccal can be freed now.
6899 */
6900 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006901 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 if (can_free_funccal(*pfc, copyID))
6903 {
6904 fc = *pfc;
6905 *pfc = fc->caller;
6906 free_funccal(fc, TRUE);
6907 did_free = TRUE;
6908 did_free_funccal = TRUE;
6909 }
6910 else
6911 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006912 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 if (did_free_funccal)
6914 /* When a funccal was freed some more items might be garbage
6915 * collected, so run again. */
6916 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006917 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 else if (p_verbose > 0)
6919 {
6920 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6921 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006922
6923 return did_free;
6924}
6925
6926/*
6927 * Free lists and dictionaries that are no longer referenced.
6928 */
6929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006931{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006932 dict_T *dd, *dd_next;
6933 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 int did_free = FALSE;
6935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 */
6939 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006940 {
6941 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006942 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006944 /* Free the Dictionary and ordinary items it contains, but don't
6945 * recurse into Lists and Dictionaries, they will be in the list
6946 * of dicts or list of lists. */
6947 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006950 dd = dd_next;
6951 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952
6953 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 * Go through the list of lists and free items without the copyID.
6955 * But don't free a list that has a watcher (used in a for loop), these
6956 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 */
6958 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006959 {
6960 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6962 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006964 /* Free the List and ordinary items it contains, but don't recurse
6965 * into Lists and Dictionaries, they will be in the list of dicts
6966 * or list of lists. */
6967 list_free(ll, 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 ll = ll_next;
6971 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 return did_free;
6973}
6974
6975/*
6976 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 * "list_stack" is used to add lists to be marked. Can be NULL.
6978 *
6979 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983{
6984 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006987 hashtab_T *cur_ht;
6988 ht_stack_T *ht_stack = NULL;
6989 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 cur_ht = ht;
6992 for (;;)
6993 {
6994 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 /* Mark each item in the hashtab. If the item contains a hashtab
6997 * it is added to ht_stack, if it contains a list it is added to
6998 * list_stack. */
6999 todo = (int)cur_ht->ht_used;
7000 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7001 if (!HASHITEM_EMPTY(hi))
7002 {
7003 --todo;
7004 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7005 &ht_stack, list_stack);
7006 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008
7009 if (ht_stack == NULL)
7010 break;
7011
7012 /* take an item from the stack */
7013 cur_ht = ht_stack->ht;
7014 tempitem = ht_stack;
7015 ht_stack = ht_stack->prev;
7016 free(tempitem);
7017 }
7018
7019 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020}
7021
7022/*
7023 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7025 *
7026 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007029set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 listitem_T *li;
7032 int abort = FALSE;
7033 list_T *cur_l;
7034 list_stack_T *list_stack = NULL;
7035 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 cur_l = l;
7038 for (;;)
7039 {
7040 if (!abort)
7041 /* Mark each item in the list. If the item contains a hashtab
7042 * it is added to ht_stack, if it contains a list it is added to
7043 * list_stack. */
7044 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7045 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7046 ht_stack, &list_stack);
7047 if (list_stack == NULL)
7048 break;
7049
7050 /* take an item from the stack */
7051 cur_l = list_stack->list;
7052 tempitem = list_stack;
7053 list_stack = list_stack->prev;
7054 free(tempitem);
7055 }
7056
7057 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058}
7059
7060/*
7061 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "list_stack" is used to add lists to be marked. Can be NULL.
7063 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7064 *
7065 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068set_ref_in_item(
7069 typval_T *tv,
7070 int copyID,
7071 ht_stack_T **ht_stack,
7072 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
7074 dict_T *dd;
7075 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077
Bram Moolenaara03f2332016-02-06 18:09:59 +01007078 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007079 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007080 dd = tv->vval.v_dict;
7081 if (dd != NULL && dd->dv_copyID != copyID)
7082 {
7083 /* Didn't see this dict yet. */
7084 dd->dv_copyID = copyID;
7085 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007086 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007087 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7088 }
7089 else
7090 {
7091 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7092 if (newitem == NULL)
7093 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 else
7095 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007096 newitem->ht = &dd->dv_hashtab;
7097 newitem->prev = *ht_stack;
7098 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007100 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007101 }
7102 }
7103 else if (tv->v_type == VAR_LIST)
7104 {
7105 ll = tv->vval.v_list;
7106 if (ll != NULL && ll->lv_copyID != copyID)
7107 {
7108 /* Didn't see this list yet. */
7109 ll->lv_copyID = copyID;
7110 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007111 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007112 abort = set_ref_in_list(ll, copyID, ht_stack);
7113 }
7114 else
7115 {
7116 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 if (newitem == NULL)
7119 abort = TRUE;
7120 else
7121 {
7122 newitem->list = ll;
7123 newitem->prev = *list_stack;
7124 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130}
7131
7132/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 * Allocate an empty header for a dictionary.
7134 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007135 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007136dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137{
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007139
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007142 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007143 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144 if (first_dict != NULL)
7145 first_dict->dv_used_prev = d;
7146 d->dv_used_next = first_dict;
7147 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007148 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007149
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007151 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007152 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007153 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007154 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007155 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007156 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157}
7158
7159/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007160 * Allocate an empty dict for a return value.
7161 * Returns OK or FAIL.
7162 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007165{
7166 dict_T *d = dict_alloc();
7167
7168 if (d == NULL)
7169 return FAIL;
7170
7171 rettv->vval.v_dict = d;
7172 rettv->v_type = VAR_DICT;
7173 ++d->dv_refcount;
7174 return OK;
7175}
7176
7177
7178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 * Unreference a Dictionary: decrement the reference count and free it when it
7180 * becomes zero.
7181 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007183dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007185 if (d != NULL && --d->dv_refcount <= 0)
7186 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187}
7188
7189/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007190 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191 * Ignores the reference count.
7192 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194dict_free(
7195 dict_T *d,
7196 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007200 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007202 /* Remove the dict from the list of dicts for garbage collection. */
7203 if (d->dv_used_prev == NULL)
7204 first_dict = d->dv_used_next;
7205 else
7206 d->dv_used_prev->dv_used_next = d->dv_used_next;
7207 if (d->dv_used_next != NULL)
7208 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7209
7210 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007211 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007212 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 if (!HASHITEM_EMPTY(hi))
7216 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007217 /* Remove the item before deleting it, just in case there is
7218 * something recursive causing trouble. */
7219 di = HI2DI(hi);
7220 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 if (recurse || (di->di_tv.v_type != VAR_LIST
7222 && di->di_tv.v_type != VAR_DICT))
7223 clear_tv(&di->di_tv);
7224 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225 --todo;
7226 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 vim_free(d);
7230}
7231
7232/*
7233 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007234 * The "key" is copied to the new item.
7235 * Note that the value of the item "di_tv" still needs to be initialized!
7236 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007238 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007239dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007243 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007247 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250}
7251
7252/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 * Make a copy of a Dictionary item.
7254 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007256dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007257{
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007260 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7261 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007262 if (di != NULL)
7263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007265 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266 copy_tv(&org->di_tv, &di->di_tv);
7267 }
7268 return di;
7269}
7270
7271/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 * Remove item "item" from Dictionary "dict" and free it.
7273 */
7274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276{
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (HASHITEM_EMPTY(hi))
7281 EMSG2(_(e_intern2), "dictitem_remove()");
7282 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 dictitem_free(item);
7285}
7286
7287/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 * Free a dict item. Also clears the value.
7289 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007294 if (item->di_flags & DI_FLAGS_ALLOC)
7295 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296}
7297
7298/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7300 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007301 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 * Returns NULL when out of memory.
7303 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dict_T *copy;
7308 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311
7312 if (orig == NULL)
7313 return NULL;
7314
7315 copy = dict_alloc();
7316 if (copy != NULL)
7317 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007318 if (copyID != 0)
7319 {
7320 orig->dv_copyID = copyID;
7321 orig->dv_copydict = copy;
7322 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007323 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007324 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 --todo;
7329
7330 di = dictitem_alloc(hi->hi_key);
7331 if (di == NULL)
7332 break;
7333 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007334 {
7335 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7336 copyID) == FAIL)
7337 {
7338 vim_free(di);
7339 break;
7340 }
7341 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 else
7343 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7344 if (dict_add(copy, di) == FAIL)
7345 {
7346 dictitem_free(di);
7347 break;
7348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007353 if (todo > 0)
7354 {
7355 dict_unref(copy);
7356 copy = NULL;
7357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 }
7359
7360 return copy;
7361}
7362
7363/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007365 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007368dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369{
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371}
7372
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007374 * Add a number or string entry to dictionary "d".
7375 * When "str" is NULL use number "nr", otherwise use "str".
7376 * Returns FAIL when out of memory and when key already exists.
7377 */
7378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007379dict_add_nr_str(
7380 dict_T *d,
7381 char *key,
7382 long nr,
7383 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007384{
7385 dictitem_T *item;
7386
7387 item = dictitem_alloc((char_u *)key);
7388 if (item == NULL)
7389 return FAIL;
7390 item->di_tv.v_lock = 0;
7391 if (str == NULL)
7392 {
7393 item->di_tv.v_type = VAR_NUMBER;
7394 item->di_tv.vval.v_number = nr;
7395 }
7396 else
7397 {
7398 item->di_tv.v_type = VAR_STRING;
7399 item->di_tv.vval.v_string = vim_strsave(str);
7400 }
7401 if (dict_add(d, item) == FAIL)
7402 {
7403 dictitem_free(item);
7404 return FAIL;
7405 }
7406 return OK;
7407}
7408
7409/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007410 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007411 * Returns FAIL when out of memory and when key already exists.
7412 */
7413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007414dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007415{
7416 dictitem_T *item;
7417
7418 item = dictitem_alloc((char_u *)key);
7419 if (item == NULL)
7420 return FAIL;
7421 item->di_tv.v_lock = 0;
7422 item->di_tv.v_type = VAR_LIST;
7423 item->di_tv.vval.v_list = list;
7424 if (dict_add(d, item) == FAIL)
7425 {
7426 dictitem_free(item);
7427 return FAIL;
7428 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007429 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007430 return OK;
7431}
7432
7433/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 * Get the number of items in a Dictionary.
7435 */
7436 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007437dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007438{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007439 if (d == NULL)
7440 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007441 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442}
7443
7444/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445 * Find item "key[len]" in Dictionary "d".
7446 * If "len" is negative use strlen(key).
7447 * Returns NULL when not found.
7448 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007449 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452#define AKEYLEN 200
7453 char_u buf[AKEYLEN];
7454 char_u *akey;
7455 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 if (len < 0)
7459 akey = key;
7460 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 tofree = akey = vim_strnsave(key, len);
7463 if (akey == NULL)
7464 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007465 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007466 else
7467 {
7468 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007469 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007470 akey = buf;
7471 }
7472
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 vim_free(tofree);
7475 if (HASHITEM_EMPTY(hi))
7476 return NULL;
7477 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478}
7479
7480/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007481 * Get a string item from a dictionary.
7482 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007483 * Returns NULL if the entry doesn't exist or out of memory.
7484 */
7485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007487{
7488 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007489 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007490
7491 di = dict_find(d, key, -1);
7492 if (di == NULL)
7493 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007494 s = get_tv_string(&di->di_tv);
7495 if (save && s != NULL)
7496 s = vim_strsave(s);
7497 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007498}
7499
7500/*
7501 * Get a number item from a dictionary.
7502 * Returns 0 if the entry doesn't exist or out of memory.
7503 */
7504 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007506{
7507 dictitem_T *di;
7508
7509 di = dict_find(d, key, -1);
7510 if (di == NULL)
7511 return 0;
7512 return get_tv_number(&di->di_tv);
7513}
7514
7515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 * Return an allocated string with the string representation of a Dictionary.
7517 * May return NULL.
7518 */
7519 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007520dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521{
7522 garray_T ga;
7523 int first = TRUE;
7524 char_u *tofree;
7525 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 return NULL;
7533 ga_init2(&ga, (int)sizeof(char), 80);
7534 ga_append(&ga, '{');
7535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007536 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007537 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 --todo;
7542
7543 if (first)
7544 first = FALSE;
7545 else
7546 ga_concat(&ga, (char_u *)", ");
7547
7548 tofree = string_quote(hi->hi_key, FALSE);
7549 if (tofree != NULL)
7550 {
7551 ga_concat(&ga, tofree);
7552 vim_free(tofree);
7553 }
7554 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007555 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 if (s != NULL)
7557 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007559 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007560 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007561 line_breakcheck();
7562
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007565 if (todo > 0)
7566 {
7567 vim_free(ga.ga_data);
7568 return NULL;
7569 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570
7571 ga_append(&ga, '}');
7572 ga_append(&ga, NUL);
7573 return (char_u *)ga.ga_data;
7574}
7575
7576/*
7577 * Allocate a variable for a Dictionary and fill it from "*arg".
7578 * Return OK or FAIL. Returns NOTDONE for {expr}.
7579 */
7580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582{
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 dict_T *d = NULL;
7584 typval_T tvkey;
7585 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007586 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590
7591 /*
7592 * First check if it's not a curly-braces thing: {expr}.
7593 * Must do this without evaluating, otherwise a function may be called
7594 * twice. Unfortunately this means we need to call eval1() twice for the
7595 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007596 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 if (*start != '}')
7599 {
7600 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7601 return FAIL;
7602 if (*start == '}')
7603 return NOTDONE;
7604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605
7606 if (evaluate)
7607 {
7608 d = dict_alloc();
7609 if (d == NULL)
7610 return FAIL;
7611 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 tvkey.v_type = VAR_UNKNOWN;
7613 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614
7615 *arg = skipwhite(*arg + 1);
7616 while (**arg != '}' && **arg != NUL)
7617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 goto failret;
7620 if (**arg != ':')
7621 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007622 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 goto failret;
7625 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007626 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007628 key = get_tv_string_buf_chk(&tvkey, buf);
7629 if (key == NULL || *key == NUL)
7630 {
7631 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7632 if (key != NULL)
7633 EMSG(_(e_emptykey));
7634 clear_tv(&tvkey);
7635 goto failret;
7636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638
7639 *arg = skipwhite(*arg + 1);
7640 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7641 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007642 if (evaluate)
7643 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 goto failret;
7645 }
7646 if (evaluate)
7647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 if (item != NULL)
7650 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007651 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 clear_tv(&tv);
7654 goto failret;
7655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 item = dictitem_alloc(key);
7657 clear_tv(&tvkey);
7658 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007661 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 if (dict_add(d, item) == FAIL)
7663 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 }
7665 }
7666
7667 if (**arg == '}')
7668 break;
7669 if (**arg != ',')
7670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007671 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 goto failret;
7673 }
7674 *arg = skipwhite(*arg + 1);
7675 }
7676
7677 if (**arg != '}')
7678 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007679 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680failret:
7681 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007682 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 return FAIL;
7684 }
7685
7686 *arg = skipwhite(*arg + 1);
7687 if (evaluate)
7688 {
7689 rettv->v_type = VAR_DICT;
7690 rettv->vval.v_dict = d;
7691 ++d->dv_refcount;
7692 }
7693
7694 return OK;
7695}
7696
Bram Moolenaar17a13432016-01-24 14:22:10 +01007697 static char *
7698get_var_special_name(int nr)
7699{
7700 switch (nr)
7701 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007702 case VVAL_FALSE: return "v:false";
7703 case VVAL_TRUE: return "v:true";
7704 case VVAL_NONE: return "v:none";
7705 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007706 }
7707 EMSG2(_(e_intern2), "get_var_special_name()");
7708 return "42";
7709}
7710
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 * Return a string with the string representation of a variable.
7713 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007714 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007716 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007717 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 */
7719 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720echo_string(
7721 typval_T *tv,
7722 char_u **tofree,
7723 char_u *numbuf,
7724 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726 static int recurse = 0;
7727 char_u *r = NULL;
7728
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007730 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007731 if (!did_echo_string_emsg)
7732 {
7733 /* Only give this message once for a recursive call to avoid
7734 * flooding the user with errors. And stop iterating over lists
7735 * and dicts. */
7736 did_echo_string_emsg = TRUE;
7737 EMSG(_("E724: variable nested too deep for displaying"));
7738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007740 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 }
7742 ++recurse;
7743
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007744 switch (tv->v_type)
7745 {
7746 case VAR_FUNC:
7747 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748 r = tv->vval.v_string;
7749 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007750
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007751 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007752 if (tv->vval.v_list == NULL)
7753 {
7754 *tofree = NULL;
7755 r = NULL;
7756 }
7757 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7758 {
7759 *tofree = NULL;
7760 r = (char_u *)"[...]";
7761 }
7762 else
7763 {
7764 tv->vval.v_list->lv_copyID = copyID;
7765 *tofree = list2string(tv, copyID);
7766 r = *tofree;
7767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007768 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007769
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007771 if (tv->vval.v_dict == NULL)
7772 {
7773 *tofree = NULL;
7774 r = NULL;
7775 }
7776 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7777 {
7778 *tofree = NULL;
7779 r = (char_u *)"{...}";
7780 }
7781 else
7782 {
7783 tv->vval.v_dict->dv_copyID = copyID;
7784 *tofree = dict2string(tv, copyID);
7785 r = *tofree;
7786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 case VAR_STRING:
7790 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007791 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007792 *tofree = NULL;
7793 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007795
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007796#ifdef FEAT_FLOAT
7797 case VAR_FLOAT:
7798 *tofree = NULL;
7799 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7800 r = numbuf;
7801 break;
7802#endif
7803
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007804 case VAR_SPECIAL:
7805 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007806 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007807 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809
Bram Moolenaar8502c702014-06-17 12:51:16 +02007810 if (--recurse == 0)
7811 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007813}
7814
7815/*
7816 * Return a string with the string representation of a variable.
7817 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7818 * "numbuf" is used for a number.
7819 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007820 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007821 */
7822 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007823tv2string(
7824 typval_T *tv,
7825 char_u **tofree,
7826 char_u *numbuf,
7827 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007828{
7829 switch (tv->v_type)
7830 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007831 case VAR_FUNC:
7832 *tofree = string_quote(tv->vval.v_string, TRUE);
7833 return *tofree;
7834 case VAR_STRING:
7835 *tofree = string_quote(tv->vval.v_string, FALSE);
7836 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007837#ifdef FEAT_FLOAT
7838 case VAR_FLOAT:
7839 *tofree = NULL;
7840 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7841 return numbuf;
7842#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007844 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007845 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007846 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007847 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007849 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007850 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007851}
7852
7853/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 * Return string "str" in ' quotes, doubling ' characters.
7855 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 */
7858 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007859string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007860{
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007862 char_u *p, *r, *s;
7863
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 len = (function ? 13 : 3);
7865 if (str != NULL)
7866 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007867 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 for (p = str; *p != NUL; mb_ptr_adv(p))
7869 if (*p == '\'')
7870 ++len;
7871 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 s = r = alloc(len);
7873 if (r != NULL)
7874 {
7875 if (function)
7876 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007877 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 r += 10;
7879 }
7880 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 if (str != NULL)
7883 for (p = str; *p != NUL; )
7884 {
7885 if (*p == '\'')
7886 *r++ = '\'';
7887 MB_COPY_CHAR(p, r);
7888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007889 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 if (function)
7891 *r++ = ')';
7892 *r++ = NUL;
7893 }
7894 return s;
7895}
7896
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007897#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898/*
7899 * Convert the string "text" to a floating point number.
7900 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7901 * this always uses a decimal point.
7902 * Returns the length of the text that was consumed.
7903 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007904 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007905string2float(
7906 char_u *text,
7907 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908{
7909 char *s = (char *)text;
7910 float_T f;
7911
7912 f = strtod(s, &s);
7913 *value = f;
7914 return (int)((char_u *)s - text);
7915}
7916#endif
7917
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * Get the value of an environment variable.
7920 * "arg" is pointing to the '$'. It is advanced to after the name.
7921 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007922 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 */
7924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007925get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 char_u *string = NULL;
7928 int len;
7929 int cc;
7930 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007931 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 ++*arg;
7934 name = *arg;
7935 len = get_env_len(arg);
7936 if (evaluate)
7937 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007938 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007939 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007940
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007941 cc = name[len];
7942 name[len] = NUL;
7943 /* first try vim_getenv(), fast for normal environment vars */
7944 string = vim_getenv(name, &mustfree);
7945 if (string != NULL && *string != NUL)
7946 {
7947 if (!mustfree)
7948 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007950 else
7951 {
7952 if (mustfree)
7953 vim_free(string);
7954
7955 /* next try expanding things like $VIM and ${HOME} */
7956 string = expand_env_save(name - 1);
7957 if (string != NULL && *string == '$')
7958 {
7959 vim_free(string);
7960 string = NULL;
7961 }
7962 }
7963 name[len] = cc;
7964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->v_type = VAR_STRING;
7966 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
7968
7969 return OK;
7970}
7971
7972/*
7973 * Array with names and number of arguments of all internal functions
7974 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7975 */
7976static struct fst
7977{
7978 char *f_name; /* function name */
7979 char f_min_argc; /* minimal number of arguments */
7980 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007981 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007982 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983} functions[] =
7984{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007987 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007989 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007990 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007991 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"append", 2, 2, f_append},
7993 {"argc", 0, 0, f_argc},
7994 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007995 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007996 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007997#ifdef FEAT_FLOAT
7998 {"asin", 1, 1, f_asin}, /* WJMc */
7999#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008000 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008001 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008002 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008003 {"assert_false", 1, 2, f_assert_false},
8004 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005#ifdef FEAT_FLOAT
8006 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008007 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008010 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"bufexists", 1, 1, f_bufexists},
8012 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8013 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8014 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8015 {"buflisted", 1, 1, f_buflisted},
8016 {"bufloaded", 1, 1, f_bufloaded},
8017 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008018 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {"bufwinnr", 1, 1, f_bufwinnr},
8020 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008021 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008022 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"ceil", 1, 1, f_ceil},
8026#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008027#ifdef FEAT_CHANNEL
8028 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008029 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008030 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8031 {"ch_sendraw", 2, 3, f_ch_sendraw},
8032#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008033 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008034 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008036 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008038#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008039 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008040 {"complete_add", 1, 1, f_complete_add},
8041 {"complete_check", 0, 0, f_complete_check},
8042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008044 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#ifdef FEAT_FLOAT
8046 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008047 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008049 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008051 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008052 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008053 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008055 {"diff_filler", 1, 1, f_diff_filler},
8056 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008057 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"eventhandler", 0, 0, f_eventhandler},
8061 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008062 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064#ifdef FEAT_FLOAT
8065 {"exp", 1, 1, f_exp},
8066#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008067 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008069 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8071 {"filereadable", 1, 1, f_filereadable},
8072 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008073 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008074 {"finddir", 1, 3, f_finddir},
8075 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"float2nr", 1, 1, f_float2nr},
8078 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008081 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"fnamemodify", 2, 2, f_fnamemodify},
8083 {"foldclosed", 1, 1, f_foldclosed},
8084 {"foldclosedend", 1, 1, f_foldclosedend},
8085 {"foldlevel", 1, 1, f_foldlevel},
8086 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008087 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008090 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008091 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008092 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008093 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"getchar", 0, 1, f_getchar},
8095 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008096 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"getcmdline", 0, 0, f_getcmdline},
8098 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008099 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008100 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008101 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008102 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008103 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008104 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"getfsize", 1, 1, f_getfsize},
8106 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008107 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008108 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008109 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008110 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008111 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008112 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008113 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008114 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008116 {"gettabvar", 2, 3, f_gettabvar},
8117 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"getwinposx", 0, 0, f_getwinposx},
8119 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008120 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008121 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008122 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008123 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008126 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008127 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8129 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8130 {"histadd", 2, 2, f_histadd},
8131 {"histdel", 1, 2, f_histdel},
8132 {"histget", 1, 2, f_histget},
8133 {"histnr", 1, 1, f_histnr},
8134 {"hlID", 1, 1, f_hlID},
8135 {"hlexists", 1, 1, f_hlexists},
8136 {"hostname", 0, 0, f_hostname},
8137 {"iconv", 3, 3, f_iconv},
8138 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008139 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008140 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008142 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"inputrestore", 0, 0, f_inputrestore},
8144 {"inputsave", 0, 0, f_inputsave},
8145 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008147 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008149 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008150 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008151 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008152 {"jsondecode", 1, 1, f_jsondecode},
8153 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008154 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"libcall", 3, 3, f_libcall},
8158 {"libcallnr", 3, 3, f_libcallnr},
8159 {"line", 1, 1, f_line},
8160 {"line2byte", 1, 1, f_line2byte},
8161 {"lispindent", 1, 1, f_lispindent},
8162 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008163#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008164 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165 {"log10", 1, 1, f_log10},
8166#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008167#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008168 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008169#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008170 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008171 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008172 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008173 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008174 {"matchadd", 2, 5, f_matchadd},
8175 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008176 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008177 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008178 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008179 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008180 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008181 {"max", 1, 1, f_max},
8182 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008183#ifdef vim_mkdir
8184 {"mkdir", 1, 3, f_mkdir},
8185#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008187#ifdef FEAT_MZSCHEME
8188 {"mzeval", 1, 1, f_mzeval},
8189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008191 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008192 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008193 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008194#ifdef FEAT_PERL
8195 {"perleval", 1, 1, f_perleval},
8196#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008197#ifdef FEAT_FLOAT
8198 {"pow", 2, 2, f_pow},
8199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008201 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008202 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008203#ifdef FEAT_PYTHON3
8204 {"py3eval", 1, 1, f_py3eval},
8205#endif
8206#ifdef FEAT_PYTHON
8207 {"pyeval", 1, 1, f_pyeval},
8208#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008209 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008210 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008211 {"reltime", 0, 2, f_reltime},
8212 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"remote_expr", 2, 3, f_remote_expr},
8214 {"remote_foreground", 1, 1, f_remote_foreground},
8215 {"remote_peek", 1, 2, f_remote_peek},
8216 {"remote_read", 1, 1, f_remote_read},
8217 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008218 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008220 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008222 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008223#ifdef FEAT_FLOAT
8224 {"round", 1, 1, f_round},
8225#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008226 {"screenattr", 2, 2, f_screenattr},
8227 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008228 {"screencol", 0, 0, f_screencol},
8229 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008230 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008231 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008232 {"searchpair", 3, 7, f_searchpair},
8233 {"searchpairpos", 3, 7, f_searchpairpos},
8234 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"server2client", 2, 2, f_server2client},
8236 {"serverlist", 0, 0, f_serverlist},
8237 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008238 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"setcmdpos", 1, 1, f_setcmdpos},
8240 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008241 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008242 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008243 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008244 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008246 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008247 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008249#ifdef FEAT_CRYPT
8250 {"sha256", 1, 1, f_sha256},
8251#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008252 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008253 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255#ifdef FEAT_FLOAT
8256 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008259 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008260 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008261 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008262 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008263 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008264#ifdef FEAT_FLOAT
8265 {"sqrt", 1, 1, f_sqrt},
8266 {"str2float", 1, 1, f_str2float},
8267#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008268 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008269 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008270 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271#ifdef HAVE_STRFTIME
8272 {"strftime", 1, 2, f_strftime},
8273#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"strlen", 1, 1, f_strlen},
8277 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008278 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008280 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008281 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"substitute", 4, 4, f_substitute},
8283 {"synID", 3, 3, f_synID},
8284 {"synIDattr", 2, 3, f_synIDattr},
8285 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008286 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008287 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008288 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008289 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008290 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008291 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008292 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008293 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008294 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008295#ifdef FEAT_FLOAT
8296 {"tan", 1, 1, f_tan},
8297 {"tanh", 1, 1, f_tanh},
8298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008300 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"tolower", 1, 1, f_tolower},
8302 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008303 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008304#ifdef FEAT_FLOAT
8305 {"trunc", 1, 1, f_trunc},
8306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008308 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008309 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008310 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008311 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"virtcol", 1, 1, f_virtcol},
8313 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008314 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"winbufnr", 1, 1, f_winbufnr},
8316 {"wincol", 0, 0, f_wincol},
8317 {"winheight", 1, 1, f_winheight},
8318 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008319 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008321 {"winrestview", 1, 1, f_winrestview},
8322 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008324 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008325 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008326 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327};
8328
8329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8330
8331/*
8332 * Function given to ExpandGeneric() to obtain the list of internal
8333 * or user defined function names.
8334 */
8335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008336get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 static int intidx = -1;
8339 char_u *name;
8340
8341 if (idx == 0)
8342 intidx = -1;
8343 if (intidx < 0)
8344 {
8345 name = get_user_func_name(xp, idx);
8346 if (name != NULL)
8347 return name;
8348 }
8349 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8350 {
8351 STRCPY(IObuff, functions[intidx].f_name);
8352 STRCAT(IObuff, "(");
8353 if (functions[intidx].f_max_argc == 0)
8354 STRCAT(IObuff, ")");
8355 return IObuff;
8356 }
8357
8358 return NULL;
8359}
8360
8361/*
8362 * Function given to ExpandGeneric() to obtain the list of internal or
8363 * user defined variable or function names.
8364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008366get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 static int intidx = -1;
8369 char_u *name;
8370
8371 if (idx == 0)
8372 intidx = -1;
8373 if (intidx < 0)
8374 {
8375 name = get_function_name(xp, idx);
8376 if (name != NULL)
8377 return name;
8378 }
8379 return get_user_var_name(xp, ++intidx);
8380}
8381
8382#endif /* FEAT_CMDL_COMPL */
8383
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008384#if defined(EBCDIC) || defined(PROTO)
8385/*
8386 * Compare struct fst by function name.
8387 */
8388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008389compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008390{
8391 struct fst *p1 = (struct fst *)s1;
8392 struct fst *p2 = (struct fst *)s2;
8393
8394 return STRCMP(p1->f_name, p2->f_name);
8395}
8396
8397/*
8398 * Sort the function table by function name.
8399 * The sorting of the table above is ASCII dependant.
8400 * On machines using EBCDIC we have to sort it.
8401 */
8402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008403sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008404{
8405 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8406
8407 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8408}
8409#endif
8410
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412/*
8413 * Find internal function in table above.
8414 * Return index, or -1 if not found
8415 */
8416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008417find_internal_func(
8418 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 int first = 0;
8421 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8422 int cmp;
8423 int x;
8424
8425 /*
8426 * Find the function name in the table. Binary search.
8427 */
8428 while (first <= last)
8429 {
8430 x = first + ((unsigned)(last - first) >> 1);
8431 cmp = STRCMP(name, functions[x].f_name);
8432 if (cmp < 0)
8433 last = x - 1;
8434 else if (cmp > 0)
8435 first = x + 1;
8436 else
8437 return x;
8438 }
8439 return -1;
8440}
8441
8442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8444 * name it contains, otherwise return "name".
8445 */
8446 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008447deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008448{
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450 int cc;
8451
8452 cc = name[*lenp];
8453 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008454 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008455 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008456 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008459 {
8460 *lenp = 0;
8461 return (char_u *)""; /* just in case */
8462 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008463 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008465 }
8466
8467 return name;
8468}
8469
8470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 * Allocate a variable for the result of a function.
8472 * Return OK or FAIL.
8473 */
8474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008475get_func_tv(
8476 char_u *name, /* name of the function */
8477 int len, /* length of "name" */
8478 typval_T *rettv,
8479 char_u **arg, /* argument, pointing to the '(' */
8480 linenr_T firstline, /* first line of range */
8481 linenr_T lastline, /* last line of range */
8482 int *doesrange, /* return: function handled range */
8483 int evaluate,
8484 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 char_u *argp;
8487 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008488 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 int argcount = 0; /* number of arguments found */
8490
8491 /*
8492 * Get the arguments.
8493 */
8494 argp = *arg;
8495 while (argcount < MAX_FUNC_ARGS)
8496 {
8497 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8498 if (*argp == ')' || *argp == ',' || *argp == NUL)
8499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8501 {
8502 ret = FAIL;
8503 break;
8504 }
8505 ++argcount;
8506 if (*argp != ',')
8507 break;
8508 }
8509 if (*argp == ')')
8510 ++argp;
8511 else
8512 ret = FAIL;
8513
8514 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 {
8519 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008520 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008522 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
8525 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527
8528 *arg = skipwhite(argp);
8529 return ret;
8530}
8531
8532
8533/*
8534 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008535 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008536 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008539call_func(
8540 char_u *funcname, /* name of the function */
8541 int len, /* length of "name" */
8542 typval_T *rettv, /* return value goes here */
8543 int argcount, /* number of "argvars" */
8544 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008545 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008546 linenr_T firstline, /* first line of range */
8547 linenr_T lastline, /* last line of range */
8548 int *doesrange, /* return: function handled range */
8549 int evaluate,
8550 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553#define ERROR_UNKNOWN 0
8554#define ERROR_TOOMANY 1
8555#define ERROR_TOOFEW 2
8556#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557#define ERROR_DICT 4
8558#define ERROR_NONE 5
8559#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 int error = ERROR_NONE;
8561 int i;
8562 int llen;
8563 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564#define FLEN_FIXED 40
8565 char_u fname_buf[FLEN_FIXED + 1];
8566 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008567 char_u *name;
8568
8569 /* Make a copy of the name, if it comes from a funcref variable it could
8570 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008571 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008572 if (name == NULL)
8573 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 /*
8576 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8577 * Change <SNR>123_name() to K_SNR 123_name().
8578 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 llen = eval_fname_script(name);
8581 if (llen > 0)
8582 {
8583 fname_buf[0] = K_SPECIAL;
8584 fname_buf[1] = KS_EXTRA;
8585 fname_buf[2] = (int)KE_SNR;
8586 i = 3;
8587 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8588 {
8589 if (current_SID <= 0)
8590 error = ERROR_SCRIPT;
8591 else
8592 {
8593 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8594 i = (int)STRLEN(fname_buf);
8595 }
8596 }
8597 if (i + STRLEN(name + llen) < FLEN_FIXED)
8598 {
8599 STRCPY(fname_buf + i, name + llen);
8600 fname = fname_buf;
8601 }
8602 else
8603 {
8604 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8605 if (fname == NULL)
8606 error = ERROR_OTHER;
8607 else
8608 {
8609 mch_memmove(fname, fname_buf, (size_t)i);
8610 STRCPY(fname + i, name + llen);
8611 }
8612 }
8613 }
8614 else
8615 fname = name;
8616
8617 *doesrange = FALSE;
8618
8619
8620 /* execute the function if no errors detected and executing */
8621 if (evaluate && error == ERROR_NONE)
8622 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008623 char_u *rfname = fname;
8624
8625 /* Ignore "g:" before a function name. */
8626 if (fname[0] == 'g' && fname[1] == ':')
8627 rfname = fname + 2;
8628
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008629 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8630 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 error = ERROR_UNKNOWN;
8632
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008633 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {
8635 /*
8636 * User defined function.
8637 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008638 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008641 /* Trigger FuncUndefined event, may load the function. */
8642 if (fp == NULL
8643 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008644 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008645 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008647 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008648 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 }
8650#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008651 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008652 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008653 {
8654 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008655 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008656 }
8657
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (fp != NULL)
8659 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008660 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008662 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008664 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008666 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 else
8669 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008670 int did_save_redo = FALSE;
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 /*
8673 * Call the user function.
8674 * Save and restore search patterns, script variables and
8675 * redo buffer.
8676 */
8677 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008678#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008679 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008680#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008681 {
8682 saveRedobuff();
8683 did_save_redo = TRUE;
8684 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008685 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008687 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008688 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8689 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8690 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008691 /* Function was unreferenced while being used, free it
8692 * now. */
8693 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008694 if (did_save_redo)
8695 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 restore_search_patterns();
8697 error = ERROR_NONE;
8698 }
8699 }
8700 }
8701 else
8702 {
8703 /*
8704 * Find the function name in the table, call its implementation.
8705 */
8706 i = find_internal_func(fname);
8707 if (i >= 0)
8708 {
8709 if (argcount < functions[i].f_min_argc)
8710 error = ERROR_TOOFEW;
8711 else if (argcount > functions[i].f_max_argc)
8712 error = ERROR_TOOMANY;
8713 else
8714 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008715 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 error = ERROR_NONE;
8718 }
8719 }
8720 }
8721 /*
8722 * The function call (or "FuncUndefined" autocommand sequence) might
8723 * have been aborted by an error, an interrupt, or an explicitly thrown
8724 * exception that has not been caught so far. This situation can be
8725 * tested for by calling aborting(). For an error in an internal
8726 * function or for the "E132" error in call_user_func(), however, the
8727 * throw point at which the "force_abort" flag (temporarily reset by
8728 * emsg()) is normally updated has not been reached yet. We need to
8729 * update that flag first to make aborting() reliable.
8730 */
8731 update_force_abort();
8732 }
8733 if (error == ERROR_NONE)
8734 ret = OK;
8735
8736 /*
8737 * Report an error unless the argument evaluation or function call has been
8738 * cancelled due to an aborting error, an interrupt, or an exception.
8739 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008740 if (!aborting())
8741 {
8742 switch (error)
8743 {
8744 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008745 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008746 break;
8747 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008748 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008749 break;
8750 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008751 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008752 name);
8753 break;
8754 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008755 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008756 name);
8757 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008758 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008759 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008760 name);
8761 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008762 }
8763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (fname != name && fname != fname_buf)
8766 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008767 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 return ret;
8770}
8771
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008772/*
8773 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008774 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008775 */
8776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008778{
8779 char_u *p;
8780
8781 if (*name == K_SPECIAL)
8782 p = concat_str((char_u *)"<SNR>", name + 3);
8783 else
8784 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008785 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008786 if (p != name)
8787 vim_free(p);
8788}
8789
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008790/*
8791 * Return TRUE for a non-zero Number and a non-empty String.
8792 */
8793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008794non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008795{
8796 return ((argvars[0].v_type == VAR_NUMBER
8797 && argvars[0].vval.v_number != 0)
8798 || (argvars[0].v_type == VAR_STRING
8799 && argvars[0].vval.v_string != NULL
8800 && *argvars[0].vval.v_string != NUL));
8801}
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803/*********************************************
8804 * Implementation of the built-in functions
8805 */
8806
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008807#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008808static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008809
8810/*
8811 * Get the float value of "argvars[0]" into "f".
8812 * Returns FAIL when the argument is not a Number or Float.
8813 */
8814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008815get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008816{
8817 if (argvars[0].v_type == VAR_FLOAT)
8818 {
8819 *f = argvars[0].vval.v_float;
8820 return OK;
8821 }
8822 if (argvars[0].v_type == VAR_NUMBER)
8823 {
8824 *f = (float_T)argvars[0].vval.v_number;
8825 return OK;
8826 }
8827 EMSG(_("E808: Number or Float required"));
8828 return FAIL;
8829}
8830
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008831/*
8832 * "abs(expr)" function
8833 */
8834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008835f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836{
8837 if (argvars[0].v_type == VAR_FLOAT)
8838 {
8839 rettv->v_type = VAR_FLOAT;
8840 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8841 }
8842 else
8843 {
8844 varnumber_T n;
8845 int error = FALSE;
8846
8847 n = get_tv_number_chk(&argvars[0], &error);
8848 if (error)
8849 rettv->vval.v_number = -1;
8850 else if (n > 0)
8851 rettv->vval.v_number = n;
8852 else
8853 rettv->vval.v_number = -n;
8854 }
8855}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856
8857/*
8858 * "acos()" function
8859 */
8860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008862{
8863 float_T f;
8864
8865 rettv->v_type = VAR_FLOAT;
8866 if (get_float_arg(argvars, &f) == OK)
8867 rettv->vval.v_float = acos(f);
8868 else
8869 rettv->vval.v_float = 0.0;
8870}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008871#endif
8872
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008874 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 */
8876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008882 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008884 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008885 && !tv_check_lock(l->lv_lock,
8886 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008887 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008889 }
8890 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008891 EMSG(_(e_listreq));
8892}
8893
8894/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008895 * "alloc_fail(id, countdown, repeat)" function
8896 */
8897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008899{
8900 if (argvars[0].v_type != VAR_NUMBER
8901 || argvars[0].vval.v_number <= 0
8902 || argvars[1].v_type != VAR_NUMBER
8903 || argvars[1].vval.v_number < 0
8904 || argvars[2].v_type != VAR_NUMBER)
8905 EMSG(_(e_invarg));
8906 else
8907 {
8908 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008909 if (alloc_fail_id >= aid_last)
8910 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008911 alloc_fail_countdown = argvars[1].vval.v_number;
8912 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008913 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008914 }
8915}
8916
8917/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008918 * "and(expr, expr)" function
8919 */
8920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008921f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008922{
8923 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8924 & get_tv_number_chk(&argvars[1], NULL);
8925}
8926
8927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008928 * "append(lnum, string/list)" function
8929 */
8930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008931f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932{
8933 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 list_T *l = NULL;
8936 listitem_T *li = NULL;
8937 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938 long added = 0;
8939
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008940 /* When coming here from Insert mode, sync undo, so that this can be
8941 * undone separately from what was previously inserted. */
8942 if (u_sync_once == 2)
8943 {
8944 u_sync_once = 1; /* notify that u_sync() was called */
8945 u_sync(TRUE);
8946 }
8947
Bram Moolenaar0d660222005-01-07 21:51:51 +00008948 lnum = get_tv_lnum(argvars);
8949 if (lnum >= 0
8950 && lnum <= curbuf->b_ml.ml_line_count
8951 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008953 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008955 l = argvars[1].vval.v_list;
8956 if (l == NULL)
8957 return;
8958 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008960 for (;;)
8961 {
8962 if (l == NULL)
8963 tv = &argvars[1]; /* append a string */
8964 else if (li == NULL)
8965 break; /* end of list */
8966 else
8967 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 line = get_tv_string_chk(tv);
8969 if (line == NULL) /* type error */
8970 {
8971 rettv->vval.v_number = 1; /* Failed */
8972 break;
8973 }
8974 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008975 ++added;
8976 if (l == NULL)
8977 break;
8978 li = li->li_next;
8979 }
8980
8981 appended_lines_mark(lnum, added);
8982 if (curwin->w_cursor.lnum > lnum)
8983 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 else
8986 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
8990 * "argc()" function
8991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
8998/*
8999 * "argidx()" function
9000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009002f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005}
9006
9007/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009008 * "arglistid()" function
9009 */
9010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009012{
9013 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009014
9015 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009016 wp = find_tabwin(&argvars[0], &argvars[1]);
9017 if (wp != NULL)
9018 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009019}
9020
9021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 * "argv(nr)" function
9023 */
9024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009025f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 int idx;
9028
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009029 if (argvars[0].v_type != VAR_UNKNOWN)
9030 {
9031 idx = get_tv_number_chk(&argvars[0], NULL);
9032 if (idx >= 0 && idx < ARGCOUNT)
9033 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9034 else
9035 rettv->vval.v_string = NULL;
9036 rettv->v_type = VAR_STRING;
9037 }
9038 else if (rettv_list_alloc(rettv) == OK)
9039 for (idx = 0; idx < ARGCOUNT; ++idx)
9040 list_append_string(rettv->vval.v_list,
9041 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042}
9043
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009044static void prepare_assert_error(garray_T*gap);
9045static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9046static void assert_error(garray_T *gap);
9047static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009048
9049/*
9050 * Prepare "gap" for an assert error and add the sourcing position.
9051 */
9052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009054{
9055 char buf[NUMBUFLEN];
9056
9057 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009058 if (sourcing_name != NULL)
9059 {
9060 ga_concat(gap, sourcing_name);
9061 if (sourcing_lnum > 0)
9062 ga_concat(gap, (char_u *)" ");
9063 }
9064 if (sourcing_lnum > 0)
9065 {
9066 sprintf(buf, "line %ld", (long)sourcing_lnum);
9067 ga_concat(gap, (char_u *)buf);
9068 }
9069 if (sourcing_name != NULL || sourcing_lnum > 0)
9070 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009071}
9072
9073/*
9074 * Fill "gap" with information about an assert error.
9075 */
9076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009077fill_assert_error(
9078 garray_T *gap,
9079 typval_T *opt_msg_tv,
9080 char_u *exp_str,
9081 typval_T *exp_tv,
9082 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009083{
9084 char_u numbuf[NUMBUFLEN];
9085 char_u *tofree;
9086
9087 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9088 {
9089 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9090 vim_free(tofree);
9091 }
9092 else
9093 {
9094 ga_concat(gap, (char_u *)"Expected ");
9095 if (exp_str == NULL)
9096 {
9097 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9098 vim_free(tofree);
9099 }
9100 else
9101 ga_concat(gap, exp_str);
9102 ga_concat(gap, (char_u *)" but got ");
9103 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9104 vim_free(tofree);
9105 }
9106}
Bram Moolenaar43345542015-11-29 17:35:35 +01009107
9108/*
9109 * Add an assert error to v:errors.
9110 */
9111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009112assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009113{
9114 struct vimvar *vp = &vimvars[VV_ERRORS];
9115
9116 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9117 /* Make sure v:errors is a list. */
9118 set_vim_var_list(VV_ERRORS, list_alloc());
9119 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9120}
9121
Bram Moolenaar43345542015-11-29 17:35:35 +01009122/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009123 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009124 */
9125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009126f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009127{
9128 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009129
9130 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9131 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009132 prepare_assert_error(&ga);
9133 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9134 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009135 ga_clear(&ga);
9136 }
9137}
9138
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009139/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009140 * "assert_exception(string[, msg])" function
9141 */
9142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009143f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009144{
9145 garray_T ga;
9146 char *error;
9147
9148 error = (char *)get_tv_string_chk(&argvars[0]);
9149 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9150 {
9151 prepare_assert_error(&ga);
9152 ga_concat(&ga, (char_u *)"v:exception is not set");
9153 assert_error(&ga);
9154 ga_clear(&ga);
9155 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009156 else if (error != NULL
9157 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009158 {
9159 prepare_assert_error(&ga);
9160 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9161 &vimvars[VV_EXCEPTION].vv_tv);
9162 assert_error(&ga);
9163 ga_clear(&ga);
9164 }
9165}
9166
9167/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009168 * "assert_fails(cmd [, error])" function
9169 */
9170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009171f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009172{
9173 char_u *cmd = get_tv_string_chk(&argvars[0]);
9174 garray_T ga;
9175
9176 called_emsg = FALSE;
9177 suppress_errthrow = TRUE;
9178 emsg_silent = TRUE;
9179 do_cmdline_cmd(cmd);
9180 if (!called_emsg)
9181 {
9182 prepare_assert_error(&ga);
9183 ga_concat(&ga, (char_u *)"command did not fail: ");
9184 ga_concat(&ga, cmd);
9185 assert_error(&ga);
9186 ga_clear(&ga);
9187 }
9188 else if (argvars[1].v_type != VAR_UNKNOWN)
9189 {
9190 char_u buf[NUMBUFLEN];
9191 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9192
9193 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9194 {
9195 prepare_assert_error(&ga);
9196 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9197 &vimvars[VV_ERRMSG].vv_tv);
9198 assert_error(&ga);
9199 ga_clear(&ga);
9200 }
9201 }
9202
9203 called_emsg = FALSE;
9204 suppress_errthrow = FALSE;
9205 emsg_silent = FALSE;
9206 emsg_on_display = FALSE;
9207 set_vim_var_string(VV_ERRMSG, NULL, 0);
9208}
9209
9210/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009211 * Common for assert_true() and assert_false().
9212 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009214assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009215{
9216 int error = FALSE;
9217 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009218
Bram Moolenaar37127922016-02-06 20:29:28 +01009219 if (argvars[0].v_type == VAR_SPECIAL
9220 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VV_FALSE))
9221 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009222 if (argvars[0].v_type != VAR_NUMBER
9223 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9224 || error)
9225 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009226 prepare_assert_error(&ga);
9227 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009228 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009229 NULL, &argvars[0]);
9230 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009231 ga_clear(&ga);
9232 }
9233}
9234
9235/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009236 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009237 */
9238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009239f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009240{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009241 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009242}
9243
9244/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009246 */
9247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009248f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009249{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009250 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009251}
9252
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009254/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009255 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009256 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009258f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009259{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009260 float_T f;
9261
9262 rettv->v_type = VAR_FLOAT;
9263 if (get_float_arg(argvars, &f) == OK)
9264 rettv->vval.v_float = asin(f);
9265 else
9266 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009267}
9268
9269/*
9270 * "atan()" function
9271 */
9272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274{
9275 float_T f;
9276
9277 rettv->v_type = VAR_FLOAT;
9278 if (get_float_arg(argvars, &f) == OK)
9279 rettv->vval.v_float = atan(f);
9280 else
9281 rettv->vval.v_float = 0.0;
9282}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009283
9284/*
9285 * "atan2()" function
9286 */
9287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009289{
9290 float_T fx, fy;
9291
9292 rettv->v_type = VAR_FLOAT;
9293 if (get_float_arg(argvars, &fx) == OK
9294 && get_float_arg(&argvars[1], &fy) == OK)
9295 rettv->vval.v_float = atan2(fx, fy);
9296 else
9297 rettv->vval.v_float = 0.0;
9298}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009299#endif
9300
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301/*
9302 * "browse(save, title, initdir, default)" function
9303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009305f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307#ifdef FEAT_BROWSE
9308 int save;
9309 char_u *title;
9310 char_u *initdir;
9311 char_u *defname;
9312 char_u buf[NUMBUFLEN];
9313 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009314 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 save = get_tv_number_chk(&argvars[0], &error);
9317 title = get_tv_string_chk(&argvars[1]);
9318 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9319 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 if (error || title == NULL || initdir == NULL || defname == NULL)
9322 rettv->vval.v_string = NULL;
9323 else
9324 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009325 do_browse(save ? BROWSE_SAVE : 0,
9326 title, defname, NULL, initdir, NULL, curbuf);
9327#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009331}
9332
9333/*
9334 * "browsedir(title, initdir)" function
9335 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009337f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009338{
9339#ifdef FEAT_BROWSE
9340 char_u *title;
9341 char_u *initdir;
9342 char_u buf[NUMBUFLEN];
9343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 title = get_tv_string_chk(&argvars[0]);
9345 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 if (title == NULL || initdir == NULL)
9348 rettv->vval.v_string = NULL;
9349 else
9350 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009351 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356}
9357
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009358static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009359
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360/*
9361 * Find a buffer by number or exact name.
9362 */
9363 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009364find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009368 if (avar->v_type == VAR_NUMBER)
9369 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009370 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009372 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009373 if (buf == NULL)
9374 {
9375 /* No full path name match, try a match with a URL or a "nofile"
9376 * buffer, these don't use the full path. */
9377 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9378 if (buf->b_fname != NULL
9379 && (path_with_url(buf->b_fname)
9380#ifdef FEAT_QUICKFIX
9381 || bt_nofile(buf)
9382#endif
9383 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009384 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009385 break;
9386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 }
9388 return buf;
9389}
9390
9391/*
9392 * "bufexists(expr)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
9400/*
9401 * "buflisted(expr)" function
9402 */
9403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406 buf_T *buf;
9407
9408 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411
9412/*
9413 * "bufloaded(expr)" function
9414 */
9415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009416f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
9418 buf_T *buf;
9419
9420 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009424static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009425
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426/*
9427 * Get buffer by number or pattern.
9428 */
9429 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 int save_magic;
9434 char_u *save_cpo;
9435 buf_T *buf;
9436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 if (tv->v_type == VAR_NUMBER)
9438 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009439 if (tv->v_type != VAR_STRING)
9440 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 if (name == NULL || *name == NUL)
9442 return curbuf;
9443 if (name[0] == '$' && name[1] == NUL)
9444 return lastbuf;
9445
9446 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9447 save_magic = p_magic;
9448 p_magic = TRUE;
9449 save_cpo = p_cpo;
9450 p_cpo = (char_u *)"";
9451
9452 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009453 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
9455 p_magic = save_magic;
9456 p_cpo = save_cpo;
9457
9458 /* If not found, try expanding the name, like done for bufexists(). */
9459 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
9462 return buf;
9463}
9464
9465/*
9466 * "bufname(expr)" function
9467 */
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 buf_T *buf;
9472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009475 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 --emsg_off;
9482}
9483
9484/*
9485 * "bufnr(expr)" function
9486 */
9487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009488f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009491 int error = FALSE;
9492 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009496 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009497 --emsg_off;
9498
9499 /* If the buffer isn't found and the second argument is not zero create a
9500 * new buffer. */
9501 if (buf == NULL
9502 && argvars[1].v_type != VAR_UNKNOWN
9503 && get_tv_number_chk(&argvars[1], &error) != 0
9504 && !error
9505 && (name = get_tv_string_chk(&argvars[0])) != NULL
9506 && !error)
9507 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9508
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
9516 * "bufwinnr(nr)" function
9517 */
9518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009519f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521#ifdef FEAT_WINDOWS
9522 win_T *wp;
9523 int winnr = 0;
9524#endif
9525 buf_T *buf;
9526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009529 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530#ifdef FEAT_WINDOWS
9531 for (wp = firstwin; wp; wp = wp->w_next)
9532 {
9533 ++winnr;
9534 if (wp->w_buffer == buf)
9535 break;
9536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#endif
9541 --emsg_off;
9542}
9543
9544/*
9545 * "byte2line(byte)" function
9546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009548f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552#else
9553 long boff = 0;
9554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 (linenr_T)0, &boff);
9561#endif
9562}
9563
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009566{
9567#ifdef FEAT_MBYTE
9568 char_u *t;
9569#endif
9570 char_u *str;
9571 long idx;
9572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 str = get_tv_string_chk(&argvars[0]);
9574 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009577 return;
9578
9579#ifdef FEAT_MBYTE
9580 t = str;
9581 for ( ; idx > 0; idx--)
9582 {
9583 if (*t == NUL) /* EOL reached */
9584 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009585 if (enc_utf8 && comp)
9586 t += utf_ptr2len(t);
9587 else
9588 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009589 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009590 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009591#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009592 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009594#endif
9595}
9596
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009597/*
9598 * "byteidx()" function
9599 */
9600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009602{
9603 byteidx(argvars, rettv, FALSE);
9604}
9605
9606/*
9607 * "byteidxcomp()" function
9608 */
9609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009610f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009611{
9612 byteidx(argvars, rettv, TRUE);
9613}
9614
Bram Moolenaardb913952012-06-29 12:54:53 +02009615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009616func_call(
9617 char_u *name,
9618 typval_T *args,
9619 dict_T *selfdict,
9620 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009621{
9622 listitem_T *item;
9623 typval_T argv[MAX_FUNC_ARGS + 1];
9624 int argc = 0;
9625 int dummy;
9626 int r = 0;
9627
9628 for (item = args->vval.v_list->lv_first; item != NULL;
9629 item = item->li_next)
9630 {
9631 if (argc == MAX_FUNC_ARGS)
9632 {
9633 EMSG(_("E699: Too many arguments"));
9634 break;
9635 }
9636 /* Make a copy of each argument. This is needed to be able to set
9637 * v_lock to VAR_FIXED in the copy without changing the original list.
9638 */
9639 copy_tv(&item->li_tv, &argv[argc++]);
9640 }
9641
9642 if (item == NULL)
9643 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9644 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9645 &dummy, TRUE, selfdict);
9646
9647 /* Free the arguments. */
9648 while (argc > 0)
9649 clear_tv(&argv[--argc]);
9650
9651 return r;
9652}
9653
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009654/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009655 * "call(func, arglist)" function
9656 */
9657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009659{
9660 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009662
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009663 if (argvars[1].v_type != VAR_LIST)
9664 {
9665 EMSG(_(e_listreq));
9666 return;
9667 }
9668 if (argvars[1].vval.v_list == NULL)
9669 return;
9670
9671 if (argvars[0].v_type == VAR_FUNC)
9672 func = argvars[0].vval.v_string;
9673 else
9674 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 if (*func == NUL)
9676 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009677
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if (argvars[2].v_type != VAR_UNKNOWN)
9679 {
9680 if (argvars[2].v_type != VAR_DICT)
9681 {
9682 EMSG(_(e_dictreq));
9683 return;
9684 }
9685 selfdict = argvars[2].vval.v_dict;
9686 }
9687
Bram Moolenaardb913952012-06-29 12:54:53 +02009688 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009689}
9690
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009691#ifdef FEAT_FLOAT
9692/*
9693 * "ceil({float})" function
9694 */
9695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009696f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009697{
9698 float_T f;
9699
9700 rettv->v_type = VAR_FLOAT;
9701 if (get_float_arg(argvars, &f) == OK)
9702 rettv->vval.v_float = ceil(f);
9703 else
9704 rettv->vval.v_float = 0.0;
9705}
9706#endif
9707
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009708#ifdef FEAT_CHANNEL
9709/*
9710 * Get the channel index from the handle argument.
9711 * Returns -1 if the handle is invalid or the channel is closed.
9712 */
9713 static int
9714get_channel_arg(typval_T *tv)
9715{
9716 int ch_idx;
9717
9718 if (tv->v_type != VAR_NUMBER)
9719 {
9720 EMSG2(_(e_invarg2), get_tv_string(tv));
9721 return -1;
9722 }
9723 ch_idx = tv->vval.v_number;
9724
9725 if (!channel_is_open(ch_idx))
9726 {
9727 EMSGN(_("E906: not an open channel"), ch_idx);
9728 return -1;
9729 }
9730 return ch_idx;
9731}
9732
9733/*
9734 * "ch_close()" function
9735 */
9736 static void
9737f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9738{
9739 int ch_idx = get_channel_arg(&argvars[0]);
9740
9741 if (ch_idx >= 0)
9742 channel_close(ch_idx);
9743}
9744
9745/*
9746 * Get a callback from "arg". It can be a Funcref or a function name.
9747 * When "arg" is zero return an empty string.
9748 * Return NULL for an invalid argument.
9749 */
9750 static char_u *
9751get_callback(typval_T *arg)
9752{
9753 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9754 return arg->vval.v_string;
9755 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9756 return (char_u *)"";
9757 EMSG(_("E999: Invalid callback argument"));
9758 return NULL;
9759}
9760
9761/*
9762 * "ch_open()" function
9763 */
9764 static void
9765f_ch_open(typval_T *argvars, typval_T *rettv)
9766{
9767 char_u *address;
9768 char_u *mode;
9769 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009770 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009771 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009772 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009773 int waittime = 0;
9774 int timeout = 2000;
9775 int json_mode = TRUE;
9776 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009777
9778 /* default: fail */
9779 rettv->vval.v_number = -1;
9780
9781 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009782 if (argvars[1].v_type != VAR_UNKNOWN
9783 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009784 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009785 EMSG(_(e_invarg));
9786 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009787 }
9788
9789 /* parse address */
9790 p = vim_strchr(address, ':');
9791 if (p == NULL)
9792 {
9793 EMSG2(_(e_invarg2), address);
9794 return;
9795 }
9796 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009797 port = strtol((char *)p, &rest, 10);
9798 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009799 {
9800 p[-1] = ':';
9801 EMSG2(_(e_invarg2), address);
9802 return;
9803 }
9804
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009805 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009806 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009807 /* parse argdict */
9808 dict_T *dict = argvars[1].vval.v_dict;
9809
9810 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9811 {
9812 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9813 if (STRCMP(mode, "raw") == 0)
9814 json_mode = FALSE;
9815 else if (STRCMP(mode, "json") != 0)
9816 {
9817 EMSG2(_(e_invarg2), mode);
9818 return;
9819 }
9820 }
9821 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9822 waittime = get_dict_number(dict, (char_u *)"waittime");
9823 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9824 timeout = get_dict_number(dict, (char_u *)"timeout");
9825 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9826 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9827 }
9828 if (waittime < 0 || timeout < 0)
9829 {
9830 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009831 return;
9832 }
9833
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009834 ch_idx = channel_open((char *)address, port, waittime, NULL);
9835 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009836 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009837 channel_set_json_mode(ch_idx, json_mode);
9838 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009839 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009840 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009841 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009842 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009843}
9844
9845/*
9846 * common for "sendexpr()" and "sendraw()"
9847 * Returns the channel index if the caller should read the response.
9848 * Otherwise returns -1.
9849 */
9850 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009851send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009852{
9853 int ch_idx;
9854 char_u *callback = NULL;
9855
9856 ch_idx = get_channel_arg(&argvars[0]);
9857 if (ch_idx < 0)
9858 return -1;
9859
9860 if (argvars[2].v_type != VAR_UNKNOWN)
9861 {
9862 callback = get_callback(&argvars[2]);
9863 if (callback == NULL)
9864 return -1;
9865 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009866 /* Set the callback. An empty callback means no callback and not reading
9867 * the response. */
9868 if (callback != NULL && *callback != NUL)
9869 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009870
9871 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9872 return ch_idx;
9873 return -1;
9874}
9875
9876/*
9877 * "ch_sendexpr()" function
9878 */
9879 static void
9880f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9881{
9882 char_u *text;
9883 typval_T *listtv;
9884 int ch_idx;
9885 int id;
9886
9887 /* return an empty string by default */
9888 rettv->v_type = VAR_STRING;
9889 rettv->vval.v_string = NULL;
9890
9891 id = channel_get_id();
9892 text = json_encode_nr_expr(id, &argvars[1]);
9893 if (text == NULL)
9894 return;
9895
Bram Moolenaara07fec92016-02-05 21:04:08 +01009896 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009897 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009898 if (ch_idx >= 0)
9899 {
9900 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9901 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009902 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009903
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009904 /* Move the item from the list and then change the type to
9905 * avoid the value being freed. */
9906 *rettv = list->lv_last->li_tv;
9907 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009908 clear_tv(listtv);
9909 }
9910 }
9911}
9912
9913/*
9914 * "ch_sendraw()" function
9915 */
9916 static void
9917f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9918{
9919 char_u buf[NUMBUFLEN];
9920 char_u *text;
9921 int ch_idx;
9922
9923 /* return an empty string by default */
9924 rettv->v_type = VAR_STRING;
9925 rettv->vval.v_string = NULL;
9926
9927 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009928 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009929 if (ch_idx >= 0)
9930 rettv->vval.v_string = channel_read_block(ch_idx);
9931}
9932#endif
9933
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009934/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009935 * "changenr()" function
9936 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009938f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009939{
9940 rettv->vval.v_number = curbuf->b_u_seq_cur;
9941}
9942
9943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 * "char2nr(string)" function
9945 */
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949#ifdef FEAT_MBYTE
9950 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009951 {
9952 int utf8 = 0;
9953
9954 if (argvars[1].v_type != VAR_UNKNOWN)
9955 utf8 = get_tv_number_chk(&argvars[1], NULL);
9956
9957 if (utf8)
9958 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9959 else
9960 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 else
9963#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965}
9966
9967/*
9968 * "cindent(lnum)" function
9969 */
9970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
9973#ifdef FEAT_CINDENT
9974 pos_T pos;
9975 linenr_T lnum;
9976
9977 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9980 {
9981 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 curwin->w_cursor = pos;
9984 }
9985 else
9986#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988}
9989
9990/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009991 * "clearmatches()" function
9992 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009994f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009995{
9996#ifdef FEAT_SEARCH_EXTRA
9997 clear_matches(curwin);
9998#endif
9999}
10000
10001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 * "col(string)" function
10003 */
10004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010005f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 colnr_T col = 0;
10008 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010009 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010011 fp = var2fpos(&argvars[0], FALSE, &fnum);
10012 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 {
10014 if (fp->col == MAXCOL)
10015 {
10016 /* '> can be MAXCOL, get the length of the line then */
10017 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010018 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020 col = MAXCOL;
10021 }
10022 else
10023 {
10024 col = fp->col + 1;
10025#ifdef FEAT_VIRTUALEDIT
10026 /* col(".") when the cursor is on the NUL at the end of the line
10027 * because of "coladd" can be seen as an extra column. */
10028 if (virtual_active() && fp == &curwin->w_cursor)
10029 {
10030 char_u *p = ml_get_cursor();
10031
10032 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10033 curwin->w_virtcol - curwin->w_cursor.coladd))
10034 {
10035# ifdef FEAT_MBYTE
10036 int l;
10037
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010038 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 col += l;
10040# else
10041 if (*p != NUL && p[1] == NUL)
10042 ++col;
10043# endif
10044 }
10045 }
10046#endif
10047 }
10048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050}
10051
Bram Moolenaar572cb562005-08-05 21:35:02 +000010052#if defined(FEAT_INS_EXPAND)
10053/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010054 * "complete()" function
10055 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010057f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010058{
10059 int startcol;
10060
10061 if ((State & INSERT) == 0)
10062 {
10063 EMSG(_("E785: complete() can only be used in Insert mode"));
10064 return;
10065 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010066
10067 /* Check for undo allowed here, because if something was already inserted
10068 * the line was already saved for undo and this check isn't done. */
10069 if (!undo_allowed())
10070 return;
10071
Bram Moolenaarade00832006-03-10 21:46:58 +000010072 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10073 {
10074 EMSG(_(e_invarg));
10075 return;
10076 }
10077
10078 startcol = get_tv_number_chk(&argvars[0], NULL);
10079 if (startcol <= 0)
10080 return;
10081
10082 set_completion(startcol - 1, argvars[1].vval.v_list);
10083}
10084
10085/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010086 * "complete_add()" function
10087 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010089f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010090{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010091 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010092}
10093
10094/*
10095 * "complete_check()" function
10096 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010098f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010099{
10100 int saved = RedrawingDisabled;
10101
10102 RedrawingDisabled = 0;
10103 ins_compl_check_keys(0);
10104 rettv->vval.v_number = compl_interrupted;
10105 RedrawingDisabled = saved;
10106}
10107#endif
10108
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109/*
10110 * "confirm(message, buttons[, default [, type]])" function
10111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010113f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10116 char_u *message;
10117 char_u *buttons = NULL;
10118 char_u buf[NUMBUFLEN];
10119 char_u buf2[NUMBUFLEN];
10120 int def = 1;
10121 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 char_u *typestr;
10123 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 message = get_tv_string_chk(&argvars[0]);
10126 if (message == NULL)
10127 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10131 if (buttons == NULL)
10132 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10139 if (typestr == NULL)
10140 error = TRUE;
10141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 switch (TOUPPER_ASC(*typestr))
10144 {
10145 case 'E': type = VIM_ERROR; break;
10146 case 'Q': type = VIM_QUESTION; break;
10147 case 'I': type = VIM_INFO; break;
10148 case 'W': type = VIM_WARNING; break;
10149 case 'G': type = VIM_GENERIC; break;
10150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 }
10152 }
10153 }
10154 }
10155
10156 if (buttons == NULL || *buttons == NUL)
10157 buttons = (char_u *)_("&Ok");
10158
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010159 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010161 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162#endif
10163}
10164
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010165/*
10166 * "copy()" function
10167 */
10168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010169f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010170{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010171 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010172}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010174#ifdef FEAT_FLOAT
10175/*
10176 * "cos()" function
10177 */
10178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010179f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010180{
10181 float_T f;
10182
10183 rettv->v_type = VAR_FLOAT;
10184 if (get_float_arg(argvars, &f) == OK)
10185 rettv->vval.v_float = cos(f);
10186 else
10187 rettv->vval.v_float = 0.0;
10188}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010189
10190/*
10191 * "cosh()" function
10192 */
10193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010194f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010195{
10196 float_T f;
10197
10198 rettv->v_type = VAR_FLOAT;
10199 if (get_float_arg(argvars, &f) == OK)
10200 rettv->vval.v_float = cosh(f);
10201 else
10202 rettv->vval.v_float = 0.0;
10203}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010204#endif
10205
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010207 * "count()" function
10208 */
10209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010210f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010211{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010212 long n = 0;
10213 int ic = FALSE;
10214
Bram Moolenaare9a41262005-01-15 22:18:47 +000010215 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010216 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 listitem_T *li;
10218 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010220
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 if ((l = argvars[0].vval.v_list) != NULL)
10222 {
10223 li = l->lv_first;
10224 if (argvars[2].v_type != VAR_UNKNOWN)
10225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 int error = FALSE;
10227
10228 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 if (argvars[3].v_type != VAR_UNKNOWN)
10230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 idx = get_tv_number_chk(&argvars[3], &error);
10232 if (!error)
10233 {
10234 li = list_find(l, idx);
10235 if (li == NULL)
10236 EMSGN(_(e_listidx), idx);
10237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 if (error)
10240 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241 }
10242
10243 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010244 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 ++n;
10246 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 else if (argvars[0].v_type == VAR_DICT)
10249 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 int todo;
10251 dict_T *d;
10252 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010254 if ((d = argvars[0].vval.v_dict) != NULL)
10255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 int error = FALSE;
10257
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 if (argvars[2].v_type != VAR_UNKNOWN)
10259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010261 if (argvars[3].v_type != VAR_UNKNOWN)
10262 EMSG(_(e_invarg));
10263 }
10264
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010265 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010267 {
10268 if (!HASHITEM_EMPTY(hi))
10269 {
10270 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010271 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010272 ++n;
10273 }
10274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 }
10276 }
10277 else
10278 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279 rettv->vval.v_number = n;
10280}
10281
10282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10284 *
10285 * Checks the existence of a cscope connection.
10286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010288f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290#ifdef FEAT_CSCOPE
10291 int num = 0;
10292 char_u *dbpath = NULL;
10293 char_u *prepend = NULL;
10294 char_u buf[NUMBUFLEN];
10295
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010296 if (argvars[0].v_type != VAR_UNKNOWN
10297 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 num = (int)get_tv_number(&argvars[0]);
10300 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010301 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 }
10304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#endif
10307}
10308
10309/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010310 * "cursor(lnum, col)" function, or
10311 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010313 * Moves the cursor to the specified line and column.
10314 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010317f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318{
10319 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010320#ifdef FEAT_VIRTUALEDIT
10321 long coladd = 0;
10322#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010323 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010325 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010326 if (argvars[1].v_type == VAR_UNKNOWN)
10327 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010328 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010329 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010330
Bram Moolenaar493c1782014-05-28 14:34:46 +020010331 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010332 {
10333 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010334 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010335 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010336 line = pos.lnum;
10337 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010338#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010339 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010340#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010341 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010342 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010343 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010344 set_curswant = FALSE;
10345 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010346 }
10347 else
10348 {
10349 line = get_tv_lnum(argvars);
10350 col = get_tv_number_chk(&argvars[1], NULL);
10351#ifdef FEAT_VIRTUALEDIT
10352 if (argvars[2].v_type != VAR_UNKNOWN)
10353 coladd = get_tv_number_chk(&argvars[2], NULL);
10354#endif
10355 }
10356 if (line < 0 || col < 0
10357#ifdef FEAT_VIRTUALEDIT
10358 || coladd < 0
10359#endif
10360 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 if (line > 0)
10363 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 if (col > 0)
10365 curwin->w_cursor.col = col - 1;
10366#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010367 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368#endif
10369
10370 /* Make sure the cursor is in a valid position. */
10371 check_cursor();
10372#ifdef FEAT_MBYTE
10373 /* Correct cursor for multi-byte character. */
10374 if (has_mbyte)
10375 mb_adjust_cursor();
10376#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010377
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010378 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010379 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
10381
10382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010383 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 */
10385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010388 int noref = 0;
10389
10390 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010391 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010392 if (noref < 0 || noref > 1)
10393 EMSG(_(e_invarg));
10394 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010395 {
10396 current_copyID += COPYID_INC;
10397 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
10401/*
10402 * "delete()" function
10403 */
10404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010405f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010407 char_u nbuf[NUMBUFLEN];
10408 char_u *name;
10409 char_u *flags;
10410
10411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010413 return;
10414
10415 name = get_tv_string(&argvars[0]);
10416 if (name == NULL || *name == NUL)
10417 {
10418 EMSG(_(e_invarg));
10419 return;
10420 }
10421
10422 if (argvars[1].v_type != VAR_UNKNOWN)
10423 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010425 flags = (char_u *)"";
10426
10427 if (*flags == NUL)
10428 /* delete a file */
10429 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10430 else if (STRCMP(flags, "d") == 0)
10431 /* delete an empty directory */
10432 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10433 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010434 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010435 rettv->vval.v_number = delete_recursive(name);
10436 else
10437 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438}
10439
10440/*
10441 * "did_filetype()" function
10442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010444f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445{
10446#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448#endif
10449}
10450
10451/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010452 * "diff_filler()" function
10453 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010455f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010456{
10457#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010459#endif
10460}
10461
10462/*
10463 * "diff_hlID()" function
10464 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010466f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010467{
10468#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010469 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010470 static linenr_T prev_lnum = 0;
10471 static int changedtick = 0;
10472 static int fnum = 0;
10473 static int change_start = 0;
10474 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010475 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010476 int filler_lines;
10477 int col;
10478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 if (lnum < 0) /* ignore type error in {lnum} arg */
10480 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010481 if (lnum != prev_lnum
10482 || changedtick != curbuf->b_changedtick
10483 || fnum != curbuf->b_fnum)
10484 {
10485 /* New line, buffer, change: need to get the values. */
10486 filler_lines = diff_check(curwin, lnum);
10487 if (filler_lines < 0)
10488 {
10489 if (filler_lines == -1)
10490 {
10491 change_start = MAXCOL;
10492 change_end = -1;
10493 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10494 hlID = HLF_ADD; /* added line */
10495 else
10496 hlID = HLF_CHD; /* changed line */
10497 }
10498 else
10499 hlID = HLF_ADD; /* added line */
10500 }
10501 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010502 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010503 prev_lnum = lnum;
10504 changedtick = curbuf->b_changedtick;
10505 fnum = curbuf->b_fnum;
10506 }
10507
10508 if (hlID == HLF_CHD || hlID == HLF_TXD)
10509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010511 if (col >= change_start && col <= change_end)
10512 hlID = HLF_TXD; /* changed text */
10513 else
10514 hlID = HLF_CHD; /* changed line */
10515 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010516 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010517#endif
10518}
10519
10520/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010521 * "empty({expr})" function
10522 */
10523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010525{
10526 int n;
10527
10528 switch (argvars[0].v_type)
10529 {
10530 case VAR_STRING:
10531 case VAR_FUNC:
10532 n = argvars[0].vval.v_string == NULL
10533 || *argvars[0].vval.v_string == NUL;
10534 break;
10535 case VAR_NUMBER:
10536 n = argvars[0].vval.v_number == 0;
10537 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010538#ifdef FEAT_FLOAT
10539 case VAR_FLOAT:
10540 n = argvars[0].vval.v_float == 0.0;
10541 break;
10542#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010543 case VAR_LIST:
10544 n = argvars[0].vval.v_list == NULL
10545 || argvars[0].vval.v_list->lv_first == NULL;
10546 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 case VAR_DICT:
10548 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010551 case VAR_SPECIAL:
10552 n = argvars[0].vval.v_number != VVAL_TRUE;
10553 break;
10554
Bram Moolenaara03f2332016-02-06 18:09:59 +010010555 case VAR_UNKNOWN:
10556 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10557 n = TRUE;
10558 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010559 }
10560
10561 rettv->vval.v_number = n;
10562}
10563
10564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 * "escape({string}, {chars})" function
10566 */
10567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 char_u buf[NUMBUFLEN];
10571
Bram Moolenaar758711c2005-02-02 23:11:38 +000010572 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10573 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
10577/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010578 * "eval()" function
10579 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010581f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010583 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 s = get_tv_string_chk(&argvars[0]);
10586 if (s != NULL)
10587 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588
Bram Moolenaar615b9972015-01-14 17:15:05 +010010589 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10591 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010592 if (p != NULL && !aborting())
10593 EMSG2(_(e_invexpr2), p);
10594 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010596 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010598 else if (*s != NUL)
10599 EMSG(_(e_trailing));
10600}
10601
10602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 * "eventhandler()" function
10604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010606f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010608 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609}
10610
10611/*
10612 * "executable()" function
10613 */
10614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010615f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010617 char_u *name = get_tv_string(&argvars[0]);
10618
10619 /* Check in $PATH and also check directly if there is a directory name. */
10620 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10621 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010622}
10623
10624/*
10625 * "exepath()" function
10626 */
10627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010628f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010629{
10630 char_u *p = NULL;
10631
Bram Moolenaarb5971142015-03-21 17:32:19 +010010632 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010633 rettv->v_type = VAR_STRING;
10634 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635}
10636
10637/*
10638 * "exists()" function
10639 */
10640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010641f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 char_u *p;
10644 char_u *name;
10645 int n = FALSE;
10646 int len = 0;
10647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 if (*p == '$') /* environment variable */
10650 {
10651 /* first try "normal" environment variables (fast) */
10652 if (mch_getenv(p + 1) != NULL)
10653 n = TRUE;
10654 else
10655 {
10656 /* try expanding things like $VIM and ${HOME} */
10657 p = expand_env_save(p);
10658 if (p != NULL && *p != '$')
10659 n = TRUE;
10660 vim_free(p);
10661 }
10662 }
10663 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010666 if (*skipwhite(p) != NUL)
10667 n = FALSE; /* trailing garbage */
10668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 else if (*p == '*') /* internal or user defined function */
10670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010671 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 }
10673 else if (*p == ':')
10674 {
10675 n = cmd_exists(p + 1);
10676 }
10677 else if (*p == '#')
10678 {
10679#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010680 if (p[1] == '#')
10681 n = autocmd_supported(p + 2);
10682 else
10683 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684#endif
10685 }
10686 else /* internal variable */
10687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010688 char_u *tofree;
10689 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010691 /* get_name_len() takes care of expanding curly braces */
10692 name = p;
10693 len = get_name_len(&p, &tofree, TRUE, FALSE);
10694 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010696 if (tofree != NULL)
10697 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010698 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010699 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010701 /* handle d.key, l[idx], f(expr) */
10702 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10703 if (n)
10704 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 }
10706 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010707 if (*p != NUL)
10708 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010710 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 }
10712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010716#ifdef FEAT_FLOAT
10717/*
10718 * "exp()" function
10719 */
10720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010721f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010722{
10723 float_T f;
10724
10725 rettv->v_type = VAR_FLOAT;
10726 if (get_float_arg(argvars, &f) == OK)
10727 rettv->vval.v_float = exp(f);
10728 else
10729 rettv->vval.v_float = 0.0;
10730}
10731#endif
10732
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733/*
10734 * "expand()" function
10735 */
10736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010737f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
10739 char_u *s;
10740 int len;
10741 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010742 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010745 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010748 if (argvars[1].v_type != VAR_UNKNOWN
10749 && argvars[2].v_type != VAR_UNKNOWN
10750 && get_tv_number_chk(&argvars[2], &error)
10751 && !error)
10752 {
10753 rettv->v_type = VAR_LIST;
10754 rettv->vval.v_list = NULL;
10755 }
10756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 if (*s == '%' || *s == '#' || *s == '<')
10759 {
10760 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010761 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010763 if (rettv->v_type == VAR_LIST)
10764 {
10765 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10766 list_append_string(rettv->vval.v_list, result, -1);
10767 else
10768 vim_free(result);
10769 }
10770 else
10771 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 }
10773 else
10774 {
10775 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010776 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 if (argvars[1].v_type != VAR_UNKNOWN
10778 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010779 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010780 if (!error)
10781 {
10782 ExpandInit(&xpc);
10783 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010784 if (p_wic)
10785 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010786 if (rettv->v_type == VAR_STRING)
10787 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10788 options, WILD_ALL);
10789 else if (rettv_list_alloc(rettv) != FAIL)
10790 {
10791 int i;
10792
10793 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10794 for (i = 0; i < xpc.xp_numfiles; i++)
10795 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10796 ExpandCleanup(&xpc);
10797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 }
10799 else
10800 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 }
10802}
10803
10804/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010805 * Go over all entries in "d2" and add them to "d1".
10806 * When "action" is "error" then a duplicate key is an error.
10807 * When "action" is "force" then a duplicate key is overwritten.
10808 * Otherwise duplicate keys are ignored ("action" is "keep").
10809 */
10810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010811dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010812{
10813 dictitem_T *di1;
10814 hashitem_T *hi2;
10815 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010816 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010817
10818 todo = (int)d2->dv_hashtab.ht_used;
10819 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10820 {
10821 if (!HASHITEM_EMPTY(hi2))
10822 {
10823 --todo;
10824 di1 = dict_find(d1, hi2->hi_key, -1);
10825 if (d1->dv_scope != 0)
10826 {
10827 /* Disallow replacing a builtin function in l: and g:.
10828 * Check the key to be valid when adding to any
10829 * scope. */
10830 if (d1->dv_scope == VAR_DEF_SCOPE
10831 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10832 && var_check_func_name(hi2->hi_key,
10833 di1 == NULL))
10834 break;
10835 if (!valid_varname(hi2->hi_key))
10836 break;
10837 }
10838 if (di1 == NULL)
10839 {
10840 di1 = dictitem_copy(HI2DI(hi2));
10841 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10842 dictitem_free(di1);
10843 }
10844 else if (*action == 'e')
10845 {
10846 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10847 break;
10848 }
10849 else if (*action == 'f' && HI2DI(hi2) != di1)
10850 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010851 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10852 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010853 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010854 clear_tv(&di1->di_tv);
10855 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10856 }
10857 }
10858 }
10859}
10860
10861/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010862 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010863 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010864 */
10865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010866f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010867{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010868 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010869
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010871 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010872 list_T *l1, *l2;
10873 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010876
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 l1 = argvars[0].vval.v_list;
10878 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010879 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010880 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 {
10882 if (argvars[2].v_type != VAR_UNKNOWN)
10883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 before = get_tv_number_chk(&argvars[2], &error);
10885 if (error)
10886 return; /* type error; errmsg already given */
10887
Bram Moolenaar758711c2005-02-02 23:11:38 +000010888 if (before == l1->lv_len)
10889 item = NULL;
10890 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010892 item = list_find(l1, before);
10893 if (item == NULL)
10894 {
10895 EMSGN(_(e_listidx), before);
10896 return;
10897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 }
10899 }
10900 else
10901 item = NULL;
10902 list_extend(l1, l2, item);
10903
Bram Moolenaare9a41262005-01-15 22:18:47 +000010904 copy_tv(&argvars[0], rettv);
10905 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10908 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010909 dict_T *d1, *d2;
10910 char_u *action;
10911 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912
10913 d1 = argvars[0].vval.v_dict;
10914 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010915 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917 {
10918 /* Check the third argument. */
10919 if (argvars[2].v_type != VAR_UNKNOWN)
10920 {
10921 static char *(av[]) = {"keep", "force", "error"};
10922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 action = get_tv_string_chk(&argvars[2]);
10924 if (action == NULL)
10925 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010926 for (i = 0; i < 3; ++i)
10927 if (STRCMP(action, av[i]) == 0)
10928 break;
10929 if (i == 3)
10930 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010931 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 return;
10933 }
10934 }
10935 else
10936 action = (char_u *)"force";
10937
Bram Moolenaara9922d62013-05-30 13:01:18 +020010938 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 copy_tv(&argvars[0], rettv);
10941 }
10942 }
10943 else
10944 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010945}
10946
10947/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010948 * "feedkeys()" function
10949 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010951f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010952{
10953 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010954 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010955 char_u *keys, *flags;
10956 char_u nbuf[NUMBUFLEN];
10957 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010958 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010959 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010960
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010961 /* This is not allowed in the sandbox. If the commands would still be
10962 * executed in the sandbox it would be OK, but it probably happens later,
10963 * when "sandbox" is no longer set. */
10964 if (check_secure())
10965 return;
10966
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010967 keys = get_tv_string(&argvars[0]);
10968 if (*keys != NUL)
10969 {
10970 if (argvars[1].v_type != VAR_UNKNOWN)
10971 {
10972 flags = get_tv_string_buf(&argvars[1], nbuf);
10973 for ( ; *flags != NUL; ++flags)
10974 {
10975 switch (*flags)
10976 {
10977 case 'n': remap = FALSE; break;
10978 case 'm': remap = TRUE; break;
10979 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010980 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010981 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010982 }
10983 }
10984 }
10985
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010986 /* Need to escape K_SPECIAL and CSI before putting the string in the
10987 * typeahead buffer. */
10988 keys_esc = vim_strsave_escape_csi(keys);
10989 if (keys_esc != NULL)
10990 {
10991 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010992 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010993 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010994 if (vgetc_busy)
10995 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010996 if (execute)
10997 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010998 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010999 }
11000}
11001
11002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 * "filereadable()" function
11004 */
11005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011006f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011008 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 char_u *p;
11010 int n;
11011
Bram Moolenaarc236c162008-07-13 17:41:49 +000011012#ifndef O_NONBLOCK
11013# define O_NONBLOCK 0
11014#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011016 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11017 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 {
11019 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011020 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022 else
11023 n = FALSE;
11024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026}
11027
11028/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011029 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 * rights to write into.
11031 */
11032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011033f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011035 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036}
11037
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011038 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011039findfilendir(
11040 typval_T *argvars UNUSED,
11041 typval_T *rettv,
11042 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011043{
11044#ifdef FEAT_SEARCHPATH
11045 char_u *fname;
11046 char_u *fresult = NULL;
11047 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11048 char_u *p;
11049 char_u pathbuf[NUMBUFLEN];
11050 int count = 1;
11051 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011052 int error = FALSE;
11053#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011054
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011055 rettv->vval.v_string = NULL;
11056 rettv->v_type = VAR_STRING;
11057
11058#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011060
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011061 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11064 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011065 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 else
11067 {
11068 if (*p != NUL)
11069 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011072 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011074 }
11075
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011076 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11077 error = TRUE;
11078
11079 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 do
11082 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011083 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011084 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 fresult = find_file_in_path_option(first ? fname : NULL,
11086 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011087 0, first, path,
11088 find_what,
11089 curbuf->b_ffname,
11090 find_what == FINDFILE_DIR
11091 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011093
11094 if (fresult != NULL && rettv->v_type == VAR_LIST)
11095 list_append_string(rettv->vval.v_list, fresult, -1);
11096
11097 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011099
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011100 if (rettv->v_type == VAR_STRING)
11101 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011102#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011103}
11104
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011105static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11106static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011107
11108/*
11109 * Implementation of map() and filter().
11110 */
11111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011112filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011113{
11114 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 listitem_T *li, *nli;
11117 list_T *l = NULL;
11118 dictitem_T *di;
11119 hashtab_T *ht;
11120 hashitem_T *hi;
11121 dict_T *d = NULL;
11122 typval_T save_val;
11123 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011125 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011126 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011127 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011128 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011129 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011130 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011131
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011133 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011134 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011135 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 return;
11137 }
11138 else if (argvars[0].v_type == VAR_DICT)
11139 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011140 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011141 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 return;
11143 }
11144 else
11145 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011146 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011147 return;
11148 }
11149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 expr = get_tv_string_buf_chk(&argvars[1], buf);
11151 /* On type errors, the preceding call has already displayed an error
11152 * message. Avoid a misleading error message for an empty string that
11153 * was not passed as argument. */
11154 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 prepare_vimvar(VV_VAL, &save_val);
11157 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011158
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011159 /* We reset "did_emsg" to be able to detect whether an error
11160 * occurred during evaluation of the expression. */
11161 save_did_emsg = did_emsg;
11162 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011163
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011164 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 vimvars[VV_KEY].vv_type = VAR_STRING;
11168
11169 ht = &d->dv_hashtab;
11170 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011171 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 if (!HASHITEM_EMPTY(hi))
11175 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011176 int r;
11177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 --todo;
11179 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011180 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011181 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11182 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 break;
11184 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011185 r = filter_map_one(&di->di_tv, expr, map, &rem);
11186 clear_tv(&vimvars[VV_KEY].vv_tv);
11187 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 break;
11189 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011190 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011191 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11192 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011193 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 }
11197 }
11198 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 }
11200 else
11201 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011202 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 for (li = l->lv_first; li != NULL; li = nli)
11205 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011206 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011207 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011209 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011210 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011211 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011212 break;
11213 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011215 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011216 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011217 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011219 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011221
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011222 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224
11225 copy_tv(&argvars[0], rettv);
11226}
11227
11228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011229filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230{
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011233 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 s = expr;
11237 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011238 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 if (*s != NUL) /* check for trailing chars after expr */
11240 {
11241 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011242 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011243 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 }
11245 if (map)
11246 {
11247 /* map(): replace the list item value */
11248 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011249 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 *tv = rettv;
11251 }
11252 else
11253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 int error = FALSE;
11255
Bram Moolenaare9a41262005-01-15 22:18:47 +000011256 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011259 /* On type error, nothing has been removed; return FAIL to stop the
11260 * loop. The error message was given by get_tv_number_chk(). */
11261 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011262 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011264 retval = OK;
11265theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011267 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011268}
11269
11270/*
11271 * "filter()" function
11272 */
11273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011274f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011275{
11276 filter_map(argvars, rettv, FALSE);
11277}
11278
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280 * "finddir({fname}[, {path}[, {count}]])" function
11281 */
11282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011283f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011285 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011286}
11287
11288/*
11289 * "findfile({fname}[, {path}[, {count}]])" function
11290 */
11291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011292f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011294 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011295}
11296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011297#ifdef FEAT_FLOAT
11298/*
11299 * "float2nr({float})" function
11300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011303{
11304 float_T f;
11305
11306 if (get_float_arg(argvars, &f) == OK)
11307 {
11308 if (f < -0x7fffffff)
11309 rettv->vval.v_number = -0x7fffffff;
11310 else if (f > 0x7fffffff)
11311 rettv->vval.v_number = 0x7fffffff;
11312 else
11313 rettv->vval.v_number = (varnumber_T)f;
11314 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011315}
11316
11317/*
11318 * "floor({float})" function
11319 */
11320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011321f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011322{
11323 float_T f;
11324
11325 rettv->v_type = VAR_FLOAT;
11326 if (get_float_arg(argvars, &f) == OK)
11327 rettv->vval.v_float = floor(f);
11328 else
11329 rettv->vval.v_float = 0.0;
11330}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011331
11332/*
11333 * "fmod()" function
11334 */
11335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011337{
11338 float_T fx, fy;
11339
11340 rettv->v_type = VAR_FLOAT;
11341 if (get_float_arg(argvars, &fx) == OK
11342 && get_float_arg(&argvars[1], &fy) == OK)
11343 rettv->vval.v_float = fmod(fx, fy);
11344 else
11345 rettv->vval.v_float = 0.0;
11346}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011347#endif
11348
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011350 * "fnameescape({string})" function
11351 */
11352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011353f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011354{
11355 rettv->vval.v_string = vim_strsave_fnameescape(
11356 get_tv_string(&argvars[0]), FALSE);
11357 rettv->v_type = VAR_STRING;
11358}
11359
11360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 * "fnamemodify({fname}, {mods})" function
11362 */
11363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011364f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366 char_u *fname;
11367 char_u *mods;
11368 int usedlen = 0;
11369 int len;
11370 char_u *fbuf = NULL;
11371 char_u buf[NUMBUFLEN];
11372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 fname = get_tv_string_chk(&argvars[0]);
11374 mods = get_tv_string_buf_chk(&argvars[1], buf);
11375 if (fname == NULL || mods == NULL)
11376 fname = NULL;
11377 else
11378 {
11379 len = (int)STRLEN(fname);
11380 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 vim_free(fbuf);
11389}
11390
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011391static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392
11393/*
11394 * "foldclosed()" function
11395 */
11396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011397foldclosed_both(
11398 typval_T *argvars UNUSED,
11399 typval_T *rettv,
11400 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
11402#ifdef FEAT_FOLDING
11403 linenr_T lnum;
11404 linenr_T first, last;
11405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11408 {
11409 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11410 {
11411 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 return;
11416 }
11417 }
11418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420}
11421
11422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011423 * "foldclosed()" function
11424 */
11425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011427{
11428 foldclosed_both(argvars, rettv, FALSE);
11429}
11430
11431/*
11432 * "foldclosedend()" function
11433 */
11434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011435f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011436{
11437 foldclosed_both(argvars, rettv, TRUE);
11438}
11439
11440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 * "foldlevel()" function
11442 */
11443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011444f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445{
11446#ifdef FEAT_FOLDING
11447 linenr_T lnum;
11448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453}
11454
11455/*
11456 * "foldtext()" function
11457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011459f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460{
11461#ifdef FEAT_FOLDING
11462 linenr_T lnum;
11463 char_u *s;
11464 char_u *r;
11465 int len;
11466 char *txt;
11467#endif
11468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11473 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11474 <= curbuf->b_ml.ml_line_count
11475 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 {
11477 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011478 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11479 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 {
11481 if (!linewhite(lnum))
11482 break;
11483 ++lnum;
11484 }
11485
11486 /* Find interesting text in this line. */
11487 s = skipwhite(ml_get(lnum));
11488 /* skip C comment-start */
11489 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011492 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011494 {
11495 s = skipwhite(ml_get(lnum + 1));
11496 if (*s == '*')
11497 s = skipwhite(s + 1);
11498 }
11499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 txt = _("+-%s%3ld lines: ");
11501 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 + 20 /* for %3ld */
11504 + STRLEN(s))); /* concatenated */
11505 if (r != NULL)
11506 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11508 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11509 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 len = (int)STRLEN(r);
11511 STRCAT(r, s);
11512 /* remove 'foldmarker' and 'commentstring' */
11513 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516 }
11517#endif
11518}
11519
11520/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011521 * "foldtextresult(lnum)" function
11522 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011524f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011525{
11526#ifdef FEAT_FOLDING
11527 linenr_T lnum;
11528 char_u *text;
11529 char_u buf[51];
11530 foldinfo_T foldinfo;
11531 int fold_count;
11532#endif
11533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->v_type = VAR_STRING;
11535 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011536#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 /* treat illegal types and illegal string values for {lnum} the same */
11539 if (lnum < 0)
11540 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011541 fold_count = foldedCount(curwin, lnum, &foldinfo);
11542 if (fold_count > 0)
11543 {
11544 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11545 &foldinfo, buf);
11546 if (text == buf)
11547 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011549 }
11550#endif
11551}
11552
11553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 * "foreground()" function
11555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011557f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559#ifdef FEAT_GUI
11560 if (gui.in_use)
11561 gui_mch_set_foreground();
11562#else
11563# ifdef WIN32
11564 win32_set_foreground();
11565# endif
11566#endif
11567}
11568
11569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011570 * "function()" function
11571 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011573f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011574{
11575 char_u *s;
11576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011579 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011580 /* Don't check an autoload name for existence here. */
11581 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011582 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583 else
11584 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011585 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011586 {
11587 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011588 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011589
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011590 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11591 * also be called from another script. Using trans_function_name()
11592 * would also work, but some plugins depend on the name being
11593 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011594 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011595 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011596 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011597 if (rettv->vval.v_string != NULL)
11598 {
11599 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011600 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011601 }
11602 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011603 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011604 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011606 }
11607}
11608
11609/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011610 * "garbagecollect()" function
11611 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011613f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011614{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011615 /* This is postponed until we are back at the toplevel, because we may be
11616 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11617 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011618
11619 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11620 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011621}
11622
11623/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624 * "get()" function
11625 */
11626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011627f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011628{
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 listitem_T *li;
11630 list_T *l;
11631 dictitem_T *di;
11632 dict_T *d;
11633 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634
Bram Moolenaare9a41262005-01-15 22:18:47 +000011635 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011637 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 int error = FALSE;
11640
11641 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11642 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011643 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011646 else if (argvars[0].v_type == VAR_DICT)
11647 {
11648 if ((d = argvars[0].vval.v_dict) != NULL)
11649 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011650 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011651 if (di != NULL)
11652 tv = &di->di_tv;
11653 }
11654 }
11655 else
11656 EMSG2(_(e_listdictarg), "get()");
11657
11658 if (tv == NULL)
11659 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 copy_tv(&argvars[2], rettv);
11662 }
11663 else
11664 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011665}
11666
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011667static 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 +000011668
11669/*
11670 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011671 * Return a range (from start to end) of lines in rettv from the specified
11672 * buffer.
11673 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011674 */
11675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011676get_buffer_lines(
11677 buf_T *buf,
11678 linenr_T start,
11679 linenr_T end,
11680 int retlist,
11681 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011682{
11683 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011684
Bram Moolenaar959a1432013-12-14 12:17:38 +010011685 rettv->v_type = VAR_STRING;
11686 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011687 if (retlist && rettv_list_alloc(rettv) == FAIL)
11688 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011689
11690 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11691 return;
11692
11693 if (!retlist)
11694 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011695 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11696 p = ml_get_buf(buf, start, FALSE);
11697 else
11698 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011699 rettv->vval.v_string = vim_strsave(p);
11700 }
11701 else
11702 {
11703 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011704 return;
11705
11706 if (start < 1)
11707 start = 1;
11708 if (end > buf->b_ml.ml_line_count)
11709 end = buf->b_ml.ml_line_count;
11710 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011711 if (list_append_string(rettv->vval.v_list,
11712 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011713 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011714 }
11715}
11716
11717/*
11718 * "getbufline()" function
11719 */
11720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011721f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011722{
11723 linenr_T lnum;
11724 linenr_T end;
11725 buf_T *buf;
11726
11727 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11728 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011729 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011730 --emsg_off;
11731
Bram Moolenaar661b1822005-07-28 22:36:45 +000011732 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011733 if (argvars[2].v_type == VAR_UNKNOWN)
11734 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011735 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011736 end = get_tv_lnum_buf(&argvars[2], buf);
11737
Bram Moolenaar342337a2005-07-21 21:11:17 +000011738 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011739}
11740
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741/*
11742 * "getbufvar()" function
11743 */
11744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011745f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011746{
11747 buf_T *buf;
11748 buf_T *save_curbuf;
11749 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011751 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11754 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011755 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011756 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011758 rettv->v_type = VAR_STRING;
11759 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011760
11761 if (buf != NULL && varname != NULL)
11762 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011763 /* set curbuf to be our buf, temporarily */
11764 save_curbuf = curbuf;
11765 curbuf = buf;
11766
Bram Moolenaar0d660222005-01-07 21:51:51 +000011767 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011768 {
11769 if (get_option_tv(&varname, rettv, TRUE) == OK)
11770 done = TRUE;
11771 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011772 else if (STRCMP(varname, "changedtick") == 0)
11773 {
11774 rettv->v_type = VAR_NUMBER;
11775 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011776 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011777 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778 else
11779 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011780 /* Look up the variable. */
11781 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11782 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11783 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011784 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011786 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011787 done = TRUE;
11788 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011789 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011790
11791 /* restore previous notion of curbuf */
11792 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793 }
11794
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011795 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11796 /* use the default value */
11797 copy_tv(&argvars[2], rettv);
11798
Bram Moolenaar0d660222005-01-07 21:51:51 +000011799 --emsg_off;
11800}
11801
11802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 * "getchar()" function
11804 */
11805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011806f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807{
11808 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011811 /* Position the cursor. Needed after a message that ends in a space. */
11812 windgoto(msg_row, msg_col);
11813
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 ++no_mapping;
11815 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011816 for (;;)
11817 {
11818 if (argvars[0].v_type == VAR_UNKNOWN)
11819 /* getchar(): blocking wait. */
11820 n = safe_vgetc();
11821 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11822 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011823 n = vpeekc_any();
11824 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011825 /* illegal argument or getchar(0) and no char avail: return zero */
11826 n = 0;
11827 else
11828 /* getchar(0) and char avail: return char */
11829 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011830
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011831 if (n == K_IGNORE)
11832 continue;
11833 break;
11834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 --no_mapping;
11836 --allow_keys;
11837
Bram Moolenaar219b8702006-11-01 14:32:36 +000011838 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11839 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11840 vimvars[VV_MOUSE_COL].vv_nr = 0;
11841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 if (IS_SPECIAL(n) || mod_mask != 0)
11844 {
11845 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11846 int i = 0;
11847
11848 /* Turn a special key into three bytes, plus modifier. */
11849 if (mod_mask != 0)
11850 {
11851 temp[i++] = K_SPECIAL;
11852 temp[i++] = KS_MODIFIER;
11853 temp[i++] = mod_mask;
11854 }
11855 if (IS_SPECIAL(n))
11856 {
11857 temp[i++] = K_SPECIAL;
11858 temp[i++] = K_SECOND(n);
11859 temp[i++] = K_THIRD(n);
11860 }
11861#ifdef FEAT_MBYTE
11862 else if (has_mbyte)
11863 i += (*mb_char2bytes)(n, temp + i);
11864#endif
11865 else
11866 temp[i++] = n;
11867 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 rettv->v_type = VAR_STRING;
11869 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011870
11871#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011872 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011873 {
11874 int row = mouse_row;
11875 int col = mouse_col;
11876 win_T *win;
11877 linenr_T lnum;
11878# ifdef FEAT_WINDOWS
11879 win_T *wp;
11880# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011881 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011882
11883 if (row >= 0 && col >= 0)
11884 {
11885 /* Find the window at the mouse coordinates and compute the
11886 * text position. */
11887 win = mouse_find_win(&row, &col);
11888 (void)mouse_comp_pos(win, &row, &col, &lnum);
11889# ifdef FEAT_WINDOWS
11890 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011891 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011892# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011893 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011894 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11895 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11896 }
11897 }
11898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 }
11900}
11901
11902/*
11903 * "getcharmod()" function
11904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011906f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909}
11910
11911/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011912 * "getcharsearch()" function
11913 */
11914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011915f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011916{
11917 if (rettv_dict_alloc(rettv) != FAIL)
11918 {
11919 dict_T *dict = rettv->vval.v_dict;
11920
11921 dict_add_nr_str(dict, "char", 0L, last_csearch());
11922 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11923 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11924 }
11925}
11926
11927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 * "getcmdline()" function
11929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011931f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
11934 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935}
11936
11937/*
11938 * "getcmdpos()" function
11939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011941f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944}
11945
11946/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011947 * "getcmdtype()" function
11948 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011950f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011951{
11952 rettv->v_type = VAR_STRING;
11953 rettv->vval.v_string = alloc(2);
11954 if (rettv->vval.v_string != NULL)
11955 {
11956 rettv->vval.v_string[0] = get_cmdline_type();
11957 rettv->vval.v_string[1] = NUL;
11958 }
11959}
11960
11961/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011962 * "getcmdwintype()" function
11963 */
11964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011966{
11967 rettv->v_type = VAR_STRING;
11968 rettv->vval.v_string = NULL;
11969#ifdef FEAT_CMDWIN
11970 rettv->vval.v_string = alloc(2);
11971 if (rettv->vval.v_string != NULL)
11972 {
11973 rettv->vval.v_string[0] = cmdwin_type;
11974 rettv->vval.v_string[1] = NUL;
11975 }
11976#endif
11977}
11978
11979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 * "getcwd()" function
11981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011985 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011986 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011989 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011990
11991 wp = find_tabwin(&argvars[0], &argvars[1]);
11992 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011994 if (wp->w_localdir != NULL)
11995 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11996 else if(globaldir != NULL)
11997 rettv->vval.v_string = vim_strsave(globaldir);
11998 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011999 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012000 cwd = alloc(MAXPATHL);
12001 if (cwd != NULL)
12002 {
12003 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12004 rettv->vval.v_string = vim_strsave(cwd);
12005 vim_free(cwd);
12006 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012007 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012008#ifdef BACKSLASH_IN_FILENAME
12009 if (rettv->vval.v_string != NULL)
12010 slash_adjust(rettv->vval.v_string);
12011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 }
12013}
12014
12015/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012016 * "getfontname()" function
12017 */
12018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->v_type = VAR_STRING;
12022 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012023#ifdef FEAT_GUI
12024 if (gui.in_use)
12025 {
12026 GuiFont font;
12027 char_u *name = NULL;
12028
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012029 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012030 {
12031 /* Get the "Normal" font. Either the name saved by
12032 * hl_set_font_name() or from the font ID. */
12033 font = gui.norm_font;
12034 name = hl_get_font_name();
12035 }
12036 else
12037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012039 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12040 return;
12041 font = gui_mch_get_font(name, FALSE);
12042 if (font == NOFONT)
12043 return; /* Invalid font name, return empty string. */
12044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012046 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012047 gui_mch_free_font(font);
12048 }
12049#endif
12050}
12051
12052/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012053 * "getfperm({fname})" function
12054 */
12055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012056f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012057{
12058 char_u *fname;
12059 struct stat st;
12060 char_u *perm = NULL;
12061 char_u flags[] = "rwx";
12062 int i;
12063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012067 if (mch_stat((char *)fname, &st) >= 0)
12068 {
12069 perm = vim_strsave((char_u *)"---------");
12070 if (perm != NULL)
12071 {
12072 for (i = 0; i < 9; i++)
12073 {
12074 if (st.st_mode & (1 << (8 - i)))
12075 perm[i] = flags[i % 3];
12076 }
12077 }
12078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012080}
12081
12082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 * "getfsize({fname})" function
12084 */
12085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012086f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087{
12088 char_u *fname;
12089 struct stat st;
12090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094
12095 if (mch_stat((char *)fname, &st) >= 0)
12096 {
12097 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012102
12103 /* non-perfect check for overflow */
12104 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12105 rettv->vval.v_number = -2;
12106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 }
12108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110}
12111
12112/*
12113 * "getftime({fname})" function
12114 */
12115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012116f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
12118 char_u *fname;
12119 struct stat st;
12120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122
12123 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127}
12128
12129/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012130 * "getftype({fname})" function
12131 */
12132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012134{
12135 char_u *fname;
12136 struct stat st;
12137 char_u *type = NULL;
12138 char *t;
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012143 if (mch_lstat((char *)fname, &st) >= 0)
12144 {
12145#ifdef S_ISREG
12146 if (S_ISREG(st.st_mode))
12147 t = "file";
12148 else if (S_ISDIR(st.st_mode))
12149 t = "dir";
12150# ifdef S_ISLNK
12151 else if (S_ISLNK(st.st_mode))
12152 t = "link";
12153# endif
12154# ifdef S_ISBLK
12155 else if (S_ISBLK(st.st_mode))
12156 t = "bdev";
12157# endif
12158# ifdef S_ISCHR
12159 else if (S_ISCHR(st.st_mode))
12160 t = "cdev";
12161# endif
12162# ifdef S_ISFIFO
12163 else if (S_ISFIFO(st.st_mode))
12164 t = "fifo";
12165# endif
12166# ifdef S_ISSOCK
12167 else if (S_ISSOCK(st.st_mode))
12168 t = "fifo";
12169# endif
12170 else
12171 t = "other";
12172#else
12173# ifdef S_IFMT
12174 switch (st.st_mode & S_IFMT)
12175 {
12176 case S_IFREG: t = "file"; break;
12177 case S_IFDIR: t = "dir"; break;
12178# ifdef S_IFLNK
12179 case S_IFLNK: t = "link"; break;
12180# endif
12181# ifdef S_IFBLK
12182 case S_IFBLK: t = "bdev"; break;
12183# endif
12184# ifdef S_IFCHR
12185 case S_IFCHR: t = "cdev"; break;
12186# endif
12187# ifdef S_IFIFO
12188 case S_IFIFO: t = "fifo"; break;
12189# endif
12190# ifdef S_IFSOCK
12191 case S_IFSOCK: t = "socket"; break;
12192# endif
12193 default: t = "other";
12194 }
12195# else
12196 if (mch_isdir(fname))
12197 t = "dir";
12198 else
12199 t = "file";
12200# endif
12201#endif
12202 type = vim_strsave((char_u *)t);
12203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012205}
12206
12207/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012208 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012209 */
12210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012211f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212{
12213 linenr_T lnum;
12214 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012215 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216
12217 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012218 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012219 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012220 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012221 retlist = FALSE;
12222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012224 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012225 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012226 retlist = TRUE;
12227 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012228
Bram Moolenaar342337a2005-07-21 21:11:17 +000012229 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230}
12231
12232/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012233 * "getmatches()" function
12234 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012236f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012237{
12238#ifdef FEAT_SEARCH_EXTRA
12239 dict_T *dict;
12240 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012241 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012242
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012243 if (rettv_list_alloc(rettv) == OK)
12244 {
12245 while (cur != NULL)
12246 {
12247 dict = dict_alloc();
12248 if (dict == NULL)
12249 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012250 if (cur->match.regprog == NULL)
12251 {
12252 /* match added with matchaddpos() */
12253 for (i = 0; i < MAXPOSMATCH; ++i)
12254 {
12255 llpos_T *llpos;
12256 char buf[6];
12257 list_T *l;
12258
12259 llpos = &cur->pos.pos[i];
12260 if (llpos->lnum == 0)
12261 break;
12262 l = list_alloc();
12263 if (l == NULL)
12264 break;
12265 list_append_number(l, (varnumber_T)llpos->lnum);
12266 if (llpos->col > 0)
12267 {
12268 list_append_number(l, (varnumber_T)llpos->col);
12269 list_append_number(l, (varnumber_T)llpos->len);
12270 }
12271 sprintf(buf, "pos%d", i + 1);
12272 dict_add_list(dict, buf, l);
12273 }
12274 }
12275 else
12276 {
12277 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12278 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012279 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012280 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12281 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012282# ifdef FEAT_CONCEAL
12283 if (cur->conceal_char)
12284 {
12285 char_u buf[MB_MAXBYTES + 1];
12286
12287 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12288 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12289 }
12290# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012291 list_append_dict(rettv->vval.v_list, dict);
12292 cur = cur->next;
12293 }
12294 }
12295#endif
12296}
12297
12298/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012299 * "getpid()" function
12300 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012302f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012303{
12304 rettv->vval.v_number = mch_get_pid();
12305}
12306
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012307static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012308
12309/*
12310 * "getcurpos()" function
12311 */
12312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012313f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012314{
12315 getpos_both(argvars, rettv, TRUE);
12316}
12317
Bram Moolenaar18081e32008-02-20 19:11:07 +000012318/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012319 * "getpos(string)" function
12320 */
12321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012322f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012323{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012324 getpos_both(argvars, rettv, FALSE);
12325}
12326
12327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012328getpos_both(
12329 typval_T *argvars,
12330 typval_T *rettv,
12331 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012332{
Bram Moolenaara5525202006-03-02 22:52:09 +000012333 pos_T *fp;
12334 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012335 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012336
12337 if (rettv_list_alloc(rettv) == OK)
12338 {
12339 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012340 if (getcurpos)
12341 fp = &curwin->w_cursor;
12342 else
12343 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012344 if (fnum != -1)
12345 list_append_number(l, (varnumber_T)fnum);
12346 else
12347 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012348 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12349 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012350 list_append_number(l, (fp != NULL)
12351 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012352 : (varnumber_T)0);
12353 list_append_number(l,
12354#ifdef FEAT_VIRTUALEDIT
12355 (fp != NULL) ? (varnumber_T)fp->coladd :
12356#endif
12357 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012358 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012359 list_append_number(l, curwin->w_curswant == MAXCOL ?
12360 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012361 }
12362 else
12363 rettv->vval.v_number = FALSE;
12364}
12365
12366/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012367 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012368 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012371{
12372#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012373 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012374#endif
12375
Bram Moolenaar2641f772005-03-25 21:58:17 +000012376#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012377 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012378 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012379 wp = NULL;
12380 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12381 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012382 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012383 if (wp == NULL)
12384 return;
12385 }
12386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012387 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012388 }
12389#endif
12390}
12391
12392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 * "getreg()" function
12394 */
12395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012396f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397{
12398 char_u *strregname;
12399 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012400 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012401 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012402 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012406 strregname = get_tv_string_chk(&argvars[0]);
12407 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012408 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012411 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12412 return_list = get_tv_number_chk(&argvars[2], &error);
12413 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012417
12418 if (error)
12419 return;
12420
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 regname = (strregname == NULL ? '"' : *strregname);
12422 if (regname == 0)
12423 regname = '"';
12424
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012425 if (return_list)
12426 {
12427 rettv->v_type = VAR_LIST;
12428 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12429 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012430 if (rettv->vval.v_list != NULL)
12431 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012432 }
12433 else
12434 {
12435 rettv->v_type = VAR_STRING;
12436 rettv->vval.v_string = get_reg_contents(regname,
12437 arg2 ? GREG_EXPR_SRC : 0);
12438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439}
12440
12441/*
12442 * "getregtype()" function
12443 */
12444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012445f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446{
12447 char_u *strregname;
12448 int regname;
12449 char_u buf[NUMBUFLEN + 2];
12450 long reglen = 0;
12451
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012452 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 {
12454 strregname = get_tv_string_chk(&argvars[0]);
12455 if (strregname == NULL) /* type error; errmsg already given */
12456 {
12457 rettv->v_type = VAR_STRING;
12458 rettv->vval.v_string = NULL;
12459 return;
12460 }
12461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 else
12463 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012464 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465
12466 regname = (strregname == NULL ? '"' : *strregname);
12467 if (regname == 0)
12468 regname = '"';
12469
12470 buf[0] = NUL;
12471 buf[1] = NUL;
12472 switch (get_reg_type(regname, &reglen))
12473 {
12474 case MLINE: buf[0] = 'V'; break;
12475 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 case MBLOCK:
12477 buf[0] = Ctrl_V;
12478 sprintf((char *)buf + 1, "%ld", reglen + 1);
12479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->v_type = VAR_STRING;
12482 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483}
12484
12485/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012486 * "gettabvar()" function
12487 */
12488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012490{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012491 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012492 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012493 dictitem_T *v;
12494 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012495 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012496
12497 rettv->v_type = VAR_STRING;
12498 rettv->vval.v_string = NULL;
12499
12500 varname = get_tv_string_chk(&argvars[1]);
12501 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12502 if (tp != NULL && varname != NULL)
12503 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012504 /* Set tp to be our tabpage, temporarily. Also set the window to the
12505 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012506 if (switch_win(&oldcurwin, &oldtabpage,
12507 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012508 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012509 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012510 /* look up the variable */
12511 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12512 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12513 if (v != NULL)
12514 {
12515 copy_tv(&v->di_tv, rettv);
12516 done = TRUE;
12517 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012518 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012519
12520 /* restore previous notion of curwin */
12521 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012522 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012523
12524 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012525 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012526}
12527
12528/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012529 * "gettabwinvar()" function
12530 */
12531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012532f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012533{
12534 getwinvar(argvars, rettv, 1);
12535}
12536
12537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 * "getwinposx()" function
12539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012541f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544#ifdef FEAT_GUI
12545 if (gui.in_use)
12546 {
12547 int x, y;
12548
12549 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 }
12552#endif
12553}
12554
12555/*
12556 * "getwinposy()" function
12557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012559f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562#ifdef FEAT_GUI
12563 if (gui.in_use)
12564 {
12565 int x, y;
12566
12567 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 }
12570#endif
12571}
12572
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012573/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012574 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012575 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012576 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012577find_win_by_nr(
12578 typval_T *vp,
12579 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012580{
12581#ifdef FEAT_WINDOWS
12582 win_T *wp;
12583#endif
12584 int nr;
12585
12586 nr = get_tv_number_chk(vp, NULL);
12587
12588#ifdef FEAT_WINDOWS
12589 if (nr < 0)
12590 return NULL;
12591 if (nr == 0)
12592 return curwin;
12593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012594 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12595 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012596 if (--nr <= 0)
12597 break;
12598 return wp;
12599#else
12600 if (nr == 0 || nr == 1)
12601 return curwin;
12602 return NULL;
12603#endif
12604}
12605
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012607 * Find window specified by "wvp" in tabpage "tvp".
12608 */
12609 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012610find_tabwin(
12611 typval_T *wvp, /* VAR_UNKNOWN for current window */
12612 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012613{
12614 win_T *wp = NULL;
12615 tabpage_T *tp = NULL;
12616 long n;
12617
12618 if (wvp->v_type != VAR_UNKNOWN)
12619 {
12620 if (tvp->v_type != VAR_UNKNOWN)
12621 {
12622 n = get_tv_number(tvp);
12623 if (n >= 0)
12624 tp = find_tabpage(n);
12625 }
12626 else
12627 tp = curtab;
12628
12629 if (tp != NULL)
12630 wp = find_win_by_nr(wvp, tp);
12631 }
12632 else
12633 wp = curwin;
12634
12635 return wp;
12636}
12637
12638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 * "getwinvar()" function
12640 */
12641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012642f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012644 getwinvar(argvars, rettv, 0);
12645}
12646
12647/*
12648 * getwinvar() and gettabwinvar()
12649 */
12650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651getwinvar(
12652 typval_T *argvars,
12653 typval_T *rettv,
12654 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012655{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012656 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012659 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012660 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012661#ifdef FEAT_WINDOWS
12662 win_T *oldcurwin;
12663 tabpage_T *oldtabpage;
12664 int need_switch_win;
12665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012667#ifdef FEAT_WINDOWS
12668 if (off == 1)
12669 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12670 else
12671 tp = curtab;
12672#endif
12673 win = find_win_by_nr(&argvars[off], tp);
12674 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679
12680 if (win != NULL && varname != NULL)
12681 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012682#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012683 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012684 * otherwise the window is not valid. Only do this when needed,
12685 * autocommands get blocked. */
12686 need_switch_win = !(tp == curtab && win == curwin);
12687 if (!need_switch_win
12688 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12689#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012690 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012691 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012692 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012693 if (get_option_tv(&varname, rettv, 1) == OK)
12694 done = TRUE;
12695 }
12696 else
12697 {
12698 /* Look up the variable. */
12699 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12700 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12701 varname, FALSE);
12702 if (v != NULL)
12703 {
12704 copy_tv(&v->di_tv, rettv);
12705 done = TRUE;
12706 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012709
Bram Moolenaarba117c22015-09-29 16:53:22 +020012710#ifdef FEAT_WINDOWS
12711 if (need_switch_win)
12712 /* restore previous notion of curwin */
12713 restore_win(oldcurwin, oldtabpage, TRUE);
12714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 }
12716
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012717 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12718 /* use the default return value */
12719 copy_tv(&argvars[off + 2], rettv);
12720
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 --emsg_off;
12722}
12723
12724/*
12725 * "glob()" function
12726 */
12727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012728f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012730 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012732 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012734 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012735 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012737 if (argvars[1].v_type != VAR_UNKNOWN)
12738 {
12739 if (get_tv_number_chk(&argvars[1], &error))
12740 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012742 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012743 if (get_tv_number_chk(&argvars[2], &error))
12744 {
12745 rettv->v_type = VAR_LIST;
12746 rettv->vval.v_list = NULL;
12747 }
12748 if (argvars[3].v_type != VAR_UNKNOWN
12749 && get_tv_number_chk(&argvars[3], &error))
12750 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012751 }
12752 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012753 if (!error)
12754 {
12755 ExpandInit(&xpc);
12756 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012757 if (p_wic)
12758 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012759 if (rettv->v_type == VAR_STRING)
12760 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012761 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012762 else if (rettv_list_alloc(rettv) != FAIL)
12763 {
12764 int i;
12765
12766 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12767 NULL, options, WILD_ALL_KEEP);
12768 for (i = 0; i < xpc.xp_numfiles; i++)
12769 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12770
12771 ExpandCleanup(&xpc);
12772 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012773 }
12774 else
12775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
12779 * "globpath()" function
12780 */
12781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012782f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012784 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012787 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012788 garray_T ga;
12789 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012791 /* When the optional second argument is non-zero, don't remove matches
12792 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012794 if (argvars[2].v_type != VAR_UNKNOWN)
12795 {
12796 if (get_tv_number_chk(&argvars[2], &error))
12797 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012798 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012799 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012800 if (get_tv_number_chk(&argvars[3], &error))
12801 {
12802 rettv->v_type = VAR_LIST;
12803 rettv->vval.v_list = NULL;
12804 }
12805 if (argvars[4].v_type != VAR_UNKNOWN
12806 && get_tv_number_chk(&argvars[4], &error))
12807 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012808 }
12809 }
12810 if (file != NULL && !error)
12811 {
12812 ga_init2(&ga, (int)sizeof(char_u *), 10);
12813 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12814 if (rettv->v_type == VAR_STRING)
12815 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12816 else if (rettv_list_alloc(rettv) != FAIL)
12817 for (i = 0; i < ga.ga_len; ++i)
12818 list_append_string(rettv->vval.v_list,
12819 ((char_u **)(ga.ga_data))[i], -1);
12820 ga_clear_strings(&ga);
12821 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012822 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012823 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012827 * "glob2regpat()" function
12828 */
12829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012830f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012831{
12832 char_u *pat = get_tv_string_chk(&argvars[0]);
12833
12834 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012835 rettv->vval.v_string = (pat == NULL)
12836 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012837}
12838
12839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 * "has()" function
12841 */
12842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012843f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844{
12845 int i;
12846 char_u *name;
12847 int n = FALSE;
12848 static char *(has_list[]) =
12849 {
12850#ifdef AMIGA
12851 "amiga",
12852# ifdef FEAT_ARP
12853 "arp",
12854# endif
12855#endif
12856#ifdef __BEOS__
12857 "beos",
12858#endif
12859#ifdef MSDOS
12860# ifdef DJGPP
12861 "dos32",
12862# else
12863 "dos16",
12864# endif
12865#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012866#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 "mac",
12868#endif
12869#if defined(MACOS_X_UNIX)
12870 "macunix",
12871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#ifdef __QNX__
12873 "qnx",
12874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875#ifdef UNIX
12876 "unix",
12877#endif
12878#ifdef VMS
12879 "vms",
12880#endif
12881#ifdef WIN16
12882 "win16",
12883#endif
12884#ifdef WIN32
12885 "win32",
12886#endif
12887#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12888 "win32unix",
12889#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012890#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 "win64",
12892#endif
12893#ifdef EBCDIC
12894 "ebcdic",
12895#endif
12896#ifndef CASE_INSENSITIVE_FILENAME
12897 "fname_case",
12898#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012899#ifdef HAVE_ACL
12900 "acl",
12901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#ifdef FEAT_ARABIC
12903 "arabic",
12904#endif
12905#ifdef FEAT_AUTOCMD
12906 "autocmd",
12907#endif
12908#ifdef FEAT_BEVAL
12909 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012910# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12911 "balloon_multiline",
12912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913#endif
12914#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12915 "builtin_terms",
12916# ifdef ALL_BUILTIN_TCAPS
12917 "all_builtin_terms",
12918# endif
12919#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012920#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12921 || defined(FEAT_GUI_W32) \
12922 || defined(FEAT_GUI_MOTIF))
12923 "browsefilter",
12924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925#ifdef FEAT_BYTEOFF
12926 "byte_offset",
12927#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012928#ifdef FEAT_CHANNEL
12929 "channel",
12930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931#ifdef FEAT_CINDENT
12932 "cindent",
12933#endif
12934#ifdef FEAT_CLIENTSERVER
12935 "clientserver",
12936#endif
12937#ifdef FEAT_CLIPBOARD
12938 "clipboard",
12939#endif
12940#ifdef FEAT_CMDL_COMPL
12941 "cmdline_compl",
12942#endif
12943#ifdef FEAT_CMDHIST
12944 "cmdline_hist",
12945#endif
12946#ifdef FEAT_COMMENTS
12947 "comments",
12948#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012949#ifdef FEAT_CONCEAL
12950 "conceal",
12951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#ifdef FEAT_CRYPT
12953 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012954 "crypt-blowfish",
12955 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956#endif
12957#ifdef FEAT_CSCOPE
12958 "cscope",
12959#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012960#ifdef FEAT_CURSORBIND
12961 "cursorbind",
12962#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012963#ifdef CURSOR_SHAPE
12964 "cursorshape",
12965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966#ifdef DEBUG
12967 "debug",
12968#endif
12969#ifdef FEAT_CON_DIALOG
12970 "dialog_con",
12971#endif
12972#ifdef FEAT_GUI_DIALOG
12973 "dialog_gui",
12974#endif
12975#ifdef FEAT_DIFF
12976 "diff",
12977#endif
12978#ifdef FEAT_DIGRAPHS
12979 "digraphs",
12980#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012981#ifdef FEAT_DIRECTX
12982 "directx",
12983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984#ifdef FEAT_DND
12985 "dnd",
12986#endif
12987#ifdef FEAT_EMACS_TAGS
12988 "emacs_tags",
12989#endif
12990 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012991 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992#ifdef FEAT_SEARCH_EXTRA
12993 "extra_search",
12994#endif
12995#ifdef FEAT_FKMAP
12996 "farsi",
12997#endif
12998#ifdef FEAT_SEARCHPATH
12999 "file_in_path",
13000#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013001#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013002 "filterpipe",
13003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004#ifdef FEAT_FIND_ID
13005 "find_in_path",
13006#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013007#ifdef FEAT_FLOAT
13008 "float",
13009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010#ifdef FEAT_FOLDING
13011 "folding",
13012#endif
13013#ifdef FEAT_FOOTER
13014 "footer",
13015#endif
13016#if !defined(USE_SYSTEM) && defined(UNIX)
13017 "fork",
13018#endif
13019#ifdef FEAT_GETTEXT
13020 "gettext",
13021#endif
13022#ifdef FEAT_GUI
13023 "gui",
13024#endif
13025#ifdef FEAT_GUI_ATHENA
13026# ifdef FEAT_GUI_NEXTAW
13027 "gui_neXtaw",
13028# else
13029 "gui_athena",
13030# endif
13031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#ifdef FEAT_GUI_GTK
13033 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013036#ifdef FEAT_GUI_GNOME
13037 "gui_gnome",
13038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef FEAT_GUI_MAC
13040 "gui_mac",
13041#endif
13042#ifdef FEAT_GUI_MOTIF
13043 "gui_motif",
13044#endif
13045#ifdef FEAT_GUI_PHOTON
13046 "gui_photon",
13047#endif
13048#ifdef FEAT_GUI_W16
13049 "gui_win16",
13050#endif
13051#ifdef FEAT_GUI_W32
13052 "gui_win32",
13053#endif
13054#ifdef FEAT_HANGULIN
13055 "hangul_input",
13056#endif
13057#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13058 "iconv",
13059#endif
13060#ifdef FEAT_INS_EXPAND
13061 "insert_expand",
13062#endif
13063#ifdef FEAT_JUMPLIST
13064 "jumplist",
13065#endif
13066#ifdef FEAT_KEYMAP
13067 "keymap",
13068#endif
13069#ifdef FEAT_LANGMAP
13070 "langmap",
13071#endif
13072#ifdef FEAT_LIBCALL
13073 "libcall",
13074#endif
13075#ifdef FEAT_LINEBREAK
13076 "linebreak",
13077#endif
13078#ifdef FEAT_LISP
13079 "lispindent",
13080#endif
13081#ifdef FEAT_LISTCMDS
13082 "listcmds",
13083#endif
13084#ifdef FEAT_LOCALMAP
13085 "localmap",
13086#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013087#ifdef FEAT_LUA
13088# ifndef DYNAMIC_LUA
13089 "lua",
13090# endif
13091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092#ifdef FEAT_MENU
13093 "menu",
13094#endif
13095#ifdef FEAT_SESSION
13096 "mksession",
13097#endif
13098#ifdef FEAT_MODIFY_FNAME
13099 "modify_fname",
13100#endif
13101#ifdef FEAT_MOUSE
13102 "mouse",
13103#endif
13104#ifdef FEAT_MOUSESHAPE
13105 "mouseshape",
13106#endif
13107#if defined(UNIX) || defined(VMS)
13108# ifdef FEAT_MOUSE_DEC
13109 "mouse_dec",
13110# endif
13111# ifdef FEAT_MOUSE_GPM
13112 "mouse_gpm",
13113# endif
13114# ifdef FEAT_MOUSE_JSB
13115 "mouse_jsbterm",
13116# endif
13117# ifdef FEAT_MOUSE_NET
13118 "mouse_netterm",
13119# endif
13120# ifdef FEAT_MOUSE_PTERM
13121 "mouse_pterm",
13122# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013123# ifdef FEAT_MOUSE_SGR
13124 "mouse_sgr",
13125# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013126# ifdef FEAT_SYSMOUSE
13127 "mouse_sysmouse",
13128# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013129# ifdef FEAT_MOUSE_URXVT
13130 "mouse_urxvt",
13131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132# ifdef FEAT_MOUSE_XTERM
13133 "mouse_xterm",
13134# endif
13135#endif
13136#ifdef FEAT_MBYTE
13137 "multi_byte",
13138#endif
13139#ifdef FEAT_MBYTE_IME
13140 "multi_byte_ime",
13141#endif
13142#ifdef FEAT_MULTI_LANG
13143 "multi_lang",
13144#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013145#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013146#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013147 "mzscheme",
13148#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_OLE
13151 "ole",
13152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153#ifdef FEAT_PATH_EXTRA
13154 "path_extra",
13155#endif
13156#ifdef FEAT_PERL
13157#ifndef DYNAMIC_PERL
13158 "perl",
13159#endif
13160#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013161#ifdef FEAT_PERSISTENT_UNDO
13162 "persistent_undo",
13163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164#ifdef FEAT_PYTHON
13165#ifndef DYNAMIC_PYTHON
13166 "python",
13167#endif
13168#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013169#ifdef FEAT_PYTHON3
13170#ifndef DYNAMIC_PYTHON3
13171 "python3",
13172#endif
13173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174#ifdef FEAT_POSTSCRIPT
13175 "postscript",
13176#endif
13177#ifdef FEAT_PRINTER
13178 "printer",
13179#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013180#ifdef FEAT_PROFILE
13181 "profile",
13182#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013183#ifdef FEAT_RELTIME
13184 "reltime",
13185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186#ifdef FEAT_QUICKFIX
13187 "quickfix",
13188#endif
13189#ifdef FEAT_RIGHTLEFT
13190 "rightleft",
13191#endif
13192#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13193 "ruby",
13194#endif
13195#ifdef FEAT_SCROLLBIND
13196 "scrollbind",
13197#endif
13198#ifdef FEAT_CMDL_INFO
13199 "showcmd",
13200 "cmdline_info",
13201#endif
13202#ifdef FEAT_SIGNS
13203 "signs",
13204#endif
13205#ifdef FEAT_SMARTINDENT
13206 "smartindent",
13207#endif
13208#ifdef FEAT_SNIFF
13209 "sniff",
13210#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013211#ifdef STARTUPTIME
13212 "startuptime",
13213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214#ifdef FEAT_STL_OPT
13215 "statusline",
13216#endif
13217#ifdef FEAT_SUN_WORKSHOP
13218 "sun_workshop",
13219#endif
13220#ifdef FEAT_NETBEANS_INTG
13221 "netbeans_intg",
13222#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013223#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013224 "spell",
13225#endif
13226#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 "syntax",
13228#endif
13229#if defined(USE_SYSTEM) || !defined(UNIX)
13230 "system",
13231#endif
13232#ifdef FEAT_TAG_BINS
13233 "tag_binary",
13234#endif
13235#ifdef FEAT_TAG_OLDSTATIC
13236 "tag_old_static",
13237#endif
13238#ifdef FEAT_TAG_ANYWHITE
13239 "tag_any_white",
13240#endif
13241#ifdef FEAT_TCL
13242# ifndef DYNAMIC_TCL
13243 "tcl",
13244# endif
13245#endif
13246#ifdef TERMINFO
13247 "terminfo",
13248#endif
13249#ifdef FEAT_TERMRESPONSE
13250 "termresponse",
13251#endif
13252#ifdef FEAT_TEXTOBJ
13253 "textobjects",
13254#endif
13255#ifdef HAVE_TGETENT
13256 "tgetent",
13257#endif
13258#ifdef FEAT_TITLE
13259 "title",
13260#endif
13261#ifdef FEAT_TOOLBAR
13262 "toolbar",
13263#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013264#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13265 "unnamedplus",
13266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267#ifdef FEAT_USR_CMDS
13268 "user-commands", /* was accidentally included in 5.4 */
13269 "user_commands",
13270#endif
13271#ifdef FEAT_VIMINFO
13272 "viminfo",
13273#endif
13274#ifdef FEAT_VERTSPLIT
13275 "vertsplit",
13276#endif
13277#ifdef FEAT_VIRTUALEDIT
13278 "virtualedit",
13279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#ifdef FEAT_VISUALEXTRA
13282 "visualextra",
13283#endif
13284#ifdef FEAT_VREPLACE
13285 "vreplace",
13286#endif
13287#ifdef FEAT_WILDIGN
13288 "wildignore",
13289#endif
13290#ifdef FEAT_WILDMENU
13291 "wildmenu",
13292#endif
13293#ifdef FEAT_WINDOWS
13294 "windows",
13295#endif
13296#ifdef FEAT_WAK
13297 "winaltkeys",
13298#endif
13299#ifdef FEAT_WRITEBACKUP
13300 "writebackup",
13301#endif
13302#ifdef FEAT_XIM
13303 "xim",
13304#endif
13305#ifdef FEAT_XFONTSET
13306 "xfontset",
13307#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013308#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013309 "xpm",
13310 "xpm_w32", /* for backward compatibility */
13311#else
13312# if defined(HAVE_XPM)
13313 "xpm",
13314# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316#ifdef USE_XSMP
13317 "xsmp",
13318#endif
13319#ifdef USE_XSMP_INTERACT
13320 "xsmp_interact",
13321#endif
13322#ifdef FEAT_XCLIPBOARD
13323 "xterm_clipboard",
13324#endif
13325#ifdef FEAT_XTERM_SAVE
13326 "xterm_save",
13327#endif
13328#if defined(UNIX) && defined(FEAT_X11)
13329 "X11",
13330#endif
13331 NULL
13332 };
13333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 for (i = 0; has_list[i] != NULL; ++i)
13336 if (STRICMP(name, has_list[i]) == 0)
13337 {
13338 n = TRUE;
13339 break;
13340 }
13341
13342 if (n == FALSE)
13343 {
13344 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013345 {
13346 if (name[5] == '-'
13347 && STRLEN(name) > 11
13348 && vim_isdigit(name[6])
13349 && vim_isdigit(name[8])
13350 && vim_isdigit(name[10]))
13351 {
13352 int major = atoi((char *)name + 6);
13353 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013354
13355 /* Expect "patch-9.9.01234". */
13356 n = (major < VIM_VERSION_MAJOR
13357 || (major == VIM_VERSION_MAJOR
13358 && (minor < VIM_VERSION_MINOR
13359 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013360 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013361 }
13362 else
13363 n = has_patch(atoi((char *)name + 5));
13364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 else if (STRICMP(name, "vim_starting") == 0)
13366 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013367#ifdef FEAT_MBYTE
13368 else if (STRICMP(name, "multi_byte_encoding") == 0)
13369 n = has_mbyte;
13370#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013371#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13372 else if (STRICMP(name, "balloon_multiline") == 0)
13373 n = multiline_balloon_available();
13374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375#ifdef DYNAMIC_TCL
13376 else if (STRICMP(name, "tcl") == 0)
13377 n = tcl_enabled(FALSE);
13378#endif
13379#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13380 else if (STRICMP(name, "iconv") == 0)
13381 n = iconv_enabled(FALSE);
13382#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013383#ifdef DYNAMIC_LUA
13384 else if (STRICMP(name, "lua") == 0)
13385 n = lua_enabled(FALSE);
13386#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013387#ifdef DYNAMIC_MZSCHEME
13388 else if (STRICMP(name, "mzscheme") == 0)
13389 n = mzscheme_enabled(FALSE);
13390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef DYNAMIC_RUBY
13392 else if (STRICMP(name, "ruby") == 0)
13393 n = ruby_enabled(FALSE);
13394#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013395#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396#ifdef DYNAMIC_PYTHON
13397 else if (STRICMP(name, "python") == 0)
13398 n = python_enabled(FALSE);
13399#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013400#endif
13401#ifdef FEAT_PYTHON3
13402#ifdef DYNAMIC_PYTHON3
13403 else if (STRICMP(name, "python3") == 0)
13404 n = python3_enabled(FALSE);
13405#endif
13406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407#ifdef DYNAMIC_PERL
13408 else if (STRICMP(name, "perl") == 0)
13409 n = perl_enabled(FALSE);
13410#endif
13411#ifdef FEAT_GUI
13412 else if (STRICMP(name, "gui_running") == 0)
13413 n = (gui.in_use || gui.starting);
13414# ifdef FEAT_GUI_W32
13415 else if (STRICMP(name, "gui_win32s") == 0)
13416 n = gui_is_win32s();
13417# endif
13418# ifdef FEAT_BROWSE
13419 else if (STRICMP(name, "browse") == 0)
13420 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13421# endif
13422#endif
13423#ifdef FEAT_SYN_HL
13424 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013425 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#endif
13427#if defined(WIN3264)
13428 else if (STRICMP(name, "win95") == 0)
13429 n = mch_windows95();
13430#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013431#ifdef FEAT_NETBEANS_INTG
13432 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013433 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 }
13436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438}
13439
13440/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013441 * "has_key()" function
13442 */
13443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013444f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013445{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013446 if (argvars[0].v_type != VAR_DICT)
13447 {
13448 EMSG(_(e_dictreq));
13449 return;
13450 }
13451 if (argvars[0].vval.v_dict == NULL)
13452 return;
13453
13454 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013455 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013456}
13457
13458/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013459 * "haslocaldir()" function
13460 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013462f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013463{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013464 win_T *wp = NULL;
13465
13466 wp = find_tabwin(&argvars[0], &argvars[1]);
13467 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013468}
13469
13470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 * "hasmapto()" function
13472 */
13473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013474f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
13476 char_u *name;
13477 char_u *mode;
13478 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013479 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 mode = (char_u *)"nvo";
13484 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013487 if (argvars[2].v_type != VAR_UNKNOWN)
13488 abbr = get_tv_number(&argvars[2]);
13489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490
Bram Moolenaar2c932302006-03-18 21:42:09 +000013491 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495}
13496
13497/*
13498 * "histadd()" function
13499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013501f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
13503#ifdef FEAT_CMDHIST
13504 int histype;
13505 char_u *str;
13506 char_u buf[NUMBUFLEN];
13507#endif
13508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 if (check_restricted() || check_secure())
13511 return;
13512#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13514 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 if (histype >= 0)
13516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 if (*str != NUL)
13519 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013520 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 return;
13524 }
13525 }
13526#endif
13527}
13528
13529/*
13530 * "histdel()" function
13531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013533f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534{
13535#ifdef FEAT_CMDHIST
13536 int n;
13537 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013538 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13541 if (str == NULL)
13542 n = 0;
13543 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013546 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 else
13551 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 get_tv_string_buf(&argvars[1], buf));
13554 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555#endif
13556}
13557
13558/*
13559 * "histget()" function
13560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
13564#ifdef FEAT_CMDHIST
13565 int type;
13566 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13570 if (str == NULL)
13571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 {
13574 type = get_histtype(str);
13575 if (argvars[1].v_type == VAR_UNKNOWN)
13576 idx = get_history_idx(type);
13577 else
13578 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13579 /* -1 on type error */
13580 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * "histnr()" function
13590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013592f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
13594 int i;
13595
13596#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 char_u *history = get_tv_string_chk(&argvars[0]);
13598
13599 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 if (i >= HIST_CMD && i < HIST_COUNT)
13601 i = get_history_idx(i);
13602 else
13603#endif
13604 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 * "highlightID(name)" function
13610 */
13611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013612f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013618 * "highlight_exists()" function
13619 */
13620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013621f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622{
13623 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13624}
13625
13626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 * "hostname()" function
13628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013630f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631{
13632 char_u hostname[256];
13633
13634 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->v_type = VAR_STRING;
13636 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
13639/*
13640 * iconv() function
13641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645#ifdef FEAT_MBYTE
13646 char_u buf1[NUMBUFLEN];
13647 char_u buf2[NUMBUFLEN];
13648 char_u *from, *to, *str;
13649 vimconv_T vimconv;
13650#endif
13651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->v_type = VAR_STRING;
13653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654
13655#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 str = get_tv_string(&argvars[0]);
13657 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13658 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 vimconv.vc_type = CONV_NONE;
13660 convert_setup(&vimconv, from, to);
13661
13662 /* If the encodings are equal, no conversion needed. */
13663 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667
13668 convert_setup(&vimconv, NULL, NULL);
13669 vim_free(from);
13670 vim_free(to);
13671#endif
13672}
13673
13674/*
13675 * "indent()" function
13676 */
13677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013678f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679{
13680 linenr_T lnum;
13681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687}
13688
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013689/*
13690 * "index()" function
13691 */
13692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013693f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013694{
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 list_T *l;
13696 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013697 long idx = 0;
13698 int ic = FALSE;
13699
13700 rettv->vval.v_number = -1;
13701 if (argvars[0].v_type != VAR_LIST)
13702 {
13703 EMSG(_(e_listreq));
13704 return;
13705 }
13706 l = argvars[0].vval.v_list;
13707 if (l != NULL)
13708 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013709 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013710 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 int error = FALSE;
13713
Bram Moolenaar758711c2005-02-02 23:11:38 +000013714 /* Start at specified item. Use the cached index that list_find()
13715 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013717 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013718 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 ic = get_tv_number_chk(&argvars[3], &error);
13720 if (error)
13721 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013722 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013723
Bram Moolenaar758711c2005-02-02 23:11:38 +000013724 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013725 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013726 {
13727 rettv->vval.v_number = idx;
13728 break;
13729 }
13730 }
13731}
13732
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733static int inputsecret_flag = 0;
13734
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013735static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013736
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013738 * This function is used by f_input() and f_inputdialog() functions. The third
13739 * argument to f_input() specifies the type of completion to use at the
13740 * prompt. The third argument to f_inputdialog() specifies the value to return
13741 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 */
13743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013744get_user_input(
13745 typval_T *argvars,
13746 typval_T *rettv,
13747 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 char_u *p = NULL;
13751 int c;
13752 char_u buf[NUMBUFLEN];
13753 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013755 int xp_type = EXPAND_NOTHING;
13756 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
13761#ifdef NO_CONSOLE_INPUT
13762 /* While starting up, there is no place to enter text. */
13763 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765#endif
13766
13767 cmd_silent = FALSE; /* Want to see the prompt. */
13768 if (prompt != NULL)
13769 {
13770 /* Only the part of the message after the last NL is considered as
13771 * prompt for the command line */
13772 p = vim_strrchr(prompt, '\n');
13773 if (p == NULL)
13774 p = prompt;
13775 else
13776 {
13777 ++p;
13778 c = *p;
13779 *p = NUL;
13780 msg_start();
13781 msg_clr_eos();
13782 msg_puts_attr(prompt, echo_attr);
13783 msg_didout = FALSE;
13784 msg_starthere();
13785 *p = c;
13786 }
13787 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 if (argvars[1].v_type != VAR_UNKNOWN)
13790 {
13791 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13792 if (defstr != NULL)
13793 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013795 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013796 {
13797 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013798 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013799 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013800
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013801 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013802 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013803
Bram Moolenaar4463f292005-09-25 22:20:24 +000013804 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13805 if (xp_name == NULL)
13806 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013807
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013808 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013809
Bram Moolenaar4463f292005-09-25 22:20:24 +000013810 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13811 &xp_arg) == FAIL)
13812 return;
13813 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013814 }
13815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013816 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013817 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013818 int save_ex_normal_busy = ex_normal_busy;
13819 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013821 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13822 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013823 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013824 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013825 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013826 && argvars[1].v_type != VAR_UNKNOWN
13827 && argvars[2].v_type != VAR_UNKNOWN)
13828 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13829 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013830
13831 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 /* since the user typed this, no need to wait for return */
13834 need_wait_return = FALSE;
13835 msg_didout = FALSE;
13836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 cmd_silent = cmd_silent_save;
13838}
13839
13840/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013841 * "input()" function
13842 * Also handles inputsecret() when inputsecret is set.
13843 */
13844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013845f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013846{
13847 get_user_input(argvars, rettv, FALSE);
13848}
13849
13850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 * "inputdialog()" function
13852 */
13853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013854f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855{
13856#if defined(FEAT_GUI_TEXTDIALOG)
13857 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13858 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13859 {
13860 char_u *message;
13861 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 message = get_tv_string_chk(&argvars[0]);
13865 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013866 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013867 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 else
13869 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 if (message != NULL && defstr != NULL
13871 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013872 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 else
13875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 if (message != NULL && defstr != NULL
13877 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013878 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->vval.v_string = vim_strsave(
13880 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 }
13886 else
13887#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013888 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889}
13890
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013891/*
13892 * "inputlist()" function
13893 */
13894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013895f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013896{
13897 listitem_T *li;
13898 int selected;
13899 int mouse_used;
13900
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013901#ifdef NO_CONSOLE_INPUT
13902 /* While starting up, there is no place to enter text. */
13903 if (no_console_input())
13904 return;
13905#endif
13906 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13907 {
13908 EMSG2(_(e_listarg), "inputlist()");
13909 return;
13910 }
13911
13912 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013913 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013914 lines_left = Rows; /* avoid more prompt */
13915 msg_scroll = TRUE;
13916 msg_clr_eos();
13917
13918 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13919 {
13920 msg_puts(get_tv_string(&li->li_tv));
13921 msg_putchar('\n');
13922 }
13923
13924 /* Ask for choice. */
13925 selected = prompt_for_number(&mouse_used);
13926 if (mouse_used)
13927 selected -= lines_left;
13928
13929 rettv->vval.v_number = selected;
13930}
13931
13932
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13934
13935/*
13936 * "inputrestore()" function
13937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013939f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940{
13941 if (ga_userinput.ga_len > 0)
13942 {
13943 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13945 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013946 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 }
13948 else if (p_verbose > 1)
13949 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013950 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 }
13953}
13954
13955/*
13956 * "inputsave()" function
13957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013959f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013961 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 if (ga_grow(&ga_userinput, 1) == OK)
13963 {
13964 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13965 + ga_userinput.ga_len);
13966 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013967 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 }
13969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971}
13972
13973/*
13974 * "inputsecret()" function
13975 */
13976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013977f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978{
13979 ++cmdline_star;
13980 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 --cmdline_star;
13983 --inputsecret_flag;
13984}
13985
13986/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987 * "insert()" function
13988 */
13989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013990f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013991{
13992 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 listitem_T *item;
13994 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013996
13997 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013998 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013999 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014000 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014001 {
14002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 before = get_tv_number_chk(&argvars[2], &error);
14004 if (error)
14005 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014006
Bram Moolenaar758711c2005-02-02 23:11:38 +000014007 if (before == l->lv_len)
14008 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014009 else
14010 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014011 item = list_find(l, before);
14012 if (item == NULL)
14013 {
14014 EMSGN(_(e_listidx), before);
14015 l = NULL;
14016 }
14017 }
14018 if (l != NULL)
14019 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014020 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014021 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022 }
14023 }
14024}
14025
14026/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014027 * "invert(expr)" function
14028 */
14029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014030f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014031{
14032 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14033}
14034
14035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 * "isdirectory()" function
14037 */
14038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014039f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014044/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014045 * "islocked()" function
14046 */
14047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014048f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014049{
14050 lval_T lv;
14051 char_u *end;
14052 dictitem_T *di;
14053
14054 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014055 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14056 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014057 if (end != NULL && lv.ll_name != NULL)
14058 {
14059 if (*end != NUL)
14060 EMSG(_(e_trailing));
14061 else
14062 {
14063 if (lv.ll_tv == NULL)
14064 {
14065 if (check_changedtick(lv.ll_name))
14066 rettv->vval.v_number = 1; /* always locked */
14067 else
14068 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014069 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014070 if (di != NULL)
14071 {
14072 /* Consider a variable locked when:
14073 * 1. the variable itself is locked
14074 * 2. the value of the variable is locked.
14075 * 3. the List or Dict value is locked.
14076 */
14077 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14078 || tv_islocked(&di->di_tv));
14079 }
14080 }
14081 }
14082 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014083 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014084 else if (lv.ll_newkey != NULL)
14085 EMSG2(_(e_dictkey), lv.ll_newkey);
14086 else if (lv.ll_list != NULL)
14087 /* List item. */
14088 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14089 else
14090 /* Dictionary item. */
14091 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14092 }
14093 }
14094
14095 clear_lval(&lv);
14096}
14097
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014098static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099
14100/*
14101 * Turn a dict into a list:
14102 * "what" == 0: list of keys
14103 * "what" == 1: list of values
14104 * "what" == 2: list of items
14105 */
14106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108{
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 list_T *l2;
14110 dictitem_T *di;
14111 hashitem_T *hi;
14112 listitem_T *li;
14113 listitem_T *li2;
14114 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014115 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014116
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117 if (argvars[0].v_type != VAR_DICT)
14118 {
14119 EMSG(_(e_dictreq));
14120 return;
14121 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014122 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 return;
14124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014126 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014128 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014129 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 --todo;
14134 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014135
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014136 li = listitem_alloc();
14137 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014138 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014139 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014140
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014141 if (what == 0)
14142 {
14143 /* keys() */
14144 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014145 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014146 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14147 }
14148 else if (what == 1)
14149 {
14150 /* values() */
14151 copy_tv(&di->di_tv, &li->li_tv);
14152 }
14153 else
14154 {
14155 /* items() */
14156 l2 = list_alloc();
14157 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014158 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 li->li_tv.vval.v_list = l2;
14160 if (l2 == NULL)
14161 break;
14162 ++l2->lv_refcount;
14163
14164 li2 = listitem_alloc();
14165 if (li2 == NULL)
14166 break;
14167 list_append(l2, li2);
14168 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014169 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014170 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14171
14172 li2 = listitem_alloc();
14173 if (li2 == NULL)
14174 break;
14175 list_append(l2, li2);
14176 copy_tv(&di->di_tv, &li2->li_tv);
14177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014178 }
14179 }
14180}
14181
14182/*
14183 * "items(dict)" function
14184 */
14185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014186f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014187{
14188 dict_list(argvars, rettv, 2);
14189}
14190
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014192 * "join()" function
14193 */
14194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014195f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014196{
14197 garray_T ga;
14198 char_u *sep;
14199
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014200 if (argvars[0].v_type != VAR_LIST)
14201 {
14202 EMSG(_(e_listreq));
14203 return;
14204 }
14205 if (argvars[0].vval.v_list == NULL)
14206 return;
14207 if (argvars[1].v_type == VAR_UNKNOWN)
14208 sep = (char_u *)" ";
14209 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014211
14212 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213
14214 if (sep != NULL)
14215 {
14216 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014217 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 ga_append(&ga, NUL);
14219 rettv->vval.v_string = (char_u *)ga.ga_data;
14220 }
14221 else
14222 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014223}
14224
14225/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014226 * "jsondecode()" function
14227 */
14228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014229f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014230{
14231 js_read_T reader;
14232
14233 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014234 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014235 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014236 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014237 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014238}
14239
14240/*
14241 * "jsonencode()" function
14242 */
14243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014244f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014245{
14246 rettv->v_type = VAR_STRING;
14247 rettv->vval.v_string = json_encode(&argvars[0]);
14248}
14249
14250/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014251 * "keys()" function
14252 */
14253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014254f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255{
14256 dict_list(argvars, rettv, 0);
14257}
14258
14259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 * "last_buffer_nr()" function.
14261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264{
14265 int n = 0;
14266 buf_T *buf;
14267
14268 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14269 if (n < buf->b_fnum)
14270 n = buf->b_fnum;
14271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014273}
14274
14275/*
14276 * "len()" function
14277 */
14278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014280{
14281 switch (argvars[0].v_type)
14282 {
14283 case VAR_STRING:
14284 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_number = (varnumber_T)STRLEN(
14286 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 break;
14288 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014289 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014291 case VAR_DICT:
14292 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14293 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014294 case VAR_UNKNOWN:
14295 case VAR_SPECIAL:
14296 case VAR_FLOAT:
14297 case VAR_FUNC:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014298 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014299 break;
14300 }
14301}
14302
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014303static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014304
14305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014307{
14308#ifdef FEAT_LIBCALL
14309 char_u *string_in;
14310 char_u **string_result;
14311 int nr_result;
14312#endif
14313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014315 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014317
14318 if (check_restricted() || check_secure())
14319 return;
14320
14321#ifdef FEAT_LIBCALL
14322 /* The first two args must be strings, otherwise its meaningless */
14323 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14324 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014325 string_in = NULL;
14326 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014327 string_in = argvars[2].vval.v_string;
14328 if (type == VAR_NUMBER)
14329 string_result = NULL;
14330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014332 if (mch_libcall(argvars[0].vval.v_string,
14333 argvars[1].vval.v_string,
14334 string_in,
14335 argvars[2].vval.v_number,
14336 string_result,
14337 &nr_result) == OK
14338 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014340 }
14341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342}
14343
14344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345 * "libcall()" function
14346 */
14347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014348f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349{
14350 libcall_common(argvars, rettv, VAR_STRING);
14351}
14352
14353/*
14354 * "libcallnr()" function
14355 */
14356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014357f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358{
14359 libcall_common(argvars, rettv, VAR_NUMBER);
14360}
14361
14362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 * "line(string)" function
14364 */
14365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014366f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367{
14368 linenr_T lnum = 0;
14369 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014370 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014372 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 if (fp != NULL)
14374 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376}
14377
14378/*
14379 * "line2byte(lnum)" function
14380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014382f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
14384#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386#else
14387 linenr_T lnum;
14388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14394 if (rettv->vval.v_number >= 0)
14395 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396#endif
14397}
14398
14399/*
14400 * "lispindent(lnum)" function
14401 */
14402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014403f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404{
14405#ifdef FEAT_LISP
14406 pos_T pos;
14407 linenr_T lnum;
14408
14409 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14412 {
14413 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 curwin->w_cursor = pos;
14416 }
14417 else
14418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420}
14421
14422/*
14423 * "localtime()" function
14424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429}
14430
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014431static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
14433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436 char_u *keys;
14437 char_u *which;
14438 char_u buf[NUMBUFLEN];
14439 char_u *keys_buf = NULL;
14440 char_u *rhs;
14441 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014442 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014443 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014444 mapblock_T *mp;
14445 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446
14447 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 rettv->v_type = VAR_STRING;
14449 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014451 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 if (*keys == NUL)
14453 return;
14454
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014455 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014459 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014460 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014461 if (argvars[3].v_type != VAR_UNKNOWN)
14462 get_dict = get_tv_number(&argvars[3]);
14463 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 else
14466 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467 if (which == NULL)
14468 return;
14469
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 mode = get_map_mode(&which, 0);
14471
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014472 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014473 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014475
14476 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014478 /* Return a string. */
14479 if (rhs != NULL)
14480 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481
Bram Moolenaarbd743252010-10-20 21:23:33 +020014482 }
14483 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14484 {
14485 /* Return a dictionary. */
14486 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14487 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14488 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
Bram Moolenaarbd743252010-10-20 21:23:33 +020014490 dict_add_nr_str(dict, "lhs", 0L, lhs);
14491 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14492 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14493 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14494 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14495 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14496 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014497 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014498 dict_add_nr_str(dict, "mode", 0L, mapmode);
14499
14500 vim_free(lhs);
14501 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 }
14503}
14504
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014505#ifdef FEAT_FLOAT
14506/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014507 * "log()" function
14508 */
14509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014510f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014511{
14512 float_T f;
14513
14514 rettv->v_type = VAR_FLOAT;
14515 if (get_float_arg(argvars, &f) == OK)
14516 rettv->vval.v_float = log(f);
14517 else
14518 rettv->vval.v_float = 0.0;
14519}
14520
14521/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014522 * "log10()" function
14523 */
14524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014525f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014526{
14527 float_T f;
14528
14529 rettv->v_type = VAR_FLOAT;
14530 if (get_float_arg(argvars, &f) == OK)
14531 rettv->vval.v_float = log10(f);
14532 else
14533 rettv->vval.v_float = 0.0;
14534}
14535#endif
14536
Bram Moolenaar1dced572012-04-05 16:54:08 +020014537#ifdef FEAT_LUA
14538/*
14539 * "luaeval()" function
14540 */
14541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014542f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014543{
14544 char_u *str;
14545 char_u buf[NUMBUFLEN];
14546
14547 str = get_tv_string_buf(&argvars[0], buf);
14548 do_luaeval(str, argvars + 1, rettv);
14549}
14550#endif
14551
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014553 * "map()" function
14554 */
14555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014556f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014557{
14558 filter_map(argvars, rettv, TRUE);
14559}
14560
14561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014562 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 */
14564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568}
14569
14570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 */
14573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014574f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014579static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
14581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014582find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014584 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014585 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014586 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 char_u *pat;
14588 regmatch_T regmatch;
14589 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 char_u *save_cpo;
14592 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014593 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014594 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014595 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 list_T *l = NULL;
14597 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014598 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014599 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600
14601 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14602 save_cpo = p_cpo;
14603 p_cpo = (char_u *)"";
14604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014605 rettv->vval.v_number = -1;
14606 if (type == 3)
14607 {
14608 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014609 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014610 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014611 }
14612 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 rettv->v_type = VAR_STRING;
14615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014618 if (argvars[0].v_type == VAR_LIST)
14619 {
14620 if ((l = argvars[0].vval.v_list) == NULL)
14621 goto theend;
14622 li = l->lv_first;
14623 }
14624 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014625 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014626 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014627 len = (long)STRLEN(str);
14628 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14631 if (pat == NULL)
14632 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014633
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 int error = FALSE;
14637
14638 start = get_tv_number_chk(&argvars[2], &error);
14639 if (error)
14640 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014641 if (l != NULL)
14642 {
14643 li = list_find(l, start);
14644 if (li == NULL)
14645 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014646 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014647 }
14648 else
14649 {
14650 if (start < 0)
14651 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014652 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014653 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014654 /* When "count" argument is there ignore matches before "start",
14655 * otherwise skip part of the string. Differs when pattern is "^"
14656 * or "\<". */
14657 if (argvars[3].v_type != VAR_UNKNOWN)
14658 startcol = start;
14659 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014660 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014661 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014662 len -= start;
14663 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014664 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014665
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014666 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 nth = get_tv_number_chk(&argvars[3], &error);
14668 if (error)
14669 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 }
14671
14672 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14673 if (regmatch.regprog != NULL)
14674 {
14675 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014677 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014678 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014679 if (l != NULL)
14680 {
14681 if (li == NULL)
14682 {
14683 match = FALSE;
14684 break;
14685 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014686 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014687 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014688 if (str == NULL)
14689 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014690 }
14691
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014692 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014693
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014694 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014695 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014696 if (l == NULL && !match)
14697 break;
14698
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014699 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014700 if (l != NULL)
14701 {
14702 li = li->li_next;
14703 ++idx;
14704 }
14705 else
14706 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014707#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014708 startcol = (colnr_T)(regmatch.startp[0]
14709 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014710#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014711 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014712#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014713 if (startcol > (colnr_T)len
14714 || str + startcol <= regmatch.startp[0])
14715 {
14716 match = FALSE;
14717 break;
14718 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014720 }
14721
14722 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014725 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 int i;
14727
14728 /* return list with matched string and submatches */
14729 for (i = 0; i < NSUBEXP; ++i)
14730 {
14731 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014732 {
14733 if (list_append_string(rettv->vval.v_list,
14734 (char_u *)"", 0) == FAIL)
14735 break;
14736 }
14737 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014738 regmatch.startp[i],
14739 (int)(regmatch.endp[i] - regmatch.startp[i]))
14740 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014742 }
14743 }
14744 else if (type == 2)
14745 {
14746 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014747 if (l != NULL)
14748 copy_tv(&li->li_tv, rettv);
14749 else
14750 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014752 }
14753 else if (l != NULL)
14754 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 else
14756 {
14757 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 (varnumber_T)(regmatch.startp[0] - str);
14760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014763 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 }
14765 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014766 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 }
14768
14769theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 p_cpo = save_cpo;
14772}
14773
14774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775 * "match()" function
14776 */
14777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014778f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014779{
14780 find_some_match(argvars, rettv, 1);
14781}
14782
14783/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014784 * "matchadd()" function
14785 */
14786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014787f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014788{
14789#ifdef FEAT_SEARCH_EXTRA
14790 char_u buf[NUMBUFLEN];
14791 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14792 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14793 int prio = 10; /* default priority */
14794 int id = -1;
14795 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014796 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014797
14798 rettv->vval.v_number = -1;
14799
14800 if (grp == NULL || pat == NULL)
14801 return;
14802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014803 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014804 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014805 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014806 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014807 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014808 if (argvars[4].v_type != VAR_UNKNOWN)
14809 {
14810 if (argvars[4].v_type != VAR_DICT)
14811 {
14812 EMSG(_(e_dictreq));
14813 return;
14814 }
14815 if (dict_find(argvars[4].vval.v_dict,
14816 (char_u *)"conceal", -1) != NULL)
14817 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14818 (char_u *)"conceal", FALSE);
14819 }
14820 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014821 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014822 if (error == TRUE)
14823 return;
14824 if (id >= 1 && id <= 3)
14825 {
14826 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14827 return;
14828 }
14829
Bram Moolenaar6561d522015-07-21 15:48:27 +020014830 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14831 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014832#endif
14833}
14834
14835/*
14836 * "matchaddpos()" function
14837 */
14838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014839f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014840{
14841#ifdef FEAT_SEARCH_EXTRA
14842 char_u buf[NUMBUFLEN];
14843 char_u *group;
14844 int prio = 10;
14845 int id = -1;
14846 int error = FALSE;
14847 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014848 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014849
14850 rettv->vval.v_number = -1;
14851
14852 group = get_tv_string_buf_chk(&argvars[0], buf);
14853 if (group == NULL)
14854 return;
14855
14856 if (argvars[1].v_type != VAR_LIST)
14857 {
14858 EMSG2(_(e_listarg), "matchaddpos()");
14859 return;
14860 }
14861 l = argvars[1].vval.v_list;
14862 if (l == NULL)
14863 return;
14864
14865 if (argvars[2].v_type != VAR_UNKNOWN)
14866 {
14867 prio = get_tv_number_chk(&argvars[2], &error);
14868 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014869 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014870 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014871 if (argvars[4].v_type != VAR_UNKNOWN)
14872 {
14873 if (argvars[4].v_type != VAR_DICT)
14874 {
14875 EMSG(_(e_dictreq));
14876 return;
14877 }
14878 if (dict_find(argvars[4].vval.v_dict,
14879 (char_u *)"conceal", -1) != NULL)
14880 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14881 (char_u *)"conceal", FALSE);
14882 }
14883 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014884 }
14885 if (error == TRUE)
14886 return;
14887
14888 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14889 if (id == 1 || id == 2)
14890 {
14891 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14892 return;
14893 }
14894
Bram Moolenaar6561d522015-07-21 15:48:27 +020014895 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14896 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014897#endif
14898}
14899
14900/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014901 * "matcharg()" function
14902 */
14903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014904f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014905{
14906 if (rettv_list_alloc(rettv) == OK)
14907 {
14908#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014909 int id = get_tv_number(&argvars[0]);
14910 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014911
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014912 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014913 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014914 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14915 {
14916 list_append_string(rettv->vval.v_list,
14917 syn_id2name(m->hlg_id), -1);
14918 list_append_string(rettv->vval.v_list, m->pattern, -1);
14919 }
14920 else
14921 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014922 list_append_string(rettv->vval.v_list, NULL, -1);
14923 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014924 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014925 }
14926#endif
14927 }
14928}
14929
14930/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014931 * "matchdelete()" function
14932 */
14933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014934f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014935{
14936#ifdef FEAT_SEARCH_EXTRA
14937 rettv->vval.v_number = match_delete(curwin,
14938 (int)get_tv_number(&argvars[0]), TRUE);
14939#endif
14940}
14941
14942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943 * "matchend()" function
14944 */
14945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014946f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947{
14948 find_some_match(argvars, rettv, 0);
14949}
14950
14951/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014952 * "matchlist()" function
14953 */
14954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014955f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014956{
14957 find_some_match(argvars, rettv, 3);
14958}
14959
14960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 * "matchstr()" function
14962 */
14963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014964f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965{
14966 find_some_match(argvars, rettv, 2);
14967}
14968
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014969static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014970
14971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014972max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014973{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014974 long n = 0;
14975 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014976 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014977
14978 if (argvars[0].v_type == VAR_LIST)
14979 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 list_T *l;
14981 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014982
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014983 l = argvars[0].vval.v_list;
14984 if (l != NULL)
14985 {
14986 li = l->lv_first;
14987 if (li != NULL)
14988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014989 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014990 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014991 {
14992 li = li->li_next;
14993 if (li == NULL)
14994 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014995 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014996 if (domax ? i > n : i < n)
14997 n = i;
14998 }
14999 }
15000 }
15001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015002 else if (argvars[0].v_type == VAR_DICT)
15003 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015004 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015005 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015006 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015007 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015008
15009 d = argvars[0].vval.v_dict;
15010 if (d != NULL)
15011 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015012 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015017 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015018 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015019 if (first)
15020 {
15021 n = i;
15022 first = FALSE;
15023 }
15024 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015025 n = i;
15026 }
15027 }
15028 }
15029 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015030 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015031 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015032 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015033}
15034
15035/*
15036 * "max()" function
15037 */
15038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015039f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015040{
15041 max_min(argvars, rettv, TRUE);
15042}
15043
15044/*
15045 * "min()" function
15046 */
15047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015048f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015049{
15050 max_min(argvars, rettv, FALSE);
15051}
15052
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015053static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015054
15055/*
15056 * Create the directory in which "dir" is located, and higher levels when
15057 * needed.
15058 */
15059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015061{
15062 char_u *p;
15063 char_u *updir;
15064 int r = FAIL;
15065
15066 /* Get end of directory name in "dir".
15067 * We're done when it's "/" or "c:/". */
15068 p = gettail_sep(dir);
15069 if (p <= get_past_head(dir))
15070 return OK;
15071
15072 /* If the directory exists we're done. Otherwise: create it.*/
15073 updir = vim_strnsave(dir, (int)(p - dir));
15074 if (updir == NULL)
15075 return FAIL;
15076 if (mch_isdir(updir))
15077 r = OK;
15078 else if (mkdir_recurse(updir, prot) == OK)
15079 r = vim_mkdir_emsg(updir, prot);
15080 vim_free(updir);
15081 return r;
15082}
15083
15084#ifdef vim_mkdir
15085/*
15086 * "mkdir()" function
15087 */
15088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015089f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015090{
15091 char_u *dir;
15092 char_u buf[NUMBUFLEN];
15093 int prot = 0755;
15094
15095 rettv->vval.v_number = FAIL;
15096 if (check_restricted() || check_secure())
15097 return;
15098
15099 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015100 if (*dir == NUL)
15101 rettv->vval.v_number = FAIL;
15102 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015103 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015104 if (*gettail(dir) == NUL)
15105 /* remove trailing slashes */
15106 *gettail_sep(dir) = NUL;
15107
15108 if (argvars[1].v_type != VAR_UNKNOWN)
15109 {
15110 if (argvars[2].v_type != VAR_UNKNOWN)
15111 prot = get_tv_number_chk(&argvars[2], NULL);
15112 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15113 mkdir_recurse(dir, prot);
15114 }
15115 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015116 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015117}
15118#endif
15119
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 * "mode()" function
15122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015126 char_u buf[3];
15127
15128 buf[1] = NUL;
15129 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 if (VIsual_active)
15132 {
15133 if (VIsual_select)
15134 buf[0] = VIsual_mode + 's' - 'v';
15135 else
15136 buf[0] = VIsual_mode;
15137 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015138 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015139 || State == CONFIRM)
15140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015142 if (State == ASKMORE)
15143 buf[1] = 'm';
15144 else if (State == CONFIRM)
15145 buf[1] = '?';
15146 }
15147 else if (State == EXTERNCMD)
15148 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 else if (State & INSERT)
15150 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015151#ifdef FEAT_VREPLACE
15152 if (State & VREPLACE_FLAG)
15153 {
15154 buf[0] = 'R';
15155 buf[1] = 'v';
15156 }
15157 else
15158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 if (State & REPLACE_FLAG)
15160 buf[0] = 'R';
15161 else
15162 buf[0] = 'i';
15163 }
15164 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015167 if (exmode_active)
15168 buf[1] = 'v';
15169 }
15170 else if (exmode_active)
15171 {
15172 buf[0] = 'c';
15173 buf[1] = 'e';
15174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015178 if (finish_op)
15179 buf[1] = 'o';
15180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015182 /* Clear out the minor mode when the argument is not a non-zero number or
15183 * non-empty string. */
15184 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185 buf[1] = NUL;
15186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 rettv->vval.v_string = vim_strsave(buf);
15188 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189}
15190
Bram Moolenaar429fa852013-04-15 12:27:36 +020015191#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015192/*
15193 * "mzeval()" function
15194 */
15195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015196f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015197{
15198 char_u *str;
15199 char_u buf[NUMBUFLEN];
15200
15201 str = get_tv_string_buf(&argvars[0], buf);
15202 do_mzeval(str, rettv);
15203}
Bram Moolenaar75676462013-01-30 14:55:42 +010015204
15205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015206mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015207{
15208 typval_T argvars[3];
15209
15210 argvars[0].v_type = VAR_STRING;
15211 argvars[0].vval.v_string = name;
15212 copy_tv(args, &argvars[1]);
15213 argvars[2].v_type = VAR_UNKNOWN;
15214 f_call(argvars, rettv);
15215 clear_tv(&argvars[1]);
15216}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015217#endif
15218
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220 * "nextnonblank()" function
15221 */
15222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015223f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224{
15225 linenr_T lnum;
15226
15227 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230 {
15231 lnum = 0;
15232 break;
15233 }
15234 if (*skipwhite(ml_get(lnum)) != NUL)
15235 break;
15236 }
15237 rettv->vval.v_number = lnum;
15238}
15239
15240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 * "nr2char()" function
15242 */
15243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015244f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245{
15246 char_u buf[NUMBUFLEN];
15247
15248#ifdef FEAT_MBYTE
15249 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015250 {
15251 int utf8 = 0;
15252
15253 if (argvars[1].v_type != VAR_UNKNOWN)
15254 utf8 = get_tv_number_chk(&argvars[1], NULL);
15255 if (utf8)
15256 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15257 else
15258 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 else
15261#endif
15262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 buf[1] = NUL;
15265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266 rettv->v_type = VAR_STRING;
15267 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015268}
15269
15270/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015271 * "or(expr, expr)" function
15272 */
15273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015274f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015275{
15276 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15277 | get_tv_number_chk(&argvars[1], NULL);
15278}
15279
15280/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015281 * "pathshorten()" function
15282 */
15283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015284f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015285{
15286 char_u *p;
15287
15288 rettv->v_type = VAR_STRING;
15289 p = get_tv_string_chk(&argvars[0]);
15290 if (p == NULL)
15291 rettv->vval.v_string = NULL;
15292 else
15293 {
15294 p = vim_strsave(p);
15295 rettv->vval.v_string = p;
15296 if (p != NULL)
15297 shorten_dir(p);
15298 }
15299}
15300
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015301#ifdef FEAT_PERL
15302/*
15303 * "perleval()" function
15304 */
15305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015306f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015307{
15308 char_u *str;
15309 char_u buf[NUMBUFLEN];
15310
15311 str = get_tv_string_buf(&argvars[0], buf);
15312 do_perleval(str, rettv);
15313}
15314#endif
15315
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015316#ifdef FEAT_FLOAT
15317/*
15318 * "pow()" function
15319 */
15320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015321f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015322{
15323 float_T fx, fy;
15324
15325 rettv->v_type = VAR_FLOAT;
15326 if (get_float_arg(argvars, &fx) == OK
15327 && get_float_arg(&argvars[1], &fy) == OK)
15328 rettv->vval.v_float = pow(fx, fy);
15329 else
15330 rettv->vval.v_float = 0.0;
15331}
15332#endif
15333
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015334/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 * "prevnonblank()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339{
15340 linenr_T lnum;
15341
15342 lnum = get_tv_lnum(argvars);
15343 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15344 lnum = 0;
15345 else
15346 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15347 --lnum;
15348 rettv->vval.v_number = lnum;
15349}
15350
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015351/* This dummy va_list is here because:
15352 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15353 * - locally in the function results in a "used before set" warning
15354 * - using va_start() to initialize it gives "function with fixed args" error */
15355static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015356
Bram Moolenaar8c711452005-01-14 21:53:12 +000015357/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015358 * "printf()" function
15359 */
15360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015361f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015362{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015363 char_u buf[NUMBUFLEN];
15364 int len;
15365 char_u *s;
15366 int saved_did_emsg = did_emsg;
15367 char *fmt;
15368
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015369 rettv->v_type = VAR_STRING;
15370 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015371
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015372 /* Get the required length, allocate the buffer and do it for real. */
15373 did_emsg = FALSE;
15374 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15375 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15376 if (!did_emsg)
15377 {
15378 s = alloc(len + 1);
15379 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015380 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015381 rettv->vval.v_string = s;
15382 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015383 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015384 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015385 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015386}
15387
15388/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015389 * "pumvisible()" function
15390 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015392f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015393{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015394#ifdef FEAT_INS_EXPAND
15395 if (pum_visible())
15396 rettv->vval.v_number = 1;
15397#endif
15398}
15399
Bram Moolenaardb913952012-06-29 12:54:53 +020015400#ifdef FEAT_PYTHON3
15401/*
15402 * "py3eval()" function
15403 */
15404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015405f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015406{
15407 char_u *str;
15408 char_u buf[NUMBUFLEN];
15409
15410 str = get_tv_string_buf(&argvars[0], buf);
15411 do_py3eval(str, rettv);
15412}
15413#endif
15414
15415#ifdef FEAT_PYTHON
15416/*
15417 * "pyeval()" function
15418 */
15419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015420f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015421{
15422 char_u *str;
15423 char_u buf[NUMBUFLEN];
15424
15425 str = get_tv_string_buf(&argvars[0], buf);
15426 do_pyeval(str, rettv);
15427}
15428#endif
15429
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015430/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015431 * "range()" function
15432 */
15433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015434f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015435{
15436 long start;
15437 long end;
15438 long stride = 1;
15439 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015440 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015442 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015443 if (argvars[1].v_type == VAR_UNKNOWN)
15444 {
15445 end = start - 1;
15446 start = 0;
15447 }
15448 else
15449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015450 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015452 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015453 }
15454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015455 if (error)
15456 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015457 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015458 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015459 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015460 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015461 else
15462 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015463 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015464 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015465 if (list_append_number(rettv->vval.v_list,
15466 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015467 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015468 }
15469}
15470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015471/*
15472 * "readfile()" function
15473 */
15474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015475f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015476{
15477 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015478 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015479 char_u *fname;
15480 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015481 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15482 int io_size = sizeof(buf);
15483 int readlen; /* size of last fread() */
15484 char_u *prev = NULL; /* previously read bytes, if any */
15485 long prevlen = 0; /* length of data in prev */
15486 long prevsize = 0; /* size of prev buffer */
15487 long maxline = MAXLNUM;
15488 long cnt = 0;
15489 char_u *p; /* position in buf */
15490 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015492 if (argvars[1].v_type != VAR_UNKNOWN)
15493 {
15494 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15495 binary = TRUE;
15496 if (argvars[2].v_type != VAR_UNKNOWN)
15497 maxline = get_tv_number(&argvars[2]);
15498 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015499
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015500 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015501 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015502
15503 /* Always open the file in binary mode, library functions have a mind of
15504 * their own about CR-LF conversion. */
15505 fname = get_tv_string(&argvars[0]);
15506 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15507 {
15508 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15509 return;
15510 }
15511
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015512 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015513 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015515
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015516 /* This for loop processes what was read, but is also entered at end
15517 * of file so that either:
15518 * - an incomplete line gets written
15519 * - a "binary" file gets an empty line at the end if it ends in a
15520 * newline. */
15521 for (p = buf, start = buf;
15522 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15523 ++p)
15524 {
15525 if (*p == '\n' || readlen <= 0)
15526 {
15527 listitem_T *li;
15528 char_u *s = NULL;
15529 long_u len = p - start;
15530
15531 /* Finished a line. Remove CRs before NL. */
15532 if (readlen > 0 && !binary)
15533 {
15534 while (len > 0 && start[len - 1] == '\r')
15535 --len;
15536 /* removal may cross back to the "prev" string */
15537 if (len == 0)
15538 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15539 --prevlen;
15540 }
15541 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015542 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 else
15544 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015545 /* Change "prev" buffer to be the right size. This way
15546 * the bytes are only copied once, and very long lines are
15547 * allocated only once. */
15548 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015550 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015552 prev = NULL; /* the list will own the string */
15553 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015554 }
15555 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015556 if (s == NULL)
15557 {
15558 do_outofmem_msg((long_u) prevlen + len + 1);
15559 failed = TRUE;
15560 break;
15561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015562
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015563 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015564 {
15565 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015566 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015567 break;
15568 }
15569 li->li_tv.v_type = VAR_STRING;
15570 li->li_tv.v_lock = 0;
15571 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015572 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015574 start = p + 1; /* step over newline */
15575 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576 break;
15577 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015578 else if (*p == NUL)
15579 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015580#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015581 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15582 * when finding the BF and check the previous two bytes. */
15583 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015584 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015585 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15586 * + 1, these may be in the "prev" string. */
15587 char_u back1 = p >= buf + 1 ? p[-1]
15588 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15589 char_u back2 = p >= buf + 2 ? p[-2]
15590 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15591 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015593 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015594 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015595 char_u *dest = p - 2;
15596
15597 /* Usually a BOM is at the beginning of a file, and so at
15598 * the beginning of a line; then we can just step over it.
15599 */
15600 if (start == dest)
15601 start = p + 1;
15602 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015603 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015604 /* have to shuffle buf to close gap */
15605 int adjust_prevlen = 0;
15606
15607 if (dest < buf)
15608 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015609 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015610 dest = buf;
15611 }
15612 if (readlen > p - buf + 1)
15613 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15614 readlen -= 3 - adjust_prevlen;
15615 prevlen -= adjust_prevlen;
15616 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015617 }
15618 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015619 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015620#endif
15621 } /* for */
15622
15623 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15624 break;
15625 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015626 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015627 /* There's part of a line in buf, store it in "prev". */
15628 if (p - start + prevlen >= prevsize)
15629 {
15630 /* need bigger "prev" buffer */
15631 char_u *newprev;
15632
15633 /* A common use case is ordinary text files and "prev" gets a
15634 * fragment of a line, so the first allocation is made
15635 * small, to avoid repeatedly 'allocing' large and
15636 * 'reallocing' small. */
15637 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015638 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015639 else
15640 {
15641 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015642 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015643 prevsize = grow50pc > growmin ? grow50pc : growmin;
15644 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015645 newprev = prev == NULL ? alloc(prevsize)
15646 : vim_realloc(prev, prevsize);
15647 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015648 {
15649 do_outofmem_msg((long_u)prevsize);
15650 failed = TRUE;
15651 break;
15652 }
15653 prev = newprev;
15654 }
15655 /* Add the line part to end of "prev". */
15656 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015657 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015658 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015659 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015660
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015661 /*
15662 * For a negative line count use only the lines at the end of the file,
15663 * free the rest.
15664 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015665 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015666 while (cnt > -maxline)
15667 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015668 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015669 --cnt;
15670 }
15671
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 if (failed)
15673 {
15674 list_free(rettv->vval.v_list, TRUE);
15675 /* readfile doc says an empty list is returned on error */
15676 rettv->vval.v_list = list_alloc();
15677 }
15678
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015679 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015680 fclose(fd);
15681}
15682
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015683#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015684static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015685
15686/*
15687 * Convert a List to proftime_T.
15688 * Return FAIL when there is something wrong.
15689 */
15690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015691list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015692{
15693 long n1, n2;
15694 int error = FALSE;
15695
15696 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15697 || arg->vval.v_list->lv_len != 2)
15698 return FAIL;
15699 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15700 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15701# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015702 tm->HighPart = n1;
15703 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015704# else
15705 tm->tv_sec = n1;
15706 tm->tv_usec = n2;
15707# endif
15708 return error ? FAIL : OK;
15709}
15710#endif /* FEAT_RELTIME */
15711
15712/*
15713 * "reltime()" function
15714 */
15715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015716f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015717{
15718#ifdef FEAT_RELTIME
15719 proftime_T res;
15720 proftime_T start;
15721
15722 if (argvars[0].v_type == VAR_UNKNOWN)
15723 {
15724 /* No arguments: get current time. */
15725 profile_start(&res);
15726 }
15727 else if (argvars[1].v_type == VAR_UNKNOWN)
15728 {
15729 if (list2proftime(&argvars[0], &res) == FAIL)
15730 return;
15731 profile_end(&res);
15732 }
15733 else
15734 {
15735 /* Two arguments: compute the difference. */
15736 if (list2proftime(&argvars[0], &start) == FAIL
15737 || list2proftime(&argvars[1], &res) == FAIL)
15738 return;
15739 profile_sub(&res, &start);
15740 }
15741
15742 if (rettv_list_alloc(rettv) == OK)
15743 {
15744 long n1, n2;
15745
15746# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015747 n1 = res.HighPart;
15748 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015749# else
15750 n1 = res.tv_sec;
15751 n2 = res.tv_usec;
15752# endif
15753 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15754 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15755 }
15756#endif
15757}
15758
15759/*
15760 * "reltimestr()" function
15761 */
15762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015763f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015764{
15765#ifdef FEAT_RELTIME
15766 proftime_T tm;
15767#endif
15768
15769 rettv->v_type = VAR_STRING;
15770 rettv->vval.v_string = NULL;
15771#ifdef FEAT_RELTIME
15772 if (list2proftime(&argvars[0], &tm) == OK)
15773 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15774#endif
15775}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015776
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015778static void make_connection(void);
15779static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780
15781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015782make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783{
15784 if (X_DISPLAY == NULL
15785# ifdef FEAT_GUI
15786 && !gui.in_use
15787# endif
15788 )
15789 {
15790 x_force_connect = TRUE;
15791 setup_term_clip();
15792 x_force_connect = FALSE;
15793 }
15794}
15795
15796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015797check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798{
15799 make_connection();
15800 if (X_DISPLAY == NULL)
15801 {
15802 EMSG(_("E240: No connection to Vim server"));
15803 return FAIL;
15804 }
15805 return OK;
15806}
15807#endif
15808
15809#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015810static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814{
15815 char_u *server_name;
15816 char_u *keys;
15817 char_u *r = NULL;
15818 char_u buf[NUMBUFLEN];
15819# ifdef WIN32
15820 HWND w;
15821# else
15822 Window w;
15823# endif
15824
15825 if (check_restricted() || check_secure())
15826 return;
15827
15828# ifdef FEAT_X11
15829 if (check_connection() == FAIL)
15830 return;
15831# endif
15832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015833 server_name = get_tv_string_chk(&argvars[0]);
15834 if (server_name == NULL)
15835 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836 keys = get_tv_string_buf(&argvars[1], buf);
15837# ifdef WIN32
15838 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15839# else
15840 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15841 < 0)
15842# endif
15843 {
15844 if (r != NULL)
15845 EMSG(r); /* sending worked but evaluation failed */
15846 else
15847 EMSG2(_("E241: Unable to send to %s"), server_name);
15848 return;
15849 }
15850
15851 rettv->vval.v_string = r;
15852
15853 if (argvars[2].v_type != VAR_UNKNOWN)
15854 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015855 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015856 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015857 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015858
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015859 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015860 v.di_tv.v_type = VAR_STRING;
15861 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 idvar = get_tv_string_chk(&argvars[2]);
15863 if (idvar != NULL)
15864 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866 }
15867}
15868#endif
15869
15870/*
15871 * "remote_expr()" function
15872 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015874f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015875{
15876 rettv->v_type = VAR_STRING;
15877 rettv->vval.v_string = NULL;
15878#ifdef FEAT_CLIENTSERVER
15879 remote_common(argvars, rettv, TRUE);
15880#endif
15881}
15882
15883/*
15884 * "remote_foreground()" function
15885 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015887f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889#ifdef FEAT_CLIENTSERVER
15890# ifdef WIN32
15891 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 {
15893 char_u *server_name = get_tv_string_chk(&argvars[0]);
15894
15895 if (server_name != NULL)
15896 serverForeground(server_name);
15897 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015898# else
15899 /* Send a foreground() expression to the server. */
15900 argvars[1].v_type = VAR_STRING;
15901 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15902 argvars[2].v_type = VAR_UNKNOWN;
15903 remote_common(argvars, rettv, TRUE);
15904 vim_free(argvars[1].vval.v_string);
15905# endif
15906#endif
15907}
15908
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015910f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911{
15912#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015913 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914 char_u *s = NULL;
15915# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015916 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015918 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919
15920 if (check_restricted() || check_secure())
15921 {
15922 rettv->vval.v_number = -1;
15923 return;
15924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 serverid = get_tv_string_chk(&argvars[0]);
15926 if (serverid == NULL)
15927 {
15928 rettv->vval.v_number = -1;
15929 return; /* type error; errmsg already given */
15930 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015932 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 if (n == 0)
15934 rettv->vval.v_number = -1;
15935 else
15936 {
15937 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15938 rettv->vval.v_number = (s != NULL);
15939 }
15940# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941 if (check_connection() == FAIL)
15942 return;
15943
15944 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946# endif
15947
15948 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 char_u *retvar;
15951
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 v.di_tv.v_type = VAR_STRING;
15953 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 retvar = get_tv_string_chk(&argvars[1]);
15955 if (retvar != NULL)
15956 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015958 }
15959#else
15960 rettv->vval.v_number = -1;
15961#endif
15962}
15963
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015965f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015966{
15967 char_u *r = NULL;
15968
15969#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 char_u *serverid = get_tv_string_chk(&argvars[0]);
15971
15972 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015973 {
15974# ifdef WIN32
15975 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015976 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015977
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015978 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979 if (n != 0)
15980 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15981 if (r == NULL)
15982# else
15983 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015984 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015985# endif
15986 EMSG(_("E277: Unable to read a server reply"));
15987 }
15988#endif
15989 rettv->v_type = VAR_STRING;
15990 rettv->vval.v_string = r;
15991}
15992
15993/*
15994 * "remote_send()" function
15995 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015997f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015998{
15999 rettv->v_type = VAR_STRING;
16000 rettv->vval.v_string = NULL;
16001#ifdef FEAT_CLIENTSERVER
16002 remote_common(argvars, rettv, FALSE);
16003#endif
16004}
16005
16006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016007 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008 */
16009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016010f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016011{
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 list_T *l;
16013 listitem_T *item, *item2;
16014 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016015 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016016 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016017 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 dict_T *d;
16019 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016020 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016021
Bram Moolenaar8c711452005-01-14 21:53:12 +000016022 if (argvars[0].v_type == VAR_DICT)
16023 {
16024 if (argvars[2].v_type != VAR_UNKNOWN)
16025 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016026 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016027 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 key = get_tv_string_chk(&argvars[1]);
16030 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 di = dict_find(d, key, -1);
16033 if (di == NULL)
16034 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016035 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16036 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016037 {
16038 *rettv = di->di_tv;
16039 init_tv(&di->di_tv);
16040 dictitem_remove(d, di);
16041 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016043 }
16044 }
16045 else if (argvars[0].v_type != VAR_LIST)
16046 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016047 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016048 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 int error = FALSE;
16051
16052 idx = get_tv_number_chk(&argvars[1], &error);
16053 if (error)
16054 ; /* type error: do nothing, errmsg already given */
16055 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016056 EMSGN(_(e_listidx), idx);
16057 else
16058 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016059 if (argvars[2].v_type == VAR_UNKNOWN)
16060 {
16061 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016062 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016063 *rettv = item->li_tv;
16064 vim_free(item);
16065 }
16066 else
16067 {
16068 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 end = get_tv_number_chk(&argvars[2], &error);
16070 if (error)
16071 ; /* type error: do nothing */
16072 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016073 EMSGN(_(e_listidx), end);
16074 else
16075 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016076 int cnt = 0;
16077
16078 for (li = item; li != NULL; li = li->li_next)
16079 {
16080 ++cnt;
16081 if (li == item2)
16082 break;
16083 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016084 if (li == NULL) /* didn't find "item2" after "item" */
16085 EMSG(_(e_invrange));
16086 else
16087 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016088 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016089 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016090 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016091 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016092 l->lv_first = item;
16093 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016094 item->li_prev = NULL;
16095 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016096 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016097 }
16098 }
16099 }
16100 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 }
16102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103}
16104
16105/*
16106 * "rename({from}, {to})" function
16107 */
16108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016109f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110{
16111 char_u buf[NUMBUFLEN];
16112
16113 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16117 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118}
16119
16120/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016121 * "repeat()" function
16122 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016124f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016125{
16126 char_u *p;
16127 int n;
16128 int slen;
16129 int len;
16130 char_u *r;
16131 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016132
16133 n = get_tv_number(&argvars[1]);
16134 if (argvars[0].v_type == VAR_LIST)
16135 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016136 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016137 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 if (list_extend(rettv->vval.v_list,
16139 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016140 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016141 }
16142 else
16143 {
16144 p = get_tv_string(&argvars[0]);
16145 rettv->v_type = VAR_STRING;
16146 rettv->vval.v_string = NULL;
16147
16148 slen = (int)STRLEN(p);
16149 len = slen * n;
16150 if (len <= 0)
16151 return;
16152
16153 r = alloc(len + 1);
16154 if (r != NULL)
16155 {
16156 for (i = 0; i < n; i++)
16157 mch_memmove(r + i * slen, p, (size_t)slen);
16158 r[len] = NUL;
16159 }
16160
16161 rettv->vval.v_string = r;
16162 }
16163}
16164
16165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 * "resolve()" function
16167 */
16168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016169f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
16171 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016172#ifdef HAVE_READLINK
16173 char_u *buf = NULL;
16174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177#ifdef FEAT_SHORTCUT
16178 {
16179 char_u *v = NULL;
16180
16181 v = mch_resolve_shortcut(p);
16182 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 }
16187#else
16188# ifdef HAVE_READLINK
16189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 char_u *cpy;
16191 int len;
16192 char_u *remain = NULL;
16193 char_u *q;
16194 int is_relative_to_current = FALSE;
16195 int has_trailing_pathsep = FALSE;
16196 int limit = 100;
16197
16198 p = vim_strsave(p);
16199
16200 if (p[0] == '.' && (vim_ispathsep(p[1])
16201 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16202 is_relative_to_current = TRUE;
16203
16204 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016205 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016208 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210
16211 q = getnextcomp(p);
16212 if (*q != NUL)
16213 {
16214 /* Separate the first path component in "p", and keep the
16215 * remainder (beginning with the path separator). */
16216 remain = vim_strsave(q - 1);
16217 q[-1] = NUL;
16218 }
16219
Bram Moolenaard9462e32011-04-11 21:35:11 +020016220 buf = alloc(MAXPATHL + 1);
16221 if (buf == NULL)
16222 goto fail;
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 for (;;)
16225 {
16226 for (;;)
16227 {
16228 len = readlink((char *)p, (char *)buf, MAXPATHL);
16229 if (len <= 0)
16230 break;
16231 buf[len] = NUL;
16232
16233 if (limit-- == 0)
16234 {
16235 vim_free(p);
16236 vim_free(remain);
16237 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 goto fail;
16240 }
16241
16242 /* Ensure that the result will have a trailing path separator
16243 * if the argument has one. */
16244 if (remain == NULL && has_trailing_pathsep)
16245 add_pathsep(buf);
16246
16247 /* Separate the first path component in the link value and
16248 * concatenate the remainders. */
16249 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16250 if (*q != NUL)
16251 {
16252 if (remain == NULL)
16253 remain = vim_strsave(q - 1);
16254 else
16255 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016256 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 if (cpy != NULL)
16258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 vim_free(remain);
16260 remain = cpy;
16261 }
16262 }
16263 q[-1] = NUL;
16264 }
16265
16266 q = gettail(p);
16267 if (q > p && *q == NUL)
16268 {
16269 /* Ignore trailing path separator. */
16270 q[-1] = NUL;
16271 q = gettail(p);
16272 }
16273 if (q > p && !mch_isFullName(buf))
16274 {
16275 /* symlink is relative to directory of argument */
16276 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16277 if (cpy != NULL)
16278 {
16279 STRCPY(cpy, p);
16280 STRCPY(gettail(cpy), buf);
16281 vim_free(p);
16282 p = cpy;
16283 }
16284 }
16285 else
16286 {
16287 vim_free(p);
16288 p = vim_strsave(buf);
16289 }
16290 }
16291
16292 if (remain == NULL)
16293 break;
16294
16295 /* Append the first path component of "remain" to "p". */
16296 q = getnextcomp(remain + 1);
16297 len = q - remain - (*q != NUL);
16298 cpy = vim_strnsave(p, STRLEN(p) + len);
16299 if (cpy != NULL)
16300 {
16301 STRNCAT(cpy, remain, len);
16302 vim_free(p);
16303 p = cpy;
16304 }
16305 /* Shorten "remain". */
16306 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016307 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 else
16309 {
16310 vim_free(remain);
16311 remain = NULL;
16312 }
16313 }
16314
16315 /* If the result is a relative path name, make it explicitly relative to
16316 * the current directory if and only if the argument had this form. */
16317 if (!vim_ispathsep(*p))
16318 {
16319 if (is_relative_to_current
16320 && *p != NUL
16321 && !(p[0] == '.'
16322 && (p[1] == NUL
16323 || vim_ispathsep(p[1])
16324 || (p[1] == '.'
16325 && (p[2] == NUL
16326 || vim_ispathsep(p[2]))))))
16327 {
16328 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016329 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 if (cpy != NULL)
16331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 vim_free(p);
16333 p = cpy;
16334 }
16335 }
16336 else if (!is_relative_to_current)
16337 {
16338 /* Strip leading "./". */
16339 q = p;
16340 while (q[0] == '.' && vim_ispathsep(q[1]))
16341 q += 2;
16342 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016343 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 }
16345 }
16346
16347 /* Ensure that the result will have no trailing path separator
16348 * if the argument had none. But keep "/" or "//". */
16349 if (!has_trailing_pathsep)
16350 {
16351 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016352 if (after_pathsep(p, q))
16353 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 }
16355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 }
16358# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360# endif
16361#endif
16362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016363 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364
16365#ifdef HAVE_READLINK
16366fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016367 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370}
16371
16372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 */
16375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016376f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377{
Bram Moolenaar33570922005-01-25 22:26:29 +000016378 list_T *l;
16379 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 if (argvars[0].v_type != VAR_LIST)
16382 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016383 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016384 && !tv_check_lock(l->lv_lock,
16385 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 {
16387 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016388 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016389 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 while (li != NULL)
16391 {
16392 ni = li->li_prev;
16393 list_append(l, li);
16394 li = ni;
16395 }
16396 rettv->vval.v_list = l;
16397 rettv->v_type = VAR_LIST;
16398 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016399 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401}
16402
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016403#define SP_NOMOVE 0x01 /* don't move cursor */
16404#define SP_REPEAT 0x02 /* repeat to find outer pair */
16405#define SP_RETCOUNT 0x04 /* return matchcount */
16406#define SP_SETPCMARK 0x08 /* set previous context mark */
16407#define SP_START 0x10 /* accept match at start position */
16408#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16409#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016410#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016411
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016412static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016413
16414/*
16415 * Get flags for a search function.
16416 * Possibly sets "p_ws".
16417 * Returns BACKWARD, FORWARD or zero (for an error).
16418 */
16419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421{
16422 int dir = FORWARD;
16423 char_u *flags;
16424 char_u nbuf[NUMBUFLEN];
16425 int mask;
16426
16427 if (varp->v_type != VAR_UNKNOWN)
16428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 flags = get_tv_string_buf_chk(varp, nbuf);
16430 if (flags == NULL)
16431 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432 while (*flags != NUL)
16433 {
16434 switch (*flags)
16435 {
16436 case 'b': dir = BACKWARD; break;
16437 case 'w': p_ws = TRUE; break;
16438 case 'W': p_ws = FALSE; break;
16439 default: mask = 0;
16440 if (flagsp != NULL)
16441 switch (*flags)
16442 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016443 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016444 case 'e': mask = SP_END; break;
16445 case 'm': mask = SP_RETCOUNT; break;
16446 case 'n': mask = SP_NOMOVE; break;
16447 case 'p': mask = SP_SUBPAT; break;
16448 case 'r': mask = SP_REPEAT; break;
16449 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016450 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451 }
16452 if (mask == 0)
16453 {
16454 EMSG2(_(e_invarg2), flags);
16455 dir = 0;
16456 }
16457 else
16458 *flagsp |= mask;
16459 }
16460 if (dir == 0)
16461 break;
16462 ++flags;
16463 }
16464 }
16465 return dir;
16466}
16467
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016469 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016472search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016474 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 char_u *pat;
16476 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016477 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 int save_p_ws = p_ws;
16479 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016480 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016482 proftime_T tm;
16483#ifdef FEAT_RELTIME
16484 long time_limit = 0;
16485#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016486 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016487 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016490 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016491 if (dir == 0)
16492 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016493 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016494 if (flags & SP_START)
16495 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016496 if (flags & SP_END)
16497 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016498 if (flags & SP_COLUMN)
16499 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016500
Bram Moolenaar76929292008-01-06 19:07:36 +000016501 /* Optional arguments: line number to stop searching and timeout. */
16502 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016503 {
16504 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16505 if (lnum_stop < 0)
16506 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016507#ifdef FEAT_RELTIME
16508 if (argvars[3].v_type != VAR_UNKNOWN)
16509 {
16510 time_limit = get_tv_number_chk(&argvars[3], NULL);
16511 if (time_limit < 0)
16512 goto theend;
16513 }
16514#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016515 }
16516
Bram Moolenaar76929292008-01-06 19:07:36 +000016517#ifdef FEAT_RELTIME
16518 /* Set the time limit, if there is one. */
16519 profile_setlimit(time_limit, &tm);
16520#endif
16521
Bram Moolenaar231334e2005-07-25 20:46:57 +000016522 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016523 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016524 * Check to make sure only those flags are set.
16525 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16526 * flags cannot be set. Check for that condition also.
16527 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016528 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016529 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016532 goto theend;
16533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016535 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016536 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016537 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016538 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016540 if (flags & SP_SUBPAT)
16541 retval = subpatnum;
16542 else
16543 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016544 if (flags & SP_SETPCMARK)
16545 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016547 if (match_pos != NULL)
16548 {
16549 /* Store the match cursor position */
16550 match_pos->lnum = pos.lnum;
16551 match_pos->col = pos.col + 1;
16552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 /* "/$" will put the cursor after the end of the line, may need to
16554 * correct that here */
16555 check_cursor();
16556 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016557
16558 /* If 'n' flag is used: restore cursor position. */
16559 if (flags & SP_NOMOVE)
16560 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016561 else
16562 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016563theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016565
16566 return retval;
16567}
16568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016569#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016570
16571/*
16572 * round() is not in C90, use ceil() or floor() instead.
16573 */
16574 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016575vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016576{
16577 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16578}
16579
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016580/*
16581 * "round({float})" function
16582 */
16583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016584f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016585{
16586 float_T f;
16587
16588 rettv->v_type = VAR_FLOAT;
16589 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016590 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016591 else
16592 rettv->vval.v_float = 0.0;
16593}
16594#endif
16595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016596/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016597 * "screenattr()" function
16598 */
16599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016600f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016601{
16602 int row;
16603 int col;
16604 int c;
16605
16606 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16607 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16608 if (row < 0 || row >= screen_Rows
16609 || col < 0 || col >= screen_Columns)
16610 c = -1;
16611 else
16612 c = ScreenAttrs[LineOffset[row] + col];
16613 rettv->vval.v_number = c;
16614}
16615
16616/*
16617 * "screenchar()" function
16618 */
16619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016620f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016621{
16622 int row;
16623 int col;
16624 int off;
16625 int c;
16626
16627 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16628 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16629 if (row < 0 || row >= screen_Rows
16630 || col < 0 || col >= screen_Columns)
16631 c = -1;
16632 else
16633 {
16634 off = LineOffset[row] + col;
16635#ifdef FEAT_MBYTE
16636 if (enc_utf8 && ScreenLinesUC[off] != 0)
16637 c = ScreenLinesUC[off];
16638 else
16639#endif
16640 c = ScreenLines[off];
16641 }
16642 rettv->vval.v_number = c;
16643}
16644
16645/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016646 * "screencol()" function
16647 *
16648 * First column is 1 to be consistent with virtcol().
16649 */
16650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016651f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016652{
16653 rettv->vval.v_number = screen_screencol() + 1;
16654}
16655
16656/*
16657 * "screenrow()" function
16658 */
16659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016660f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016661{
16662 rettv->vval.v_number = screen_screenrow() + 1;
16663}
16664
16665/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016666 * "search()" function
16667 */
16668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016669f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016670{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016671 int flags = 0;
16672
16673 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674}
16675
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016677 * "searchdecl()" function
16678 */
16679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016681{
16682 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016683 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016684 int error = FALSE;
16685 char_u *name;
16686
16687 rettv->vval.v_number = 1; /* default: FAIL */
16688
16689 name = get_tv_string_chk(&argvars[0]);
16690 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016691 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016692 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016693 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16694 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16695 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016696 if (!error && name != NULL)
16697 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016698 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016699}
16700
16701/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016702 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016705searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706{
16707 char_u *spat, *mpat, *epat;
16708 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 int dir;
16711 int flags = 0;
16712 char_u nbuf1[NUMBUFLEN];
16713 char_u nbuf2[NUMBUFLEN];
16714 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016715 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016716 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016717 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016720 spat = get_tv_string_chk(&argvars[0]);
16721 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16722 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16723 if (spat == NULL || mpat == NULL || epat == NULL)
16724 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 /* Handle the optional fourth argument: flags */
16727 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016728 if (dir == 0)
16729 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016730
16731 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016732 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16733 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016734 if ((flags & (SP_END | SP_SUBPAT)) != 0
16735 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016736 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016737 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016738 goto theend;
16739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016741 /* Using 'r' implies 'W', otherwise it doesn't work. */
16742 if (flags & SP_REPEAT)
16743 p_ws = FALSE;
16744
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016745 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016746 if (argvars[3].v_type == VAR_UNKNOWN
16747 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 skip = (char_u *)"";
16749 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016751 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 if (argvars[5].v_type != VAR_UNKNOWN)
16753 {
16754 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16755 if (lnum_stop < 0)
16756 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016757#ifdef FEAT_RELTIME
16758 if (argvars[6].v_type != VAR_UNKNOWN)
16759 {
16760 time_limit = get_tv_number_chk(&argvars[6], NULL);
16761 if (time_limit < 0)
16762 goto theend;
16763 }
16764#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016765 }
16766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016767 if (skip == NULL)
16768 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016770 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016771 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016772
16773theend:
16774 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016775
16776 return retval;
16777}
16778
16779/*
16780 * "searchpair()" function
16781 */
16782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016783f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016784{
16785 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16786}
16787
16788/*
16789 * "searchpairpos()" function
16790 */
16791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016792f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016793{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016794 pos_T match_pos;
16795 int lnum = 0;
16796 int col = 0;
16797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016799 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016800
16801 if (searchpair_cmn(argvars, &match_pos) > 0)
16802 {
16803 lnum = match_pos.lnum;
16804 col = match_pos.col;
16805 }
16806
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016807 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16808 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016809}
16810
16811/*
16812 * Search for a start/middle/end thing.
16813 * Used by searchpair(), see its documentation for the details.
16814 * Returns 0 or -1 for no match,
16815 */
16816 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016817do_searchpair(
16818 char_u *spat, /* start pattern */
16819 char_u *mpat, /* middle pattern */
16820 char_u *epat, /* end pattern */
16821 int dir, /* BACKWARD or FORWARD */
16822 char_u *skip, /* skip expression */
16823 int flags, /* SP_SETPCMARK and other SP_ values */
16824 pos_T *match_pos,
16825 linenr_T lnum_stop, /* stop at this line if not zero */
16826 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016827{
16828 char_u *save_cpo;
16829 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16830 long retval = 0;
16831 pos_T pos;
16832 pos_T firstpos;
16833 pos_T foundpos;
16834 pos_T save_cursor;
16835 pos_T save_pos;
16836 int n;
16837 int r;
16838 int nest = 1;
16839 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016840 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016841 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016842
16843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16844 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016845 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016846
Bram Moolenaar76929292008-01-06 19:07:36 +000016847#ifdef FEAT_RELTIME
16848 /* Set the time limit, if there is one. */
16849 profile_setlimit(time_limit, &tm);
16850#endif
16851
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016852 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16853 * start/middle/end (pat3, for the top pair). */
16854 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16855 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16856 if (pat2 == NULL || pat3 == NULL)
16857 goto theend;
16858 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16859 if (*mpat == NUL)
16860 STRCPY(pat3, pat2);
16861 else
16862 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16863 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016864 if (flags & SP_START)
16865 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016866
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 save_cursor = curwin->w_cursor;
16868 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016869 clearpos(&firstpos);
16870 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 pat = pat3;
16872 for (;;)
16873 {
16874 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016875 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16877 /* didn't find it or found the first match again: FAIL */
16878 break;
16879
16880 if (firstpos.lnum == 0)
16881 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016882 if (equalpos(pos, foundpos))
16883 {
16884 /* Found the same position again. Can happen with a pattern that
16885 * has "\zs" at the end and searching backwards. Advance one
16886 * character and try again. */
16887 if (dir == BACKWARD)
16888 decl(&pos);
16889 else
16890 incl(&pos);
16891 }
16892 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016894 /* clear the start flag to avoid getting stuck here */
16895 options &= ~SEARCH_START;
16896
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 /* If the skip pattern matches, ignore this match. */
16898 if (*skip != NUL)
16899 {
16900 save_pos = curwin->w_cursor;
16901 curwin->w_cursor = pos;
16902 r = eval_to_bool(skip, &err, NULL, FALSE);
16903 curwin->w_cursor = save_pos;
16904 if (err)
16905 {
16906 /* Evaluating {skip} caused an error, break here. */
16907 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016908 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 break;
16910 }
16911 if (r)
16912 continue;
16913 }
16914
16915 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16916 {
16917 /* Found end when searching backwards or start when searching
16918 * forward: nested pair. */
16919 ++nest;
16920 pat = pat2; /* nested, don't search for middle */
16921 }
16922 else
16923 {
16924 /* Found end when searching forward or start when searching
16925 * backward: end of (nested) pair; or found middle in outer pair. */
16926 if (--nest == 1)
16927 pat = pat3; /* outer level, search for middle */
16928 }
16929
16930 if (nest == 0)
16931 {
16932 /* Found the match: return matchcount or line number. */
16933 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016934 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016936 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016937 if (flags & SP_SETPCMARK)
16938 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 curwin->w_cursor = pos;
16940 if (!(flags & SP_REPEAT))
16941 break;
16942 nest = 1; /* search for next unmatched */
16943 }
16944 }
16945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016946 if (match_pos != NULL)
16947 {
16948 /* Store the match cursor position */
16949 match_pos->lnum = curwin->w_cursor.lnum;
16950 match_pos->col = curwin->w_cursor.col + 1;
16951 }
16952
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016954 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 curwin->w_cursor = save_cursor;
16956
16957theend:
16958 vim_free(pat2);
16959 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016960 if (p_cpo == empty_option)
16961 p_cpo = save_cpo;
16962 else
16963 /* Darn, evaluating the {skip} expression changed the value. */
16964 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016965
16966 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967}
16968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016969/*
16970 * "searchpos()" function
16971 */
16972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016973f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016974{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016975 pos_T match_pos;
16976 int lnum = 0;
16977 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016978 int n;
16979 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016982 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016983
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016984 n = search_cmn(argvars, &match_pos, &flags);
16985 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016986 {
16987 lnum = match_pos.lnum;
16988 col = match_pos.col;
16989 }
16990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016991 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16992 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016993 if (flags & SP_SUBPAT)
16994 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016995}
16996
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016998f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000#ifdef FEAT_CLIENTSERVER
17001 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 char_u *server = get_tv_string_chk(&argvars[0]);
17003 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 if (server == NULL || reply == NULL)
17007 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008 if (check_restricted() || check_secure())
17009 return;
17010# ifdef FEAT_X11
17011 if (check_connection() == FAIL)
17012 return;
17013# endif
17014
17015 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 EMSG(_("E258: Unable to send to client"));
17018 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017020 rettv->vval.v_number = 0;
17021#else
17022 rettv->vval.v_number = -1;
17023#endif
17024}
17025
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017027f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028{
17029 char_u *r = NULL;
17030
17031#ifdef FEAT_CLIENTSERVER
17032# ifdef WIN32
17033 r = serverGetVimNames();
17034# else
17035 make_connection();
17036 if (X_DISPLAY != NULL)
17037 r = serverGetVimNames(X_DISPLAY);
17038# endif
17039#endif
17040 rettv->v_type = VAR_STRING;
17041 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042}
17043
17044/*
17045 * "setbufvar()" function
17046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017048f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049{
17050 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 char_u nbuf[NUMBUFLEN];
17055
17056 if (check_restricted() || check_secure())
17057 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17059 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017060 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 varp = &argvars[2];
17062
17063 if (buf != NULL && varname != NULL && varp != NULL)
17064 {
17065 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
17068 if (*varname == '&')
17069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017070 long numval;
17071 char_u *strval;
17072 int error = FALSE;
17073
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 numval = get_tv_number_chk(varp, &error);
17076 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 if (!error && strval != NULL)
17078 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 }
17080 else
17081 {
17082 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17083 if (bufvarname != NULL)
17084 {
17085 STRCPY(bufvarname, "b:");
17086 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017087 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 vim_free(bufvarname);
17089 }
17090 }
17091
17092 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095}
17096
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017098f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017099{
17100 dict_T *d;
17101 dictitem_T *di;
17102 char_u *csearch;
17103
17104 if (argvars[0].v_type != VAR_DICT)
17105 {
17106 EMSG(_(e_dictreq));
17107 return;
17108 }
17109
17110 if ((d = argvars[0].vval.v_dict) != NULL)
17111 {
17112 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17113 if (csearch != NULL)
17114 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017115#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017116 if (enc_utf8)
17117 {
17118 int pcc[MAX_MCO];
17119 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017120
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017121 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17122 }
17123 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017124#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017125 set_last_csearch(PTR2CHAR(csearch),
17126 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017127 }
17128
17129 di = dict_find(d, (char_u *)"forward", -1);
17130 if (di != NULL)
17131 set_csearch_direction(get_tv_number(&di->di_tv)
17132 ? FORWARD : BACKWARD);
17133
17134 di = dict_find(d, (char_u *)"until", -1);
17135 if (di != NULL)
17136 set_csearch_until(!!get_tv_number(&di->di_tv));
17137 }
17138}
17139
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140/*
17141 * "setcmdpos()" function
17142 */
17143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017144f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017146 int pos = (int)get_tv_number(&argvars[0]) - 1;
17147
17148 if (pos >= 0)
17149 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150}
17151
17152/*
17153 * "setline()" function
17154 */
17155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017156f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157{
17158 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017159 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 list_T *l = NULL;
17161 listitem_T *li = NULL;
17162 long added = 0;
17163 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017165 lnum = get_tv_lnum(&argvars[0]);
17166 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 l = argvars[1].vval.v_list;
17169 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017171 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017173
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017174 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017175 for (;;)
17176 {
17177 if (l != NULL)
17178 {
17179 /* list argument, get next string */
17180 if (li == NULL)
17181 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017183 li = li->li_next;
17184 }
17185
17186 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017188 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017189
17190 /* When coming here from Insert mode, sync undo, so that this can be
17191 * undone separately from what was previously inserted. */
17192 if (u_sync_once == 2)
17193 {
17194 u_sync_once = 1; /* notify that u_sync() was called */
17195 u_sync(TRUE);
17196 }
17197
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017198 if (lnum <= curbuf->b_ml.ml_line_count)
17199 {
17200 /* existing line, replace it */
17201 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17202 {
17203 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017204 if (lnum == curwin->w_cursor.lnum)
17205 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017206 rettv->vval.v_number = 0; /* OK */
17207 }
17208 }
17209 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17210 {
17211 /* lnum is one past the last line, append the line */
17212 ++added;
17213 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17214 rettv->vval.v_number = 0; /* OK */
17215 }
17216
17217 if (l == NULL) /* only one string argument */
17218 break;
17219 ++lnum;
17220 }
17221
17222 if (added > 0)
17223 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224}
17225
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017226static 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 +000017227
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017229 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017230 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017232set_qf_ll_list(
17233 win_T *wp UNUSED,
17234 typval_T *list_arg UNUSED,
17235 typval_T *action_arg UNUSED,
17236 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017237{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017238#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017239 char_u *act;
17240 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017241#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017242
Bram Moolenaar2641f772005-03-25 21:58:17 +000017243 rettv->vval.v_number = -1;
17244
17245#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017246 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017247 EMSG(_(e_listreq));
17248 else
17249 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017250 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017251
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017252 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017253 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017254 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 if (act == NULL)
17256 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017257 if (*act == 'a' || *act == 'r')
17258 action = *act;
17259 }
17260
Bram Moolenaar81484f42012-12-05 15:16:47 +010017261 if (l != NULL && set_errorlist(wp, l, action,
17262 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017263 rettv->vval.v_number = 0;
17264 }
17265#endif
17266}
17267
17268/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017269 * "setloclist()" function
17270 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017272f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017273{
17274 win_T *win;
17275
17276 rettv->vval.v_number = -1;
17277
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017278 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017279 if (win != NULL)
17280 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17281}
17282
17283/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017284 * "setmatches()" function
17285 */
17286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017287f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017288{
17289#ifdef FEAT_SEARCH_EXTRA
17290 list_T *l;
17291 listitem_T *li;
17292 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017293 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017294
17295 rettv->vval.v_number = -1;
17296 if (argvars[0].v_type != VAR_LIST)
17297 {
17298 EMSG(_(e_listreq));
17299 return;
17300 }
17301 if ((l = argvars[0].vval.v_list) != NULL)
17302 {
17303
17304 /* To some extent make sure that we are dealing with a list from
17305 * "getmatches()". */
17306 li = l->lv_first;
17307 while (li != NULL)
17308 {
17309 if (li->li_tv.v_type != VAR_DICT
17310 || (d = li->li_tv.vval.v_dict) == NULL)
17311 {
17312 EMSG(_(e_invarg));
17313 return;
17314 }
17315 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017316 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17317 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017318 && dict_find(d, (char_u *)"priority", -1) != NULL
17319 && dict_find(d, (char_u *)"id", -1) != NULL))
17320 {
17321 EMSG(_(e_invarg));
17322 return;
17323 }
17324 li = li->li_next;
17325 }
17326
17327 clear_matches(curwin);
17328 li = l->lv_first;
17329 while (li != NULL)
17330 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017331 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017332 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017333 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017334 char_u *group;
17335 int priority;
17336 int id;
17337 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017338
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017339 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017340 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17341 {
17342 if (s == NULL)
17343 {
17344 s = list_alloc();
17345 if (s == NULL)
17346 return;
17347 }
17348
17349 /* match from matchaddpos() */
17350 for (i = 1; i < 9; i++)
17351 {
17352 sprintf((char *)buf, (char *)"pos%d", i);
17353 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17354 {
17355 if (di->di_tv.v_type != VAR_LIST)
17356 return;
17357
17358 list_append_tv(s, &di->di_tv);
17359 s->lv_refcount++;
17360 }
17361 else
17362 break;
17363 }
17364 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017365
17366 group = get_dict_string(d, (char_u *)"group", FALSE);
17367 priority = (int)get_dict_number(d, (char_u *)"priority");
17368 id = (int)get_dict_number(d, (char_u *)"id");
17369 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17370 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17371 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017372 if (i == 0)
17373 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017374 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017375 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017376 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017377 }
17378 else
17379 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017380 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017381 list_unref(s);
17382 s = NULL;
17383 }
17384
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017385 li = li->li_next;
17386 }
17387 rettv->vval.v_number = 0;
17388 }
17389#endif
17390}
17391
17392/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017393 * "setpos()" function
17394 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017396f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017397{
17398 pos_T pos;
17399 int fnum;
17400 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017401 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017402
Bram Moolenaar08250432008-02-13 11:42:46 +000017403 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017404 name = get_tv_string_chk(argvars);
17405 if (name != NULL)
17406 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017407 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017408 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017409 if (--pos.col < 0)
17410 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017411 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017412 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017413 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017414 if (fnum == curbuf->b_fnum)
17415 {
17416 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017417 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017418 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017419 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017420 curwin->w_set_curswant = FALSE;
17421 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017422 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017423 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017424 }
17425 else
17426 EMSG(_(e_invarg));
17427 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017428 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17429 {
17430 /* set mark */
17431 if (setmark_pos(name[1], &pos, fnum) == OK)
17432 rettv->vval.v_number = 0;
17433 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017434 else
17435 EMSG(_(e_invarg));
17436 }
17437 }
17438}
17439
17440/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017441 * "setqflist()" function
17442 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017444f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017445{
17446 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17447}
17448
17449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 * "setreg()" function
17451 */
17452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017453f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454{
17455 int regname;
17456 char_u *strregname;
17457 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017458 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 int append;
17460 char_u yank_type;
17461 long block_len;
17462
17463 block_len = -1;
17464 yank_type = MAUTO;
17465 append = FALSE;
17466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017467 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017470 if (strregname == NULL)
17471 return; /* type error; errmsg already given */
17472 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 if (regname == 0 || regname == '@')
17474 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017478 stropt = get_tv_string_chk(&argvars[2]);
17479 if (stropt == NULL)
17480 return; /* type error */
17481 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 switch (*stropt)
17483 {
17484 case 'a': case 'A': /* append */
17485 append = TRUE;
17486 break;
17487 case 'v': case 'c': /* character-wise selection */
17488 yank_type = MCHAR;
17489 break;
17490 case 'V': case 'l': /* line-wise selection */
17491 yank_type = MLINE;
17492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 case 'b': case Ctrl_V: /* block-wise selection */
17494 yank_type = MBLOCK;
17495 if (VIM_ISDIGIT(stropt[1]))
17496 {
17497 ++stropt;
17498 block_len = getdigits(&stropt) - 1;
17499 --stropt;
17500 }
17501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 }
17503 }
17504
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017505 if (argvars[1].v_type == VAR_LIST)
17506 {
17507 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017508 char_u **allocval;
17509 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017510 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017511 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017512 int len = argvars[1].vval.v_list->lv_len;
17513 listitem_T *li;
17514
Bram Moolenaar7d647822014-04-05 21:28:56 +020017515 /* First half: use for pointers to result lines; second half: use for
17516 * pointers to allocated copies. */
17517 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017518 if (lstval == NULL)
17519 return;
17520 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017521 allocval = lstval + len + 2;
17522 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017523
17524 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17525 li = li->li_next)
17526 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017527 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017528 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017529 goto free_lstval;
17530 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017531 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017532 /* Need to make a copy, next get_tv_string_buf_chk() will
17533 * overwrite the string. */
17534 strval = vim_strsave(buf);
17535 if (strval == NULL)
17536 goto free_lstval;
17537 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017538 }
17539 *curval++ = strval;
17540 }
17541 *curval++ = NULL;
17542
17543 write_reg_contents_lst(regname, lstval, -1,
17544 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017545free_lstval:
17546 while (curallocval > allocval)
17547 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017548 vim_free(lstval);
17549 }
17550 else
17551 {
17552 strval = get_tv_string_chk(&argvars[1]);
17553 if (strval == NULL)
17554 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017555 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559}
17560
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017561/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017562 * "settabvar()" function
17563 */
17564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017565f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017566{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017567#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017568 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017569 tabpage_T *tp;
17570#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017571 char_u *varname, *tabvarname;
17572 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017573
17574 rettv->vval.v_number = 0;
17575
17576 if (check_restricted() || check_secure())
17577 return;
17578
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017579#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017580 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017581#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017582 varname = get_tv_string_chk(&argvars[1]);
17583 varp = &argvars[2];
17584
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017585 if (varname != NULL && varp != NULL
17586#ifdef FEAT_WINDOWS
17587 && tp != NULL
17588#endif
17589 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017590 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017591#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017592 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017593 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017594#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017595
17596 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17597 if (tabvarname != NULL)
17598 {
17599 STRCPY(tabvarname, "t:");
17600 STRCPY(tabvarname + 2, varname);
17601 set_var(tabvarname, varp, TRUE);
17602 vim_free(tabvarname);
17603 }
17604
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017605#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017606 /* Restore current tabpage */
17607 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017608 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017609#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017610 }
17611}
17612
17613/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017614 * "settabwinvar()" function
17615 */
17616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017617f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017618{
17619 setwinvar(argvars, rettv, 1);
17620}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621
17622/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017623 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017626f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017628 setwinvar(argvars, rettv, 0);
17629}
17630
17631/*
17632 * "setwinvar()" and "settabwinvar()" functions
17633 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017634
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017636setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017637{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 win_T *win;
17639#ifdef FEAT_WINDOWS
17640 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017641 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017642 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643#endif
17644 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017645 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017647 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648
17649 if (check_restricted() || check_secure())
17650 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017651
17652#ifdef FEAT_WINDOWS
17653 if (off == 1)
17654 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17655 else
17656 tp = curtab;
17657#endif
17658 win = find_win_by_nr(&argvars[off], tp);
17659 varname = get_tv_string_chk(&argvars[off + 1]);
17660 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661
17662 if (win != NULL && varname != NULL && varp != NULL)
17663 {
17664#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017665 need_switch_win = !(tp == curtab && win == curwin);
17666 if (!need_switch_win
17667 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017670 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017672 long numval;
17673 char_u *strval;
17674 int error = FALSE;
17675
17676 ++varname;
17677 numval = get_tv_number_chk(varp, &error);
17678 strval = get_tv_string_buf_chk(varp, nbuf);
17679 if (!error && strval != NULL)
17680 set_option_value(varname, numval, strval, OPT_LOCAL);
17681 }
17682 else
17683 {
17684 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17685 if (winvarname != NULL)
17686 {
17687 STRCPY(winvarname, "w:");
17688 STRCPY(winvarname + 2, varname);
17689 set_var(winvarname, varp, TRUE);
17690 vim_free(winvarname);
17691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 }
17693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017695 if (need_switch_win)
17696 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697#endif
17698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699}
17700
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017701#ifdef FEAT_CRYPT
17702/*
17703 * "sha256({string})" function
17704 */
17705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017707{
17708 char_u *p;
17709
17710 p = get_tv_string(&argvars[0]);
17711 rettv->vval.v_string = vim_strsave(
17712 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17713 rettv->v_type = VAR_STRING;
17714}
17715#endif /* FEAT_CRYPT */
17716
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017718 * "shellescape({string})" function
17719 */
17720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017721f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017722{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017723 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017724 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017725 rettv->v_type = VAR_STRING;
17726}
17727
17728/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017729 * shiftwidth() function
17730 */
17731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017732f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017733{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017734 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017735}
17736
17737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 */
17740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017741f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017743 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745 p = get_tv_string(&argvars[0]);
17746 rettv->vval.v_string = vim_strsave(p);
17747 simplify_filename(rettv->vval.v_string); /* simplify in place */
17748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749}
17750
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017751#ifdef FEAT_FLOAT
17752/*
17753 * "sin()" function
17754 */
17755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017756f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017757{
17758 float_T f;
17759
17760 rettv->v_type = VAR_FLOAT;
17761 if (get_float_arg(argvars, &f) == OK)
17762 rettv->vval.v_float = sin(f);
17763 else
17764 rettv->vval.v_float = 0.0;
17765}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017766
17767/*
17768 * "sinh()" function
17769 */
17770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017771f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017772{
17773 float_T f;
17774
17775 rettv->v_type = VAR_FLOAT;
17776 if (get_float_arg(argvars, &f) == OK)
17777 rettv->vval.v_float = sinh(f);
17778 else
17779 rettv->vval.v_float = 0.0;
17780}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017781#endif
17782
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783static int
17784#ifdef __BORLANDC__
17785 _RTLENTRYF
17786#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017787 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788static int
17789#ifdef __BORLANDC__
17790 _RTLENTRYF
17791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017792 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017793
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017794/* struct used in the array that's given to qsort() */
17795typedef struct
17796{
17797 listitem_T *item;
17798 int idx;
17799} sortItem_T;
17800
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017802static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017803static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017804#ifdef FEAT_FLOAT
17805static int item_compare_float;
17806#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017807static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017808static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017810static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017811static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017812#define ITEM_COMPARE_FAIL 999
17813
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017815 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817 static int
17818#ifdef __BORLANDC__
17819_RTLENTRYF
17820#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017821item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017823 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017824 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017826 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017827 int res;
17828 char_u numbuf1[NUMBUFLEN];
17829 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017831 si1 = (sortItem_T *)s1;
17832 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017833 tv1 = &si1->item->li_tv;
17834 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017835
17836 if (item_compare_numbers)
17837 {
17838 long v1 = get_tv_number(tv1);
17839 long v2 = get_tv_number(tv2);
17840
17841 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17842 }
17843
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017844#ifdef FEAT_FLOAT
17845 if (item_compare_float)
17846 {
17847 float_T v1 = get_tv_float(tv1);
17848 float_T v2 = get_tv_float(tv2);
17849
17850 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17851 }
17852#endif
17853
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017854 /* tv2string() puts quotes around a string and allocates memory. Don't do
17855 * that for string variables. Use a single quote when comparing with a
17856 * non-string to do what the docs promise. */
17857 if (tv1->v_type == VAR_STRING)
17858 {
17859 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17860 p1 = (char_u *)"'";
17861 else
17862 p1 = tv1->vval.v_string;
17863 }
17864 else
17865 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17866 if (tv2->v_type == VAR_STRING)
17867 {
17868 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17869 p2 = (char_u *)"'";
17870 else
17871 p2 = tv2->vval.v_string;
17872 }
17873 else
17874 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017875 if (p1 == NULL)
17876 p1 = (char_u *)"";
17877 if (p2 == NULL)
17878 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017879 if (!item_compare_numeric)
17880 {
17881 if (item_compare_ic)
17882 res = STRICMP(p1, p2);
17883 else
17884 res = STRCMP(p1, p2);
17885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017887 {
17888 double n1, n2;
17889 n1 = strtod((char *)p1, (char **)&p1);
17890 n2 = strtod((char *)p2, (char **)&p2);
17891 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17892 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017893
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017894 /* When the result would be zero, compare the item indexes. Makes the
17895 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017896 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017897 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017898
Bram Moolenaar0d660222005-01-07 21:51:51 +000017899 vim_free(tofree1);
17900 vim_free(tofree2);
17901 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902}
17903
17904 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017905#ifdef __BORLANDC__
17906_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017908item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017909{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017910 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017913 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 /* shortcut after failure in previous call; compare all items equal */
17917 if (item_compare_func_err)
17918 return 0;
17919
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017920 si1 = (sortItem_T *)s1;
17921 si2 = (sortItem_T *)s2;
17922
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017923 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017924 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017925 copy_tv(&si1->item->li_tv, &argv[0]);
17926 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927
17928 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017929 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017930 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17931 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 clear_tv(&argv[0]);
17933 clear_tv(&argv[1]);
17934
17935 if (res == FAIL)
17936 res = ITEM_COMPARE_FAIL;
17937 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017938 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17939 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017940 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017942
17943 /* When the result would be zero, compare the pointers themselves. Makes
17944 * the sort stable. */
17945 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017946 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017947
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 return res;
17949}
17950
17951/*
17952 * "sort({list})" function
17953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017955do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956{
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 list_T *l;
17958 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017959 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 long len;
17961 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017964 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 else
17966 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017967 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017968 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017969 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17970 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017971 return;
17972 rettv->vval.v_list = l;
17973 rettv->v_type = VAR_LIST;
17974 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976 len = list_len(l);
17977 if (len <= 1)
17978 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
Bram Moolenaar0d660222005-01-07 21:51:51 +000017980 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017981 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017982 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017983#ifdef FEAT_FLOAT
17984 item_compare_float = FALSE;
17985#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017986 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017987 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017988 if (argvars[1].v_type != VAR_UNKNOWN)
17989 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017990 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017991 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017992 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017993 else
17994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017995 int error = FALSE;
17996
17997 i = get_tv_number_chk(&argvars[1], &error);
17998 if (error)
17999 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000 if (i == 1)
18001 item_compare_ic = TRUE;
18002 else
18003 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018004 if (item_compare_func != NULL)
18005 {
18006 if (STRCMP(item_compare_func, "n") == 0)
18007 {
18008 item_compare_func = NULL;
18009 item_compare_numeric = TRUE;
18010 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018011 else if (STRCMP(item_compare_func, "N") == 0)
18012 {
18013 item_compare_func = NULL;
18014 item_compare_numbers = TRUE;
18015 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018016#ifdef FEAT_FLOAT
18017 else if (STRCMP(item_compare_func, "f") == 0)
18018 {
18019 item_compare_func = NULL;
18020 item_compare_float = TRUE;
18021 }
18022#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018023 else if (STRCMP(item_compare_func, "i") == 0)
18024 {
18025 item_compare_func = NULL;
18026 item_compare_ic = TRUE;
18027 }
18028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018030
18031 if (argvars[2].v_type != VAR_UNKNOWN)
18032 {
18033 /* optional third argument: {dict} */
18034 if (argvars[2].v_type != VAR_DICT)
18035 {
18036 EMSG(_(e_dictreq));
18037 return;
18038 }
18039 item_compare_selfdict = argvars[2].vval.v_dict;
18040 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042
Bram Moolenaar0d660222005-01-07 21:51:51 +000018043 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018044 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 if (ptrs == NULL)
18046 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047
Bram Moolenaar327aa022014-03-25 18:24:23 +010018048 i = 0;
18049 if (sort)
18050 {
18051 /* sort(): ptrs will be the list to sort */
18052 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018053 {
18054 ptrs[i].item = li;
18055 ptrs[i].idx = i;
18056 ++i;
18057 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018058
18059 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018060 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061 /* test the compare function */
18062 if (item_compare_func != NULL
18063 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018064 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018065 EMSG(_("E702: Sort compare function failed"));
18066 else
18067 {
18068 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018069 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018070 item_compare_func == NULL ? item_compare : item_compare2);
18071
18072 if (!item_compare_func_err)
18073 {
18074 /* Clear the List and append the items in sorted order. */
18075 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18076 l->lv_len = 0;
18077 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018078 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018079 }
18080 }
18081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018084 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018085
18086 /* f_uniq(): ptrs will be a stack of items to remove */
18087 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018088 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018089 item_compare_func_ptr = item_compare_func
18090 ? item_compare2 : item_compare;
18091
18092 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18093 li = li->li_next)
18094 {
18095 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18096 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018097 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018098 if (item_compare_func_err)
18099 {
18100 EMSG(_("E882: Uniq compare function failed"));
18101 break;
18102 }
18103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105 if (!item_compare_func_err)
18106 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018107 while (--i >= 0)
18108 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018109 li = ptrs[i].item->li_next;
18110 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018111 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018112 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018113 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018114 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018115 list_fix_watch(l, li);
18116 listitem_free(li);
18117 l->lv_len--;
18118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018119 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 }
18121
18122 vim_free(ptrs);
18123 }
18124}
18125
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018126/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018127 * "sort({list})" function
18128 */
18129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018130f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018131{
18132 do_sort_uniq(argvars, rettv, TRUE);
18133}
18134
18135/*
18136 * "uniq({list})" function
18137 */
18138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018139f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018140{
18141 do_sort_uniq(argvars, rettv, FALSE);
18142}
18143
18144/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018145 * "soundfold({word})" function
18146 */
18147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018149{
18150 char_u *s;
18151
18152 rettv->v_type = VAR_STRING;
18153 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018154#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018155 rettv->vval.v_string = eval_soundfold(s);
18156#else
18157 rettv->vval.v_string = vim_strsave(s);
18158#endif
18159}
18160
18161/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018162 * "spellbadword()" function
18163 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018165f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018166{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018167 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018168 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018169 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018170
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018171 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018172 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018173
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018174#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018175 if (argvars[0].v_type == VAR_UNKNOWN)
18176 {
18177 /* Find the start and length of the badly spelled word. */
18178 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18179 if (len != 0)
18180 word = ml_get_cursor();
18181 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018182 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018183 {
18184 char_u *str = get_tv_string_chk(&argvars[0]);
18185 int capcol = -1;
18186
18187 if (str != NULL)
18188 {
18189 /* Check the argument for spelling. */
18190 while (*str != NUL)
18191 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018192 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018193 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018194 {
18195 word = str;
18196 break;
18197 }
18198 str += len;
18199 }
18200 }
18201 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018202#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018203
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018204 list_append_string(rettv->vval.v_list, word, len);
18205 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018206 attr == HLF_SPB ? "bad" :
18207 attr == HLF_SPR ? "rare" :
18208 attr == HLF_SPL ? "local" :
18209 attr == HLF_SPC ? "caps" :
18210 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018211}
18212
18213/*
18214 * "spellsuggest()" function
18215 */
18216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018217f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018218{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018219#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018220 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018221 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018222 int maxcount;
18223 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018224 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018225 listitem_T *li;
18226 int need_capital = FALSE;
18227#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018229 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018230 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018231
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018232#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018233 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018234 {
18235 str = get_tv_string(&argvars[0]);
18236 if (argvars[1].v_type != VAR_UNKNOWN)
18237 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018238 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018239 if (maxcount <= 0)
18240 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018241 if (argvars[2].v_type != VAR_UNKNOWN)
18242 {
18243 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18244 if (typeerr)
18245 return;
18246 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018247 }
18248 else
18249 maxcount = 25;
18250
Bram Moolenaar4770d092006-01-12 23:22:24 +000018251 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018252
18253 for (i = 0; i < ga.ga_len; ++i)
18254 {
18255 str = ((char_u **)ga.ga_data)[i];
18256
18257 li = listitem_alloc();
18258 if (li == NULL)
18259 vim_free(str);
18260 else
18261 {
18262 li->li_tv.v_type = VAR_STRING;
18263 li->li_tv.v_lock = 0;
18264 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018266 }
18267 }
18268 ga_clear(&ga);
18269 }
18270#endif
18271}
18272
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018274f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018275{
18276 char_u *str;
18277 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018278 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 regmatch_T regmatch;
18280 char_u patbuf[NUMBUFLEN];
18281 char_u *save_cpo;
18282 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018284 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018285 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286
18287 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18288 save_cpo = p_cpo;
18289 p_cpo = (char_u *)"";
18290
18291 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018292 if (argvars[1].v_type != VAR_UNKNOWN)
18293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018294 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18295 if (pat == NULL)
18296 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018297 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018298 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018299 }
18300 if (pat == NULL || *pat == NUL)
18301 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018303 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 if (typeerr)
18306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18309 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018312 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018314 if (*str == NUL)
18315 match = FALSE; /* empty item at the end */
18316 else
18317 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018318 if (match)
18319 end = regmatch.startp[0];
18320 else
18321 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018322 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18323 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018324 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018325 if (list_append_string(rettv->vval.v_list, str,
18326 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018328 }
18329 if (!match)
18330 break;
18331 /* Advance to just after the match. */
18332 if (regmatch.endp[0] > str)
18333 col = 0;
18334 else
18335 {
18336 /* Don't get stuck at the same match. */
18337#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018338 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018339#else
18340 col = 1;
18341#endif
18342 }
18343 str = regmatch.endp[0];
18344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345
Bram Moolenaar473de612013-06-08 18:19:48 +020018346 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348
Bram Moolenaar0d660222005-01-07 21:51:51 +000018349 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350}
18351
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018352#ifdef FEAT_FLOAT
18353/*
18354 * "sqrt()" function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018358{
18359 float_T f;
18360
18361 rettv->v_type = VAR_FLOAT;
18362 if (get_float_arg(argvars, &f) == OK)
18363 rettv->vval.v_float = sqrt(f);
18364 else
18365 rettv->vval.v_float = 0.0;
18366}
18367
18368/*
18369 * "str2float()" function
18370 */
18371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018372f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018373{
18374 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18375
18376 if (*p == '+')
18377 p = skipwhite(p + 1);
18378 (void)string2float(p, &rettv->vval.v_float);
18379 rettv->v_type = VAR_FLOAT;
18380}
18381#endif
18382
Bram Moolenaar2c932302006-03-18 21:42:09 +000018383/*
18384 * "str2nr()" function
18385 */
18386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018387f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018388{
18389 int base = 10;
18390 char_u *p;
18391 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018392 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018393
18394 if (argvars[1].v_type != VAR_UNKNOWN)
18395 {
18396 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018397 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018398 {
18399 EMSG(_(e_invarg));
18400 return;
18401 }
18402 }
18403
18404 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018405 if (*p == '+')
18406 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018407 switch (base)
18408 {
18409 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18410 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18411 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18412 default: what = 0;
18413 }
18414 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018415 rettv->vval.v_number = n;
18416}
18417
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418#ifdef HAVE_STRFTIME
18419/*
18420 * "strftime({format}[, {time}])" function
18421 */
18422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018423f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424{
18425 char_u result_buf[256];
18426 struct tm *curtime;
18427 time_t seconds;
18428 char_u *p;
18429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018433 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 seconds = time(NULL);
18435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 curtime = localtime(&seconds);
18438 /* MSVC returns NULL for an invalid value of seconds. */
18439 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 else
18442 {
18443# ifdef FEAT_MBYTE
18444 vimconv_T conv;
18445 char_u *enc;
18446
18447 conv.vc_type = CONV_NONE;
18448 enc = enc_locale();
18449 convert_setup(&conv, p_enc, enc);
18450 if (conv.vc_type != CONV_NONE)
18451 p = string_convert(&conv, p, NULL);
18452# endif
18453 if (p != NULL)
18454 (void)strftime((char *)result_buf, sizeof(result_buf),
18455 (char *)p, curtime);
18456 else
18457 result_buf[0] = NUL;
18458
18459# ifdef FEAT_MBYTE
18460 if (conv.vc_type != CONV_NONE)
18461 vim_free(p);
18462 convert_setup(&conv, enc, p_enc);
18463 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 else
18466# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468
18469# ifdef FEAT_MBYTE
18470 /* Release conversion descriptors */
18471 convert_setup(&conv, NULL, NULL);
18472 vim_free(enc);
18473# endif
18474 }
18475}
18476#endif
18477
18478/*
18479 * "stridx()" function
18480 */
18481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018482f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483{
18484 char_u buf[NUMBUFLEN];
18485 char_u *needle;
18486 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018487 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018489 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018491 needle = get_tv_string_chk(&argvars[1]);
18492 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018493 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018494 if (needle == NULL || haystack == NULL)
18495 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496
Bram Moolenaar33570922005-01-25 22:26:29 +000018497 if (argvars[2].v_type != VAR_UNKNOWN)
18498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018499 int error = FALSE;
18500
18501 start_idx = get_tv_number_chk(&argvars[2], &error);
18502 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018503 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018504 if (start_idx >= 0)
18505 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018506 }
18507
18508 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18509 if (pos != NULL)
18510 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511}
18512
18513/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018514 * "string()" function
18515 */
18516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018517f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018518{
18519 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018520 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018522 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018523 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018524 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018525 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018526 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527}
18528
18529/*
18530 * "strlen()" function
18531 */
18532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018533f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 rettv->vval.v_number = (varnumber_T)(STRLEN(
18536 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537}
18538
18539/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018540 * "strchars()" function
18541 */
18542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018543f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018544{
18545 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018546 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018547#ifdef FEAT_MBYTE
18548 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018549 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018550#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018551
18552 if (argvars[1].v_type != VAR_UNKNOWN)
18553 skipcc = get_tv_number_chk(&argvars[1], NULL);
18554 if (skipcc < 0 || skipcc > 1)
18555 EMSG(_(e_invarg));
18556 else
18557 {
18558#ifdef FEAT_MBYTE
18559 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18560 while (*s != NUL)
18561 {
18562 func_mb_ptr2char_adv(&s);
18563 ++len;
18564 }
18565 rettv->vval.v_number = len;
18566#else
18567 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18568#endif
18569 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018570}
18571
18572/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018573 * "strdisplaywidth()" function
18574 */
18575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018576f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018577{
18578 char_u *s = get_tv_string(&argvars[0]);
18579 int col = 0;
18580
18581 if (argvars[1].v_type != VAR_UNKNOWN)
18582 col = get_tv_number(&argvars[1]);
18583
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018584 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018585}
18586
18587/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018588 * "strwidth()" function
18589 */
18590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018591f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018592{
18593 char_u *s = get_tv_string(&argvars[0]);
18594
18595 rettv->vval.v_number = (varnumber_T)(
18596#ifdef FEAT_MBYTE
18597 mb_string2cells(s, -1)
18598#else
18599 STRLEN(s)
18600#endif
18601 );
18602}
18603
18604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 * "strpart()" function
18606 */
18607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018608f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
18610 char_u *p;
18611 int n;
18612 int len;
18613 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018614 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 slen = (int)STRLEN(p);
18618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018619 n = get_tv_number_chk(&argvars[1], &error);
18620 if (error)
18621 len = 0;
18622 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 else
18625 len = slen - n; /* default len: all bytes that are available. */
18626
18627 /*
18628 * Only return the overlap between the specified part and the actual
18629 * string.
18630 */
18631 if (n < 0)
18632 {
18633 len += n;
18634 n = 0;
18635 }
18636 else if (n > slen)
18637 n = slen;
18638 if (len < 0)
18639 len = 0;
18640 else if (n + len > slen)
18641 len = slen - n;
18642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643 rettv->v_type = VAR_STRING;
18644 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645}
18646
18647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 * "strridx()" function
18649 */
18650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018651f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652{
18653 char_u buf[NUMBUFLEN];
18654 char_u *needle;
18655 char_u *haystack;
18656 char_u *rest;
18657 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018658 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018660 needle = get_tv_string_chk(&argvars[1]);
18661 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018662
18663 rettv->vval.v_number = -1;
18664 if (needle == NULL || haystack == NULL)
18665 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018666
18667 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018668 if (argvars[2].v_type != VAR_UNKNOWN)
18669 {
18670 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018671 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018672 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018673 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018674 }
18675 else
18676 end_idx = haystack_len;
18677
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018681 lastmatch = haystack + end_idx;
18682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018683 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018684 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018685 for (rest = haystack; *rest != '\0'; ++rest)
18686 {
18687 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018688 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018689 break;
18690 lastmatch = rest;
18691 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693
18694 if (lastmatch == NULL)
18695 rettv->vval.v_number = -1;
18696 else
18697 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18698}
18699
18700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 * "strtrans()" function
18702 */
18703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018704f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->v_type = VAR_STRING;
18707 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708}
18709
18710/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 * "submatch()" function
18712 */
18713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018714f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715{
Bram Moolenaar41571762014-04-02 19:00:58 +020018716 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018717 int no;
18718 int retList = 0;
18719
18720 no = (int)get_tv_number_chk(&argvars[0], &error);
18721 if (error)
18722 return;
18723 error = FALSE;
18724 if (argvars[1].v_type != VAR_UNKNOWN)
18725 retList = get_tv_number_chk(&argvars[1], &error);
18726 if (error)
18727 return;
18728
18729 if (retList == 0)
18730 {
18731 rettv->v_type = VAR_STRING;
18732 rettv->vval.v_string = reg_submatch(no);
18733 }
18734 else
18735 {
18736 rettv->v_type = VAR_LIST;
18737 rettv->vval.v_list = reg_submatch_list(no);
18738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739}
18740
18741/*
18742 * "substitute()" function
18743 */
18744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018745f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018746{
18747 char_u patbuf[NUMBUFLEN];
18748 char_u subbuf[NUMBUFLEN];
18749 char_u flagsbuf[NUMBUFLEN];
18750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018751 char_u *str = get_tv_string_chk(&argvars[0]);
18752 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18753 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18754 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18755
Bram Moolenaar0d660222005-01-07 21:51:51 +000018756 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018757 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18758 rettv->vval.v_string = NULL;
18759 else
18760 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761}
18762
18763/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018764 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018767f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768{
18769 int id = 0;
18770#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018771 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 long col;
18773 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018774 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018776 lnum = get_tv_lnum(argvars); /* -1 on type error */
18777 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18778 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018780 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018781 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018782 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783#endif
18784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786}
18787
18788/*
18789 * "synIDattr(id, what [, mode])" function
18790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018792f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793{
18794 char_u *p = NULL;
18795#ifdef FEAT_SYN_HL
18796 int id;
18797 char_u *what;
18798 char_u *mode;
18799 char_u modebuf[NUMBUFLEN];
18800 int modec;
18801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802 id = get_tv_number(&argvars[0]);
18803 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018808 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 modec = 0; /* replace invalid with current */
18810 }
18811 else
18812 {
18813#ifdef FEAT_GUI
18814 if (gui.in_use)
18815 modec = 'g';
18816 else
18817#endif
18818 if (t_colors > 1)
18819 modec = 'c';
18820 else
18821 modec = 't';
18822 }
18823
18824
18825 switch (TOLOWER_ASC(what[0]))
18826 {
18827 case 'b':
18828 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18829 p = highlight_color(id, what, modec);
18830 else /* bold */
18831 p = highlight_has_attr(id, HL_BOLD, modec);
18832 break;
18833
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018834 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 p = highlight_color(id, what, modec);
18836 break;
18837
18838 case 'i':
18839 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18840 p = highlight_has_attr(id, HL_INVERSE, modec);
18841 else /* italic */
18842 p = highlight_has_attr(id, HL_ITALIC, modec);
18843 break;
18844
18845 case 'n': /* name */
18846 p = get_highlight_name(NULL, id - 1);
18847 break;
18848
18849 case 'r': /* reverse */
18850 p = highlight_has_attr(id, HL_INVERSE, modec);
18851 break;
18852
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018853 case 's':
18854 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18855 p = highlight_color(id, what, modec);
18856 else /* standout */
18857 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 break;
18859
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018860 case 'u':
18861 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18862 /* underline */
18863 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18864 else
18865 /* undercurl */
18866 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 break;
18868 }
18869
18870 if (p != NULL)
18871 p = vim_strsave(p);
18872#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv->v_type = VAR_STRING;
18874 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875}
18876
18877/*
18878 * "synIDtrans(id)" function
18879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018881f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882{
18883 int id;
18884
18885#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018886 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887
18888 if (id > 0)
18889 id = syn_get_final_id(id);
18890 else
18891#endif
18892 id = 0;
18893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895}
18896
18897/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018898 * "synconcealed(lnum, col)" function
18899 */
18900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018901f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018902{
18903#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18904 long lnum;
18905 long col;
18906 int syntax_flags = 0;
18907 int cchar;
18908 int matchid = 0;
18909 char_u str[NUMBUFLEN];
18910#endif
18911
18912 rettv->v_type = VAR_LIST;
18913 rettv->vval.v_list = NULL;
18914
18915#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18916 lnum = get_tv_lnum(argvars); /* -1 on type error */
18917 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18918
18919 vim_memset(str, NUL, sizeof(str));
18920
18921 if (rettv_list_alloc(rettv) != FAIL)
18922 {
18923 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18924 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18925 && curwin->w_p_cole > 0)
18926 {
18927 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18928 syntax_flags = get_syntax_info(&matchid);
18929
18930 /* get the conceal character */
18931 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18932 {
18933 cchar = syn_get_sub_char();
18934 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18935 cchar = lcs_conceal;
18936 if (cchar != NUL)
18937 {
18938# ifdef FEAT_MBYTE
18939 if (has_mbyte)
18940 (*mb_char2bytes)(cchar, str);
18941 else
18942# endif
18943 str[0] = cchar;
18944 }
18945 }
18946 }
18947
18948 list_append_number(rettv->vval.v_list,
18949 (syntax_flags & HL_CONCEAL) != 0);
18950 /* -1 to auto-determine strlen */
18951 list_append_string(rettv->vval.v_list, str, -1);
18952 list_append_number(rettv->vval.v_list, matchid);
18953 }
18954#endif
18955}
18956
18957/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018958 * "synstack(lnum, col)" function
18959 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018962{
18963#ifdef FEAT_SYN_HL
18964 long lnum;
18965 long col;
18966 int i;
18967 int id;
18968#endif
18969
18970 rettv->v_type = VAR_LIST;
18971 rettv->vval.v_list = NULL;
18972
18973#ifdef FEAT_SYN_HL
18974 lnum = get_tv_lnum(argvars); /* -1 on type error */
18975 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18976
18977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018978 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018979 && rettv_list_alloc(rettv) != FAIL)
18980 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018981 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018982 for (i = 0; ; ++i)
18983 {
18984 id = syn_get_stack_item(i);
18985 if (id < 0)
18986 break;
18987 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18988 break;
18989 }
18990 }
18991#endif
18992}
18993
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018995get_cmd_output_as_rettv(
18996 typval_T *argvars,
18997 typval_T *rettv,
18998 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019000 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019002 char_u *infile = NULL;
19003 char_u buf[NUMBUFLEN];
19004 int err = FALSE;
19005 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019006 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019007 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019009 rettv->v_type = VAR_STRING;
19010 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019011 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019012 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019013
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019014 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019015 {
19016 /*
19017 * Write the string to a temp file, to be used for input of the shell
19018 * command.
19019 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019020 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019021 {
19022 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019023 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019024 }
19025
19026 fd = mch_fopen((char *)infile, WRITEBIN);
19027 if (fd == NULL)
19028 {
19029 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019030 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019031 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019032 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019033 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019034 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19035 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019036 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019037 else
19038 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019039 size_t len;
19040
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019041 p = get_tv_string_buf_chk(&argvars[1], buf);
19042 if (p == NULL)
19043 {
19044 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019045 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019046 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019047 len = STRLEN(p);
19048 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019049 err = TRUE;
19050 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019051 if (fclose(fd) != 0)
19052 err = TRUE;
19053 if (err)
19054 {
19055 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019056 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019057 }
19058 }
19059
Bram Moolenaar52a72462014-08-29 15:53:52 +020019060 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19061 * echoes typeahead, that messes up the display. */
19062 if (!msg_silent)
19063 flags += SHELL_COOKED;
19064
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019065 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019067 int len;
19068 listitem_T *li;
19069 char_u *s = NULL;
19070 char_u *start;
19071 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019072 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073
Bram Moolenaar52a72462014-08-29 15:53:52 +020019074 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019075 if (res == NULL)
19076 goto errret;
19077
19078 list = list_alloc();
19079 if (list == NULL)
19080 goto errret;
19081
19082 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019084 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019085 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019086 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019087 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019089 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019090 if (s == NULL)
19091 goto errret;
19092
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019093 for (p = s; start < end; ++p, ++start)
19094 *p = *start == NUL ? NL : *start;
19095 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019096
19097 li = listitem_alloc();
19098 if (li == NULL)
19099 {
19100 vim_free(s);
19101 goto errret;
19102 }
19103 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019104 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105 li->li_tv.vval.v_string = s;
19106 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019108
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019109 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019110 rettv->v_type = VAR_LIST;
19111 rettv->vval.v_list = list;
19112 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019114 else
19115 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019116 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019117#ifdef USE_CR
19118 /* translate <CR> into <NL> */
19119 if (res != NULL)
19120 {
19121 char_u *s;
19122
19123 for (s = res; *s; ++s)
19124 {
19125 if (*s == CAR)
19126 *s = NL;
19127 }
19128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129#else
19130# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131 /* translate <CR><NL> into <NL> */
19132 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019134 char_u *s, *d;
19135
19136 d = res;
19137 for (s = res; *s; ++s)
19138 {
19139 if (s[0] == CAR && s[1] == NL)
19140 ++s;
19141 *d++ = *s;
19142 }
19143 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145# endif
19146#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019147 rettv->vval.v_string = res;
19148 res = NULL;
19149 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019150
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019151errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019152 if (infile != NULL)
19153 {
19154 mch_remove(infile);
19155 vim_free(infile);
19156 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019157 if (res != NULL)
19158 vim_free(res);
19159 if (list != NULL)
19160 list_free(list, TRUE);
19161}
19162
19163/*
19164 * "system()" function
19165 */
19166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019167f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019168{
19169 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19170}
19171
19172/*
19173 * "systemlist()" function
19174 */
19175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019176f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019177{
19178 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179}
19180
19181/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019182 * "tabpagebuflist()" function
19183 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019185f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019186{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019187#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019188 tabpage_T *tp;
19189 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019190
19191 if (argvars[0].v_type == VAR_UNKNOWN)
19192 wp = firstwin;
19193 else
19194 {
19195 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19196 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019197 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019198 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019199 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019200 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019201 for (; wp != NULL; wp = wp->w_next)
19202 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019203 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019204 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019205 }
19206#endif
19207}
19208
19209
19210/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019211 * "tabpagenr()" function
19212 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019214f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019215{
19216 int nr = 1;
19217#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019218 char_u *arg;
19219
19220 if (argvars[0].v_type != VAR_UNKNOWN)
19221 {
19222 arg = get_tv_string_chk(&argvars[0]);
19223 nr = 0;
19224 if (arg != NULL)
19225 {
19226 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019227 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019228 else
19229 EMSG2(_(e_invexpr2), arg);
19230 }
19231 }
19232 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019233 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019234#endif
19235 rettv->vval.v_number = nr;
19236}
19237
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019238
19239#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019240static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019241
19242/*
19243 * Common code for tabpagewinnr() and winnr().
19244 */
19245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019246get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019247{
19248 win_T *twin;
19249 int nr = 1;
19250 win_T *wp;
19251 char_u *arg;
19252
19253 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19254 if (argvar->v_type != VAR_UNKNOWN)
19255 {
19256 arg = get_tv_string_chk(argvar);
19257 if (arg == NULL)
19258 nr = 0; /* type error; errmsg already given */
19259 else if (STRCMP(arg, "$") == 0)
19260 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19261 else if (STRCMP(arg, "#") == 0)
19262 {
19263 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19264 if (twin == NULL)
19265 nr = 0;
19266 }
19267 else
19268 {
19269 EMSG2(_(e_invexpr2), arg);
19270 nr = 0;
19271 }
19272 }
19273
19274 if (nr > 0)
19275 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19276 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019277 {
19278 if (wp == NULL)
19279 {
19280 /* didn't find it in this tabpage */
19281 nr = 0;
19282 break;
19283 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019284 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019285 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019286 return nr;
19287}
19288#endif
19289
19290/*
19291 * "tabpagewinnr()" function
19292 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019294f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019295{
19296 int nr = 1;
19297#ifdef FEAT_WINDOWS
19298 tabpage_T *tp;
19299
19300 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19301 if (tp == NULL)
19302 nr = 0;
19303 else
19304 nr = get_winnr(tp, &argvars[1]);
19305#endif
19306 rettv->vval.v_number = nr;
19307}
19308
19309
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019310/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019311 * "tagfiles()" function
19312 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019314f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019315{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019316 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019317 tagname_T tn;
19318 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019320 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019321 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019322 fname = alloc(MAXPATHL);
19323 if (fname == NULL)
19324 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019326 for (first = TRUE; ; first = FALSE)
19327 if (get_tagfname(&tn, first, fname) == FAIL
19328 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019329 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019330 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019331 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019332}
19333
19334/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019335 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019336 */
19337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019339{
19340 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019341
19342 tag_pattern = get_tv_string(&argvars[0]);
19343
19344 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019345 if (*tag_pattern == NUL)
19346 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019348 if (rettv_list_alloc(rettv) == OK)
19349 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019350}
19351
19352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 * "tempname()" function
19354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019356f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357{
19358 static int x = 'A';
19359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019360 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019361 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
19363 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19364 * names. Skip 'I' and 'O', they are used for shell redirection. */
19365 do
19366 {
19367 if (x == 'Z')
19368 x = '0';
19369 else if (x == '9')
19370 x = 'A';
19371 else
19372 {
19373#ifdef EBCDIC
19374 if (x == 'I')
19375 x = 'J';
19376 else if (x == 'R')
19377 x = 'S';
19378 else
19379#endif
19380 ++x;
19381 }
19382 } while (x == 'I' || x == 'O');
19383}
19384
19385/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019386 * "test(list)" function: Just checking the walls...
19387 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019390{
19391 /* Used for unit testing. Change the code below to your liking. */
19392#if 0
19393 listitem_T *li;
19394 list_T *l;
19395 char_u *bad, *good;
19396
19397 if (argvars[0].v_type != VAR_LIST)
19398 return;
19399 l = argvars[0].vval.v_list;
19400 if (l == NULL)
19401 return;
19402 li = l->lv_first;
19403 if (li == NULL)
19404 return;
19405 bad = get_tv_string(&li->li_tv);
19406 li = li->li_next;
19407 if (li == NULL)
19408 return;
19409 good = get_tv_string(&li->li_tv);
19410 rettv->vval.v_number = test_edit_score(bad, good);
19411#endif
19412}
19413
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019414#ifdef FEAT_FLOAT
19415/*
19416 * "tan()" function
19417 */
19418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019419f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019420{
19421 float_T f;
19422
19423 rettv->v_type = VAR_FLOAT;
19424 if (get_float_arg(argvars, &f) == OK)
19425 rettv->vval.v_float = tan(f);
19426 else
19427 rettv->vval.v_float = 0.0;
19428}
19429
19430/*
19431 * "tanh()" function
19432 */
19433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019434f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019435{
19436 float_T f;
19437
19438 rettv->v_type = VAR_FLOAT;
19439 if (get_float_arg(argvars, &f) == OK)
19440 rettv->vval.v_float = tanh(f);
19441 else
19442 rettv->vval.v_float = 0.0;
19443}
19444#endif
19445
Bram Moolenaard52d9742005-08-21 22:20:28 +000019446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 * "tolower(string)" function
19448 */
19449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019450f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451{
19452 char_u *p;
19453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019454 p = vim_strsave(get_tv_string(&argvars[0]));
19455 rettv->v_type = VAR_STRING;
19456 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
19458 if (p != NULL)
19459 while (*p != NUL)
19460 {
19461#ifdef FEAT_MBYTE
19462 int l;
19463
19464 if (enc_utf8)
19465 {
19466 int c, lc;
19467
19468 c = utf_ptr2char(p);
19469 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019470 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 /* TODO: reallocate string when byte count changes. */
19472 if (utf_char2len(lc) == l)
19473 utf_char2bytes(lc, p);
19474 p += l;
19475 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019476 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 p += l; /* skip multi-byte character */
19478 else
19479#endif
19480 {
19481 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19482 ++p;
19483 }
19484 }
19485}
19486
19487/*
19488 * "toupper(string)" function
19489 */
19490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019491f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019493 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019494 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495}
19496
19497/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019498 * "tr(string, fromstr, tostr)" function
19499 */
19500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019501f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019502{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019503 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019504 char_u *fromstr;
19505 char_u *tostr;
19506 char_u *p;
19507#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019508 int inlen;
19509 int fromlen;
19510 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019511 int idx;
19512 char_u *cpstr;
19513 int cplen;
19514 int first = TRUE;
19515#endif
19516 char_u buf[NUMBUFLEN];
19517 char_u buf2[NUMBUFLEN];
19518 garray_T ga;
19519
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019520 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019521 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19522 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019523
19524 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019525 rettv->v_type = VAR_STRING;
19526 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019527 if (fromstr == NULL || tostr == NULL)
19528 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019529 ga_init2(&ga, (int)sizeof(char), 80);
19530
19531#ifdef FEAT_MBYTE
19532 if (!has_mbyte)
19533#endif
19534 /* not multi-byte: fromstr and tostr must be the same length */
19535 if (STRLEN(fromstr) != STRLEN(tostr))
19536 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019537#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019538error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019539#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019540 EMSG2(_(e_invarg2), fromstr);
19541 ga_clear(&ga);
19542 return;
19543 }
19544
19545 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019546 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019547 {
19548#ifdef FEAT_MBYTE
19549 if (has_mbyte)
19550 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019551 inlen = (*mb_ptr2len)(in_str);
19552 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019553 cplen = inlen;
19554 idx = 0;
19555 for (p = fromstr; *p != NUL; p += fromlen)
19556 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019557 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019558 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019559 {
19560 for (p = tostr; *p != NUL; p += tolen)
19561 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019562 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019563 if (idx-- == 0)
19564 {
19565 cplen = tolen;
19566 cpstr = p;
19567 break;
19568 }
19569 }
19570 if (*p == NUL) /* tostr is shorter than fromstr */
19571 goto error;
19572 break;
19573 }
19574 ++idx;
19575 }
19576
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019577 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019578 {
19579 /* Check that fromstr and tostr have the same number of
19580 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019581 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019582 first = FALSE;
19583 for (p = tostr; *p != NUL; p += tolen)
19584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019585 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019586 --idx;
19587 }
19588 if (idx != 0)
19589 goto error;
19590 }
19591
Bram Moolenaarcde88542015-08-11 19:14:00 +020019592 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019593 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019594 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019595
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019596 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019597 }
19598 else
19599#endif
19600 {
19601 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019602 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019603 if (p != NULL)
19604 ga_append(&ga, tostr[p - fromstr]);
19605 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019606 ga_append(&ga, *in_str);
19607 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019608 }
19609 }
19610
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019611 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019612 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019613 ga_append(&ga, NUL);
19614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019616}
19617
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019618#ifdef FEAT_FLOAT
19619/*
19620 * "trunc({float})" function
19621 */
19622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019623f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019624{
19625 float_T f;
19626
19627 rettv->v_type = VAR_FLOAT;
19628 if (get_float_arg(argvars, &f) == OK)
19629 /* trunc() is not in C90, use floor() or ceil() instead. */
19630 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19631 else
19632 rettv->vval.v_float = 0.0;
19633}
19634#endif
19635
Bram Moolenaar8299df92004-07-10 09:47:34 +000019636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 * "type(expr)" function
19638 */
19639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019642 int n;
19643
19644 switch (argvars[0].v_type)
19645 {
19646 case VAR_NUMBER: n = 0; break;
19647 case VAR_STRING: n = 1; break;
19648 case VAR_FUNC: n = 2; break;
19649 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019650 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019651#ifdef FEAT_FLOAT
19652 case VAR_FLOAT: n = 5; break;
19653#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019654 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019655 if (argvars[0].vval.v_number == VVAL_FALSE
19656 || argvars[0].vval.v_number == VVAL_TRUE)
19657 n = 6;
19658 else
19659 n = 7;
19660 break;
19661 case VAR_UNKNOWN:
19662 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19663 n = -1;
19664 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019665 }
19666 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667}
19668
19669/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019670 * "undofile(name)" function
19671 */
19672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019673f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019674{
19675 rettv->v_type = VAR_STRING;
19676#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019677 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019678 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019679
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019680 if (*fname == NUL)
19681 {
19682 /* If there is no file name there will be no undo file. */
19683 rettv->vval.v_string = NULL;
19684 }
19685 else
19686 {
19687 char_u *ffname = FullName_save(fname, FALSE);
19688
19689 if (ffname != NULL)
19690 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19691 vim_free(ffname);
19692 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019693 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019694#else
19695 rettv->vval.v_string = NULL;
19696#endif
19697}
19698
19699/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019700 * "undotree()" function
19701 */
19702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019703f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019704{
19705 if (rettv_dict_alloc(rettv) == OK)
19706 {
19707 dict_T *dict = rettv->vval.v_dict;
19708 list_T *list;
19709
Bram Moolenaar730cde92010-06-27 05:18:54 +020019710 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019711 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019712 dict_add_nr_str(dict, "save_last",
19713 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019714 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19715 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019716 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019717
19718 list = list_alloc();
19719 if (list != NULL)
19720 {
19721 u_eval_tree(curbuf->b_u_oldhead, list);
19722 dict_add_list(dict, "entries", list);
19723 }
19724 }
19725}
19726
19727/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019728 * "values(dict)" function
19729 */
19730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019732{
19733 dict_list(argvars, rettv, 1);
19734}
19735
19736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 * "virtcol(string)" function
19738 */
19739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019740f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741{
19742 colnr_T vcol = 0;
19743 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019744 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019746 fp = var2fpos(&argvars[0], FALSE, &fnum);
19747 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19748 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 {
19750 getvvcol(curwin, fp, NULL, NULL, &vcol);
19751 ++vcol;
19752 }
19753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755}
19756
19757/*
19758 * "visualmode()" function
19759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019761f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 char_u str[2];
19764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 str[0] = curbuf->b_visual_mode_eval;
19767 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769
19770 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019771 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
19775/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019776 * "wildmenumode()" function
19777 */
19778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019779f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019780{
19781#ifdef FEAT_WILDMENU
19782 if (wild_menu_showing)
19783 rettv->vval.v_number = 1;
19784#endif
19785}
19786
19787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 * "winbufnr(nr)" function
19789 */
19790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019791f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792{
19793 win_T *wp;
19794
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019795 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800}
19801
19802/*
19803 * "wincol()" function
19804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019806f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807{
19808 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810}
19811
19812/*
19813 * "winheight(nr)" function
19814 */
19815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019816f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817{
19818 win_T *wp;
19819
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019820 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825}
19826
19827/*
19828 * "winline()" function
19829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019831f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832{
19833 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835}
19836
19837/*
19838 * "winnr()" function
19839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019841f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842{
19843 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019844
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019846 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849}
19850
19851/*
19852 * "winrestcmd()" function
19853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019855f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856{
19857#ifdef FEAT_WINDOWS
19858 win_T *wp;
19859 int winnr = 1;
19860 garray_T ga;
19861 char_u buf[50];
19862
19863 ga_init2(&ga, (int)sizeof(char), 70);
19864 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19865 {
19866 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19867 ga_concat(&ga, buf);
19868# ifdef FEAT_VERTSPLIT
19869 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19870 ga_concat(&ga, buf);
19871# endif
19872 ++winnr;
19873 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019874 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019876 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019880 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881}
19882
19883/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019884 * "winrestview()" function
19885 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019887f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019888{
19889 dict_T *dict;
19890
19891 if (argvars[0].v_type != VAR_DICT
19892 || (dict = argvars[0].vval.v_dict) == NULL)
19893 EMSG(_(e_invarg));
19894 else
19895 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019896 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19897 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19898 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19899 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019900#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019901 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19902 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019903#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019904 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19905 {
19906 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19907 curwin->w_set_curswant = FALSE;
19908 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909
Bram Moolenaar82c25852014-05-28 16:47:16 +020019910 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19911 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019912#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019913 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19914 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019915#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019916 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19917 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19918 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19919 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019920
19921 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019922 win_new_height(curwin, curwin->w_height);
19923# ifdef FEAT_VERTSPLIT
19924 win_new_width(curwin, W_WIDTH(curwin));
19925# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019926 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019927
Bram Moolenaarb851a962014-10-31 15:45:52 +010019928 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019929 curwin->w_topline = 1;
19930 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19931 curwin->w_topline = curbuf->b_ml.ml_line_count;
19932#ifdef FEAT_DIFF
19933 check_topfill(curwin, TRUE);
19934#endif
19935 }
19936}
19937
19938/*
19939 * "winsaveview()" function
19940 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019942f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019943{
19944 dict_T *dict;
19945
Bram Moolenaara800b422010-06-27 01:15:55 +020019946 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019947 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019948 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019949
19950 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19951 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19952#ifdef FEAT_VIRTUALEDIT
19953 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19954#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019955 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019956 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19957
19958 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19959#ifdef FEAT_DIFF
19960 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19961#endif
19962 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19963 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19964}
19965
19966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 * "winwidth(nr)" function
19968 */
19969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019970f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
19972 win_T *wp;
19973
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019974 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 else
19978#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019979 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982#endif
19983}
19984
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019986 * "wordcount()" function
19987 */
19988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019989f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019990{
19991 if (rettv_dict_alloc(rettv) == FAIL)
19992 return;
19993 cursor_pos_info(rettv->vval.v_dict);
19994}
19995
19996/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019997 * Write list of strings to file
19998 */
19999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020000write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020001{
20002 listitem_T *li;
20003 int c;
20004 int ret = OK;
20005 char_u *s;
20006
20007 for (li = list->lv_first; li != NULL; li = li->li_next)
20008 {
20009 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20010 {
20011 if (*s == '\n')
20012 c = putc(NUL, fd);
20013 else
20014 c = putc(*s, fd);
20015 if (c == EOF)
20016 {
20017 ret = FAIL;
20018 break;
20019 }
20020 }
20021 if (!binary || li->li_next != NULL)
20022 if (putc('\n', fd) == EOF)
20023 {
20024 ret = FAIL;
20025 break;
20026 }
20027 if (ret == FAIL)
20028 {
20029 EMSG(_(e_write));
20030 break;
20031 }
20032 }
20033 return ret;
20034}
20035
20036/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020037 * "writefile()" function
20038 */
20039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020040f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020041{
20042 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020043 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020044 char_u *fname;
20045 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020046 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020047
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020048 if (check_restricted() || check_secure())
20049 return;
20050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020051 if (argvars[0].v_type != VAR_LIST)
20052 {
20053 EMSG2(_(e_listarg), "writefile()");
20054 return;
20055 }
20056 if (argvars[0].vval.v_list == NULL)
20057 return;
20058
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020059 if (argvars[2].v_type != VAR_UNKNOWN)
20060 {
20061 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20062 binary = TRUE;
20063 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20064 append = TRUE;
20065 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020066
20067 /* Always open the file in binary mode, library functions have a mind of
20068 * their own about CR-LF conversion. */
20069 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020070 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20071 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020072 {
20073 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20074 ret = -1;
20075 }
20076 else
20077 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020078 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20079 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020080 fclose(fd);
20081 }
20082
20083 rettv->vval.v_number = ret;
20084}
20085
20086/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020087 * "xor(expr, expr)" function
20088 */
20089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020090f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020091{
20092 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20093 ^ get_tv_number_chk(&argvars[1], NULL);
20094}
20095
20096
20097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020099 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 */
20101 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020102var2fpos(
20103 typval_T *varp,
20104 int dollar_lnum, /* TRUE when $ is last line */
20105 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020107 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020109 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110
Bram Moolenaara5525202006-03-02 22:52:09 +000020111 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020112 if (varp->v_type == VAR_LIST)
20113 {
20114 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020115 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020116 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020117 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020118
20119 l = varp->vval.v_list;
20120 if (l == NULL)
20121 return NULL;
20122
20123 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020124 pos.lnum = list_find_nr(l, 0L, &error);
20125 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020126 return NULL; /* invalid line number */
20127
20128 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020129 pos.col = list_find_nr(l, 1L, &error);
20130 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020131 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020132 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020133
20134 /* We accept "$" for the column number: last column. */
20135 li = list_find(l, 1L);
20136 if (li != NULL && li->li_tv.v_type == VAR_STRING
20137 && li->li_tv.vval.v_string != NULL
20138 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20139 pos.col = len + 1;
20140
Bram Moolenaara5525202006-03-02 22:52:09 +000020141 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020142 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020143 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020144 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020145
Bram Moolenaara5525202006-03-02 22:52:09 +000020146#ifdef FEAT_VIRTUALEDIT
20147 /* Get the virtual offset. Defaults to zero. */
20148 pos.coladd = list_find_nr(l, 2L, &error);
20149 if (error)
20150 pos.coladd = 0;
20151#endif
20152
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020153 return &pos;
20154 }
20155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020156 name = get_tv_string_chk(varp);
20157 if (name == NULL)
20158 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020159 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020161 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20162 {
20163 if (VIsual_active)
20164 return &VIsual;
20165 return &curwin->w_cursor;
20166 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020167 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020169 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20171 return NULL;
20172 return pp;
20173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020174
20175#ifdef FEAT_VIRTUALEDIT
20176 pos.coladd = 0;
20177#endif
20178
Bram Moolenaar477933c2007-07-17 14:32:23 +000020179 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020180 {
20181 pos.col = 0;
20182 if (name[1] == '0') /* "w0": first visible line */
20183 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020184 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020185 pos.lnum = curwin->w_topline;
20186 return &pos;
20187 }
20188 else if (name[1] == '$') /* "w$": last visible line */
20189 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020190 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020191 pos.lnum = curwin->w_botline - 1;
20192 return &pos;
20193 }
20194 }
20195 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020197 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 {
20199 pos.lnum = curbuf->b_ml.ml_line_count;
20200 pos.col = 0;
20201 }
20202 else
20203 {
20204 pos.lnum = curwin->w_cursor.lnum;
20205 pos.col = (colnr_T)STRLEN(ml_get_curline());
20206 }
20207 return &pos;
20208 }
20209 return NULL;
20210}
20211
20212/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020213 * Convert list in "arg" into a position and optional file number.
20214 * When "fnump" is NULL there is no file number, only 3 items.
20215 * Note that the column is passed on as-is, the caller may want to decrement
20216 * it to use 1 for the first column.
20217 * Return FAIL when conversion is not possible, doesn't check the position for
20218 * validity.
20219 */
20220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020221list2fpos(
20222 typval_T *arg,
20223 pos_T *posp,
20224 int *fnump,
20225 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020226{
20227 list_T *l = arg->vval.v_list;
20228 long i = 0;
20229 long n;
20230
Bram Moolenaar493c1782014-05-28 14:34:46 +020020231 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20232 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020233 if (arg->v_type != VAR_LIST
20234 || l == NULL
20235 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020236 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020237 return FAIL;
20238
20239 if (fnump != NULL)
20240 {
20241 n = list_find_nr(l, i++, NULL); /* fnum */
20242 if (n < 0)
20243 return FAIL;
20244 if (n == 0)
20245 n = curbuf->b_fnum; /* current buffer */
20246 *fnump = n;
20247 }
20248
20249 n = list_find_nr(l, i++, NULL); /* lnum */
20250 if (n < 0)
20251 return FAIL;
20252 posp->lnum = n;
20253
20254 n = list_find_nr(l, i++, NULL); /* col */
20255 if (n < 0)
20256 return FAIL;
20257 posp->col = n;
20258
20259#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020260 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020261 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020262 posp->coladd = 0;
20263 else
20264 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020265#endif
20266
Bram Moolenaar493c1782014-05-28 14:34:46 +020020267 if (curswantp != NULL)
20268 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20269
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020270 return OK;
20271}
20272
20273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 * Get the length of an environment variable name.
20275 * Advance "arg" to the first character after the name.
20276 * Return 0 for error.
20277 */
20278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020279get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
20281 char_u *p;
20282 int len;
20283
20284 for (p = *arg; vim_isIDc(*p); ++p)
20285 ;
20286 if (p == *arg) /* no name found */
20287 return 0;
20288
20289 len = (int)(p - *arg);
20290 *arg = p;
20291 return len;
20292}
20293
20294/*
20295 * Get the length of the name of a function or internal variable.
20296 * "arg" is advanced to the first non-white character after the name.
20297 * Return 0 if something is wrong.
20298 */
20299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020300get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301{
20302 char_u *p;
20303 int len;
20304
20305 /* Find the end of the name. */
20306 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020307 {
20308 if (*p == ':')
20309 {
20310 /* "s:" is start of "s:var", but "n:" is not and can be used in
20311 * slice "[n:]". Also "xx:" is not a namespace. */
20312 len = (int)(p - *arg);
20313 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20314 || len > 1)
20315 break;
20316 }
20317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 if (p == *arg) /* no name found */
20319 return 0;
20320
20321 len = (int)(p - *arg);
20322 *arg = skipwhite(p);
20323
20324 return len;
20325}
20326
20327/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020328 * Get the length of the name of a variable or function.
20329 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020331 * Return -1 if curly braces expansion failed.
20332 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 * If the name contains 'magic' {}'s, expand them and return the
20334 * expanded name in an allocated string via 'alias' - caller must free.
20335 */
20336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020337get_name_len(
20338 char_u **arg,
20339 char_u **alias,
20340 int evaluate,
20341 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342{
20343 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 char_u *p;
20345 char_u *expr_start;
20346 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347
20348 *alias = NULL; /* default to no alias */
20349
20350 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20351 && (*arg)[2] == (int)KE_SNR)
20352 {
20353 /* hard coded <SNR>, already translated */
20354 *arg += 3;
20355 return get_id_len(arg) + 3;
20356 }
20357 len = eval_fname_script(*arg);
20358 if (len > 0)
20359 {
20360 /* literal "<SID>", "s:" or "<SNR>" */
20361 *arg += len;
20362 }
20363
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020367 p = find_name_end(*arg, &expr_start, &expr_end,
20368 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 if (expr_start != NULL)
20370 {
20371 char_u *temp_string;
20372
20373 if (!evaluate)
20374 {
20375 len += (int)(p - *arg);
20376 *arg = skipwhite(p);
20377 return len;
20378 }
20379
20380 /*
20381 * Include any <SID> etc in the expanded string:
20382 * Thus the -len here.
20383 */
20384 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20385 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020386 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 *alias = temp_string;
20388 *arg = skipwhite(p);
20389 return (int)STRLEN(temp_string);
20390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391
20392 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020393 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 EMSG2(_(e_invexpr2), *arg);
20395
20396 return len;
20397}
20398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020399/*
20400 * Find the end of a variable or function name, taking care of magic braces.
20401 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20402 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020403 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 * Return a pointer to just after the name. Equal to "arg" if there is no
20405 * valid name.
20406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020408find_name_end(
20409 char_u *arg,
20410 char_u **expr_start,
20411 char_u **expr_end,
20412 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414 int mb_nest = 0;
20415 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020417 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020421 *expr_start = NULL;
20422 *expr_end = NULL;
20423 }
20424
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020425 /* Quick check for valid starting character. */
20426 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20427 return arg;
20428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 for (p = arg; *p != NUL
20430 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020431 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020432 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020434 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020436 if (*p == '\'')
20437 {
20438 /* skip over 'string' to avoid counting [ and ] inside it. */
20439 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20440 ;
20441 if (*p == NUL)
20442 break;
20443 }
20444 else if (*p == '"')
20445 {
20446 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20447 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20448 if (*p == '\\' && p[1] != NUL)
20449 ++p;
20450 if (*p == NUL)
20451 break;
20452 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020453 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20454 {
20455 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020456 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020457 len = (int)(p - arg);
20458 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020459 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020460 break;
20461 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020465 if (*p == '[')
20466 ++br_nest;
20467 else if (*p == ']')
20468 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020471 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 if (*p == '{')
20474 {
20475 mb_nest++;
20476 if (expr_start != NULL && *expr_start == NULL)
20477 *expr_start = p;
20478 }
20479 else if (*p == '}')
20480 {
20481 mb_nest--;
20482 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20483 *expr_end = p;
20484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 }
20487
20488 return p;
20489}
20490
20491/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020492 * Expands out the 'magic' {}'s in a variable/function name.
20493 * Note that this can call itself recursively, to deal with
20494 * constructs like foo{bar}{baz}{bam}
20495 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20496 * "in_start" ^
20497 * "expr_start" ^
20498 * "expr_end" ^
20499 * "in_end" ^
20500 *
20501 * Returns a new allocated string, which the caller must free.
20502 * Returns NULL for failure.
20503 */
20504 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020505make_expanded_name(
20506 char_u *in_start,
20507 char_u *expr_start,
20508 char_u *expr_end,
20509 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020510{
20511 char_u c1;
20512 char_u *retval = NULL;
20513 char_u *temp_result;
20514 char_u *nextcmd = NULL;
20515
20516 if (expr_end == NULL || in_end == NULL)
20517 return NULL;
20518 *expr_start = NUL;
20519 *expr_end = NUL;
20520 c1 = *in_end;
20521 *in_end = NUL;
20522
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020523 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020524 if (temp_result != NULL && nextcmd == NULL)
20525 {
20526 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20527 + (in_end - expr_end) + 1));
20528 if (retval != NULL)
20529 {
20530 STRCPY(retval, in_start);
20531 STRCAT(retval, temp_result);
20532 STRCAT(retval, expr_end + 1);
20533 }
20534 }
20535 vim_free(temp_result);
20536
20537 *in_end = c1; /* put char back for error messages */
20538 *expr_start = '{';
20539 *expr_end = '}';
20540
20541 if (retval != NULL)
20542 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020543 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020544 if (expr_start != NULL)
20545 {
20546 /* Further expansion! */
20547 temp_result = make_expanded_name(retval, expr_start,
20548 expr_end, temp_result);
20549 vim_free(retval);
20550 retval = temp_result;
20551 }
20552 }
20553
20554 return retval;
20555}
20556
20557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020559 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 */
20561 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020562eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020564 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20565}
20566
20567/*
20568 * Return TRUE if character "c" can be used as the first character in a
20569 * variable or function name (excluding '{' and '}').
20570 */
20571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020572eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020573{
20574 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575}
20576
20577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 * Set number v: variable to "val".
20579 */
20580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020581set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020583 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584}
20585
20586/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020587 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 */
20589 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020590get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020592 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593}
20594
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020595/*
20596 * Get string v: variable value. Uses a static buffer, can only be used once.
20597 */
20598 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020599get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020600{
20601 return get_tv_string(&vimvars[idx].vv_tv);
20602}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020603
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020605 * Get List v: variable value. Caller must take care of reference count when
20606 * needed.
20607 */
20608 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020610{
20611 return vimvars[idx].vv_list;
20612}
20613
20614/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020615 * Set v:char to character "c".
20616 */
20617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020618set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020619{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020620 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020621
20622#ifdef FEAT_MBYTE
20623 if (has_mbyte)
20624 buf[(*mb_char2bytes)(c, buf)] = NUL;
20625 else
20626#endif
20627 {
20628 buf[0] = c;
20629 buf[1] = NUL;
20630 }
20631 set_vim_var_string(VV_CHAR, buf, -1);
20632}
20633
20634/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020635 * Set v:count to "count" and v:count1 to "count1".
20636 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 */
20638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020639set_vcount(
20640 long count,
20641 long count1,
20642 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020644 if (set_prevcount)
20645 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020646 vimvars[VV_COUNT].vv_nr = count;
20647 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648}
20649
20650/*
20651 * Set string v: variable to a copy of "val".
20652 */
20653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654set_vim_var_string(
20655 int idx,
20656 char_u *val,
20657 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
Bram Moolenaara542c682016-01-31 16:28:04 +010020659 clear_tv(&vimvars[idx].vv_di.di_tv);
20660 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020662 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020664 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020666 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667}
20668
20669/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020670 * Set List v: variable to "val".
20671 */
20672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020673set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020674{
Bram Moolenaara542c682016-01-31 16:28:04 +010020675 clear_tv(&vimvars[idx].vv_di.di_tv);
20676 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020677 vimvars[idx].vv_list = val;
20678 if (val != NULL)
20679 ++val->lv_refcount;
20680}
20681
20682/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020683 * Set Dictionary v: variable to "val".
20684 */
20685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020686set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020687{
20688 int todo;
20689 hashitem_T *hi;
20690
Bram Moolenaara542c682016-01-31 16:28:04 +010020691 clear_tv(&vimvars[idx].vv_di.di_tv);
20692 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020693 vimvars[idx].vv_dict = val;
20694 if (val != NULL)
20695 {
20696 ++val->dv_refcount;
20697
20698 /* Set readonly */
20699 todo = (int)val->dv_hashtab.ht_used;
20700 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20701 {
20702 if (HASHITEM_EMPTY(hi))
20703 continue;
20704 --todo;
20705 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20706 }
20707 }
20708}
20709
20710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 * Set v:register if needed.
20712 */
20713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020714set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715{
20716 char_u regname;
20717
20718 if (c == 0 || c == ' ')
20719 regname = '"';
20720 else
20721 regname = c;
20722 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020723 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 set_vim_var_string(VV_REG, &regname, 1);
20725}
20726
20727/*
20728 * Get or set v:exception. If "oldval" == NULL, return the current value.
20729 * Otherwise, restore the value to "oldval" and return NULL.
20730 * Must always be called in pairs to save and restore v:exception! Does not
20731 * take care of memory allocations.
20732 */
20733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020734v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735{
20736 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020737 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738
Bram Moolenaare9a41262005-01-15 22:18:47 +000020739 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 return NULL;
20741}
20742
20743/*
20744 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20745 * Otherwise, restore the value to "oldval" and return NULL.
20746 * Must always be called in pairs to save and restore v:throwpoint! Does not
20747 * take care of memory allocations.
20748 */
20749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020750v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751{
20752 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020753 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754
Bram Moolenaare9a41262005-01-15 22:18:47 +000020755 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 return NULL;
20757}
20758
20759#if defined(FEAT_AUTOCMD) || defined(PROTO)
20760/*
20761 * Set v:cmdarg.
20762 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20763 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20764 * Must always be called in pairs!
20765 */
20766 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020767set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768{
20769 char_u *oldval;
20770 char_u *newval;
20771 unsigned len;
20772
Bram Moolenaare9a41262005-01-15 22:18:47 +000020773 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020774 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020776 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020777 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020778 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 }
20780
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020781 if (eap->force_bin == FORCE_BIN)
20782 len = 6;
20783 else if (eap->force_bin == FORCE_NOBIN)
20784 len = 8;
20785 else
20786 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020787
20788 if (eap->read_edit)
20789 len += 7;
20790
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020791 if (eap->force_ff != 0)
20792 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20793# ifdef FEAT_MBYTE
20794 if (eap->force_enc != 0)
20795 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020796 if (eap->bad_char != 0)
20797 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020798# endif
20799
20800 newval = alloc(len + 1);
20801 if (newval == NULL)
20802 return NULL;
20803
20804 if (eap->force_bin == FORCE_BIN)
20805 sprintf((char *)newval, " ++bin");
20806 else if (eap->force_bin == FORCE_NOBIN)
20807 sprintf((char *)newval, " ++nobin");
20808 else
20809 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020810
20811 if (eap->read_edit)
20812 STRCAT(newval, " ++edit");
20813
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020814 if (eap->force_ff != 0)
20815 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20816 eap->cmd + eap->force_ff);
20817# ifdef FEAT_MBYTE
20818 if (eap->force_enc != 0)
20819 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20820 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020821 if (eap->bad_char == BAD_KEEP)
20822 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20823 else if (eap->bad_char == BAD_DROP)
20824 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20825 else if (eap->bad_char != 0)
20826 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020827# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020828 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020829 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830}
20831#endif
20832
20833/*
20834 * Get the value of internal variable "name".
20835 * Return OK or FAIL.
20836 */
20837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020838get_var_tv(
20839 char_u *name,
20840 int len, /* length of "name" */
20841 typval_T *rettv, /* NULL when only checking existence */
20842 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20843 int verbose, /* may give error message */
20844 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
20846 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020847 typval_T *tv = NULL;
20848 typval_T atv;
20849 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851
20852 /* truncate the name, so that we can use strcmp() */
20853 cc = name[len];
20854 name[len] = NUL;
20855
20856 /*
20857 * Check for "b:changedtick".
20858 */
20859 if (STRCMP(name, "b:changedtick") == 0)
20860 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020861 atv.v_type = VAR_NUMBER;
20862 atv.vval.v_number = curbuf->b_changedtick;
20863 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 }
20865
20866 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 * Check for user-defined variables.
20868 */
20869 else
20870 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020871 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020873 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020874 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020875 if (dip != NULL)
20876 *dip = v;
20877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 }
20879
Bram Moolenaare9a41262005-01-15 22:18:47 +000020880 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020882 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 ret = FAIL;
20885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020886 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020887 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888
20889 name[len] = cc;
20890
20891 return ret;
20892}
20893
20894/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020895 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20896 * Also handle function call with Funcref variable: func(expr)
20897 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20898 */
20899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020900handle_subscript(
20901 char_u **arg,
20902 typval_T *rettv,
20903 int evaluate, /* do more than finding the end */
20904 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020905{
20906 int ret = OK;
20907 dict_T *selfdict = NULL;
20908 char_u *s;
20909 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020910 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020911
20912 while (ret == OK
20913 && (**arg == '['
20914 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020915 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020916 && !vim_iswhite(*(*arg - 1)))
20917 {
20918 if (**arg == '(')
20919 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020920 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020921 if (evaluate)
20922 {
20923 functv = *rettv;
20924 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020925
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020926 /* Invoke the function. Recursive! */
20927 s = functv.vval.v_string;
20928 }
20929 else
20930 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020931 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020932 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20933 &len, evaluate, selfdict);
20934
20935 /* Clear the funcref afterwards, so that deleting it while
20936 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020937 if (evaluate)
20938 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020939
20940 /* Stop the expression evaluation when immediately aborting on
20941 * error, or when an interrupt occurred or an exception was thrown
20942 * but not caught. */
20943 if (aborting())
20944 {
20945 if (ret == OK)
20946 clear_tv(rettv);
20947 ret = FAIL;
20948 }
20949 dict_unref(selfdict);
20950 selfdict = NULL;
20951 }
20952 else /* **arg == '[' || **arg == '.' */
20953 {
20954 dict_unref(selfdict);
20955 if (rettv->v_type == VAR_DICT)
20956 {
20957 selfdict = rettv->vval.v_dict;
20958 if (selfdict != NULL)
20959 ++selfdict->dv_refcount;
20960 }
20961 else
20962 selfdict = NULL;
20963 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20964 {
20965 clear_tv(rettv);
20966 ret = FAIL;
20967 }
20968 }
20969 }
20970 dict_unref(selfdict);
20971 return ret;
20972}
20973
20974/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020975 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020976 * value).
20977 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020978 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020979alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020980{
Bram Moolenaar33570922005-01-25 22:26:29 +000020981 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982}
20983
20984/*
20985 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 * The string "s" must have been allocated, it is consumed.
20987 * Return NULL for out of memory, the variable otherwise.
20988 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020989 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020990alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991{
Bram Moolenaar33570922005-01-25 22:26:29 +000020992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 rettv = alloc_tv();
20995 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020997 rettv->v_type = VAR_STRING;
20998 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 }
21000 else
21001 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021002 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003}
21004
21005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
21011 if (varp != NULL)
21012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021013 switch (varp->v_type)
21014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021015 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 func_unref(varp->vval.v_string);
21017 /*FALLTHROUGH*/
21018 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021019 vim_free(varp->vval.v_string);
21020 break;
21021 case VAR_LIST:
21022 list_unref(varp->vval.v_list);
21023 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021024 case VAR_DICT:
21025 dict_unref(varp->vval.v_dict);
21026 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021027 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021028#ifdef FEAT_FLOAT
21029 case VAR_FLOAT:
21030#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021031 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021032 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021033 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 vim_free(varp);
21036 }
21037}
21038
21039/*
21040 * Free the memory for a variable value and set the value to NULL or 0.
21041 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021043clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044{
21045 if (varp != NULL)
21046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021047 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021049 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 func_unref(varp->vval.v_string);
21051 /*FALLTHROUGH*/
21052 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021053 vim_free(varp->vval.v_string);
21054 varp->vval.v_string = NULL;
21055 break;
21056 case VAR_LIST:
21057 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021058 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021059 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021060 case VAR_DICT:
21061 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021062 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021063 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021064 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021065 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021066 varp->vval.v_number = 0;
21067 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021068#ifdef FEAT_FLOAT
21069 case VAR_FLOAT:
21070 varp->vval.v_float = 0.0;
21071 break;
21072#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021073 case VAR_UNKNOWN:
21074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021076 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 }
21078}
21079
21080/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021081 * Set the value of a variable to NULL without freeing items.
21082 */
21083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021084init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021085{
21086 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021087 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021088}
21089
21090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 * Get the number value of a variable.
21092 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021093 * For incompatible types, return 0.
21094 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21095 * caller of incompatible types: it sets *denote to TRUE if "denote"
21096 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 */
21098 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021099get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021101 int error = FALSE;
21102
21103 return get_tv_number_chk(varp, &error); /* return 0L on error */
21104}
21105
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021106 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021107get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021108{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021109 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021113 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021114 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021115#ifdef FEAT_FLOAT
21116 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021117 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021118 break;
21119#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021120 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021121 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021122 break;
21123 case VAR_STRING:
21124 if (varp->vval.v_string != NULL)
21125 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021126 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021127 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021128 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021129 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021130 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021131 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021132 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021133 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021134 case VAR_SPECIAL:
21135 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21136 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021137 case VAR_UNKNOWN:
21138 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021141 if (denote == NULL) /* useful for values that must be unsigned */
21142 n = -1;
21143 else
21144 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021145 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146}
21147
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021148#ifdef FEAT_FLOAT
21149 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021150get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021151{
21152 switch (varp->v_type)
21153 {
21154 case VAR_NUMBER:
21155 return (float_T)(varp->vval.v_number);
21156#ifdef FEAT_FLOAT
21157 case VAR_FLOAT:
21158 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021159#endif
21160 case VAR_FUNC:
21161 EMSG(_("E891: Using a Funcref as a Float"));
21162 break;
21163 case VAR_STRING:
21164 EMSG(_("E892: Using a String as a Float"));
21165 break;
21166 case VAR_LIST:
21167 EMSG(_("E893: Using a List as a Float"));
21168 break;
21169 case VAR_DICT:
21170 EMSG(_("E894: Using a Dictionary as a Float"));
21171 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021172 case VAR_SPECIAL:
21173 EMSG(_("E907: Using a special value as a Float"));
21174 break;
21175 case VAR_UNKNOWN:
21176 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021177 break;
21178 }
21179 return 0;
21180}
21181#endif
21182
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021184 * Get the lnum from the first argument.
21185 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021186 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 */
21188 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190{
Bram Moolenaar33570922005-01-25 22:26:29 +000021191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 linenr_T lnum;
21193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021194 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 if (lnum == 0) /* no valid number, try using line() */
21196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197 rettv.v_type = VAR_NUMBER;
21198 f_line(argvars, &rettv);
21199 lnum = rettv.vval.v_number;
21200 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 }
21202 return lnum;
21203}
21204
21205/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021206 * Get the lnum from the first argument.
21207 * Also accepts "$", then "buf" is used.
21208 * Returns 0 on error.
21209 */
21210 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021212{
21213 if (argvars[0].v_type == VAR_STRING
21214 && argvars[0].vval.v_string != NULL
21215 && argvars[0].vval.v_string[0] == '$'
21216 && buf != NULL)
21217 return buf->b_ml.ml_line_count;
21218 return get_tv_number_chk(&argvars[0], NULL);
21219}
21220
21221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 * Get the string value of a variable.
21223 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021224 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21225 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 * If the String variable has never been set, return an empty string.
21227 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021228 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21229 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 */
21231 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233{
21234 static char_u mybuf[NUMBUFLEN];
21235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021240get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021242 char_u *res = get_tv_string_buf_chk(varp, buf);
21243
21244 return res != NULL ? res : (char_u *)"";
21245}
21246
Bram Moolenaar7d647822014-04-05 21:28:56 +020021247/*
21248 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21249 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021250 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021251get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021252{
21253 static char_u mybuf[NUMBUFLEN];
21254
21255 return get_tv_string_buf_chk(varp, mybuf);
21256}
21257
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021258 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021259get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021260{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021261 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021263 case VAR_NUMBER:
21264 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21265 return buf;
21266 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021267 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 break;
21269 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021270 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021271 break;
21272 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021273 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021274 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021275#ifdef FEAT_FLOAT
21276 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021277 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021278 break;
21279#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021280 case VAR_STRING:
21281 if (varp->vval.v_string != NULL)
21282 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021283 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021284 case VAR_SPECIAL:
21285 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21286 return buf;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021287 case VAR_UNKNOWN:
21288 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021291 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292}
21293
21294/*
21295 * Find variable "name" in the list of variables.
21296 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021298 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021302find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306
Bram Moolenaara7043832005-01-21 11:56:39 +000021307 ht = find_var_ht(name, &varname);
21308 if (htp != NULL)
21309 *htp = ht;
21310 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021312 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313}
21314
21315/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021316 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021317 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021319 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021320find_var_in_ht(
21321 hashtab_T *ht,
21322 int htname,
21323 char_u *varname,
21324 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021325{
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 hashitem_T *hi;
21327
21328 if (*varname == NUL)
21329 {
21330 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021331 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021333 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 case 'g': return &globvars_var;
21335 case 'v': return &vimvars_var;
21336 case 'b': return &curbuf->b_bufvar;
21337 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021338#ifdef FEAT_WINDOWS
21339 case 't': return &curtab->tp_winvar;
21340#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021341 case 'l': return current_funccal == NULL
21342 ? NULL : &current_funccal->l_vars_var;
21343 case 'a': return current_funccal == NULL
21344 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 }
21346 return NULL;
21347 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021348
21349 hi = hash_find(ht, varname);
21350 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 {
21352 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021353 * worked find the variable again. Don't auto-load a script if it was
21354 * loaded already, otherwise it would be loaded every time when
21355 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021356 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021357 {
21358 /* Note: script_autoload() may make "hi" invalid. It must either
21359 * be obtained again or not used. */
21360 if (!script_autoload(varname, FALSE) || aborting())
21361 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021362 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021363 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 if (HASHITEM_EMPTY(hi))
21365 return NULL;
21366 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021367 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021368}
21369
21370/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021372 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021373 * Set "varname" to the start of name without ':'.
21374 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021375 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021378 hashitem_T *hi;
21379
Bram Moolenaar73627d02015-08-11 15:46:09 +020021380 if (name[0] == NUL)
21381 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 if (name[1] != ':')
21383 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021384 /* The name must not start with a colon or #. */
21385 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 return NULL;
21387 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021388
21389 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021390 hi = hash_find(&compat_hashtab, name);
21391 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021392 return &compat_hashtab;
21393
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021395 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021396 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 }
21398 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021399 if (*name == 'g') /* global variable */
21400 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021401 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21402 */
21403 if (vim_strchr(name + 2, ':') != NULL
21404 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021407 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021409 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021410#ifdef FEAT_WINDOWS
21411 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021412 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021413#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021414 if (*name == 'v') /* v: variable */
21415 return &vimvarht;
21416 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021417 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021418 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021419 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 if (*name == 's' /* script variable */
21421 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21422 return &SCRIPT_VARS(current_SID);
21423 return NULL;
21424}
21425
21426/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021427 * Get function call environment based on bactrace debug level
21428 */
21429 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021430get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021431{
21432 int i;
21433 funccall_T *funccal;
21434 funccall_T *temp_funccal;
21435
21436 funccal = current_funccal;
21437 if (debug_backtrace_level > 0)
21438 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021439 for (i = 0; i < debug_backtrace_level; i++)
21440 {
21441 temp_funccal = funccal->caller;
21442 if (temp_funccal)
21443 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021444 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021445 /* backtrace level overflow. reset to max */
21446 debug_backtrace_level = i;
21447 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021448 }
21449 return funccal;
21450}
21451
21452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021454 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 * Returns NULL when it doesn't exist.
21456 */
21457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021458get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459{
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021462 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 if (v == NULL)
21464 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466}
21467
21468/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 * sourcing this script and when executing functions defined in the script.
21471 */
21472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021473new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474{
Bram Moolenaara7043832005-01-21 11:56:39 +000021475 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 hashtab_T *ht;
21477 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021478
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21480 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021481 /* Re-allocating ga_data means that an ht_array pointing to
21482 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021484 for (i = 1; i <= ga_scripts.ga_len; ++i)
21485 {
21486 ht = &SCRIPT_VARS(i);
21487 if (ht->ht_mask == HT_INIT_SIZE - 1)
21488 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021489 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021491 }
21492
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 while (ga_scripts.ga_len < id)
21494 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021495 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021496 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021497 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 }
21500 }
21501}
21502
21503/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021504 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21505 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 */
21507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
Bram Moolenaar33570922005-01-25 22:26:29 +000021510 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021511 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021512 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021513 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021514 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 dict_var->di_tv.vval.v_dict = dict;
21516 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021517 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21519 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520}
21521
21522/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021523 * Unreference a dictionary initialized by init_var_dict().
21524 */
21525 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021526unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021527{
21528 /* Now the dict needs to be freed if no one else is using it, go back to
21529 * normal reference counting. */
21530 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21531 dict_unref(dict);
21532}
21533
21534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 * Frees all allocated variables and the value they contain.
21537 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 */
21539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021540vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021541{
21542 vars_clear_ext(ht, TRUE);
21543}
21544
21545/*
21546 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21547 */
21548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021549vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550{
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 hashitem_T *hi;
21553 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021556 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021557 for (hi = ht->ht_array; todo > 0; ++hi)
21558 {
21559 if (!HASHITEM_EMPTY(hi))
21560 {
21561 --todo;
21562
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021564 * ht_array might change then. hash_clear() takes care of it
21565 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021566 v = HI2DI(hi);
21567 if (free_val)
21568 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021569 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021570 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021571 }
21572 }
21573 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021574 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575}
21576
Bram Moolenaara7043832005-01-21 11:56:39 +000021577/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021578 * Delete a variable from hashtab "ht" at item "hi".
21579 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021582delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583{
Bram Moolenaar33570922005-01-25 22:26:29 +000021584 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021585
21586 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 clear_tv(&di->di_tv);
21588 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589}
21590
21591/*
21592 * List the value of one internal variable.
21593 */
21594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021595list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597 char_u *tofree;
21598 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021599 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021601 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021602 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021603 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021604 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608list_one_var_a(
21609 char_u *prefix,
21610 char_u *name,
21611 int type,
21612 char_u *string,
21613 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614{
Bram Moolenaar31859182007-08-14 20:41:13 +000021615 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21616 msg_start();
21617 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 if (name != NULL) /* "a:" vars don't have a name stored */
21619 msg_puts(name);
21620 msg_putchar(' ');
21621 msg_advance(22);
21622 if (type == VAR_NUMBER)
21623 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 else if (type == VAR_FUNC)
21625 msg_putchar('*');
21626 else if (type == VAR_LIST)
21627 {
21628 msg_putchar('[');
21629 if (*string == '[')
21630 ++string;
21631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021632 else if (type == VAR_DICT)
21633 {
21634 msg_putchar('{');
21635 if (*string == '{')
21636 ++string;
21637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 else
21639 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021642
21643 if (type == VAR_FUNC)
21644 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021645 if (*first)
21646 {
21647 msg_clr_eos();
21648 *first = FALSE;
21649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650}
21651
21652/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021653 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 * If the variable already exists, the value is updated.
21655 * Otherwise the variable is created.
21656 */
21657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021658set_var(
21659 char_u *name,
21660 typval_T *tv,
21661 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662{
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021667 ht = find_var_ht(name, &varname);
21668 if (ht == NULL || *varname == NUL)
21669 {
21670 EMSG2(_(e_illvar), name);
21671 return;
21672 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021673 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021674
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021675 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21676 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021677
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021681 if (var_check_ro(v->di_flags, name, FALSE)
21682 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 return;
21684 if (v->di_tv.v_type != tv->v_type
21685 && !((v->di_tv.v_type == VAR_STRING
21686 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021687 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021688 || tv->v_type == VAR_NUMBER))
21689#ifdef FEAT_FLOAT
21690 && !((v->di_tv.v_type == VAR_NUMBER
21691 || v->di_tv.v_type == VAR_FLOAT)
21692 && (tv->v_type == VAR_NUMBER
21693 || tv->v_type == VAR_FLOAT))
21694#endif
21695 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021697 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021698 return;
21699 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021700
21701 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021702 * Handle setting internal v: variables separately where needed to
21703 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 */
21705 if (ht == &vimvarht)
21706 {
21707 if (v->di_tv.v_type == VAR_STRING)
21708 {
21709 vim_free(v->di_tv.vval.v_string);
21710 if (copy || tv->v_type != VAR_STRING)
21711 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21712 else
21713 {
21714 /* Take over the string to avoid an extra alloc/free. */
21715 v->di_tv.vval.v_string = tv->vval.v_string;
21716 tv->vval.v_string = NULL;
21717 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021718 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021720 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021721 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021723 if (STRCMP(varname, "searchforward") == 0)
21724 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021725#ifdef FEAT_SEARCH_EXTRA
21726 else if (STRCMP(varname, "hlsearch") == 0)
21727 {
21728 no_hlsearch = !v->di_tv.vval.v_number;
21729 redraw_all_later(SOME_VALID);
21730 }
21731#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021732 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021733 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021734 else if (v->di_tv.v_type != tv->v_type)
21735 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 }
21737
21738 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 }
21740 else /* add a new variable */
21741 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021742 /* Can't add "v:" variable. */
21743 if (ht == &vimvarht)
21744 {
21745 EMSG2(_(e_illvar), name);
21746 return;
21747 }
21748
Bram Moolenaar92124a32005-06-17 22:03:40 +000021749 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021750 if (!valid_varname(varname))
21751 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021752
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021753 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21754 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021755 if (v == NULL)
21756 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021758 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021760 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 return;
21762 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021763 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021765
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021766 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021768 else
21769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021771 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021772 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774}
21775
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021776/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021777 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 * Also give an error message.
21779 */
21780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021781var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021782{
21783 if (flags & DI_FLAGS_RO)
21784 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021785 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 return TRUE;
21787 }
21788 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21789 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021790 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 return TRUE;
21792 }
21793 return FALSE;
21794}
21795
21796/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021797 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21798 * Also give an error message.
21799 */
21800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021801var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021802{
21803 if (flags & DI_FLAGS_FIX)
21804 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021805 EMSG2(_("E795: Cannot delete variable %s"),
21806 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021807 return TRUE;
21808 }
21809 return FALSE;
21810}
21811
21812/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021813 * Check if a funcref is assigned to a valid variable name.
21814 * Return TRUE and give an error if not.
21815 */
21816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021817var_check_func_name(
21818 char_u *name, /* points to start of variable name */
21819 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021820{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021821 /* Allow for w: b: s: and t:. */
21822 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021823 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21824 ? name[2] : name[0]))
21825 {
21826 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21827 name);
21828 return TRUE;
21829 }
21830 /* Don't allow hiding a function. When "v" is not NULL we might be
21831 * assigning another function to the same var, the type is checked
21832 * below. */
21833 if (new_var && function_exists(name))
21834 {
21835 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21836 name);
21837 return TRUE;
21838 }
21839 return FALSE;
21840}
21841
21842/*
21843 * Check if a variable name is valid.
21844 * Return FALSE and give an error if not.
21845 */
21846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021847valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021848{
21849 char_u *p;
21850
21851 for (p = varname; *p != NUL; ++p)
21852 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21853 && *p != AUTOLOAD_CHAR)
21854 {
21855 EMSG2(_(e_illvar), varname);
21856 return FALSE;
21857 }
21858 return TRUE;
21859}
21860
21861/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021863 * Also give an error message, using "name" or _("name") when use_gettext is
21864 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865 */
21866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021867tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021868{
21869 if (lock & VAR_LOCKED)
21870 {
21871 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021872 name == NULL ? (char_u *)_("Unknown")
21873 : use_gettext ? (char_u *)_(name)
21874 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021875 return TRUE;
21876 }
21877 if (lock & VAR_FIXED)
21878 {
21879 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021880 name == NULL ? (char_u *)_("Unknown")
21881 : use_gettext ? (char_u *)_(name)
21882 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021883 return TRUE;
21884 }
21885 return FALSE;
21886}
21887
21888/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021889 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021890 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021891 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021892 * It is OK for "from" and "to" to point to the same item. This is used to
21893 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021894 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021898 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021899 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900 switch (from->v_type)
21901 {
21902 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021903 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021904 to->vval.v_number = from->vval.v_number;
21905 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021906#ifdef FEAT_FLOAT
21907 case VAR_FLOAT:
21908 to->vval.v_float = from->vval.v_float;
21909 break;
21910#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 case VAR_STRING:
21912 case VAR_FUNC:
21913 if (from->vval.v_string == NULL)
21914 to->vval.v_string = NULL;
21915 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021918 if (from->v_type == VAR_FUNC)
21919 func_ref(to->vval.v_string);
21920 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021921 break;
21922 case VAR_LIST:
21923 if (from->vval.v_list == NULL)
21924 to->vval.v_list = NULL;
21925 else
21926 {
21927 to->vval.v_list = from->vval.v_list;
21928 ++to->vval.v_list->lv_refcount;
21929 }
21930 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021931 case VAR_DICT:
21932 if (from->vval.v_dict == NULL)
21933 to->vval.v_dict = NULL;
21934 else
21935 {
21936 to->vval.v_dict = from->vval.v_dict;
21937 ++to->vval.v_dict->dv_refcount;
21938 }
21939 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021940 case VAR_UNKNOWN:
21941 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021942 break;
21943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944}
21945
21946/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021947 * Make a copy of an item.
21948 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021949 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21950 * reference to an already copied list/dict can be used.
21951 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021954item_copy(
21955 typval_T *from,
21956 typval_T *to,
21957 int deep,
21958 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959{
21960 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021961 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021962
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021964 {
21965 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021966 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021967 }
21968 ++recurse;
21969
21970 switch (from->v_type)
21971 {
21972 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021973#ifdef FEAT_FLOAT
21974 case VAR_FLOAT:
21975#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021976 case VAR_STRING:
21977 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021978 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021979 copy_tv(from, to);
21980 break;
21981 case VAR_LIST:
21982 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021983 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021984 if (from->vval.v_list == NULL)
21985 to->vval.v_list = NULL;
21986 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21987 {
21988 /* use the copy made earlier */
21989 to->vval.v_list = from->vval.v_list->lv_copylist;
21990 ++to->vval.v_list->lv_refcount;
21991 }
21992 else
21993 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21994 if (to->vval.v_list == NULL)
21995 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021996 break;
21997 case VAR_DICT:
21998 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021999 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022000 if (from->vval.v_dict == NULL)
22001 to->vval.v_dict = NULL;
22002 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22003 {
22004 /* use the copy made earlier */
22005 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22006 ++to->vval.v_dict->dv_refcount;
22007 }
22008 else
22009 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22010 if (to->vval.v_dict == NULL)
22011 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022012 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022013 case VAR_UNKNOWN:
22014 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022015 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022016 }
22017 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022018 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022019}
22020
22021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 * ":echo expr1 ..." print each argument separated with a space, add a
22023 * newline at the end.
22024 * ":echon expr1 ..." print each argument plain.
22025 */
22026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022027ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028{
22029 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022030 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 char_u *p;
22033 int needclr = TRUE;
22034 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022035 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036
22037 if (eap->skip)
22038 ++emsg_skip;
22039 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022041 /* If eval1() causes an error message the text from the command may
22042 * still need to be cleared. E.g., "echo 22,44". */
22043 need_clr_eos = needclr;
22044
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022046 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 {
22048 /*
22049 * Report the invalid expression unless the expression evaluation
22050 * has been cancelled due to an aborting error, an interrupt, or an
22051 * exception.
22052 */
22053 if (!aborting())
22054 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022055 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 break;
22057 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022058 need_clr_eos = FALSE;
22059
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 if (!eap->skip)
22061 {
22062 if (atstart)
22063 {
22064 atstart = FALSE;
22065 /* Call msg_start() after eval1(), evaluating the expression
22066 * may cause a message to appear. */
22067 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022068 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022069 /* Mark the saved text as finishing the line, so that what
22070 * follows is displayed on a new line when scrolling back
22071 * at the more prompt. */
22072 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 }
22076 else if (eap->cmdidx == CMD_echo)
22077 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022078 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022079 if (p != NULL)
22080 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022082 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022084 if (*p != TAB && needclr)
22085 {
22086 /* remove any text still there from the command */
22087 msg_clr_eos();
22088 needclr = FALSE;
22089 }
22090 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 }
22092 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022093 {
22094#ifdef FEAT_MBYTE
22095 if (has_mbyte)
22096 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022097 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022098
22099 (void)msg_outtrans_len_attr(p, i, echo_attr);
22100 p += i - 1;
22101 }
22102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022104 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022107 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022109 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 arg = skipwhite(arg);
22111 }
22112 eap->nextcmd = check_nextcmd(arg);
22113
22114 if (eap->skip)
22115 --emsg_skip;
22116 else
22117 {
22118 /* remove text that may still be there from the command */
22119 if (needclr)
22120 msg_clr_eos();
22121 if (eap->cmdidx == CMD_echo)
22122 msg_end();
22123 }
22124}
22125
22126/*
22127 * ":echohl {name}".
22128 */
22129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022130ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131{
22132 int id;
22133
22134 id = syn_name2id(eap->arg);
22135 if (id == 0)
22136 echo_attr = 0;
22137 else
22138 echo_attr = syn_id2attr(id);
22139}
22140
22141/*
22142 * ":execute expr1 ..." execute the result of an expression.
22143 * ":echomsg expr1 ..." Print a message
22144 * ":echoerr expr1 ..." Print an error
22145 * Each gets spaces around each argument and a newline at the end for
22146 * echo commands
22147 */
22148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022149ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150{
22151 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022152 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 int ret = OK;
22154 char_u *p;
22155 garray_T ga;
22156 int len;
22157 int save_did_emsg;
22158
22159 ga_init2(&ga, 1, 80);
22160
22161 if (eap->skip)
22162 ++emsg_skip;
22163 while (*arg != NUL && *arg != '|' && *arg != '\n')
22164 {
22165 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022166 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 {
22168 /*
22169 * Report the invalid expression unless the expression evaluation
22170 * has been cancelled due to an aborting error, an interrupt, or an
22171 * exception.
22172 */
22173 if (!aborting())
22174 EMSG2(_(e_invexpr2), p);
22175 ret = FAIL;
22176 break;
22177 }
22178
22179 if (!eap->skip)
22180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022181 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 len = (int)STRLEN(p);
22183 if (ga_grow(&ga, len + 2) == FAIL)
22184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022185 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 ret = FAIL;
22187 break;
22188 }
22189 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 ga.ga_len += len;
22193 }
22194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 arg = skipwhite(arg);
22197 }
22198
22199 if (ret != FAIL && ga.ga_data != NULL)
22200 {
22201 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022204 out_flush();
22205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 else if (eap->cmdidx == CMD_echoerr)
22207 {
22208 /* We don't want to abort following commands, restore did_emsg. */
22209 save_did_emsg = did_emsg;
22210 EMSG((char_u *)ga.ga_data);
22211 if (!force_abort)
22212 did_emsg = save_did_emsg;
22213 }
22214 else if (eap->cmdidx == CMD_execute)
22215 do_cmdline((char_u *)ga.ga_data,
22216 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22217 }
22218
22219 ga_clear(&ga);
22220
22221 if (eap->skip)
22222 --emsg_skip;
22223
22224 eap->nextcmd = check_nextcmd(arg);
22225}
22226
22227/*
22228 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22229 * "arg" points to the "&" or '+' when called, to "option" when returning.
22230 * Returns NULL when no option name found. Otherwise pointer to the char
22231 * after the option name.
22232 */
22233 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235{
22236 char_u *p = *arg;
22237
22238 ++p;
22239 if (*p == 'g' && p[1] == ':')
22240 {
22241 *opt_flags = OPT_GLOBAL;
22242 p += 2;
22243 }
22244 else if (*p == 'l' && p[1] == ':')
22245 {
22246 *opt_flags = OPT_LOCAL;
22247 p += 2;
22248 }
22249 else
22250 *opt_flags = 0;
22251
22252 if (!ASCII_ISALPHA(*p))
22253 return NULL;
22254 *arg = p;
22255
22256 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22257 p += 4; /* termcap option */
22258 else
22259 while (ASCII_ISALPHA(*p))
22260 ++p;
22261 return p;
22262}
22263
22264/*
22265 * ":function"
22266 */
22267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022268ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269{
22270 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022271 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 int j;
22273 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022275 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 char_u *name = NULL;
22277 char_u *p;
22278 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022279 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 garray_T newargs;
22281 garray_T newlines;
22282 int varargs = FALSE;
22283 int mustend = FALSE;
22284 int flags = 0;
22285 ufunc_T *fp;
22286 int indent;
22287 int nesting;
22288 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 dictitem_T *v;
22290 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022291 static int func_nr = 0; /* number for nameless function */
22292 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 hashtab_T *ht;
22294 int todo;
22295 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022296 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297
22298 /*
22299 * ":function" without argument: list functions.
22300 */
22301 if (ends_excmd(*eap->arg))
22302 {
22303 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022304 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022305 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022306 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 {
22308 if (!HASHITEM_EMPTY(hi))
22309 {
22310 --todo;
22311 fp = HI2UF(hi);
22312 if (!isdigit(*fp->uf_name))
22313 list_func_head(fp, FALSE);
22314 }
22315 }
22316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 eap->nextcmd = check_nextcmd(eap->arg);
22318 return;
22319 }
22320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022321 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022322 * ":function /pat": list functions matching pattern.
22323 */
22324 if (*eap->arg == '/')
22325 {
22326 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22327 if (!eap->skip)
22328 {
22329 regmatch_T regmatch;
22330
22331 c = *p;
22332 *p = NUL;
22333 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22334 *p = c;
22335 if (regmatch.regprog != NULL)
22336 {
22337 regmatch.rm_ic = p_ic;
22338
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022339 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022340 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22341 {
22342 if (!HASHITEM_EMPTY(hi))
22343 {
22344 --todo;
22345 fp = HI2UF(hi);
22346 if (!isdigit(*fp->uf_name)
22347 && vim_regexec(&regmatch, fp->uf_name, 0))
22348 list_func_head(fp, FALSE);
22349 }
22350 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022351 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022352 }
22353 }
22354 if (*p == '/')
22355 ++p;
22356 eap->nextcmd = check_nextcmd(p);
22357 return;
22358 }
22359
22360 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022361 * Get the function name. There are these situations:
22362 * func normal function name
22363 * "name" == func, "fudi.fd_dict" == NULL
22364 * dict.func new dictionary entry
22365 * "name" == NULL, "fudi.fd_dict" set,
22366 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22367 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022368 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022369 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22370 * dict.func existing dict entry that's not a Funcref
22371 * "name" == NULL, "fudi.fd_dict" set,
22372 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022373 * s:func script-local function name
22374 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022377 name = trans_function_name(&p, eap->skip, 0, &fudi);
22378 paren = (vim_strchr(p, '(') != NULL);
22379 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 {
22381 /*
22382 * Return on an invalid expression in braces, unless the expression
22383 * evaluation has been cancelled due to an aborting error, an
22384 * interrupt, or an exception.
22385 */
22386 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022388 if (!eap->skip && fudi.fd_newkey != NULL)
22389 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022390 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 else
22394 eap->skip = TRUE;
22395 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022396
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 /* An error in a function call during evaluation of an expression in magic
22398 * braces should not cause the function not to be defined. */
22399 saved_did_emsg = did_emsg;
22400 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401
22402 /*
22403 * ":function func" with only function name: list function.
22404 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022405 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 {
22407 if (!ends_excmd(*skipwhite(p)))
22408 {
22409 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022410 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 }
22412 eap->nextcmd = check_nextcmd(p);
22413 if (eap->nextcmd != NULL)
22414 *p = NUL;
22415 if (!eap->skip && !got_int)
22416 {
22417 fp = find_func(name);
22418 if (fp != NULL)
22419 {
22420 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022423 if (FUNCLINE(fp, j) == NULL)
22424 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 msg_putchar('\n');
22426 msg_outnum((long)(j + 1));
22427 if (j < 9)
22428 msg_putchar(' ');
22429 if (j < 99)
22430 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 out_flush(); /* show a line at a time */
22433 ui_breakcheck();
22434 }
22435 if (!got_int)
22436 {
22437 msg_putchar('\n');
22438 msg_puts((char_u *)" endfunction");
22439 }
22440 }
22441 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022442 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022444 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 }
22446
22447 /*
22448 * ":function name(arg1, arg2)" Define function.
22449 */
22450 p = skipwhite(p);
22451 if (*p != '(')
22452 {
22453 if (!eap->skip)
22454 {
22455 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022456 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 }
22458 /* attempt to continue by skipping some text */
22459 if (vim_strchr(p, '(') != NULL)
22460 p = vim_strchr(p, '(');
22461 }
22462 p = skipwhite(p + 1);
22463
22464 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22465 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22466
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022467 if (!eap->skip)
22468 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022469 /* Check the name of the function. Unless it's a dictionary function
22470 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022471 if (name != NULL)
22472 arg = name;
22473 else
22474 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022475 if (arg != NULL && (fudi.fd_di == NULL
22476 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022477 {
22478 if (*arg == K_SPECIAL)
22479 j = 3;
22480 else
22481 j = 0;
22482 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22483 : eval_isnamec(arg[j])))
22484 ++j;
22485 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022486 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022487 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022488 /* Disallow using the g: dict. */
22489 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22490 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022491 }
22492
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 /*
22494 * Isolate the arguments: "arg1, arg2, ...)"
22495 */
22496 while (*p != ')')
22497 {
22498 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22499 {
22500 varargs = TRUE;
22501 p += 3;
22502 mustend = TRUE;
22503 }
22504 else
22505 {
22506 arg = p;
22507 while (ASCII_ISALNUM(*p) || *p == '_')
22508 ++p;
22509 if (arg == p || isdigit(*arg)
22510 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22511 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22512 {
22513 if (!eap->skip)
22514 EMSG2(_("E125: Illegal argument: %s"), arg);
22515 break;
22516 }
22517 if (ga_grow(&newargs, 1) == FAIL)
22518 goto erret;
22519 c = *p;
22520 *p = NUL;
22521 arg = vim_strsave(arg);
22522 if (arg == NULL)
22523 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022524
22525 /* Check for duplicate argument name. */
22526 for (i = 0; i < newargs.ga_len; ++i)
22527 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22528 {
22529 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022530 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022531 goto erret;
22532 }
22533
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22535 *p = c;
22536 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 if (*p == ',')
22538 ++p;
22539 else
22540 mustend = TRUE;
22541 }
22542 p = skipwhite(p);
22543 if (mustend && *p != ')')
22544 {
22545 if (!eap->skip)
22546 EMSG2(_(e_invarg2), eap->arg);
22547 break;
22548 }
22549 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022550 if (*p != ')')
22551 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 ++p; /* skip the ')' */
22553
Bram Moolenaare9a41262005-01-15 22:18:47 +000022554 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 for (;;)
22556 {
22557 p = skipwhite(p);
22558 if (STRNCMP(p, "range", 5) == 0)
22559 {
22560 flags |= FC_RANGE;
22561 p += 5;
22562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022563 else if (STRNCMP(p, "dict", 4) == 0)
22564 {
22565 flags |= FC_DICT;
22566 p += 4;
22567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 else if (STRNCMP(p, "abort", 5) == 0)
22569 {
22570 flags |= FC_ABORT;
22571 p += 5;
22572 }
22573 else
22574 break;
22575 }
22576
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022577 /* When there is a line break use what follows for the function body.
22578 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22579 if (*p == '\n')
22580 line_arg = p + 1;
22581 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 EMSG(_(e_trailing));
22583
22584 /*
22585 * Read the body of the function, until ":endfunction" is found.
22586 */
22587 if (KeyTyped)
22588 {
22589 /* Check if the function already exists, don't let the user type the
22590 * whole function before telling him it doesn't work! For a script we
22591 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022592 if (!eap->skip && !eap->forceit)
22593 {
22594 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22595 EMSG(_(e_funcdict));
22596 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022597 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022600 if (!eap->skip && did_emsg)
22601 goto erret;
22602
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 msg_putchar('\n'); /* don't overwrite the function name */
22604 cmdline_row = msg_row;
22605 }
22606
22607 indent = 2;
22608 nesting = 0;
22609 for (;;)
22610 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022611 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022612 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022613 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022614 saved_wait_return = FALSE;
22615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022617 sourcing_lnum_off = sourcing_lnum;
22618
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022619 if (line_arg != NULL)
22620 {
22621 /* Use eap->arg, split up in parts by line breaks. */
22622 theline = line_arg;
22623 p = vim_strchr(theline, '\n');
22624 if (p == NULL)
22625 line_arg += STRLEN(line_arg);
22626 else
22627 {
22628 *p = NUL;
22629 line_arg = p + 1;
22630 }
22631 }
22632 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 theline = getcmdline(':', 0L, indent);
22634 else
22635 theline = eap->getline(':', eap->cookie, indent);
22636 if (KeyTyped)
22637 lines_left = Rows - 1;
22638 if (theline == NULL)
22639 {
22640 EMSG(_("E126: Missing :endfunction"));
22641 goto erret;
22642 }
22643
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022644 /* Detect line continuation: sourcing_lnum increased more than one. */
22645 if (sourcing_lnum > sourcing_lnum_off + 1)
22646 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22647 else
22648 sourcing_lnum_off = 0;
22649
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 if (skip_until != NULL)
22651 {
22652 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22653 * don't check for ":endfunc". */
22654 if (STRCMP(theline, skip_until) == 0)
22655 {
22656 vim_free(skip_until);
22657 skip_until = NULL;
22658 }
22659 }
22660 else
22661 {
22662 /* skip ':' and blanks*/
22663 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22664 ;
22665
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022666 /* Check for "endfunction". */
22667 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022669 if (line_arg == NULL)
22670 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 break;
22672 }
22673
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022674 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 * at "end". */
22676 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22677 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022678 else if (STRNCMP(p, "if", 2) == 0
22679 || STRNCMP(p, "wh", 2) == 0
22680 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 || STRNCMP(p, "try", 3) == 0)
22682 indent += 2;
22683
22684 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022685 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022687 if (*p == '!')
22688 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022690 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22691 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022693 ++nesting;
22694 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 }
22696 }
22697
22698 /* Check for ":append" or ":insert". */
22699 p = skip_range(p, NULL);
22700 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22701 || (p[0] == 'i'
22702 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22703 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22704 skip_until = vim_strsave((char_u *)".");
22705
22706 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22707 arg = skipwhite(skiptowhite(p));
22708 if (arg[0] == '<' && arg[1] =='<'
22709 && ((p[0] == 'p' && p[1] == 'y'
22710 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22711 || (p[0] == 'p' && p[1] == 'e'
22712 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22713 || (p[0] == 't' && p[1] == 'c'
22714 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022715 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22716 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22718 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022719 || (p[0] == 'm' && p[1] == 'z'
22720 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 ))
22722 {
22723 /* ":python <<" continues until a dot, like ":append" */
22724 p = skipwhite(arg + 2);
22725 if (*p == NUL)
22726 skip_until = vim_strsave((char_u *)".");
22727 else
22728 skip_until = vim_strsave(p);
22729 }
22730 }
22731
22732 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022733 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022734 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022735 if (line_arg == NULL)
22736 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022738 }
22739
22740 /* Copy the line to newly allocated memory. get_one_sourceline()
22741 * allocates 250 bytes per line, this saves 80% on average. The cost
22742 * is an extra alloc/free. */
22743 p = vim_strsave(theline);
22744 if (p != NULL)
22745 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022746 if (line_arg == NULL)
22747 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022748 theline = p;
22749 }
22750
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022751 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22752
22753 /* Add NULL lines for continuation lines, so that the line count is
22754 * equal to the index in the growarray. */
22755 while (sourcing_lnum_off-- > 0)
22756 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022757
22758 /* Check for end of eap->arg. */
22759 if (line_arg != NULL && *line_arg == NUL)
22760 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 }
22762
22763 /* Don't define the function when skipping commands or when an error was
22764 * detected. */
22765 if (eap->skip || did_emsg)
22766 goto erret;
22767
22768 /*
22769 * If there are no errors, add the function
22770 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022772 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022773 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022775 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022776 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022777 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022778 goto erret;
22779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022780
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 fp = find_func(name);
22782 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 if (!eap->forceit)
22785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022786 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022787 goto erret;
22788 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022790 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022791 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 name);
22793 goto erret;
22794 }
22795 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022796 ga_clear_strings(&(fp->uf_args));
22797 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022798 vim_free(name);
22799 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 }
22802 else
22803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 char numbuf[20];
22805
22806 fp = NULL;
22807 if (fudi.fd_newkey == NULL && !eap->forceit)
22808 {
22809 EMSG(_(e_funcdict));
22810 goto erret;
22811 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022812 if (fudi.fd_di == NULL)
22813 {
22814 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022815 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022816 goto erret;
22817 }
22818 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022819 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022820 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022821
22822 /* Give the function a sequential number. Can only be used with a
22823 * Funcref! */
22824 vim_free(name);
22825 sprintf(numbuf, "%d", ++func_nr);
22826 name = vim_strsave((char_u *)numbuf);
22827 if (name == NULL)
22828 goto erret;
22829 }
22830
22831 if (fp == NULL)
22832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022833 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022834 {
22835 int slen, plen;
22836 char_u *scriptname;
22837
22838 /* Check that the autoload name matches the script name. */
22839 j = FAIL;
22840 if (sourcing_name != NULL)
22841 {
22842 scriptname = autoload_name(name);
22843 if (scriptname != NULL)
22844 {
22845 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022846 plen = (int)STRLEN(p);
22847 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 if (slen > plen && fnamecmp(p,
22849 sourcing_name + slen - plen) == 0)
22850 j = OK;
22851 vim_free(scriptname);
22852 }
22853 }
22854 if (j == FAIL)
22855 {
22856 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22857 goto erret;
22858 }
22859 }
22860
22861 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 if (fp == NULL)
22863 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022864
22865 if (fudi.fd_dict != NULL)
22866 {
22867 if (fudi.fd_di == NULL)
22868 {
22869 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022870 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022871 if (fudi.fd_di == NULL)
22872 {
22873 vim_free(fp);
22874 goto erret;
22875 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022876 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22877 {
22878 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022879 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022880 goto erret;
22881 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022882 }
22883 else
22884 /* overwrite existing dict entry */
22885 clear_tv(&fudi.fd_di->di_tv);
22886 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022887 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022888 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022889 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022890
22891 /* behave like "dict" was used */
22892 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022893 }
22894
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022896 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022897 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22898 {
22899 vim_free(fp);
22900 goto erret;
22901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 fp->uf_args = newargs;
22904 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022905#ifdef FEAT_PROFILE
22906 fp->uf_tml_count = NULL;
22907 fp->uf_tml_total = NULL;
22908 fp->uf_tml_self = NULL;
22909 fp->uf_profiling = FALSE;
22910 if (prof_def_func())
22911 func_do_profile(fp);
22912#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022913 fp->uf_varargs = varargs;
22914 fp->uf_flags = flags;
22915 fp->uf_calls = 0;
22916 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022917 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918
22919erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 ga_clear_strings(&newargs);
22921 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022922ret_free:
22923 vim_free(skip_until);
22924 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022927 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928}
22929
22930/*
22931 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022932 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022934 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022935 * TFN_INT: internal function name OK
22936 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022937 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 * Advances "pp" to just after the function name (if no error).
22939 */
22940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022941trans_function_name(
22942 char_u **pp,
22943 int skip, /* only find the end, don't evaluate */
22944 int flags,
22945 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022947 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 char_u *start;
22949 char_u *end;
22950 int lead;
22951 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954
22955 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022958
22959 /* Check for hard coded <SNR>: already translated function ID (from a user
22960 * command). */
22961 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22962 && (*pp)[2] == (int)KE_SNR)
22963 {
22964 *pp += 3;
22965 len = get_id_len(pp) + 3;
22966 return vim_strnsave(start, len);
22967 }
22968
22969 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22970 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022972 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022974
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022975 /* Note that TFN_ flags use the same values as GLV_ flags. */
22976 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022977 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022978 if (end == start)
22979 {
22980 if (!skip)
22981 EMSG(_("E129: Function name required"));
22982 goto theend;
22983 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022984 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022985 {
22986 /*
22987 * Report an invalid expression in braces, unless the expression
22988 * evaluation has been cancelled due to an aborting error, an
22989 * interrupt, or an exception.
22990 */
22991 if (!aborting())
22992 {
22993 if (end != NULL)
22994 EMSG2(_(e_invarg2), start);
22995 }
22996 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022997 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022998 goto theend;
22999 }
23000
23001 if (lv.ll_tv != NULL)
23002 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023003 if (fdp != NULL)
23004 {
23005 fdp->fd_dict = lv.ll_dict;
23006 fdp->fd_newkey = lv.ll_newkey;
23007 lv.ll_newkey = NULL;
23008 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023010 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23011 {
23012 name = vim_strsave(lv.ll_tv->vval.v_string);
23013 *pp = end;
23014 }
23015 else
23016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023017 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23018 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023019 EMSG(_(e_funcref));
23020 else
23021 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023022 name = NULL;
23023 }
23024 goto theend;
23025 }
23026
23027 if (lv.ll_name == NULL)
23028 {
23029 /* Error found, but continue after the function name. */
23030 *pp = end;
23031 goto theend;
23032 }
23033
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023034 /* Check if the name is a Funcref. If so, use the value. */
23035 if (lv.ll_exp_name != NULL)
23036 {
23037 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023038 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023039 if (name == lv.ll_exp_name)
23040 name = NULL;
23041 }
23042 else
23043 {
23044 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023045 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023046 if (name == *pp)
23047 name = NULL;
23048 }
23049 if (name != NULL)
23050 {
23051 name = vim_strsave(name);
23052 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023053 if (STRNCMP(name, "<SNR>", 5) == 0)
23054 {
23055 /* Change "<SNR>" to the byte sequence. */
23056 name[0] = K_SPECIAL;
23057 name[1] = KS_EXTRA;
23058 name[2] = (int)KE_SNR;
23059 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23060 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023061 goto theend;
23062 }
23063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023064 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023065 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023066 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023067 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23068 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23069 {
23070 /* When there was "s:" already or the name expanded to get a
23071 * leading "s:" then remove it. */
23072 lv.ll_name += 2;
23073 len -= 2;
23074 lead = 2;
23075 }
23076 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023077 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023078 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023079 /* skip over "s:" and "g:" */
23080 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023081 lv.ll_name += 2;
23082 len = (int)(end - lv.ll_name);
23083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023084
23085 /*
23086 * Copy the function name to allocated memory.
23087 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23088 * Accept <SNR>123_name() outside a script.
23089 */
23090 if (skip)
23091 lead = 0; /* do nothing */
23092 else if (lead > 0)
23093 {
23094 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023095 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23096 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023097 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023098 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023099 if (current_SID <= 0)
23100 {
23101 EMSG(_(e_usingsid));
23102 goto theend;
23103 }
23104 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23105 lead += (int)STRLEN(sid_buf);
23106 }
23107 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023108 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023109 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023110 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023111 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023112 goto theend;
23113 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023114 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023115 {
23116 char_u *cp = vim_strchr(lv.ll_name, ':');
23117
23118 if (cp != NULL && cp < end)
23119 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023120 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023121 goto theend;
23122 }
23123 }
23124
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023125 name = alloc((unsigned)(len + lead + 1));
23126 if (name != NULL)
23127 {
23128 if (lead > 0)
23129 {
23130 name[0] = K_SPECIAL;
23131 name[1] = KS_EXTRA;
23132 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023133 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023134 STRCPY(name + 3, sid_buf);
23135 }
23136 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023137 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023138 }
23139 *pp = end;
23140
23141theend:
23142 clear_lval(&lv);
23143 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144}
23145
23146/*
23147 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23148 * Return 2 if "p" starts with "s:".
23149 * Return 0 otherwise.
23150 */
23151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023152eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023154 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23155 * the standard library function. */
23156 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23157 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 return 5;
23159 if (p[0] == 's' && p[1] == ':')
23160 return 2;
23161 return 0;
23162}
23163
23164/*
23165 * Return TRUE if "p" starts with "<SID>" or "s:".
23166 * Only works if eval_fname_script() returned non-zero for "p"!
23167 */
23168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023169eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170{
23171 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23172}
23173
23174/*
23175 * List the head of the function: "name(arg1, arg2)".
23176 */
23177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023178list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179{
23180 int j;
23181
23182 msg_start();
23183 if (indent)
23184 MSG_PUTS(" ");
23185 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023186 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 {
23188 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 }
23191 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023192 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023194 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 {
23196 if (j)
23197 MSG_PUTS(", ");
23198 msg_puts(FUNCARG(fp, j));
23199 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 {
23202 if (j)
23203 MSG_PUTS(", ");
23204 MSG_PUTS("...");
23205 }
23206 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023207 if (fp->uf_flags & FC_ABORT)
23208 MSG_PUTS(" abort");
23209 if (fp->uf_flags & FC_RANGE)
23210 MSG_PUTS(" range");
23211 if (fp->uf_flags & FC_DICT)
23212 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023213 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023214 if (p_verbose > 0)
23215 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216}
23217
23218/*
23219 * Find a function by name, return pointer to it in ufuncs.
23220 * Return NULL for unknown function.
23221 */
23222 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023223find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023225 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023227 hi = hash_find(&func_hashtab, name);
23228 if (!HASHITEM_EMPTY(hi))
23229 return HI2UF(hi);
23230 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231}
23232
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023233#if defined(EXITFREE) || defined(PROTO)
23234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023235free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023236{
23237 hashitem_T *hi;
23238
23239 /* Need to start all over every time, because func_free() may change the
23240 * hash table. */
23241 while (func_hashtab.ht_used > 0)
23242 for (hi = func_hashtab.ht_array; ; ++hi)
23243 if (!HASHITEM_EMPTY(hi))
23244 {
23245 func_free(HI2UF(hi));
23246 break;
23247 }
23248}
23249#endif
23250
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023252translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023253{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023254 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023255 return find_internal_func(name) >= 0;
23256 return find_func(name) != NULL;
23257}
23258
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023259/*
23260 * Return TRUE if a function "name" exists.
23261 */
23262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023263function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023264{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023265 char_u *nm = name;
23266 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023267 int n = FALSE;
23268
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023269 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23270 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023271 nm = skipwhite(nm);
23272
23273 /* Only accept "funcname", "funcname ", "funcname (..." and
23274 * "funcname(...", not "funcname!...". */
23275 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023276 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023277 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023278 return n;
23279}
23280
Bram Moolenaara1544c02013-05-30 12:35:52 +020023281 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023282get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023283{
23284 char_u *nm = name;
23285 char_u *p;
23286
23287 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23288
23289 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023290 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023291 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023292
Bram Moolenaara1544c02013-05-30 12:35:52 +020023293 vim_free(p);
23294 return NULL;
23295}
23296
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023297/*
23298 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023299 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23300 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023301 */
23302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023303builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023304{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023305 char_u *p;
23306
23307 if (!ASCII_ISLOWER(name[0]))
23308 return FALSE;
23309 p = vim_strchr(name, AUTOLOAD_CHAR);
23310 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023311}
23312
Bram Moolenaar05159a02005-02-26 23:04:13 +000023313#if defined(FEAT_PROFILE) || defined(PROTO)
23314/*
23315 * Start profiling function "fp".
23316 */
23317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023318func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023319{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023320 int len = fp->uf_lines.ga_len;
23321
23322 if (len == 0)
23323 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023324 fp->uf_tm_count = 0;
23325 profile_zero(&fp->uf_tm_self);
23326 profile_zero(&fp->uf_tm_total);
23327 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023328 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023329 if (fp->uf_tml_total == NULL)
23330 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023331 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023332 if (fp->uf_tml_self == NULL)
23333 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023334 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023335 fp->uf_tml_idx = -1;
23336 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23337 || fp->uf_tml_self == NULL)
23338 return; /* out of memory */
23339
23340 fp->uf_profiling = TRUE;
23341}
23342
23343/*
23344 * Dump the profiling results for all functions in file "fd".
23345 */
23346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023347func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023348{
23349 hashitem_T *hi;
23350 int todo;
23351 ufunc_T *fp;
23352 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023353 ufunc_T **sorttab;
23354 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023356 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023357 if (todo == 0)
23358 return; /* nothing to dump */
23359
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023360 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023361
Bram Moolenaar05159a02005-02-26 23:04:13 +000023362 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23363 {
23364 if (!HASHITEM_EMPTY(hi))
23365 {
23366 --todo;
23367 fp = HI2UF(hi);
23368 if (fp->uf_profiling)
23369 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023370 if (sorttab != NULL)
23371 sorttab[st_len++] = fp;
23372
Bram Moolenaar05159a02005-02-26 23:04:13 +000023373 if (fp->uf_name[0] == K_SPECIAL)
23374 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23375 else
23376 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23377 if (fp->uf_tm_count == 1)
23378 fprintf(fd, "Called 1 time\n");
23379 else
23380 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23381 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23382 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23383 fprintf(fd, "\n");
23384 fprintf(fd, "count total (s) self (s)\n");
23385
23386 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23387 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023388 if (FUNCLINE(fp, i) == NULL)
23389 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023390 prof_func_line(fd, fp->uf_tml_count[i],
23391 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023392 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23393 }
23394 fprintf(fd, "\n");
23395 }
23396 }
23397 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023398
23399 if (sorttab != NULL && st_len > 0)
23400 {
23401 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23402 prof_total_cmp);
23403 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23404 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23405 prof_self_cmp);
23406 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23407 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023408
23409 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023410}
Bram Moolenaar73830342005-02-28 22:48:19 +000023411
23412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023413prof_sort_list(
23414 FILE *fd,
23415 ufunc_T **sorttab,
23416 int st_len,
23417 char *title,
23418 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023419{
23420 int i;
23421 ufunc_T *fp;
23422
23423 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23424 fprintf(fd, "count total (s) self (s) function\n");
23425 for (i = 0; i < 20 && i < st_len; ++i)
23426 {
23427 fp = sorttab[i];
23428 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23429 prefer_self);
23430 if (fp->uf_name[0] == K_SPECIAL)
23431 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23432 else
23433 fprintf(fd, " %s()\n", fp->uf_name);
23434 }
23435 fprintf(fd, "\n");
23436}
23437
23438/*
23439 * Print the count and times for one function or function line.
23440 */
23441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023442prof_func_line(
23443 FILE *fd,
23444 int count,
23445 proftime_T *total,
23446 proftime_T *self,
23447 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023448{
23449 if (count > 0)
23450 {
23451 fprintf(fd, "%5d ", count);
23452 if (prefer_self && profile_equal(total, self))
23453 fprintf(fd, " ");
23454 else
23455 fprintf(fd, "%s ", profile_msg(total));
23456 if (!prefer_self && profile_equal(total, self))
23457 fprintf(fd, " ");
23458 else
23459 fprintf(fd, "%s ", profile_msg(self));
23460 }
23461 else
23462 fprintf(fd, " ");
23463}
23464
23465/*
23466 * Compare function for total time sorting.
23467 */
23468 static int
23469#ifdef __BORLANDC__
23470_RTLENTRYF
23471#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023472prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023473{
23474 ufunc_T *p1, *p2;
23475
23476 p1 = *(ufunc_T **)s1;
23477 p2 = *(ufunc_T **)s2;
23478 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23479}
23480
23481/*
23482 * Compare function for self time sorting.
23483 */
23484 static int
23485#ifdef __BORLANDC__
23486_RTLENTRYF
23487#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023488prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023489{
23490 ufunc_T *p1, *p2;
23491
23492 p1 = *(ufunc_T **)s1;
23493 p2 = *(ufunc_T **)s2;
23494 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23495}
23496
Bram Moolenaar05159a02005-02-26 23:04:13 +000023497#endif
23498
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023499/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023500 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023501 * Return TRUE if a package was loaded.
23502 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023504script_autoload(
23505 char_u *name,
23506 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023507{
23508 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023509 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023510 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023511 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023512
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023513 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023514 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023515 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023516 return FALSE;
23517
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023518 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023519
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023520 /* Find the name in the list of previously loaded package names. Skip
23521 * "autoload/", it's always the same. */
23522 for (i = 0; i < ga_loaded.ga_len; ++i)
23523 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23524 break;
23525 if (!reload && i < ga_loaded.ga_len)
23526 ret = FALSE; /* was loaded already */
23527 else
23528 {
23529 /* Remember the name if it wasn't loaded already. */
23530 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23531 {
23532 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23533 tofree = NULL;
23534 }
23535
23536 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023537 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023538 ret = TRUE;
23539 }
23540
23541 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 return ret;
23543}
23544
23545/*
23546 * Return the autoload script name for a function or variable name.
23547 * Returns NULL when out of memory.
23548 */
23549 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023550autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023551{
23552 char_u *p;
23553 char_u *scriptname;
23554
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023555 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023556 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23557 if (scriptname == NULL)
23558 return FALSE;
23559 STRCPY(scriptname, "autoload/");
23560 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023561 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023562 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023563 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023564 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023565 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023566}
23567
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23569
23570/*
23571 * Function given to ExpandGeneric() to obtain the list of user defined
23572 * function names.
23573 */
23574 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023575get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023577 static long_u done;
23578 static hashitem_T *hi;
23579 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580
23581 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023583 done = 0;
23584 hi = func_hashtab.ht_array;
23585 }
23586 if (done < func_hashtab.ht_used)
23587 {
23588 if (done++ > 0)
23589 ++hi;
23590 while (HASHITEM_EMPTY(hi))
23591 ++hi;
23592 fp = HI2UF(hi);
23593
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023594 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023595 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023597 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23598 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599
23600 cat_func_name(IObuff, fp);
23601 if (xp->xp_context != EXPAND_USER_FUNC)
23602 {
23603 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 STRCAT(IObuff, ")");
23606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 return IObuff;
23608 }
23609 return NULL;
23610}
23611
23612#endif /* FEAT_CMDL_COMPL */
23613
23614/*
23615 * Copy the function name of "fp" to buffer "buf".
23616 * "buf" must be able to hold the function name plus three bytes.
23617 * Takes care of script-local function names.
23618 */
23619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023620cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023622 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 {
23624 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023625 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 }
23627 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023628 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629}
23630
23631/*
23632 * ":delfunction {name}"
23633 */
23634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023635ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023637 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 char_u *p;
23639 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023640 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641
23642 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 name = trans_function_name(&p, eap->skip, 0, &fudi);
23644 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646 {
23647 if (fudi.fd_dict != NULL && !eap->skip)
23648 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 if (!ends_excmd(*skipwhite(p)))
23652 {
23653 vim_free(name);
23654 EMSG(_(e_trailing));
23655 return;
23656 }
23657 eap->nextcmd = check_nextcmd(p);
23658 if (eap->nextcmd != NULL)
23659 *p = NUL;
23660
23661 if (!eap->skip)
23662 fp = find_func(name);
23663 vim_free(name);
23664
23665 if (!eap->skip)
23666 {
23667 if (fp == NULL)
23668 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023669 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 return;
23671 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023672 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 {
23674 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23675 return;
23676 }
23677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 /* Delete the dict item that refers to the function, it will
23681 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023682 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 else
23685 func_free(fp);
23686 }
23687}
23688
23689/*
23690 * Free a function and remove it from the list of functions.
23691 */
23692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023693func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023694{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023695 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023696
23697 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023698 ga_clear_strings(&(fp->uf_args));
23699 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023700#ifdef FEAT_PROFILE
23701 vim_free(fp->uf_tml_count);
23702 vim_free(fp->uf_tml_total);
23703 vim_free(fp->uf_tml_self);
23704#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023705
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706 /* remove the function from the function hashtable */
23707 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23708 if (HASHITEM_EMPTY(hi))
23709 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023711 hash_remove(&func_hashtab, hi);
23712
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023713 vim_free(fp);
23714}
23715
23716/*
23717 * Unreference a Function: decrement the reference count and free it when it
23718 * becomes zero. Only for numbered functions.
23719 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023721func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023722{
23723 ufunc_T *fp;
23724
23725 if (name != NULL && isdigit(*name))
23726 {
23727 fp = find_func(name);
23728 if (fp == NULL)
23729 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023731 {
23732 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 * when "uf_calls" becomes zero. */
23734 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023735 func_free(fp);
23736 }
23737 }
23738}
23739
23740/*
23741 * Count a reference to a Function.
23742 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023744func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023745{
23746 ufunc_T *fp;
23747
23748 if (name != NULL && isdigit(*name))
23749 {
23750 fp = find_func(name);
23751 if (fp == NULL)
23752 EMSG2(_(e_intern2), "func_ref()");
23753 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 }
23756}
23757
23758/*
23759 * Call a user function.
23760 */
23761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023762call_user_func(
23763 ufunc_T *fp, /* pointer to function */
23764 int argcount, /* nr of args */
23765 typval_T *argvars, /* arguments */
23766 typval_T *rettv, /* return value */
23767 linenr_T firstline, /* first line of range */
23768 linenr_T lastline, /* last line of range */
23769 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770{
Bram Moolenaar33570922005-01-25 22:26:29 +000023771 char_u *save_sourcing_name;
23772 linenr_T save_sourcing_lnum;
23773 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023774 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023775 int save_did_emsg;
23776 static int depth = 0;
23777 dictitem_T *v;
23778 int fixvar_idx = 0; /* index in fixvar[] */
23779 int i;
23780 int ai;
23781 char_u numbuf[NUMBUFLEN];
23782 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023783 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784#ifdef FEAT_PROFILE
23785 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023786 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788
23789 /* If depth of calling is getting too high, don't execute the function */
23790 if (depth >= p_mfd)
23791 {
23792 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023793 rettv->v_type = VAR_NUMBER;
23794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 return;
23796 }
23797 ++depth;
23798
23799 line_breakcheck(); /* check for CTRL-C hit */
23800
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023801 fc = (funccall_T *)alloc(sizeof(funccall_T));
23802 fc->caller = current_funccal;
23803 current_funccal = fc;
23804 fc->func = fp;
23805 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023806 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 fc->linenr = 0;
23808 fc->returned = FALSE;
23809 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023811 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23812 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813
Bram Moolenaar33570922005-01-25 22:26:29 +000023814 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023815 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023816 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23817 * each argument variable and saves a lot of time.
23818 */
23819 /*
23820 * Init l: variables.
23821 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023822 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023823 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023824 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023825 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23826 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023827 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023828 name = v->di_key;
23829 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023830 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023831 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023833 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023834 v->di_tv.vval.v_dict = selfdict;
23835 ++selfdict->dv_refcount;
23836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023837
Bram Moolenaar33570922005-01-25 22:26:29 +000023838 /*
23839 * Init a: variables.
23840 * Set a:0 to "argcount".
23841 * Set a:000 to a list with room for the "..." arguments.
23842 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023843 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023844 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023845 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023846 /* Use "name" to avoid a warning from some compiler that checks the
23847 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023848 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023849 name = v->di_key;
23850 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023851 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023852 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023853 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023854 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023855 v->di_tv.vval.v_list = &fc->l_varlist;
23856 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23857 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23858 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023859
23860 /*
23861 * Set a:firstline to "firstline" and a:lastline to "lastline".
23862 * Set a:name to named arguments.
23863 * Set a:N to the "..." arguments.
23864 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023865 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023866 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023867 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023868 (varnumber_T)lastline);
23869 for (i = 0; i < argcount; ++i)
23870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023871 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023872 if (ai < 0)
23873 /* named argument a:name */
23874 name = FUNCARG(fp, i);
23875 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023876 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023877 /* "..." argument a:1, a:2, etc. */
23878 sprintf((char *)numbuf, "%d", ai + 1);
23879 name = numbuf;
23880 }
23881 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23882 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023883 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023884 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23885 }
23886 else
23887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023888 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23889 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023890 if (v == NULL)
23891 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023892 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023893 }
23894 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023895 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023897 /* Note: the values are copied directly to avoid alloc/free.
23898 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023899 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023900 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023901
23902 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23903 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023904 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23905 fc->l_listitems[ai].li_tv = argvars[i];
23906 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023907 }
23908 }
23909
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 /* Don't redraw while executing the function. */
23911 ++RedrawingDisabled;
23912 save_sourcing_name = sourcing_name;
23913 save_sourcing_lnum = sourcing_lnum;
23914 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023915 /* need space for function name + ("function " + 3) or "[number]" */
23916 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23917 + STRLEN(fp->uf_name) + 20;
23918 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 if (sourcing_name != NULL)
23920 {
23921 if (save_sourcing_name != NULL
23922 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023923 sprintf((char *)sourcing_name, "%s[%d]..",
23924 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 else
23926 STRCPY(sourcing_name, "function ");
23927 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23928
23929 if (p_verbose >= 12)
23930 {
23931 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023932 verbose_enter_scroll();
23933
Bram Moolenaar555b2802005-05-19 21:08:39 +000023934 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 if (p_verbose >= 14)
23936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023938 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023939 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023940 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941
23942 msg_puts((char_u *)"(");
23943 for (i = 0; i < argcount; ++i)
23944 {
23945 if (i > 0)
23946 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023947 if (argvars[i].v_type == VAR_NUMBER)
23948 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 else
23950 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023951 /* Do not want errors such as E724 here. */
23952 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023953 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023954 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023955 if (s != NULL)
23956 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023957 if (vim_strsize(s) > MSG_BUF_CLEN)
23958 {
23959 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23960 s = buf;
23961 }
23962 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023963 vim_free(tofree);
23964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 }
23966 }
23967 msg_puts((char_u *)")");
23968 }
23969 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023970
23971 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 --no_wait_return;
23973 }
23974 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023975#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023976 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977 {
23978 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23979 func_do_profile(fp);
23980 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023981 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023982 {
23983 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023984 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023985 profile_zero(&fp->uf_tm_children);
23986 }
23987 script_prof_save(&wait_start);
23988 }
23989#endif
23990
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023992 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 save_did_emsg = did_emsg;
23994 did_emsg = FALSE;
23995
23996 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023997 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23999
24000 --RedrawingDisabled;
24001
24002 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024003 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024005 clear_tv(rettv);
24006 rettv->v_type = VAR_NUMBER;
24007 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
24009
Bram Moolenaar05159a02005-02-26 23:04:13 +000024010#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024011 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024012 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024013 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024014 profile_end(&call_start);
24015 profile_sub_wait(&wait_start, &call_start);
24016 profile_add(&fp->uf_tm_total, &call_start);
24017 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024018 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024019 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024020 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24021 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024022 }
24023 }
24024#endif
24025
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 /* when being verbose, mention the return value */
24027 if (p_verbose >= 12)
24028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024030 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024033 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024034 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024035 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024036 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024039 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024040 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024041 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024042 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024043
Bram Moolenaar555b2802005-05-19 21:08:39 +000024044 /* The value may be very long. Skip the middle part, so that we
24045 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024046 * truncate it at the end. Don't want errors such as E724 here. */
24047 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024048 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024049 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024050 if (s != NULL)
24051 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024052 if (vim_strsize(s) > MSG_BUF_CLEN)
24053 {
24054 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24055 s = buf;
24056 }
24057 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024058 vim_free(tofree);
24059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 }
24061 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024062
24063 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 --no_wait_return;
24065 }
24066
24067 vim_free(sourcing_name);
24068 sourcing_name = save_sourcing_name;
24069 sourcing_lnum = save_sourcing_lnum;
24070 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024071#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024072 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024073 script_prof_restore(&wait_start);
24074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075
24076 if (p_verbose >= 12 && sourcing_name != NULL)
24077 {
24078 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024079 verbose_enter_scroll();
24080
Bram Moolenaar555b2802005-05-19 21:08:39 +000024081 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024083
24084 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 --no_wait_return;
24086 }
24087
24088 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024089 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024092 /* If the a:000 list and the l: and a: dicts are not referenced we can
24093 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024094 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24095 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24096 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24097 {
24098 free_funccal(fc, FALSE);
24099 }
24100 else
24101 {
24102 hashitem_T *hi;
24103 listitem_T *li;
24104 int todo;
24105
24106 /* "fc" is still in use. This can happen when returning "a:000" or
24107 * assigning "l:" to a global variable.
24108 * Link "fc" in the list for garbage collection later. */
24109 fc->caller = previous_funccal;
24110 previous_funccal = fc;
24111
24112 /* Make a copy of the a: variables, since we didn't do that above. */
24113 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24114 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24115 {
24116 if (!HASHITEM_EMPTY(hi))
24117 {
24118 --todo;
24119 v = HI2DI(hi);
24120 copy_tv(&v->di_tv, &v->di_tv);
24121 }
24122 }
24123
24124 /* Make a copy of the a:000 items, since we didn't do that above. */
24125 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24126 copy_tv(&li->li_tv, &li->li_tv);
24127 }
24128}
24129
24130/*
24131 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024132 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024133 */
24134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024135can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024136{
24137 return (fc->l_varlist.lv_copyID != copyID
24138 && fc->l_vars.dv_copyID != copyID
24139 && fc->l_avars.dv_copyID != copyID);
24140}
24141
24142/*
24143 * Free "fc" and what it contains.
24144 */
24145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146free_funccal(
24147 funccall_T *fc,
24148 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024149{
24150 listitem_T *li;
24151
24152 /* The a: variables typevals may not have been allocated, only free the
24153 * allocated variables. */
24154 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24155
24156 /* free all l: variables */
24157 vars_clear(&fc->l_vars.dv_hashtab);
24158
24159 /* Free the a:000 variables if they were allocated. */
24160 if (free_val)
24161 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24162 clear_tv(&li->li_tv);
24163
24164 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165}
24166
24167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024168 * Add a number variable "name" to dict "dp" with value "nr".
24169 */
24170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024171add_nr_var(
24172 dict_T *dp,
24173 dictitem_T *v,
24174 char *name,
24175 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024176{
24177 STRCPY(v->di_key, name);
24178 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24179 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24180 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024181 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024182 v->di_tv.vval.v_number = nr;
24183}
24184
24185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 * ":return [expr]"
24187 */
24188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024189ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190{
24191 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 int returning = FALSE;
24194
24195 if (current_funccal == NULL)
24196 {
24197 EMSG(_("E133: :return not inside a function"));
24198 return;
24199 }
24200
24201 if (eap->skip)
24202 ++emsg_skip;
24203
24204 eap->nextcmd = NULL;
24205 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024206 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 {
24208 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024209 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024211 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 }
24213 /* It's safer to return also on error. */
24214 else if (!eap->skip)
24215 {
24216 /*
24217 * Return unless the expression evaluation has been cancelled due to an
24218 * aborting error, an interrupt, or an exception.
24219 */
24220 if (!aborting())
24221 returning = do_return(eap, FALSE, TRUE, NULL);
24222 }
24223
24224 /* When skipping or the return gets pending, advance to the next command
24225 * in this line (!returning). Otherwise, ignore the rest of the line.
24226 * Following lines will be ignored by get_func_line(). */
24227 if (returning)
24228 eap->nextcmd = NULL;
24229 else if (eap->nextcmd == NULL) /* no argument */
24230 eap->nextcmd = check_nextcmd(arg);
24231
24232 if (eap->skip)
24233 --emsg_skip;
24234}
24235
24236/*
24237 * Return from a function. Possibly makes the return pending. Also called
24238 * for a pending return at the ":endtry" or after returning from an extra
24239 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024240 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024241 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 * FALSE when the return gets pending.
24243 */
24244 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024245do_return(
24246 exarg_T *eap,
24247 int reanimate,
24248 int is_cmd,
24249 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250{
24251 int idx;
24252 struct condstack *cstack = eap->cstack;
24253
24254 if (reanimate)
24255 /* Undo the return. */
24256 current_funccal->returned = FALSE;
24257
24258 /*
24259 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24260 * not in its finally clause (which then is to be executed next) is found.
24261 * In this case, make the ":return" pending for execution at the ":endtry".
24262 * Otherwise, return normally.
24263 */
24264 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24265 if (idx >= 0)
24266 {
24267 cstack->cs_pending[idx] = CSTP_RETURN;
24268
24269 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024270 /* A pending return again gets pending. "rettv" points to an
24271 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024273 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 else
24275 {
24276 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024277 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024279 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024281 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 {
24283 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024284 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024285 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 else
24287 EMSG(_(e_outofmem));
24288 }
24289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024290 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291
24292 if (reanimate)
24293 {
24294 /* The pending return value could be overwritten by a ":return"
24295 * without argument in a finally clause; reset the default
24296 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024297 current_funccal->rettv->v_type = VAR_NUMBER;
24298 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 }
24300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024301 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 }
24303 else
24304 {
24305 current_funccal->returned = TRUE;
24306
24307 /* If the return is carried out now, store the return value. For
24308 * a return immediately after reanimation, the value is already
24309 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024310 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024312 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024313 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024315 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 }
24317 }
24318
24319 return idx < 0;
24320}
24321
24322/*
24323 * Free the variable with a pending return value.
24324 */
24325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024326discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327{
Bram Moolenaar33570922005-01-25 22:26:29 +000024328 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329}
24330
24331/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024332 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 * is an allocated string. Used by report_pending() for verbose messages.
24334 */
24335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024336get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024338 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024339 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024342 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024343 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024344 if (s == NULL)
24345 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024346
24347 STRCPY(IObuff, ":return ");
24348 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24349 if (STRLEN(s) + 8 >= IOSIZE)
24350 STRCPY(IObuff + IOSIZE - 4, "...");
24351 vim_free(tofree);
24352 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353}
24354
24355/*
24356 * Get next function line.
24357 * Called by do_cmdline() to get the next line.
24358 * Returns allocated string, or NULL for end of function.
24359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024361get_func_line(
24362 int c UNUSED,
24363 void *cookie,
24364 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365{
Bram Moolenaar33570922005-01-25 22:26:29 +000024366 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367 ufunc_T *fp = fcp->func;
24368 char_u *retval;
24369 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370
24371 /* If breakpoints have been added/deleted need to check for it. */
24372 if (fcp->dbg_tick != debug_tick)
24373 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024374 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 sourcing_lnum);
24376 fcp->dbg_tick = debug_tick;
24377 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024378#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024379 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024380 func_line_end(cookie);
24381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382
Bram Moolenaar05159a02005-02-26 23:04:13 +000024383 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024384 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24385 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 retval = NULL;
24387 else
24388 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024389 /* Skip NULL lines (continuation lines). */
24390 while (fcp->linenr < gap->ga_len
24391 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24392 ++fcp->linenr;
24393 if (fcp->linenr >= gap->ga_len)
24394 retval = NULL;
24395 else
24396 {
24397 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24398 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024399#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024400 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024401 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 }
24405
24406 /* Did we encounter a breakpoint? */
24407 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24408 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024409 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024411 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 sourcing_lnum);
24413 fcp->dbg_tick = debug_tick;
24414 }
24415
24416 return retval;
24417}
24418
Bram Moolenaar05159a02005-02-26 23:04:13 +000024419#if defined(FEAT_PROFILE) || defined(PROTO)
24420/*
24421 * Called when starting to read a function line.
24422 * "sourcing_lnum" must be correct!
24423 * When skipping lines it may not actually be executed, but we won't find out
24424 * until later and we need to store the time now.
24425 */
24426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024427func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024428{
24429 funccall_T *fcp = (funccall_T *)cookie;
24430 ufunc_T *fp = fcp->func;
24431
24432 if (fp->uf_profiling && sourcing_lnum >= 1
24433 && sourcing_lnum <= fp->uf_lines.ga_len)
24434 {
24435 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024436 /* Skip continuation lines. */
24437 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24438 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024439 fp->uf_tml_execed = FALSE;
24440 profile_start(&fp->uf_tml_start);
24441 profile_zero(&fp->uf_tml_children);
24442 profile_get_wait(&fp->uf_tml_wait);
24443 }
24444}
24445
24446/*
24447 * Called when actually executing a function line.
24448 */
24449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024450func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024451{
24452 funccall_T *fcp = (funccall_T *)cookie;
24453 ufunc_T *fp = fcp->func;
24454
24455 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24456 fp->uf_tml_execed = TRUE;
24457}
24458
24459/*
24460 * Called when done with a function line.
24461 */
24462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024464{
24465 funccall_T *fcp = (funccall_T *)cookie;
24466 ufunc_T *fp = fcp->func;
24467
24468 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24469 {
24470 if (fp->uf_tml_execed)
24471 {
24472 ++fp->uf_tml_count[fp->uf_tml_idx];
24473 profile_end(&fp->uf_tml_start);
24474 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024475 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024476 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24477 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024478 }
24479 fp->uf_tml_idx = -1;
24480 }
24481}
24482#endif
24483
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484/*
24485 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024486 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 */
24488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024489func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490{
Bram Moolenaar33570922005-01-25 22:26:29 +000024491 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492
24493 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24494 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024495 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 || fcp->returned);
24497}
24498
24499/*
24500 * return TRUE if cookie indicates a function which "abort"s on errors.
24501 */
24502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024503func_has_abort(
24504 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024506 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507}
24508
24509#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24510typedef enum
24511{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024512 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24513 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24514 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515} var_flavour_T;
24516
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024517static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518
24519 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024520var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521{
24522 char_u *p = varname;
24523
24524 if (ASCII_ISUPPER(*p))
24525 {
24526 while (*(++p))
24527 if (ASCII_ISLOWER(*p))
24528 return VAR_FLAVOUR_SESSION;
24529 return VAR_FLAVOUR_VIMINFO;
24530 }
24531 else
24532 return VAR_FLAVOUR_DEFAULT;
24533}
24534#endif
24535
24536#if defined(FEAT_VIMINFO) || defined(PROTO)
24537/*
24538 * Restore global vars that start with a capital from the viminfo file
24539 */
24540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024541read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542{
24543 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024544 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024545 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024546 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547
24548 if (!writing && (find_viminfo_parameter('!') != NULL))
24549 {
24550 tab = vim_strchr(virp->vir_line + 1, '\t');
24551 if (tab != NULL)
24552 {
24553 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024554 switch (*tab)
24555 {
24556 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024557#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024558 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024559#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024560 case 'D': type = VAR_DICT; break;
24561 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024562 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564
24565 tab = vim_strchr(tab, '\t');
24566 if (tab != NULL)
24567 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024568 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024569 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024570 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024572#ifdef FEAT_FLOAT
24573 else if (type == VAR_FLOAT)
24574 (void)string2float(tab + 1, &tv.vval.v_float);
24575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024577 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024578 if (type == VAR_DICT || type == VAR_LIST)
24579 {
24580 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24581
24582 if (etv == NULL)
24583 /* Failed to parse back the dict or list, use it as a
24584 * string. */
24585 tv.v_type = VAR_STRING;
24586 else
24587 {
24588 vim_free(tv.vval.v_string);
24589 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024590 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024591 }
24592 }
24593
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024594 /* when in a function use global variables */
24595 save_funccal = current_funccal;
24596 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024597 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024598 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024599
24600 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024601 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024602 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24603 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 }
24605 }
24606 }
24607
24608 return viminfo_readline(virp);
24609}
24610
24611/*
24612 * Write global vars that start with a capital to the viminfo file
24613 */
24614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024615write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616{
Bram Moolenaar33570922005-01-25 22:26:29 +000024617 hashitem_T *hi;
24618 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024619 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024620 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024621 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024622 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624
24625 if (find_viminfo_parameter('!') == NULL)
24626 return;
24627
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024628 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024629
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024630 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024631 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024633 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024635 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024636 this_var = HI2DI(hi);
24637 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024639 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024640 {
24641 case VAR_STRING: s = "STR"; break;
24642 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024643#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024644 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024645#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024646 case VAR_DICT: s = "DIC"; break;
24647 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024648 case VAR_SPECIAL: s = "XPL"; break;
24649
24650 case VAR_UNKNOWN:
24651 case VAR_FUNC:
24652 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024653 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024654 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024655 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024656 if (p != NULL)
24657 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024658 vim_free(tofree);
24659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660 }
24661 }
24662}
24663#endif
24664
24665#if defined(FEAT_SESSION) || defined(PROTO)
24666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024667store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668{
Bram Moolenaar33570922005-01-25 22:26:29 +000024669 hashitem_T *hi;
24670 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024671 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 char_u *p, *t;
24673
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024674 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024675 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024677 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024679 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024680 this_var = HI2DI(hi);
24681 if ((this_var->di_tv.v_type == VAR_NUMBER
24682 || this_var->di_tv.v_type == VAR_STRING)
24683 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024684 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024685 /* Escape special characters with a backslash. Turn a LF and
24686 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024687 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024688 (char_u *)"\\\"\n\r");
24689 if (p == NULL) /* out of memory */
24690 break;
24691 for (t = p; *t != NUL; ++t)
24692 if (*t == '\n')
24693 *t = 'n';
24694 else if (*t == '\r')
24695 *t = 'r';
24696 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024697 this_var->di_key,
24698 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24699 : ' ',
24700 p,
24701 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24702 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024703 || put_eol(fd) == FAIL)
24704 {
24705 vim_free(p);
24706 return FAIL;
24707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 vim_free(p);
24709 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024710#ifdef FEAT_FLOAT
24711 else if (this_var->di_tv.v_type == VAR_FLOAT
24712 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24713 {
24714 float_T f = this_var->di_tv.vval.v_float;
24715 int sign = ' ';
24716
24717 if (f < 0)
24718 {
24719 f = -f;
24720 sign = '-';
24721 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024722 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024723 this_var->di_key, sign, f) < 0)
24724 || put_eol(fd) == FAIL)
24725 return FAIL;
24726 }
24727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 }
24729 }
24730 return OK;
24731}
24732#endif
24733
Bram Moolenaar661b1822005-07-28 22:36:45 +000024734/*
24735 * Display script name where an item was last set.
24736 * Should only be invoked when 'verbose' is non-zero.
24737 */
24738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024739last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024740{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024741 char_u *p;
24742
Bram Moolenaar661b1822005-07-28 22:36:45 +000024743 if (scriptID != 0)
24744 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024745 p = home_replace_save(NULL, get_scriptname(scriptID));
24746 if (p != NULL)
24747 {
24748 verbose_enter();
24749 MSG_PUTS(_("\n\tLast set from "));
24750 MSG_PUTS(p);
24751 vim_free(p);
24752 verbose_leave();
24753 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024754 }
24755}
24756
Bram Moolenaard812df62008-11-09 12:46:09 +000024757/*
24758 * List v:oldfiles in a nice way.
24759 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024761ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024762{
24763 list_T *l = vimvars[VV_OLDFILES].vv_list;
24764 listitem_T *li;
24765 int nr = 0;
24766
24767 if (l == NULL)
24768 msg((char_u *)_("No old files"));
24769 else
24770 {
24771 msg_start();
24772 msg_scroll = TRUE;
24773 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24774 {
24775 msg_outnum((long)++nr);
24776 MSG_PUTS(": ");
24777 msg_outtrans(get_tv_string(&li->li_tv));
24778 msg_putchar('\n');
24779 out_flush(); /* output one line at a time */
24780 ui_breakcheck();
24781 }
24782 /* Assume "got_int" was set to truncate the listing. */
24783 got_int = FALSE;
24784
24785#ifdef FEAT_BROWSE_CMD
24786 if (cmdmod.browse)
24787 {
24788 quit_more = FALSE;
24789 nr = prompt_for_number(FALSE);
24790 msg_starthere();
24791 if (nr > 0)
24792 {
24793 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24794 (long)nr);
24795
24796 if (p != NULL)
24797 {
24798 p = expand_env_save(p);
24799 eap->arg = p;
24800 eap->cmdidx = CMD_edit;
24801 cmdmod.browse = FALSE;
24802 do_exedit(eap, NULL);
24803 vim_free(p);
24804 }
24805 }
24806 }
24807#endif
24808 }
24809}
24810
Bram Moolenaar53744302015-07-17 17:38:22 +020024811/* reset v:option_new, v:option_old and v:option_type */
24812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024813reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024814{
24815 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24816 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24817 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24818}
24819
24820
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821#endif /* FEAT_EVAL */
24822
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024824#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825
24826#ifdef WIN3264
24827/*
24828 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24829 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024830static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24831static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24832static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833
24834/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835 * Get the short path (8.3) for the filename in "fnamep".
24836 * Only works for a valid file name.
24837 * When the path gets longer "fnamep" is changed and the allocated buffer
24838 * is put in "bufp".
24839 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24840 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 */
24842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024843get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024845 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 char_u *newbuf;
24847
24848 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849 l = GetShortPathName(*fnamep, *fnamep, len);
24850 if (l > len - 1)
24851 {
24852 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 newbuf = vim_strnsave(*fnamep, l);
24855 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024856 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857
24858 vim_free(*bufp);
24859 *fnamep = *bufp = newbuf;
24860
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024861 /* Really should always succeed, as the buffer is big enough. */
24862 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863 }
24864
24865 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024866 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867}
24868
24869/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024870 * Get the short path (8.3) for the filename in "fname". The converted
24871 * path is returned in "bufp".
24872 *
24873 * Some of the directories specified in "fname" may not exist. This function
24874 * will shorten the existing directories at the beginning of the path and then
24875 * append the remaining non-existing path.
24876 *
24877 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024878 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879 * bufp - Pointer to an allocated buffer for the filename.
24880 * fnamelen - Length of the filename pointed to by fname
24881 *
24882 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 */
24884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024885shortpath_for_invalid_fname(
24886 char_u **fname,
24887 char_u **bufp,
24888 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024890 char_u *short_fname, *save_fname, *pbuf_unused;
24891 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024893 int old_len, len;
24894 int new_len, sfx_len;
24895 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896
24897 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024898 old_len = *fnamelen;
24899 save_fname = vim_strnsave(*fname, old_len);
24900 pbuf_unused = NULL;
24901 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024903 endp = save_fname + old_len - 1; /* Find the end of the copy */
24904 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024906 /*
24907 * Try shortening the supplied path till it succeeds by removing one
24908 * directory at a time from the tail of the path.
24909 */
24910 len = 0;
24911 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024913 /* go back one path-separator */
24914 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24915 --endp;
24916 if (endp <= save_fname)
24917 break; /* processed the complete path */
24918
24919 /*
24920 * Replace the path separator with a NUL and try to shorten the
24921 * resulting path.
24922 */
24923 ch = *endp;
24924 *endp = 0;
24925 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024926 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024927 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24928 {
24929 retval = FAIL;
24930 goto theend;
24931 }
24932 *endp = ch; /* preserve the string */
24933
24934 if (len > 0)
24935 break; /* successfully shortened the path */
24936
24937 /* failed to shorten the path. Skip the path separator */
24938 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 }
24940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024941 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024943 /*
24944 * Succeeded in shortening the path. Now concatenate the shortened
24945 * path with the remaining path at the tail.
24946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024948 /* Compute the length of the new path. */
24949 sfx_len = (int)(save_endp - endp) + 1;
24950 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024952 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024954 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024956 /* There is not enough space in the currently allocated string,
24957 * copy it to a buffer big enough. */
24958 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024960 {
24961 retval = FAIL;
24962 goto theend;
24963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 }
24965 else
24966 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024967 /* Transfer short_fname to the main buffer (it's big enough),
24968 * unless get_short_pathname() did its work in-place. */
24969 *fname = *bufp = save_fname;
24970 if (short_fname != save_fname)
24971 vim_strncpy(save_fname, short_fname, len);
24972 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024974
24975 /* concat the not-shortened part of the path */
24976 vim_strncpy(*fname + len, endp, sfx_len);
24977 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024979
24980theend:
24981 vim_free(pbuf_unused);
24982 vim_free(save_fname);
24983
24984 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985}
24986
24987/*
24988 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024989 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 */
24991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024992shortpath_for_partial(
24993 char_u **fnamep,
24994 char_u **bufp,
24995 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996{
24997 int sepcount, len, tflen;
24998 char_u *p;
24999 char_u *pbuf, *tfname;
25000 int hasTilde;
25001
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025002 /* Count up the path separators from the RHS.. so we know which part
25003 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025005 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 if (vim_ispathsep(*p))
25007 ++sepcount;
25008
25009 /* Need full path first (use expand_env() to remove a "~/") */
25010 hasTilde = (**fnamep == '~');
25011 if (hasTilde)
25012 pbuf = tfname = expand_env_save(*fnamep);
25013 else
25014 pbuf = tfname = FullName_save(*fnamep, FALSE);
25015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025016 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025018 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020
25021 if (len == 0)
25022 {
25023 /* Don't have a valid filename, so shorten the rest of the
25024 * path if we can. This CAN give us invalid 8.3 filenames, but
25025 * there's not a lot of point in guessing what it might be.
25026 */
25027 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025028 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030 }
25031
25032 /* Count the paths backward to find the beginning of the desired string. */
25033 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025034 {
25035#ifdef FEAT_MBYTE
25036 if (has_mbyte)
25037 p -= mb_head_off(tfname, p);
25038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 if (vim_ispathsep(*p))
25040 {
25041 if (sepcount == 0 || (hasTilde && sepcount == 1))
25042 break;
25043 else
25044 sepcount --;
25045 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047 if (hasTilde)
25048 {
25049 --p;
25050 if (p >= tfname)
25051 *p = '~';
25052 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025053 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 }
25055 else
25056 ++p;
25057
25058 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25059 vim_free(*bufp);
25060 *fnamelen = (int)STRLEN(p);
25061 *bufp = pbuf;
25062 *fnamep = p;
25063
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025064 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065}
25066#endif /* WIN3264 */
25067
25068/*
25069 * Adjust a filename, according to a string of modifiers.
25070 * *fnamep must be NUL terminated when called. When returning, the length is
25071 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025072 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073 * When there is an error, *fnamep is set to NULL.
25074 */
25075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025076modify_fname(
25077 char_u *src, /* string with modifiers */
25078 int *usedlen, /* characters after src that are used */
25079 char_u **fnamep, /* file name so far */
25080 char_u **bufp, /* buffer for allocated file name or NULL */
25081 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082{
25083 int valid = 0;
25084 char_u *tail;
25085 char_u *s, *p, *pbuf;
25086 char_u dirname[MAXPATHL];
25087 int c;
25088 int has_fullname = 0;
25089#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025090 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 int has_shortname = 0;
25092#endif
25093
25094repeat:
25095 /* ":p" - full path/file_name */
25096 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25097 {
25098 has_fullname = 1;
25099
25100 valid |= VALID_PATH;
25101 *usedlen += 2;
25102
25103 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25104 if ((*fnamep)[0] == '~'
25105#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25106 && ((*fnamep)[1] == '/'
25107# ifdef BACKSLASH_IN_FILENAME
25108 || (*fnamep)[1] == '\\'
25109# endif
25110 || (*fnamep)[1] == NUL)
25111
25112#endif
25113 )
25114 {
25115 *fnamep = expand_env_save(*fnamep);
25116 vim_free(*bufp); /* free any allocated file name */
25117 *bufp = *fnamep;
25118 if (*fnamep == NULL)
25119 return -1;
25120 }
25121
25122 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025123 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 {
25125 if (vim_ispathsep(*p)
25126 && p[1] == '.'
25127 && (p[2] == NUL
25128 || vim_ispathsep(p[2])
25129 || (p[2] == '.'
25130 && (p[3] == NUL || vim_ispathsep(p[3])))))
25131 break;
25132 }
25133
25134 /* FullName_save() is slow, don't use it when not needed. */
25135 if (*p != NUL || !vim_isAbsName(*fnamep))
25136 {
25137 *fnamep = FullName_save(*fnamep, *p != NUL);
25138 vim_free(*bufp); /* free any allocated file name */
25139 *bufp = *fnamep;
25140 if (*fnamep == NULL)
25141 return -1;
25142 }
25143
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025144#ifdef WIN3264
25145# if _WIN32_WINNT >= 0x0500
25146 if (vim_strchr(*fnamep, '~') != NULL)
25147 {
25148 /* Expand 8.3 filename to full path. Needed to make sure the same
25149 * file does not have two different names.
25150 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25151 p = alloc(_MAX_PATH + 1);
25152 if (p != NULL)
25153 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025154 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025155 {
25156 vim_free(*bufp);
25157 *bufp = *fnamep = p;
25158 }
25159 else
25160 vim_free(p);
25161 }
25162 }
25163# endif
25164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 /* Append a path separator to a directory. */
25166 if (mch_isdir(*fnamep))
25167 {
25168 /* Make room for one or two extra characters. */
25169 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25170 vim_free(*bufp); /* free any allocated file name */
25171 *bufp = *fnamep;
25172 if (*fnamep == NULL)
25173 return -1;
25174 add_pathsep(*fnamep);
25175 }
25176 }
25177
25178 /* ":." - path relative to the current directory */
25179 /* ":~" - path relative to the home directory */
25180 /* ":8" - shortname path - postponed till after */
25181 while (src[*usedlen] == ':'
25182 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25183 {
25184 *usedlen += 2;
25185 if (c == '8')
25186 {
25187#ifdef WIN3264
25188 has_shortname = 1; /* Postpone this. */
25189#endif
25190 continue;
25191 }
25192 pbuf = NULL;
25193 /* Need full path first (use expand_env() to remove a "~/") */
25194 if (!has_fullname)
25195 {
25196 if (c == '.' && **fnamep == '~')
25197 p = pbuf = expand_env_save(*fnamep);
25198 else
25199 p = pbuf = FullName_save(*fnamep, FALSE);
25200 }
25201 else
25202 p = *fnamep;
25203
25204 has_fullname = 0;
25205
25206 if (p != NULL)
25207 {
25208 if (c == '.')
25209 {
25210 mch_dirname(dirname, MAXPATHL);
25211 s = shorten_fname(p, dirname);
25212 if (s != NULL)
25213 {
25214 *fnamep = s;
25215 if (pbuf != NULL)
25216 {
25217 vim_free(*bufp); /* free any allocated file name */
25218 *bufp = pbuf;
25219 pbuf = NULL;
25220 }
25221 }
25222 }
25223 else
25224 {
25225 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25226 /* Only replace it when it starts with '~' */
25227 if (*dirname == '~')
25228 {
25229 s = vim_strsave(dirname);
25230 if (s != NULL)
25231 {
25232 *fnamep = s;
25233 vim_free(*bufp);
25234 *bufp = s;
25235 }
25236 }
25237 }
25238 vim_free(pbuf);
25239 }
25240 }
25241
25242 tail = gettail(*fnamep);
25243 *fnamelen = (int)STRLEN(*fnamep);
25244
25245 /* ":h" - head, remove "/file_name", can be repeated */
25246 /* Don't remove the first "/" or "c:\" */
25247 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25248 {
25249 valid |= VALID_HEAD;
25250 *usedlen += 2;
25251 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025252 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025253 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 *fnamelen = (int)(tail - *fnamep);
25255#ifdef VMS
25256 if (*fnamelen > 0)
25257 *fnamelen += 1; /* the path separator is part of the path */
25258#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025259 if (*fnamelen == 0)
25260 {
25261 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25262 p = vim_strsave((char_u *)".");
25263 if (p == NULL)
25264 return -1;
25265 vim_free(*bufp);
25266 *bufp = *fnamep = tail = p;
25267 *fnamelen = 1;
25268 }
25269 else
25270 {
25271 while (tail > s && !after_pathsep(s, tail))
25272 mb_ptr_back(*fnamep, tail);
25273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 }
25275
25276 /* ":8" - shortname */
25277 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25278 {
25279 *usedlen += 2;
25280#ifdef WIN3264
25281 has_shortname = 1;
25282#endif
25283 }
25284
25285#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025286 /*
25287 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 */
25289 if (has_shortname)
25290 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025291 /* Copy the string if it is shortened by :h and when it wasn't copied
25292 * yet, because we are going to change it in place. Avoids changing
25293 * the buffer name for "%:8". */
25294 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 {
25296 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025297 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 return -1;
25299 vim_free(*bufp);
25300 *bufp = *fnamep = p;
25301 }
25302
25303 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025304 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 if (!has_fullname && !vim_isAbsName(*fnamep))
25306 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025307 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 return -1;
25309 }
25310 else
25311 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025312 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313
Bram Moolenaardc935552011-08-17 15:23:23 +020025314 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025316 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 return -1;
25318
25319 if (l == 0)
25320 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025321 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324 return -1;
25325 }
25326 *fnamelen = l;
25327 }
25328 }
25329#endif /* WIN3264 */
25330
25331 /* ":t" - tail, just the basename */
25332 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25333 {
25334 *usedlen += 2;
25335 *fnamelen -= (int)(tail - *fnamep);
25336 *fnamep = tail;
25337 }
25338
25339 /* ":e" - extension, can be repeated */
25340 /* ":r" - root, without extension, can be repeated */
25341 while (src[*usedlen] == ':'
25342 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25343 {
25344 /* find a '.' in the tail:
25345 * - for second :e: before the current fname
25346 * - otherwise: The last '.'
25347 */
25348 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25349 s = *fnamep - 2;
25350 else
25351 s = *fnamep + *fnamelen - 1;
25352 for ( ; s > tail; --s)
25353 if (s[0] == '.')
25354 break;
25355 if (src[*usedlen + 1] == 'e') /* :e */
25356 {
25357 if (s > tail)
25358 {
25359 *fnamelen += (int)(*fnamep - (s + 1));
25360 *fnamep = s + 1;
25361#ifdef VMS
25362 /* cut version from the extension */
25363 s = *fnamep + *fnamelen - 1;
25364 for ( ; s > *fnamep; --s)
25365 if (s[0] == ';')
25366 break;
25367 if (s > *fnamep)
25368 *fnamelen = s - *fnamep;
25369#endif
25370 }
25371 else if (*fnamep <= tail)
25372 *fnamelen = 0;
25373 }
25374 else /* :r */
25375 {
25376 if (s > tail) /* remove one extension */
25377 *fnamelen = (int)(s - *fnamep);
25378 }
25379 *usedlen += 2;
25380 }
25381
25382 /* ":s?pat?foo?" - substitute */
25383 /* ":gs?pat?foo?" - global substitute */
25384 if (src[*usedlen] == ':'
25385 && (src[*usedlen + 1] == 's'
25386 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25387 {
25388 char_u *str;
25389 char_u *pat;
25390 char_u *sub;
25391 int sep;
25392 char_u *flags;
25393 int didit = FALSE;
25394
25395 flags = (char_u *)"";
25396 s = src + *usedlen + 2;
25397 if (src[*usedlen + 1] == 'g')
25398 {
25399 flags = (char_u *)"g";
25400 ++s;
25401 }
25402
25403 sep = *s++;
25404 if (sep)
25405 {
25406 /* find end of pattern */
25407 p = vim_strchr(s, sep);
25408 if (p != NULL)
25409 {
25410 pat = vim_strnsave(s, (int)(p - s));
25411 if (pat != NULL)
25412 {
25413 s = p + 1;
25414 /* find end of substitution */
25415 p = vim_strchr(s, sep);
25416 if (p != NULL)
25417 {
25418 sub = vim_strnsave(s, (int)(p - s));
25419 str = vim_strnsave(*fnamep, *fnamelen);
25420 if (sub != NULL && str != NULL)
25421 {
25422 *usedlen = (int)(p + 1 - src);
25423 s = do_string_sub(str, pat, sub, flags);
25424 if (s != NULL)
25425 {
25426 *fnamep = s;
25427 *fnamelen = (int)STRLEN(s);
25428 vim_free(*bufp);
25429 *bufp = s;
25430 didit = TRUE;
25431 }
25432 }
25433 vim_free(sub);
25434 vim_free(str);
25435 }
25436 vim_free(pat);
25437 }
25438 }
25439 /* after using ":s", repeat all the modifiers */
25440 if (didit)
25441 goto repeat;
25442 }
25443 }
25444
Bram Moolenaar26df0922014-02-23 23:39:13 +010025445 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25446 {
25447 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25448 if (p == NULL)
25449 return -1;
25450 vim_free(*bufp);
25451 *bufp = *fnamep = p;
25452 *fnamelen = (int)STRLEN(p);
25453 *usedlen += 2;
25454 }
25455
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 return valid;
25457}
25458
25459/*
25460 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25461 * "flags" can be "g" to do a global substitute.
25462 * Returns an allocated string, NULL for error.
25463 */
25464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025465do_string_sub(
25466 char_u *str,
25467 char_u *pat,
25468 char_u *sub,
25469 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470{
25471 int sublen;
25472 regmatch_T regmatch;
25473 int i;
25474 int do_all;
25475 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025476 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 garray_T ga;
25478 char_u *ret;
25479 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025480 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481
25482 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25483 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025484 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485
25486 ga_init2(&ga, 1, 200);
25487
25488 do_all = (flags[0] == 'g');
25489
25490 regmatch.rm_ic = p_ic;
25491 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25492 if (regmatch.regprog != NULL)
25493 {
25494 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025495 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25497 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025498 /* Skip empty match except for first match. */
25499 if (regmatch.startp[0] == regmatch.endp[0])
25500 {
25501 if (zero_width == regmatch.startp[0])
25502 {
25503 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025504 i = MB_PTR2LEN(tail);
25505 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25506 (size_t)i);
25507 ga.ga_len += i;
25508 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025509 continue;
25510 }
25511 zero_width = regmatch.startp[0];
25512 }
25513
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 /*
25515 * Get some space for a temporary buffer to do the substitution
25516 * into. It will contain:
25517 * - The text up to where the match is.
25518 * - The substituted text.
25519 * - The text after the match.
25520 */
25521 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025522 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25524 {
25525 ga_clear(&ga);
25526 break;
25527 }
25528
25529 /* copy the text up to where the match is */
25530 i = (int)(regmatch.startp[0] - tail);
25531 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25532 /* add the substituted text */
25533 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25534 + ga.ga_len + i, TRUE, TRUE, FALSE);
25535 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025536 tail = regmatch.endp[0];
25537 if (*tail == NUL)
25538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 if (!do_all)
25540 break;
25541 }
25542
25543 if (ga.ga_data != NULL)
25544 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25545
Bram Moolenaar473de612013-06-08 18:19:48 +020025546 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 }
25548
25549 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25550 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025551 if (p_cpo == empty_option)
25552 p_cpo = save_cpo;
25553 else
25554 /* Darn, evaluating {sub} expression changed the value. */
25555 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556
25557 return ret;
25558}
25559
25560#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */