blob: 7ce739ce785c43fa0b31d2cca4182bed379b8ee6 [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
9219 if (argvars[0].v_type != VAR_NUMBER
9220 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9221 || error)
9222 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009223 prepare_assert_error(&ga);
9224 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009225 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009226 NULL, &argvars[0]);
9227 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009228 ga_clear(&ga);
9229 }
9230}
9231
9232/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009237{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009238 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009239}
9240
9241/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009242 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009243 */
9244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009245f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009246{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009248}
9249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009250#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009251/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009252 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009255f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009256{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009257 float_T f;
9258
9259 rettv->v_type = VAR_FLOAT;
9260 if (get_float_arg(argvars, &f) == OK)
9261 rettv->vval.v_float = asin(f);
9262 else
9263 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009264}
9265
9266/*
9267 * "atan()" function
9268 */
9269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009270f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271{
9272 float_T f;
9273
9274 rettv->v_type = VAR_FLOAT;
9275 if (get_float_arg(argvars, &f) == OK)
9276 rettv->vval.v_float = atan(f);
9277 else
9278 rettv->vval.v_float = 0.0;
9279}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009280
9281/*
9282 * "atan2()" function
9283 */
9284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009285f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009286{
9287 float_T fx, fy;
9288
9289 rettv->v_type = VAR_FLOAT;
9290 if (get_float_arg(argvars, &fx) == OK
9291 && get_float_arg(&argvars[1], &fy) == OK)
9292 rettv->vval.v_float = atan2(fx, fy);
9293 else
9294 rettv->vval.v_float = 0.0;
9295}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#endif
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298/*
9299 * "browse(save, title, initdir, default)" function
9300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009302f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304#ifdef FEAT_BROWSE
9305 int save;
9306 char_u *title;
9307 char_u *initdir;
9308 char_u *defname;
9309 char_u buf[NUMBUFLEN];
9310 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 save = get_tv_number_chk(&argvars[0], &error);
9314 title = get_tv_string_chk(&argvars[1]);
9315 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9316 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 if (error || title == NULL || initdir == NULL || defname == NULL)
9319 rettv->vval.v_string = NULL;
9320 else
9321 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009322 do_browse(save ? BROWSE_SAVE : 0,
9323 title, defname, NULL, initdir, NULL, curbuf);
9324#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009326#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009328}
9329
9330/*
9331 * "browsedir(title, initdir)" function
9332 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009334f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009335{
9336#ifdef FEAT_BROWSE
9337 char_u *title;
9338 char_u *initdir;
9339 char_u buf[NUMBUFLEN];
9340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 title = get_tv_string_chk(&argvars[0]);
9342 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 if (title == NULL || initdir == NULL)
9345 rettv->vval.v_string = NULL;
9346 else
9347 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009348 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353}
9354
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009355static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009356
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357/*
9358 * Find a buffer by number or exact name.
9359 */
9360 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009361find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009365 if (avar->v_type == VAR_NUMBER)
9366 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009367 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009369 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009370 if (buf == NULL)
9371 {
9372 /* No full path name match, try a match with a URL or a "nofile"
9373 * buffer, these don't use the full path. */
9374 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9375 if (buf->b_fname != NULL
9376 && (path_with_url(buf->b_fname)
9377#ifdef FEAT_QUICKFIX
9378 || bt_nofile(buf)
9379#endif
9380 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009381 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009382 break;
9383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 }
9385 return buf;
9386}
9387
9388/*
9389 * "bufexists(expr)" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
9398 * "buflisted(expr)" function
9399 */
9400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009401f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 buf_T *buf;
9404
9405 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
9410 * "bufloaded(expr)" function
9411 */
9412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009413f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 buf_T *buf;
9416
9417 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009421static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423/*
9424 * Get buffer by number or pattern.
9425 */
9426 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009427get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 int save_magic;
9431 char_u *save_cpo;
9432 buf_T *buf;
9433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 if (tv->v_type == VAR_NUMBER)
9435 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009436 if (tv->v_type != VAR_STRING)
9437 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 if (name == NULL || *name == NUL)
9439 return curbuf;
9440 if (name[0] == '$' && name[1] == NUL)
9441 return lastbuf;
9442
9443 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9444 save_magic = p_magic;
9445 p_magic = TRUE;
9446 save_cpo = p_cpo;
9447 p_cpo = (char_u *)"";
9448
9449 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009450 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
9452 p_magic = save_magic;
9453 p_cpo = save_cpo;
9454
9455 /* If not found, try expanding the name, like done for bufexists(). */
9456 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458
9459 return buf;
9460}
9461
9462/*
9463 * "bufname(expr)" function
9464 */
9465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 buf_T *buf;
9469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009472 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 --emsg_off;
9479}
9480
9481/*
9482 * "bufnr(expr)" function
9483 */
9484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009485f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009488 int error = FALSE;
9489 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009493 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009494 --emsg_off;
9495
9496 /* If the buffer isn't found and the second argument is not zero create a
9497 * new buffer. */
9498 if (buf == NULL
9499 && argvars[1].v_type != VAR_UNKNOWN
9500 && get_tv_number_chk(&argvars[1], &error) != 0
9501 && !error
9502 && (name = get_tv_string_chk(&argvars[0])) != NULL
9503 && !error)
9504 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9505
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510}
9511
9512/*
9513 * "bufwinnr(nr)" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518#ifdef FEAT_WINDOWS
9519 win_T *wp;
9520 int winnr = 0;
9521#endif
9522 buf_T *buf;
9523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009526 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527#ifdef FEAT_WINDOWS
9528 for (wp = firstwin; wp; wp = wp->w_next)
9529 {
9530 ++winnr;
9531 if (wp->w_buffer == buf)
9532 break;
9533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#endif
9538 --emsg_off;
9539}
9540
9541/*
9542 * "byte2line(byte)" function
9543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#else
9550 long boff = 0;
9551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 (linenr_T)0, &boff);
9558#endif
9559}
9560
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009562byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009563{
9564#ifdef FEAT_MBYTE
9565 char_u *t;
9566#endif
9567 char_u *str;
9568 long idx;
9569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 str = get_tv_string_chk(&argvars[0]);
9571 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009574 return;
9575
9576#ifdef FEAT_MBYTE
9577 t = str;
9578 for ( ; idx > 0; idx--)
9579 {
9580 if (*t == NUL) /* EOL reached */
9581 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009582 if (enc_utf8 && comp)
9583 t += utf_ptr2len(t);
9584 else
9585 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009586 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009587 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009588#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009589 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009591#endif
9592}
9593
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009594/*
9595 * "byteidx()" function
9596 */
9597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009598f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009599{
9600 byteidx(argvars, rettv, FALSE);
9601}
9602
9603/*
9604 * "byteidxcomp()" function
9605 */
9606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009608{
9609 byteidx(argvars, rettv, TRUE);
9610}
9611
Bram Moolenaardb913952012-06-29 12:54:53 +02009612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613func_call(
9614 char_u *name,
9615 typval_T *args,
9616 dict_T *selfdict,
9617 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009618{
9619 listitem_T *item;
9620 typval_T argv[MAX_FUNC_ARGS + 1];
9621 int argc = 0;
9622 int dummy;
9623 int r = 0;
9624
9625 for (item = args->vval.v_list->lv_first; item != NULL;
9626 item = item->li_next)
9627 {
9628 if (argc == MAX_FUNC_ARGS)
9629 {
9630 EMSG(_("E699: Too many arguments"));
9631 break;
9632 }
9633 /* Make a copy of each argument. This is needed to be able to set
9634 * v_lock to VAR_FIXED in the copy without changing the original list.
9635 */
9636 copy_tv(&item->li_tv, &argv[argc++]);
9637 }
9638
9639 if (item == NULL)
9640 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9641 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9642 &dummy, TRUE, selfdict);
9643
9644 /* Free the arguments. */
9645 while (argc > 0)
9646 clear_tv(&argv[--argc]);
9647
9648 return r;
9649}
9650
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009651/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009652 * "call(func, arglist)" function
9653 */
9654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009656{
9657 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660 if (argvars[1].v_type != VAR_LIST)
9661 {
9662 EMSG(_(e_listreq));
9663 return;
9664 }
9665 if (argvars[1].vval.v_list == NULL)
9666 return;
9667
9668 if (argvars[0].v_type == VAR_FUNC)
9669 func = argvars[0].vval.v_string;
9670 else
9671 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 if (*func == NUL)
9673 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674
Bram Moolenaare9a41262005-01-15 22:18:47 +00009675 if (argvars[2].v_type != VAR_UNKNOWN)
9676 {
9677 if (argvars[2].v_type != VAR_DICT)
9678 {
9679 EMSG(_(e_dictreq));
9680 return;
9681 }
9682 selfdict = argvars[2].vval.v_dict;
9683 }
9684
Bram Moolenaardb913952012-06-29 12:54:53 +02009685 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009686}
9687
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009688#ifdef FEAT_FLOAT
9689/*
9690 * "ceil({float})" function
9691 */
9692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009693f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009694{
9695 float_T f;
9696
9697 rettv->v_type = VAR_FLOAT;
9698 if (get_float_arg(argvars, &f) == OK)
9699 rettv->vval.v_float = ceil(f);
9700 else
9701 rettv->vval.v_float = 0.0;
9702}
9703#endif
9704
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009705#ifdef FEAT_CHANNEL
9706/*
9707 * Get the channel index from the handle argument.
9708 * Returns -1 if the handle is invalid or the channel is closed.
9709 */
9710 static int
9711get_channel_arg(typval_T *tv)
9712{
9713 int ch_idx;
9714
9715 if (tv->v_type != VAR_NUMBER)
9716 {
9717 EMSG2(_(e_invarg2), get_tv_string(tv));
9718 return -1;
9719 }
9720 ch_idx = tv->vval.v_number;
9721
9722 if (!channel_is_open(ch_idx))
9723 {
9724 EMSGN(_("E906: not an open channel"), ch_idx);
9725 return -1;
9726 }
9727 return ch_idx;
9728}
9729
9730/*
9731 * "ch_close()" function
9732 */
9733 static void
9734f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9735{
9736 int ch_idx = get_channel_arg(&argvars[0]);
9737
9738 if (ch_idx >= 0)
9739 channel_close(ch_idx);
9740}
9741
9742/*
9743 * Get a callback from "arg". It can be a Funcref or a function name.
9744 * When "arg" is zero return an empty string.
9745 * Return NULL for an invalid argument.
9746 */
9747 static char_u *
9748get_callback(typval_T *arg)
9749{
9750 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9751 return arg->vval.v_string;
9752 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9753 return (char_u *)"";
9754 EMSG(_("E999: Invalid callback argument"));
9755 return NULL;
9756}
9757
9758/*
9759 * "ch_open()" function
9760 */
9761 static void
9762f_ch_open(typval_T *argvars, typval_T *rettv)
9763{
9764 char_u *address;
9765 char_u *mode;
9766 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009767 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009768 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009769 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009770 int waittime = 0;
9771 int timeout = 2000;
9772 int json_mode = TRUE;
9773 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009774
9775 /* default: fail */
9776 rettv->vval.v_number = -1;
9777
9778 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009779 if (argvars[1].v_type != VAR_UNKNOWN
9780 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009781 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009782 EMSG(_(e_invarg));
9783 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009784 }
9785
9786 /* parse address */
9787 p = vim_strchr(address, ':');
9788 if (p == NULL)
9789 {
9790 EMSG2(_(e_invarg2), address);
9791 return;
9792 }
9793 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009794 port = strtol((char *)p, &rest, 10);
9795 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009796 {
9797 p[-1] = ':';
9798 EMSG2(_(e_invarg2), address);
9799 return;
9800 }
9801
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009802 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009803 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009804 /* parse argdict */
9805 dict_T *dict = argvars[1].vval.v_dict;
9806
9807 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9808 {
9809 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9810 if (STRCMP(mode, "raw") == 0)
9811 json_mode = FALSE;
9812 else if (STRCMP(mode, "json") != 0)
9813 {
9814 EMSG2(_(e_invarg2), mode);
9815 return;
9816 }
9817 }
9818 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9819 waittime = get_dict_number(dict, (char_u *)"waittime");
9820 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9821 timeout = get_dict_number(dict, (char_u *)"timeout");
9822 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9823 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9824 }
9825 if (waittime < 0 || timeout < 0)
9826 {
9827 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009828 return;
9829 }
9830
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009831 ch_idx = channel_open((char *)address, port, waittime, NULL);
9832 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009833 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009834 channel_set_json_mode(ch_idx, json_mode);
9835 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009836 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009837 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009838 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009839 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009840}
9841
9842/*
9843 * common for "sendexpr()" and "sendraw()"
9844 * Returns the channel index if the caller should read the response.
9845 * Otherwise returns -1.
9846 */
9847 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009848send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009849{
9850 int ch_idx;
9851 char_u *callback = NULL;
9852
9853 ch_idx = get_channel_arg(&argvars[0]);
9854 if (ch_idx < 0)
9855 return -1;
9856
9857 if (argvars[2].v_type != VAR_UNKNOWN)
9858 {
9859 callback = get_callback(&argvars[2]);
9860 if (callback == NULL)
9861 return -1;
9862 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009863 /* Set the callback. An empty callback means no callback and not reading
9864 * the response. */
9865 if (callback != NULL && *callback != NUL)
9866 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009867
9868 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9869 return ch_idx;
9870 return -1;
9871}
9872
9873/*
9874 * "ch_sendexpr()" function
9875 */
9876 static void
9877f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9878{
9879 char_u *text;
9880 typval_T *listtv;
9881 int ch_idx;
9882 int id;
9883
9884 /* return an empty string by default */
9885 rettv->v_type = VAR_STRING;
9886 rettv->vval.v_string = NULL;
9887
9888 id = channel_get_id();
9889 text = json_encode_nr_expr(id, &argvars[1]);
9890 if (text == NULL)
9891 return;
9892
Bram Moolenaara07fec92016-02-05 21:04:08 +01009893 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009894 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009895 if (ch_idx >= 0)
9896 {
9897 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9898 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009899 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009900
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009901 /* Move the item from the list and then change the type to
9902 * avoid the value being freed. */
9903 *rettv = list->lv_last->li_tv;
9904 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009905 clear_tv(listtv);
9906 }
9907 }
9908}
9909
9910/*
9911 * "ch_sendraw()" function
9912 */
9913 static void
9914f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9915{
9916 char_u buf[NUMBUFLEN];
9917 char_u *text;
9918 int ch_idx;
9919
9920 /* return an empty string by default */
9921 rettv->v_type = VAR_STRING;
9922 rettv->vval.v_string = NULL;
9923
9924 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009925 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009926 if (ch_idx >= 0)
9927 rettv->vval.v_string = channel_read_block(ch_idx);
9928}
9929#endif
9930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009931/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009932 * "changenr()" function
9933 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009935f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009936{
9937 rettv->vval.v_number = curbuf->b_u_seq_cur;
9938}
9939
9940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 * "char2nr(string)" function
9942 */
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946#ifdef FEAT_MBYTE
9947 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009948 {
9949 int utf8 = 0;
9950
9951 if (argvars[1].v_type != VAR_UNKNOWN)
9952 utf8 = get_tv_number_chk(&argvars[1], NULL);
9953
9954 if (utf8)
9955 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9956 else
9957 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 else
9960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962}
9963
9964/*
9965 * "cindent(lnum)" function
9966 */
9967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009968f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970#ifdef FEAT_CINDENT
9971 pos_T pos;
9972 linenr_T lnum;
9973
9974 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9977 {
9978 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 curwin->w_cursor = pos;
9981 }
9982 else
9983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
9987/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009988 * "clearmatches()" function
9989 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009991f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009992{
9993#ifdef FEAT_SEARCH_EXTRA
9994 clear_matches(curwin);
9995#endif
9996}
9997
9998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 * "col(string)" function
10000 */
10001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010002f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 colnr_T col = 0;
10005 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010006 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010008 fp = var2fpos(&argvars[0], FALSE, &fnum);
10009 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 {
10011 if (fp->col == MAXCOL)
10012 {
10013 /* '> can be MAXCOL, get the length of the line then */
10014 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010015 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 else
10017 col = MAXCOL;
10018 }
10019 else
10020 {
10021 col = fp->col + 1;
10022#ifdef FEAT_VIRTUALEDIT
10023 /* col(".") when the cursor is on the NUL at the end of the line
10024 * because of "coladd" can be seen as an extra column. */
10025 if (virtual_active() && fp == &curwin->w_cursor)
10026 {
10027 char_u *p = ml_get_cursor();
10028
10029 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10030 curwin->w_virtcol - curwin->w_cursor.coladd))
10031 {
10032# ifdef FEAT_MBYTE
10033 int l;
10034
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010035 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 col += l;
10037# else
10038 if (*p != NUL && p[1] == NUL)
10039 ++col;
10040# endif
10041 }
10042 }
10043#endif
10044 }
10045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047}
10048
Bram Moolenaar572cb562005-08-05 21:35:02 +000010049#if defined(FEAT_INS_EXPAND)
10050/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010051 * "complete()" function
10052 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010054f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010055{
10056 int startcol;
10057
10058 if ((State & INSERT) == 0)
10059 {
10060 EMSG(_("E785: complete() can only be used in Insert mode"));
10061 return;
10062 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010063
10064 /* Check for undo allowed here, because if something was already inserted
10065 * the line was already saved for undo and this check isn't done. */
10066 if (!undo_allowed())
10067 return;
10068
Bram Moolenaarade00832006-03-10 21:46:58 +000010069 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10070 {
10071 EMSG(_(e_invarg));
10072 return;
10073 }
10074
10075 startcol = get_tv_number_chk(&argvars[0], NULL);
10076 if (startcol <= 0)
10077 return;
10078
10079 set_completion(startcol - 1, argvars[1].vval.v_list);
10080}
10081
10082/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010083 * "complete_add()" function
10084 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010086f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010087{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010088 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010089}
10090
10091/*
10092 * "complete_check()" function
10093 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010096{
10097 int saved = RedrawingDisabled;
10098
10099 RedrawingDisabled = 0;
10100 ins_compl_check_keys(0);
10101 rettv->vval.v_number = compl_interrupted;
10102 RedrawingDisabled = saved;
10103}
10104#endif
10105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106/*
10107 * "confirm(message, buttons[, default [, type]])" function
10108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010110f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10113 char_u *message;
10114 char_u *buttons = NULL;
10115 char_u buf[NUMBUFLEN];
10116 char_u buf2[NUMBUFLEN];
10117 int def = 1;
10118 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 char_u *typestr;
10120 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 message = get_tv_string_chk(&argvars[0]);
10123 if (message == NULL)
10124 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010125 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10128 if (buttons == NULL)
10129 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010133 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10136 if (typestr == NULL)
10137 error = TRUE;
10138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 switch (TOUPPER_ASC(*typestr))
10141 {
10142 case 'E': type = VIM_ERROR; break;
10143 case 'Q': type = VIM_QUESTION; break;
10144 case 'I': type = VIM_INFO; break;
10145 case 'W': type = VIM_WARNING; break;
10146 case 'G': type = VIM_GENERIC; break;
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149 }
10150 }
10151 }
10152
10153 if (buttons == NULL || *buttons == NUL)
10154 buttons = (char_u *)_("&Ok");
10155
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010156 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010158 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159#endif
10160}
10161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010162/*
10163 * "copy()" function
10164 */
10165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010166f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010167{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010168 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010169}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010171#ifdef FEAT_FLOAT
10172/*
10173 * "cos()" function
10174 */
10175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010177{
10178 float_T f;
10179
10180 rettv->v_type = VAR_FLOAT;
10181 if (get_float_arg(argvars, &f) == OK)
10182 rettv->vval.v_float = cos(f);
10183 else
10184 rettv->vval.v_float = 0.0;
10185}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010186
10187/*
10188 * "cosh()" function
10189 */
10190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010191f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010192{
10193 float_T f;
10194
10195 rettv->v_type = VAR_FLOAT;
10196 if (get_float_arg(argvars, &f) == OK)
10197 rettv->vval.v_float = cosh(f);
10198 else
10199 rettv->vval.v_float = 0.0;
10200}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010201#endif
10202
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010204 * "count()" function
10205 */
10206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010207f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010208{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010209 long n = 0;
10210 int ic = FALSE;
10211
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010213 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 listitem_T *li;
10215 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010217
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 if ((l = argvars[0].vval.v_list) != NULL)
10219 {
10220 li = l->lv_first;
10221 if (argvars[2].v_type != VAR_UNKNOWN)
10222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 int error = FALSE;
10224
10225 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226 if (argvars[3].v_type != VAR_UNKNOWN)
10227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 idx = get_tv_number_chk(&argvars[3], &error);
10229 if (!error)
10230 {
10231 li = list_find(l, idx);
10232 if (li == NULL)
10233 EMSGN(_(e_listidx), idx);
10234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 if (error)
10237 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010238 }
10239
10240 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010241 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010242 ++n;
10243 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 else if (argvars[0].v_type == VAR_DICT)
10246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 int todo;
10248 dict_T *d;
10249 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 if ((d = argvars[0].vval.v_dict) != NULL)
10252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 int error = FALSE;
10254
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 if (argvars[2].v_type != VAR_UNKNOWN)
10256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 if (argvars[3].v_type != VAR_UNKNOWN)
10259 EMSG(_(e_invarg));
10260 }
10261
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010262 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010264 {
10265 if (!HASHITEM_EMPTY(hi))
10266 {
10267 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010268 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010269 ++n;
10270 }
10271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 }
10273 }
10274 else
10275 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 rettv->vval.v_number = n;
10277}
10278
10279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10281 *
10282 * Checks the existence of a cscope connection.
10283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286{
10287#ifdef FEAT_CSCOPE
10288 int num = 0;
10289 char_u *dbpath = NULL;
10290 char_u *prepend = NULL;
10291 char_u buf[NUMBUFLEN];
10292
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010293 if (argvars[0].v_type != VAR_UNKNOWN
10294 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 num = (int)get_tv_number(&argvars[0]);
10297 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303#endif
10304}
10305
10306/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010307 * "cursor(lnum, col)" function, or
10308 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010310 * Moves the cursor to the specified line and column.
10311 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315{
10316 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010317#ifdef FEAT_VIRTUALEDIT
10318 long coladd = 0;
10319#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010320 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010322 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010323 if (argvars[1].v_type == VAR_UNKNOWN)
10324 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010325 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010326 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010327
Bram Moolenaar493c1782014-05-28 14:34:46 +020010328 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010329 {
10330 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010331 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010332 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010333 line = pos.lnum;
10334 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010335#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010336 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010337#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010338 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010339 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010340 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010341 set_curswant = FALSE;
10342 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010343 }
10344 else
10345 {
10346 line = get_tv_lnum(argvars);
10347 col = get_tv_number_chk(&argvars[1], NULL);
10348#ifdef FEAT_VIRTUALEDIT
10349 if (argvars[2].v_type != VAR_UNKNOWN)
10350 coladd = get_tv_number_chk(&argvars[2], NULL);
10351#endif
10352 }
10353 if (line < 0 || col < 0
10354#ifdef FEAT_VIRTUALEDIT
10355 || coladd < 0
10356#endif
10357 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 if (line > 0)
10360 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 if (col > 0)
10362 curwin->w_cursor.col = col - 1;
10363#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010364 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365#endif
10366
10367 /* Make sure the cursor is in a valid position. */
10368 check_cursor();
10369#ifdef FEAT_MBYTE
10370 /* Correct cursor for multi-byte character. */
10371 if (has_mbyte)
10372 mb_adjust_cursor();
10373#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010374
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010375 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010376 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
10378
10379/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010380 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 */
10382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010383f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010385 int noref = 0;
10386
10387 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010389 if (noref < 0 || noref > 1)
10390 EMSG(_(e_invarg));
10391 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010392 {
10393 current_copyID += COPYID_INC;
10394 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396}
10397
10398/*
10399 * "delete()" function
10400 */
10401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010402f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010404 char_u nbuf[NUMBUFLEN];
10405 char_u *name;
10406 char_u *flags;
10407
10408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010410 return;
10411
10412 name = get_tv_string(&argvars[0]);
10413 if (name == NULL || *name == NUL)
10414 {
10415 EMSG(_(e_invarg));
10416 return;
10417 }
10418
10419 if (argvars[1].v_type != VAR_UNKNOWN)
10420 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010422 flags = (char_u *)"";
10423
10424 if (*flags == NUL)
10425 /* delete a file */
10426 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10427 else if (STRCMP(flags, "d") == 0)
10428 /* delete an empty directory */
10429 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10430 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010431 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010432 rettv->vval.v_number = delete_recursive(name);
10433 else
10434 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435}
10436
10437/*
10438 * "did_filetype()" function
10439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010441f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#endif
10446}
10447
10448/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010449 * "diff_filler()" function
10450 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010452f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010453{
10454#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010456#endif
10457}
10458
10459/*
10460 * "diff_hlID()" function
10461 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010463f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010464{
10465#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010467 static linenr_T prev_lnum = 0;
10468 static int changedtick = 0;
10469 static int fnum = 0;
10470 static int change_start = 0;
10471 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010472 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010473 int filler_lines;
10474 int col;
10475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 if (lnum < 0) /* ignore type error in {lnum} arg */
10477 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010478 if (lnum != prev_lnum
10479 || changedtick != curbuf->b_changedtick
10480 || fnum != curbuf->b_fnum)
10481 {
10482 /* New line, buffer, change: need to get the values. */
10483 filler_lines = diff_check(curwin, lnum);
10484 if (filler_lines < 0)
10485 {
10486 if (filler_lines == -1)
10487 {
10488 change_start = MAXCOL;
10489 change_end = -1;
10490 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10491 hlID = HLF_ADD; /* added line */
10492 else
10493 hlID = HLF_CHD; /* changed line */
10494 }
10495 else
10496 hlID = HLF_ADD; /* added line */
10497 }
10498 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010499 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010500 prev_lnum = lnum;
10501 changedtick = curbuf->b_changedtick;
10502 fnum = curbuf->b_fnum;
10503 }
10504
10505 if (hlID == HLF_CHD || hlID == HLF_TXD)
10506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010508 if (col >= change_start && col <= change_end)
10509 hlID = HLF_TXD; /* changed text */
10510 else
10511 hlID = HLF_CHD; /* changed line */
10512 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010513 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010514#endif
10515}
10516
10517/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010518 * "empty({expr})" function
10519 */
10520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010521f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010522{
10523 int n;
10524
10525 switch (argvars[0].v_type)
10526 {
10527 case VAR_STRING:
10528 case VAR_FUNC:
10529 n = argvars[0].vval.v_string == NULL
10530 || *argvars[0].vval.v_string == NUL;
10531 break;
10532 case VAR_NUMBER:
10533 n = argvars[0].vval.v_number == 0;
10534 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010535#ifdef FEAT_FLOAT
10536 case VAR_FLOAT:
10537 n = argvars[0].vval.v_float == 0.0;
10538 break;
10539#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010540 case VAR_LIST:
10541 n = argvars[0].vval.v_list == NULL
10542 || argvars[0].vval.v_list->lv_first == NULL;
10543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 case VAR_DICT:
10545 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010548 case VAR_SPECIAL:
10549 n = argvars[0].vval.v_number != VVAL_TRUE;
10550 break;
10551
Bram Moolenaara03f2332016-02-06 18:09:59 +010010552 case VAR_UNKNOWN:
10553 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10554 n = TRUE;
10555 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010556 }
10557
10558 rettv->vval.v_number = n;
10559}
10560
10561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 * "escape({string}, {chars})" function
10563 */
10564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010565f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567 char_u buf[NUMBUFLEN];
10568
Bram Moolenaar758711c2005-02-02 23:11:38 +000010569 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10570 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572}
10573
10574/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010575 * "eval()" function
10576 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010578f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010580 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 s = get_tv_string_chk(&argvars[0]);
10583 if (s != NULL)
10584 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010585
Bram Moolenaar615b9972015-01-14 17:15:05 +010010586 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10588 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010589 if (p != NULL && !aborting())
10590 EMSG2(_(e_invexpr2), p);
10591 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010593 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010595 else if (*s != NUL)
10596 EMSG(_(e_trailing));
10597}
10598
10599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 * "eventhandler()" function
10601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010603f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607
10608/*
10609 * "executable()" function
10610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010614 char_u *name = get_tv_string(&argvars[0]);
10615
10616 /* Check in $PATH and also check directly if there is a directory name. */
10617 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10618 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010619}
10620
10621/*
10622 * "exepath()" function
10623 */
10624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010625f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010626{
10627 char_u *p = NULL;
10628
Bram Moolenaarb5971142015-03-21 17:32:19 +010010629 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010630 rettv->v_type = VAR_STRING;
10631 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632}
10633
10634/*
10635 * "exists()" function
10636 */
10637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010638f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639{
10640 char_u *p;
10641 char_u *name;
10642 int n = FALSE;
10643 int len = 0;
10644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 if (*p == '$') /* environment variable */
10647 {
10648 /* first try "normal" environment variables (fast) */
10649 if (mch_getenv(p + 1) != NULL)
10650 n = TRUE;
10651 else
10652 {
10653 /* try expanding things like $VIM and ${HOME} */
10654 p = expand_env_save(p);
10655 if (p != NULL && *p != '$')
10656 n = TRUE;
10657 vim_free(p);
10658 }
10659 }
10660 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010663 if (*skipwhite(p) != NUL)
10664 n = FALSE; /* trailing garbage */
10665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 else if (*p == '*') /* internal or user defined function */
10667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010668 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 }
10670 else if (*p == ':')
10671 {
10672 n = cmd_exists(p + 1);
10673 }
10674 else if (*p == '#')
10675 {
10676#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010677 if (p[1] == '#')
10678 n = autocmd_supported(p + 2);
10679 else
10680 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681#endif
10682 }
10683 else /* internal variable */
10684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010685 char_u *tofree;
10686 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010688 /* get_name_len() takes care of expanding curly braces */
10689 name = p;
10690 len = get_name_len(&p, &tofree, TRUE, FALSE);
10691 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010693 if (tofree != NULL)
10694 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010695 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010696 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010698 /* handle d.key, l[idx], f(expr) */
10699 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10700 if (n)
10701 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 }
10703 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010704 if (*p != NUL)
10705 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010707 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 }
10709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711}
10712
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010713#ifdef FEAT_FLOAT
10714/*
10715 * "exp()" function
10716 */
10717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010719{
10720 float_T f;
10721
10722 rettv->v_type = VAR_FLOAT;
10723 if (get_float_arg(argvars, &f) == OK)
10724 rettv->vval.v_float = exp(f);
10725 else
10726 rettv->vval.v_float = 0.0;
10727}
10728#endif
10729
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730/*
10731 * "expand()" function
10732 */
10733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736 char_u *s;
10737 int len;
10738 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010739 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010742 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010745 if (argvars[1].v_type != VAR_UNKNOWN
10746 && argvars[2].v_type != VAR_UNKNOWN
10747 && get_tv_number_chk(&argvars[2], &error)
10748 && !error)
10749 {
10750 rettv->v_type = VAR_LIST;
10751 rettv->vval.v_list = NULL;
10752 }
10753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (*s == '%' || *s == '#' || *s == '<')
10756 {
10757 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010758 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010760 if (rettv->v_type == VAR_LIST)
10761 {
10762 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10763 list_append_string(rettv->vval.v_list, result, -1);
10764 else
10765 vim_free(result);
10766 }
10767 else
10768 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 }
10770 else
10771 {
10772 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010773 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 if (argvars[1].v_type != VAR_UNKNOWN
10775 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010776 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 if (!error)
10778 {
10779 ExpandInit(&xpc);
10780 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010781 if (p_wic)
10782 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010783 if (rettv->v_type == VAR_STRING)
10784 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10785 options, WILD_ALL);
10786 else if (rettv_list_alloc(rettv) != FAIL)
10787 {
10788 int i;
10789
10790 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10791 for (i = 0; i < xpc.xp_numfiles; i++)
10792 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10793 ExpandCleanup(&xpc);
10794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 }
10796 else
10797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 }
10799}
10800
10801/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010802 * Go over all entries in "d2" and add them to "d1".
10803 * When "action" is "error" then a duplicate key is an error.
10804 * When "action" is "force" then a duplicate key is overwritten.
10805 * Otherwise duplicate keys are ignored ("action" is "keep").
10806 */
10807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010808dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010809{
10810 dictitem_T *di1;
10811 hashitem_T *hi2;
10812 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010813 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010814
10815 todo = (int)d2->dv_hashtab.ht_used;
10816 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10817 {
10818 if (!HASHITEM_EMPTY(hi2))
10819 {
10820 --todo;
10821 di1 = dict_find(d1, hi2->hi_key, -1);
10822 if (d1->dv_scope != 0)
10823 {
10824 /* Disallow replacing a builtin function in l: and g:.
10825 * Check the key to be valid when adding to any
10826 * scope. */
10827 if (d1->dv_scope == VAR_DEF_SCOPE
10828 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10829 && var_check_func_name(hi2->hi_key,
10830 di1 == NULL))
10831 break;
10832 if (!valid_varname(hi2->hi_key))
10833 break;
10834 }
10835 if (di1 == NULL)
10836 {
10837 di1 = dictitem_copy(HI2DI(hi2));
10838 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10839 dictitem_free(di1);
10840 }
10841 else if (*action == 'e')
10842 {
10843 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10844 break;
10845 }
10846 else if (*action == 'f' && HI2DI(hi2) != di1)
10847 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010848 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10849 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010850 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010851 clear_tv(&di1->di_tv);
10852 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10853 }
10854 }
10855 }
10856}
10857
10858/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010859 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010861 */
10862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010864{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010865 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010866
Bram Moolenaare9a41262005-01-15 22:18:47 +000010867 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010868 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010869 list_T *l1, *l2;
10870 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010873
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 l1 = argvars[0].vval.v_list;
10875 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010876 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010877 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878 {
10879 if (argvars[2].v_type != VAR_UNKNOWN)
10880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 before = get_tv_number_chk(&argvars[2], &error);
10882 if (error)
10883 return; /* type error; errmsg already given */
10884
Bram Moolenaar758711c2005-02-02 23:11:38 +000010885 if (before == l1->lv_len)
10886 item = NULL;
10887 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010889 item = list_find(l1, before);
10890 if (item == NULL)
10891 {
10892 EMSGN(_(e_listidx), before);
10893 return;
10894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010895 }
10896 }
10897 else
10898 item = NULL;
10899 list_extend(l1, l2, item);
10900
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 copy_tv(&argvars[0], rettv);
10902 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010904 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10905 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010906 dict_T *d1, *d2;
10907 char_u *action;
10908 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010909
10910 d1 = argvars[0].vval.v_dict;
10911 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010912 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010913 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 {
10915 /* Check the third argument. */
10916 if (argvars[2].v_type != VAR_UNKNOWN)
10917 {
10918 static char *(av[]) = {"keep", "force", "error"};
10919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 action = get_tv_string_chk(&argvars[2]);
10921 if (action == NULL)
10922 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 for (i = 0; i < 3; ++i)
10924 if (STRCMP(action, av[i]) == 0)
10925 break;
10926 if (i == 3)
10927 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010928 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 return;
10930 }
10931 }
10932 else
10933 action = (char_u *)"force";
10934
Bram Moolenaara9922d62013-05-30 13:01:18 +020010935 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 copy_tv(&argvars[0], rettv);
10938 }
10939 }
10940 else
10941 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010942}
10943
10944/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010945 * "feedkeys()" function
10946 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010948f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010949{
10950 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010951 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010952 char_u *keys, *flags;
10953 char_u nbuf[NUMBUFLEN];
10954 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010955 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010956 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010957
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010958 /* This is not allowed in the sandbox. If the commands would still be
10959 * executed in the sandbox it would be OK, but it probably happens later,
10960 * when "sandbox" is no longer set. */
10961 if (check_secure())
10962 return;
10963
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010964 keys = get_tv_string(&argvars[0]);
10965 if (*keys != NUL)
10966 {
10967 if (argvars[1].v_type != VAR_UNKNOWN)
10968 {
10969 flags = get_tv_string_buf(&argvars[1], nbuf);
10970 for ( ; *flags != NUL; ++flags)
10971 {
10972 switch (*flags)
10973 {
10974 case 'n': remap = FALSE; break;
10975 case 'm': remap = TRUE; break;
10976 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010977 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010978 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010979 }
10980 }
10981 }
10982
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010983 /* Need to escape K_SPECIAL and CSI before putting the string in the
10984 * typeahead buffer. */
10985 keys_esc = vim_strsave_escape_csi(keys);
10986 if (keys_esc != NULL)
10987 {
10988 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010989 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010990 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010991 if (vgetc_busy)
10992 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010993 if (execute)
10994 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010995 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010996 }
10997}
10998
10999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 * "filereadable()" function
11001 */
11002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011003f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011005 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 char_u *p;
11007 int n;
11008
Bram Moolenaarc236c162008-07-13 17:41:49 +000011009#ifndef O_NONBLOCK
11010# define O_NONBLOCK 0
11011#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011013 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11014 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 {
11016 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011017 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 }
11019 else
11020 n = FALSE;
11021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011026 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 * rights to write into.
11028 */
11029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011030f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011032 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033}
11034
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011035 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011036findfilendir(
11037 typval_T *argvars UNUSED,
11038 typval_T *rettv,
11039 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011040{
11041#ifdef FEAT_SEARCHPATH
11042 char_u *fname;
11043 char_u *fresult = NULL;
11044 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11045 char_u *p;
11046 char_u pathbuf[NUMBUFLEN];
11047 int count = 1;
11048 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011049 int error = FALSE;
11050#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011051
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011052 rettv->vval.v_string = NULL;
11053 rettv->v_type = VAR_STRING;
11054
11055#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011058 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11061 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011062 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 else
11064 {
11065 if (*p != NUL)
11066 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011069 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011071 }
11072
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011073 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11074 error = TRUE;
11075
11076 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 do
11079 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011080 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011081 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 fresult = find_file_in_path_option(first ? fname : NULL,
11083 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011084 0, first, path,
11085 find_what,
11086 curbuf->b_ffname,
11087 find_what == FINDFILE_DIR
11088 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011090
11091 if (fresult != NULL && rettv->v_type == VAR_LIST)
11092 list_append_string(rettv->vval.v_list, fresult, -1);
11093
11094 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011096
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011097 if (rettv->v_type == VAR_STRING)
11098 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011099#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011100}
11101
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011102static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11103static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011104
11105/*
11106 * Implementation of map() and filter().
11107 */
11108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011109filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110{
11111 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 listitem_T *li, *nli;
11114 list_T *l = NULL;
11115 dictitem_T *di;
11116 hashtab_T *ht;
11117 hashitem_T *hi;
11118 dict_T *d = NULL;
11119 typval_T save_val;
11120 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011122 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011123 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011124 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011125 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011126 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011127 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011128
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011130 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011131 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011132 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 return;
11134 }
11135 else if (argvars[0].v_type == VAR_DICT)
11136 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011137 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011138 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 return;
11140 }
11141 else
11142 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011143 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011144 return;
11145 }
11146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 expr = get_tv_string_buf_chk(&argvars[1], buf);
11148 /* On type errors, the preceding call has already displayed an error
11149 * message. Avoid a misleading error message for an empty string that
11150 * was not passed as argument. */
11151 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 prepare_vimvar(VV_VAL, &save_val);
11154 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011155
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011156 /* We reset "did_emsg" to be able to detect whether an error
11157 * occurred during evaluation of the expression. */
11158 save_did_emsg = did_emsg;
11159 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011160
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011161 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 vimvars[VV_KEY].vv_type = VAR_STRING;
11165
11166 ht = &d->dv_hashtab;
11167 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011168 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 if (!HASHITEM_EMPTY(hi))
11172 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011173 int r;
11174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 --todo;
11176 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011177 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011178 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11179 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 break;
11181 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011182 r = filter_map_one(&di->di_tv, expr, map, &rem);
11183 clear_tv(&vimvars[VV_KEY].vv_tv);
11184 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 break;
11186 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011187 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011188 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11189 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011190 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011192 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 }
11194 }
11195 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 }
11197 else
11198 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011199 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 for (li = l->lv_first; li != NULL; li = nli)
11202 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011203 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011204 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011206 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011207 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011208 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011209 break;
11210 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011212 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011213 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011214 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011215
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011216 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011218
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011219 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011220 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221
11222 copy_tv(&argvars[0], rettv);
11223}
11224
11225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011226filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227{
Bram Moolenaar33570922005-01-25 22:26:29 +000011228 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011230 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 s = expr;
11234 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011235 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 if (*s != NUL) /* check for trailing chars after expr */
11237 {
11238 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011239 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011240 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011241 }
11242 if (map)
11243 {
11244 /* map(): replace the list item value */
11245 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011246 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 *tv = rettv;
11248 }
11249 else
11250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 int error = FALSE;
11252
Bram Moolenaare9a41262005-01-15 22:18:47 +000011253 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 /* On type error, nothing has been removed; return FAIL to stop the
11257 * loop. The error message was given by get_tv_number_chk(). */
11258 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011259 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011260 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011261 retval = OK;
11262theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011264 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011265}
11266
11267/*
11268 * "filter()" function
11269 */
11270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011271f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011272{
11273 filter_map(argvars, rettv, FALSE);
11274}
11275
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011276/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 * "finddir({fname}[, {path}[, {count}]])" function
11278 */
11279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011280f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011281{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011282 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283}
11284
11285/*
11286 * "findfile({fname}[, {path}[, {count}]])" function
11287 */
11288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011289f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011291 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011292}
11293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011294#ifdef FEAT_FLOAT
11295/*
11296 * "float2nr({float})" function
11297 */
11298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011299f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011300{
11301 float_T f;
11302
11303 if (get_float_arg(argvars, &f) == OK)
11304 {
11305 if (f < -0x7fffffff)
11306 rettv->vval.v_number = -0x7fffffff;
11307 else if (f > 0x7fffffff)
11308 rettv->vval.v_number = 0x7fffffff;
11309 else
11310 rettv->vval.v_number = (varnumber_T)f;
11311 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011312}
11313
11314/*
11315 * "floor({float})" function
11316 */
11317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011318f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011319{
11320 float_T f;
11321
11322 rettv->v_type = VAR_FLOAT;
11323 if (get_float_arg(argvars, &f) == OK)
11324 rettv->vval.v_float = floor(f);
11325 else
11326 rettv->vval.v_float = 0.0;
11327}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011328
11329/*
11330 * "fmod()" function
11331 */
11332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011333f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011334{
11335 float_T fx, fy;
11336
11337 rettv->v_type = VAR_FLOAT;
11338 if (get_float_arg(argvars, &fx) == OK
11339 && get_float_arg(&argvars[1], &fy) == OK)
11340 rettv->vval.v_float = fmod(fx, fy);
11341 else
11342 rettv->vval.v_float = 0.0;
11343}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011344#endif
11345
Bram Moolenaar0d660222005-01-07 21:51:51 +000011346/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011347 * "fnameescape({string})" function
11348 */
11349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011350f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011351{
11352 rettv->vval.v_string = vim_strsave_fnameescape(
11353 get_tv_string(&argvars[0]), FALSE);
11354 rettv->v_type = VAR_STRING;
11355}
11356
11357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 * "fnamemodify({fname}, {mods})" function
11359 */
11360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
11363 char_u *fname;
11364 char_u *mods;
11365 int usedlen = 0;
11366 int len;
11367 char_u *fbuf = NULL;
11368 char_u buf[NUMBUFLEN];
11369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 fname = get_tv_string_chk(&argvars[0]);
11371 mods = get_tv_string_buf_chk(&argvars[1], buf);
11372 if (fname == NULL || mods == NULL)
11373 fname = NULL;
11374 else
11375 {
11376 len = (int)STRLEN(fname);
11377 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 vim_free(fbuf);
11386}
11387
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011388static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389
11390/*
11391 * "foldclosed()" function
11392 */
11393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011394foldclosed_both(
11395 typval_T *argvars UNUSED,
11396 typval_T *rettv,
11397 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399#ifdef FEAT_FOLDING
11400 linenr_T lnum;
11401 linenr_T first, last;
11402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11405 {
11406 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11407 {
11408 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 return;
11413 }
11414 }
11415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417}
11418
11419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011420 * "foldclosed()" function
11421 */
11422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011423f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424{
11425 foldclosed_both(argvars, rettv, FALSE);
11426}
11427
11428/*
11429 * "foldclosedend()" function
11430 */
11431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011432f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011433{
11434 foldclosed_both(argvars, rettv, TRUE);
11435}
11436
11437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 * "foldlevel()" function
11439 */
11440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011441f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443#ifdef FEAT_FOLDING
11444 linenr_T lnum;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450}
11451
11452/*
11453 * "foldtext()" function
11454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011456f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
11458#ifdef FEAT_FOLDING
11459 linenr_T lnum;
11460 char_u *s;
11461 char_u *r;
11462 int len;
11463 char *txt;
11464#endif
11465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11470 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11471 <= curbuf->b_ml.ml_line_count
11472 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 {
11474 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11476 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 {
11478 if (!linewhite(lnum))
11479 break;
11480 ++lnum;
11481 }
11482
11483 /* Find interesting text in this line. */
11484 s = skipwhite(ml_get(lnum));
11485 /* skip C comment-start */
11486 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011489 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011490 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011491 {
11492 s = skipwhite(ml_get(lnum + 1));
11493 if (*s == '*')
11494 s = skipwhite(s + 1);
11495 }
11496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 txt = _("+-%s%3ld lines: ");
11498 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011499 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 + 20 /* for %3ld */
11501 + STRLEN(s))); /* concatenated */
11502 if (r != NULL)
11503 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11505 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11506 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 len = (int)STRLEN(r);
11508 STRCAT(r, s);
11509 /* remove 'foldmarker' and 'commentstring' */
11510 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513 }
11514#endif
11515}
11516
11517/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011518 * "foldtextresult(lnum)" function
11519 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011522{
11523#ifdef FEAT_FOLDING
11524 linenr_T lnum;
11525 char_u *text;
11526 char_u buf[51];
11527 foldinfo_T foldinfo;
11528 int fold_count;
11529#endif
11530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->v_type = VAR_STRING;
11532 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011533#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 /* treat illegal types and illegal string values for {lnum} the same */
11536 if (lnum < 0)
11537 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011538 fold_count = foldedCount(curwin, lnum, &foldinfo);
11539 if (fold_count > 0)
11540 {
11541 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11542 &foldinfo, buf);
11543 if (text == buf)
11544 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011546 }
11547#endif
11548}
11549
11550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 * "foreground()" function
11552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011554f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#ifdef FEAT_GUI
11557 if (gui.in_use)
11558 gui_mch_set_foreground();
11559#else
11560# ifdef WIN32
11561 win32_set_foreground();
11562# endif
11563#endif
11564}
11565
11566/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011567 * "function()" function
11568 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011570f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571{
11572 char_u *s;
11573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011575 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011576 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011577 /* Don't check an autoload name for existence here. */
11578 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011579 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580 else
11581 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011582 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011583 {
11584 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011585 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011586
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011587 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11588 * also be called from another script. Using trans_function_name()
11589 * would also work, but some plugins depend on the name being
11590 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011591 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011592 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011593 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011594 if (rettv->vval.v_string != NULL)
11595 {
11596 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011597 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011598 }
11599 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011600 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011601 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011603 }
11604}
11605
11606/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011607 * "garbagecollect()" function
11608 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011610f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011611{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011612 /* This is postponed until we are back at the toplevel, because we may be
11613 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11614 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011615
11616 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11617 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011618}
11619
11620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011621 * "get()" function
11622 */
11623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011624f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011625{
Bram Moolenaar33570922005-01-25 22:26:29 +000011626 listitem_T *li;
11627 list_T *l;
11628 dictitem_T *di;
11629 dict_T *d;
11630 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631
Bram Moolenaare9a41262005-01-15 22:18:47 +000011632 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011633 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011634 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011636 int error = FALSE;
11637
11638 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11639 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011640 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011643 else if (argvars[0].v_type == VAR_DICT)
11644 {
11645 if ((d = argvars[0].vval.v_dict) != NULL)
11646 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011647 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011648 if (di != NULL)
11649 tv = &di->di_tv;
11650 }
11651 }
11652 else
11653 EMSG2(_(e_listdictarg), "get()");
11654
11655 if (tv == NULL)
11656 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011658 copy_tv(&argvars[2], rettv);
11659 }
11660 else
11661 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011664static 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 +000011665
11666/*
11667 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011668 * Return a range (from start to end) of lines in rettv from the specified
11669 * buffer.
11670 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673get_buffer_lines(
11674 buf_T *buf,
11675 linenr_T start,
11676 linenr_T end,
11677 int retlist,
11678 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011679{
11680 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011681
Bram Moolenaar959a1432013-12-14 12:17:38 +010011682 rettv->v_type = VAR_STRING;
11683 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011684 if (retlist && rettv_list_alloc(rettv) == FAIL)
11685 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011686
11687 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11688 return;
11689
11690 if (!retlist)
11691 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011692 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11693 p = ml_get_buf(buf, start, FALSE);
11694 else
11695 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011696 rettv->vval.v_string = vim_strsave(p);
11697 }
11698 else
11699 {
11700 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011701 return;
11702
11703 if (start < 1)
11704 start = 1;
11705 if (end > buf->b_ml.ml_line_count)
11706 end = buf->b_ml.ml_line_count;
11707 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011708 if (list_append_string(rettv->vval.v_list,
11709 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011710 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011711 }
11712}
11713
11714/*
11715 * "getbufline()" function
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011719{
11720 linenr_T lnum;
11721 linenr_T end;
11722 buf_T *buf;
11723
11724 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11725 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011726 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011727 --emsg_off;
11728
Bram Moolenaar661b1822005-07-28 22:36:45 +000011729 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011730 if (argvars[2].v_type == VAR_UNKNOWN)
11731 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011732 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011733 end = get_tv_lnum_buf(&argvars[2], buf);
11734
Bram Moolenaar342337a2005-07-21 21:11:17 +000011735 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011736}
11737
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738/*
11739 * "getbufvar()" function
11740 */
11741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011742f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011743{
11744 buf_T *buf;
11745 buf_T *save_curbuf;
11746 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011748 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011750 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11751 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011753 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011754
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011755 rettv->v_type = VAR_STRING;
11756 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757
11758 if (buf != NULL && varname != NULL)
11759 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011760 /* set curbuf to be our buf, temporarily */
11761 save_curbuf = curbuf;
11762 curbuf = buf;
11763
Bram Moolenaar0d660222005-01-07 21:51:51 +000011764 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011765 {
11766 if (get_option_tv(&varname, rettv, TRUE) == OK)
11767 done = TRUE;
11768 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011769 else if (STRCMP(varname, "changedtick") == 0)
11770 {
11771 rettv->v_type = VAR_NUMBER;
11772 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011773 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011774 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011775 else
11776 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011777 /* Look up the variable. */
11778 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11779 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11780 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011782 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011784 done = TRUE;
11785 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011786 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011787
11788 /* restore previous notion of curbuf */
11789 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790 }
11791
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011792 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11793 /* use the default value */
11794 copy_tv(&argvars[2], rettv);
11795
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796 --emsg_off;
11797}
11798
11799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 * "getchar()" function
11801 */
11802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011803f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804{
11805 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011808 /* Position the cursor. Needed after a message that ends in a space. */
11809 windgoto(msg_row, msg_col);
11810
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 ++no_mapping;
11812 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011813 for (;;)
11814 {
11815 if (argvars[0].v_type == VAR_UNKNOWN)
11816 /* getchar(): blocking wait. */
11817 n = safe_vgetc();
11818 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11819 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011820 n = vpeekc_any();
11821 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011822 /* illegal argument or getchar(0) and no char avail: return zero */
11823 n = 0;
11824 else
11825 /* getchar(0) and char avail: return char */
11826 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011827
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011828 if (n == K_IGNORE)
11829 continue;
11830 break;
11831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 --no_mapping;
11833 --allow_keys;
11834
Bram Moolenaar219b8702006-11-01 14:32:36 +000011835 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11836 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11837 vimvars[VV_MOUSE_COL].vv_nr = 0;
11838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 if (IS_SPECIAL(n) || mod_mask != 0)
11841 {
11842 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11843 int i = 0;
11844
11845 /* Turn a special key into three bytes, plus modifier. */
11846 if (mod_mask != 0)
11847 {
11848 temp[i++] = K_SPECIAL;
11849 temp[i++] = KS_MODIFIER;
11850 temp[i++] = mod_mask;
11851 }
11852 if (IS_SPECIAL(n))
11853 {
11854 temp[i++] = K_SPECIAL;
11855 temp[i++] = K_SECOND(n);
11856 temp[i++] = K_THIRD(n);
11857 }
11858#ifdef FEAT_MBYTE
11859 else if (has_mbyte)
11860 i += (*mb_char2bytes)(n, temp + i);
11861#endif
11862 else
11863 temp[i++] = n;
11864 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->v_type = VAR_STRING;
11866 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011867
11868#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011869 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011870 {
11871 int row = mouse_row;
11872 int col = mouse_col;
11873 win_T *win;
11874 linenr_T lnum;
11875# ifdef FEAT_WINDOWS
11876 win_T *wp;
11877# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011878 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011879
11880 if (row >= 0 && col >= 0)
11881 {
11882 /* Find the window at the mouse coordinates and compute the
11883 * text position. */
11884 win = mouse_find_win(&row, &col);
11885 (void)mouse_comp_pos(win, &row, &col, &lnum);
11886# ifdef FEAT_WINDOWS
11887 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011888 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011889# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011890 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011891 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11892 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11893 }
11894 }
11895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 }
11897}
11898
11899/*
11900 * "getcharmod()" function
11901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011903f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906}
11907
11908/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011909 * "getcharsearch()" function
11910 */
11911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011912f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011913{
11914 if (rettv_dict_alloc(rettv) != FAIL)
11915 {
11916 dict_T *dict = rettv->vval.v_dict;
11917
11918 dict_add_nr_str(dict, "char", 0L, last_csearch());
11919 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11920 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11921 }
11922}
11923
11924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 * "getcmdline()" function
11926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->v_type = VAR_STRING;
11931 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932}
11933
11934/*
11935 * "getcmdpos()" function
11936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941}
11942
11943/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011944 * "getcmdtype()" function
11945 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011947f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011948{
11949 rettv->v_type = VAR_STRING;
11950 rettv->vval.v_string = alloc(2);
11951 if (rettv->vval.v_string != NULL)
11952 {
11953 rettv->vval.v_string[0] = get_cmdline_type();
11954 rettv->vval.v_string[1] = NUL;
11955 }
11956}
11957
11958/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011959 * "getcmdwintype()" function
11960 */
11961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011962f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011963{
11964 rettv->v_type = VAR_STRING;
11965 rettv->vval.v_string = NULL;
11966#ifdef FEAT_CMDWIN
11967 rettv->vval.v_string = alloc(2);
11968 if (rettv->vval.v_string != NULL)
11969 {
11970 rettv->vval.v_string[0] = cmdwin_type;
11971 rettv->vval.v_string[1] = NUL;
11972 }
11973#endif
11974}
11975
11976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 * "getcwd()" function
11978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011980f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011982 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011983 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011986 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011987
11988 wp = find_tabwin(&argvars[0], &argvars[1]);
11989 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011991 if (wp->w_localdir != NULL)
11992 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11993 else if(globaldir != NULL)
11994 rettv->vval.v_string = vim_strsave(globaldir);
11995 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011996 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011997 cwd = alloc(MAXPATHL);
11998 if (cwd != NULL)
11999 {
12000 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12001 rettv->vval.v_string = vim_strsave(cwd);
12002 vim_free(cwd);
12003 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012004 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012005#ifdef BACKSLASH_IN_FILENAME
12006 if (rettv->vval.v_string != NULL)
12007 slash_adjust(rettv->vval.v_string);
12008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 }
12010}
12011
12012/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012013 * "getfontname()" function
12014 */
12015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012016f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->v_type = VAR_STRING;
12019 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012020#ifdef FEAT_GUI
12021 if (gui.in_use)
12022 {
12023 GuiFont font;
12024 char_u *name = NULL;
12025
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012026 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012027 {
12028 /* Get the "Normal" font. Either the name saved by
12029 * hl_set_font_name() or from the font ID. */
12030 font = gui.norm_font;
12031 name = hl_get_font_name();
12032 }
12033 else
12034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012036 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12037 return;
12038 font = gui_mch_get_font(name, FALSE);
12039 if (font == NOFONT)
12040 return; /* Invalid font name, return empty string. */
12041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012043 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012044 gui_mch_free_font(font);
12045 }
12046#endif
12047}
12048
12049/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012050 * "getfperm({fname})" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012054{
12055 char_u *fname;
12056 struct stat st;
12057 char_u *perm = NULL;
12058 char_u flags[] = "rwx";
12059 int i;
12060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012064 if (mch_stat((char *)fname, &st) >= 0)
12065 {
12066 perm = vim_strsave((char_u *)"---------");
12067 if (perm != NULL)
12068 {
12069 for (i = 0; i < 9; i++)
12070 {
12071 if (st.st_mode & (1 << (8 - i)))
12072 perm[i] = flags[i % 3];
12073 }
12074 }
12075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012077}
12078
12079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 * "getfsize({fname})" function
12081 */
12082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012083f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
12085 char_u *fname;
12086 struct stat st;
12087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
12092 if (mch_stat((char *)fname, &st) >= 0)
12093 {
12094 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012099
12100 /* non-perfect check for overflow */
12101 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12102 rettv->vval.v_number = -2;
12103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 }
12105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107}
12108
12109/*
12110 * "getftime({fname})" function
12111 */
12112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012113f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114{
12115 char_u *fname;
12116 struct stat st;
12117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119
12120 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124}
12125
12126/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127 * "getftype({fname})" function
12128 */
12129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012130f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012131{
12132 char_u *fname;
12133 struct stat st;
12134 char_u *type = NULL;
12135 char *t;
12136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012140 if (mch_lstat((char *)fname, &st) >= 0)
12141 {
12142#ifdef S_ISREG
12143 if (S_ISREG(st.st_mode))
12144 t = "file";
12145 else if (S_ISDIR(st.st_mode))
12146 t = "dir";
12147# ifdef S_ISLNK
12148 else if (S_ISLNK(st.st_mode))
12149 t = "link";
12150# endif
12151# ifdef S_ISBLK
12152 else if (S_ISBLK(st.st_mode))
12153 t = "bdev";
12154# endif
12155# ifdef S_ISCHR
12156 else if (S_ISCHR(st.st_mode))
12157 t = "cdev";
12158# endif
12159# ifdef S_ISFIFO
12160 else if (S_ISFIFO(st.st_mode))
12161 t = "fifo";
12162# endif
12163# ifdef S_ISSOCK
12164 else if (S_ISSOCK(st.st_mode))
12165 t = "fifo";
12166# endif
12167 else
12168 t = "other";
12169#else
12170# ifdef S_IFMT
12171 switch (st.st_mode & S_IFMT)
12172 {
12173 case S_IFREG: t = "file"; break;
12174 case S_IFDIR: t = "dir"; break;
12175# ifdef S_IFLNK
12176 case S_IFLNK: t = "link"; break;
12177# endif
12178# ifdef S_IFBLK
12179 case S_IFBLK: t = "bdev"; break;
12180# endif
12181# ifdef S_IFCHR
12182 case S_IFCHR: t = "cdev"; break;
12183# endif
12184# ifdef S_IFIFO
12185 case S_IFIFO: t = "fifo"; break;
12186# endif
12187# ifdef S_IFSOCK
12188 case S_IFSOCK: t = "socket"; break;
12189# endif
12190 default: t = "other";
12191 }
12192# else
12193 if (mch_isdir(fname))
12194 t = "dir";
12195 else
12196 t = "file";
12197# endif
12198#endif
12199 type = vim_strsave((char_u *)t);
12200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012202}
12203
12204/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012205 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206 */
12207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012208f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012209{
12210 linenr_T lnum;
12211 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012212 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213
12214 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012215 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012216 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012217 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012218 retlist = FALSE;
12219 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012220 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012221 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012223 retlist = TRUE;
12224 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012225
Bram Moolenaar342337a2005-07-21 21:11:17 +000012226 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012227}
12228
12229/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012230 * "getmatches()" function
12231 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012233f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012234{
12235#ifdef FEAT_SEARCH_EXTRA
12236 dict_T *dict;
12237 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012238 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012239
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012240 if (rettv_list_alloc(rettv) == OK)
12241 {
12242 while (cur != NULL)
12243 {
12244 dict = dict_alloc();
12245 if (dict == NULL)
12246 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012247 if (cur->match.regprog == NULL)
12248 {
12249 /* match added with matchaddpos() */
12250 for (i = 0; i < MAXPOSMATCH; ++i)
12251 {
12252 llpos_T *llpos;
12253 char buf[6];
12254 list_T *l;
12255
12256 llpos = &cur->pos.pos[i];
12257 if (llpos->lnum == 0)
12258 break;
12259 l = list_alloc();
12260 if (l == NULL)
12261 break;
12262 list_append_number(l, (varnumber_T)llpos->lnum);
12263 if (llpos->col > 0)
12264 {
12265 list_append_number(l, (varnumber_T)llpos->col);
12266 list_append_number(l, (varnumber_T)llpos->len);
12267 }
12268 sprintf(buf, "pos%d", i + 1);
12269 dict_add_list(dict, buf, l);
12270 }
12271 }
12272 else
12273 {
12274 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12275 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012276 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012277 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12278 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012279# ifdef FEAT_CONCEAL
12280 if (cur->conceal_char)
12281 {
12282 char_u buf[MB_MAXBYTES + 1];
12283
12284 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12285 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12286 }
12287# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012288 list_append_dict(rettv->vval.v_list, dict);
12289 cur = cur->next;
12290 }
12291 }
12292#endif
12293}
12294
12295/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012296 * "getpid()" function
12297 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012299f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012300{
12301 rettv->vval.v_number = mch_get_pid();
12302}
12303
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012304static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012305
12306/*
12307 * "getcurpos()" function
12308 */
12309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012310f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012311{
12312 getpos_both(argvars, rettv, TRUE);
12313}
12314
Bram Moolenaar18081e32008-02-20 19:11:07 +000012315/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012316 * "getpos(string)" function
12317 */
12318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012319f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012320{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012321 getpos_both(argvars, rettv, FALSE);
12322}
12323
12324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012325getpos_both(
12326 typval_T *argvars,
12327 typval_T *rettv,
12328 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012329{
Bram Moolenaara5525202006-03-02 22:52:09 +000012330 pos_T *fp;
12331 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012332 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012333
12334 if (rettv_list_alloc(rettv) == OK)
12335 {
12336 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012337 if (getcurpos)
12338 fp = &curwin->w_cursor;
12339 else
12340 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012341 if (fnum != -1)
12342 list_append_number(l, (varnumber_T)fnum);
12343 else
12344 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012345 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12346 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012347 list_append_number(l, (fp != NULL)
12348 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012349 : (varnumber_T)0);
12350 list_append_number(l,
12351#ifdef FEAT_VIRTUALEDIT
12352 (fp != NULL) ? (varnumber_T)fp->coladd :
12353#endif
12354 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012355 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012356 list_append_number(l, curwin->w_curswant == MAXCOL ?
12357 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012358 }
12359 else
12360 rettv->vval.v_number = FALSE;
12361}
12362
12363/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012364 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012365 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012367f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012368{
12369#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012370 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012371#endif
12372
Bram Moolenaar2641f772005-03-25 21:58:17 +000012373#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012374 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012375 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012376 wp = NULL;
12377 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12378 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012379 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012380 if (wp == NULL)
12381 return;
12382 }
12383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012384 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012385 }
12386#endif
12387}
12388
12389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 * "getreg()" function
12391 */
12392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012393f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395 char_u *strregname;
12396 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012397 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012398 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012401 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 strregname = get_tv_string_chk(&argvars[0]);
12404 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012405 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012408 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12409 return_list = get_tv_number_chk(&argvars[2], &error);
12410 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012413 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012414
12415 if (error)
12416 return;
12417
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 regname = (strregname == NULL ? '"' : *strregname);
12419 if (regname == 0)
12420 regname = '"';
12421
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012422 if (return_list)
12423 {
12424 rettv->v_type = VAR_LIST;
12425 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12426 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012427 if (rettv->vval.v_list != NULL)
12428 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012429 }
12430 else
12431 {
12432 rettv->v_type = VAR_STRING;
12433 rettv->vval.v_string = get_reg_contents(regname,
12434 arg2 ? GREG_EXPR_SRC : 0);
12435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436}
12437
12438/*
12439 * "getregtype()" function
12440 */
12441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012442f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
12444 char_u *strregname;
12445 int regname;
12446 char_u buf[NUMBUFLEN + 2];
12447 long reglen = 0;
12448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012449 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 {
12451 strregname = get_tv_string_chk(&argvars[0]);
12452 if (strregname == NULL) /* type error; errmsg already given */
12453 {
12454 rettv->v_type = VAR_STRING;
12455 rettv->vval.v_string = NULL;
12456 return;
12457 }
12458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 else
12460 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012461 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462
12463 regname = (strregname == NULL ? '"' : *strregname);
12464 if (regname == 0)
12465 regname = '"';
12466
12467 buf[0] = NUL;
12468 buf[1] = NUL;
12469 switch (get_reg_type(regname, &reglen))
12470 {
12471 case MLINE: buf[0] = 'V'; break;
12472 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 case MBLOCK:
12474 buf[0] = Ctrl_V;
12475 sprintf((char *)buf + 1, "%ld", reglen + 1);
12476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->v_type = VAR_STRING;
12479 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480}
12481
12482/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012483 * "gettabvar()" function
12484 */
12485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012487{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012488 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012489 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012490 dictitem_T *v;
12491 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012492 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012493
12494 rettv->v_type = VAR_STRING;
12495 rettv->vval.v_string = NULL;
12496
12497 varname = get_tv_string_chk(&argvars[1]);
12498 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12499 if (tp != NULL && varname != NULL)
12500 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012501 /* Set tp to be our tabpage, temporarily. Also set the window to the
12502 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012503 if (switch_win(&oldcurwin, &oldtabpage,
12504 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012505 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012506 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012507 /* look up the variable */
12508 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12509 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12510 if (v != NULL)
12511 {
12512 copy_tv(&v->di_tv, rettv);
12513 done = TRUE;
12514 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012515 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012516
12517 /* restore previous notion of curwin */
12518 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012519 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012520
12521 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012522 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012523}
12524
12525/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012526 * "gettabwinvar()" function
12527 */
12528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012529f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012530{
12531 getwinvar(argvars, rettv, 1);
12532}
12533
12534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 * "getwinposx()" function
12536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012538f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541#ifdef FEAT_GUI
12542 if (gui.in_use)
12543 {
12544 int x, y;
12545
12546 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 }
12549#endif
12550}
12551
12552/*
12553 * "getwinposy()" function
12554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012556f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559#ifdef FEAT_GUI
12560 if (gui.in_use)
12561 {
12562 int x, y;
12563
12564 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 }
12567#endif
12568}
12569
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012570/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012571 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012572 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012573 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012574find_win_by_nr(
12575 typval_T *vp,
12576 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012577{
12578#ifdef FEAT_WINDOWS
12579 win_T *wp;
12580#endif
12581 int nr;
12582
12583 nr = get_tv_number_chk(vp, NULL);
12584
12585#ifdef FEAT_WINDOWS
12586 if (nr < 0)
12587 return NULL;
12588 if (nr == 0)
12589 return curwin;
12590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012591 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12592 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012593 if (--nr <= 0)
12594 break;
12595 return wp;
12596#else
12597 if (nr == 0 || nr == 1)
12598 return curwin;
12599 return NULL;
12600#endif
12601}
12602
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012604 * Find window specified by "wvp" in tabpage "tvp".
12605 */
12606 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012607find_tabwin(
12608 typval_T *wvp, /* VAR_UNKNOWN for current window */
12609 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012610{
12611 win_T *wp = NULL;
12612 tabpage_T *tp = NULL;
12613 long n;
12614
12615 if (wvp->v_type != VAR_UNKNOWN)
12616 {
12617 if (tvp->v_type != VAR_UNKNOWN)
12618 {
12619 n = get_tv_number(tvp);
12620 if (n >= 0)
12621 tp = find_tabpage(n);
12622 }
12623 else
12624 tp = curtab;
12625
12626 if (tp != NULL)
12627 wp = find_win_by_nr(wvp, tp);
12628 }
12629 else
12630 wp = curwin;
12631
12632 return wp;
12633}
12634
12635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 * "getwinvar()" function
12637 */
12638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012639f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012641 getwinvar(argvars, rettv, 0);
12642}
12643
12644/*
12645 * getwinvar() and gettabwinvar()
12646 */
12647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012648getwinvar(
12649 typval_T *argvars,
12650 typval_T *rettv,
12651 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012652{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012653 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012656 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012657 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012658#ifdef FEAT_WINDOWS
12659 win_T *oldcurwin;
12660 tabpage_T *oldtabpage;
12661 int need_switch_win;
12662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012664#ifdef FEAT_WINDOWS
12665 if (off == 1)
12666 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12667 else
12668 tp = curtab;
12669#endif
12670 win = find_win_by_nr(&argvars[off], tp);
12671 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012674 rettv->v_type = VAR_STRING;
12675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
12677 if (win != NULL && varname != NULL)
12678 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012679#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012680 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012681 * otherwise the window is not valid. Only do this when needed,
12682 * autocommands get blocked. */
12683 need_switch_win = !(tp == curtab && win == curwin);
12684 if (!need_switch_win
12685 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12686#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012687 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012688 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012689 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012690 if (get_option_tv(&varname, rettv, 1) == OK)
12691 done = TRUE;
12692 }
12693 else
12694 {
12695 /* Look up the variable. */
12696 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12697 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12698 varname, FALSE);
12699 if (v != NULL)
12700 {
12701 copy_tv(&v->di_tv, rettv);
12702 done = TRUE;
12703 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012706
Bram Moolenaarba117c22015-09-29 16:53:22 +020012707#ifdef FEAT_WINDOWS
12708 if (need_switch_win)
12709 /* restore previous notion of curwin */
12710 restore_win(oldcurwin, oldtabpage, TRUE);
12711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 }
12713
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012714 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12715 /* use the default return value */
12716 copy_tv(&argvars[off + 2], rettv);
12717
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 --emsg_off;
12719}
12720
12721/*
12722 * "glob()" function
12723 */
12724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012725f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012727 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012731 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012732 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012734 if (argvars[1].v_type != VAR_UNKNOWN)
12735 {
12736 if (get_tv_number_chk(&argvars[1], &error))
12737 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012738 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012739 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012740 if (get_tv_number_chk(&argvars[2], &error))
12741 {
12742 rettv->v_type = VAR_LIST;
12743 rettv->vval.v_list = NULL;
12744 }
12745 if (argvars[3].v_type != VAR_UNKNOWN
12746 && get_tv_number_chk(&argvars[3], &error))
12747 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012748 }
12749 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012750 if (!error)
12751 {
12752 ExpandInit(&xpc);
12753 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012754 if (p_wic)
12755 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012756 if (rettv->v_type == VAR_STRING)
12757 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012758 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012759 else if (rettv_list_alloc(rettv) != FAIL)
12760 {
12761 int i;
12762
12763 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12764 NULL, options, WILD_ALL_KEEP);
12765 for (i = 0; i < xpc.xp_numfiles; i++)
12766 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12767
12768 ExpandCleanup(&xpc);
12769 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012770 }
12771 else
12772 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
12775/*
12776 * "globpath()" function
12777 */
12778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012779f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012781 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012784 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012785 garray_T ga;
12786 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012788 /* When the optional second argument is non-zero, don't remove matches
12789 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012791 if (argvars[2].v_type != VAR_UNKNOWN)
12792 {
12793 if (get_tv_number_chk(&argvars[2], &error))
12794 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012796 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012797 if (get_tv_number_chk(&argvars[3], &error))
12798 {
12799 rettv->v_type = VAR_LIST;
12800 rettv->vval.v_list = NULL;
12801 }
12802 if (argvars[4].v_type != VAR_UNKNOWN
12803 && get_tv_number_chk(&argvars[4], &error))
12804 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012805 }
12806 }
12807 if (file != NULL && !error)
12808 {
12809 ga_init2(&ga, (int)sizeof(char_u *), 10);
12810 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12811 if (rettv->v_type == VAR_STRING)
12812 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12813 else if (rettv_list_alloc(rettv) != FAIL)
12814 for (i = 0; i < ga.ga_len; ++i)
12815 list_append_string(rettv->vval.v_list,
12816 ((char_u **)(ga.ga_data))[i], -1);
12817 ga_clear_strings(&ga);
12818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012819 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012824 * "glob2regpat()" function
12825 */
12826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012828{
12829 char_u *pat = get_tv_string_chk(&argvars[0]);
12830
12831 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012832 rettv->vval.v_string = (pat == NULL)
12833 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012834}
12835
12836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 * "has()" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842 int i;
12843 char_u *name;
12844 int n = FALSE;
12845 static char *(has_list[]) =
12846 {
12847#ifdef AMIGA
12848 "amiga",
12849# ifdef FEAT_ARP
12850 "arp",
12851# endif
12852#endif
12853#ifdef __BEOS__
12854 "beos",
12855#endif
12856#ifdef MSDOS
12857# ifdef DJGPP
12858 "dos32",
12859# else
12860 "dos16",
12861# endif
12862#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012863#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 "mac",
12865#endif
12866#if defined(MACOS_X_UNIX)
12867 "macunix",
12868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869#ifdef __QNX__
12870 "qnx",
12871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#ifdef UNIX
12873 "unix",
12874#endif
12875#ifdef VMS
12876 "vms",
12877#endif
12878#ifdef WIN16
12879 "win16",
12880#endif
12881#ifdef WIN32
12882 "win32",
12883#endif
12884#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12885 "win32unix",
12886#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012887#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 "win64",
12889#endif
12890#ifdef EBCDIC
12891 "ebcdic",
12892#endif
12893#ifndef CASE_INSENSITIVE_FILENAME
12894 "fname_case",
12895#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012896#ifdef HAVE_ACL
12897 "acl",
12898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899#ifdef FEAT_ARABIC
12900 "arabic",
12901#endif
12902#ifdef FEAT_AUTOCMD
12903 "autocmd",
12904#endif
12905#ifdef FEAT_BEVAL
12906 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012907# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12908 "balloon_multiline",
12909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#endif
12911#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12912 "builtin_terms",
12913# ifdef ALL_BUILTIN_TCAPS
12914 "all_builtin_terms",
12915# endif
12916#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012917#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12918 || defined(FEAT_GUI_W32) \
12919 || defined(FEAT_GUI_MOTIF))
12920 "browsefilter",
12921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#ifdef FEAT_BYTEOFF
12923 "byte_offset",
12924#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012925#ifdef FEAT_CHANNEL
12926 "channel",
12927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928#ifdef FEAT_CINDENT
12929 "cindent",
12930#endif
12931#ifdef FEAT_CLIENTSERVER
12932 "clientserver",
12933#endif
12934#ifdef FEAT_CLIPBOARD
12935 "clipboard",
12936#endif
12937#ifdef FEAT_CMDL_COMPL
12938 "cmdline_compl",
12939#endif
12940#ifdef FEAT_CMDHIST
12941 "cmdline_hist",
12942#endif
12943#ifdef FEAT_COMMENTS
12944 "comments",
12945#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012946#ifdef FEAT_CONCEAL
12947 "conceal",
12948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef FEAT_CRYPT
12950 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012951 "crypt-blowfish",
12952 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953#endif
12954#ifdef FEAT_CSCOPE
12955 "cscope",
12956#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012957#ifdef FEAT_CURSORBIND
12958 "cursorbind",
12959#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012960#ifdef CURSOR_SHAPE
12961 "cursorshape",
12962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963#ifdef DEBUG
12964 "debug",
12965#endif
12966#ifdef FEAT_CON_DIALOG
12967 "dialog_con",
12968#endif
12969#ifdef FEAT_GUI_DIALOG
12970 "dialog_gui",
12971#endif
12972#ifdef FEAT_DIFF
12973 "diff",
12974#endif
12975#ifdef FEAT_DIGRAPHS
12976 "digraphs",
12977#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012978#ifdef FEAT_DIRECTX
12979 "directx",
12980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981#ifdef FEAT_DND
12982 "dnd",
12983#endif
12984#ifdef FEAT_EMACS_TAGS
12985 "emacs_tags",
12986#endif
12987 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012988 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989#ifdef FEAT_SEARCH_EXTRA
12990 "extra_search",
12991#endif
12992#ifdef FEAT_FKMAP
12993 "farsi",
12994#endif
12995#ifdef FEAT_SEARCHPATH
12996 "file_in_path",
12997#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012998#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012999 "filterpipe",
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#ifdef FEAT_FIND_ID
13002 "find_in_path",
13003#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013004#ifdef FEAT_FLOAT
13005 "float",
13006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007#ifdef FEAT_FOLDING
13008 "folding",
13009#endif
13010#ifdef FEAT_FOOTER
13011 "footer",
13012#endif
13013#if !defined(USE_SYSTEM) && defined(UNIX)
13014 "fork",
13015#endif
13016#ifdef FEAT_GETTEXT
13017 "gettext",
13018#endif
13019#ifdef FEAT_GUI
13020 "gui",
13021#endif
13022#ifdef FEAT_GUI_ATHENA
13023# ifdef FEAT_GUI_NEXTAW
13024 "gui_neXtaw",
13025# else
13026 "gui_athena",
13027# endif
13028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#ifdef FEAT_GUI_GTK
13030 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013033#ifdef FEAT_GUI_GNOME
13034 "gui_gnome",
13035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#ifdef FEAT_GUI_MAC
13037 "gui_mac",
13038#endif
13039#ifdef FEAT_GUI_MOTIF
13040 "gui_motif",
13041#endif
13042#ifdef FEAT_GUI_PHOTON
13043 "gui_photon",
13044#endif
13045#ifdef FEAT_GUI_W16
13046 "gui_win16",
13047#endif
13048#ifdef FEAT_GUI_W32
13049 "gui_win32",
13050#endif
13051#ifdef FEAT_HANGULIN
13052 "hangul_input",
13053#endif
13054#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13055 "iconv",
13056#endif
13057#ifdef FEAT_INS_EXPAND
13058 "insert_expand",
13059#endif
13060#ifdef FEAT_JUMPLIST
13061 "jumplist",
13062#endif
13063#ifdef FEAT_KEYMAP
13064 "keymap",
13065#endif
13066#ifdef FEAT_LANGMAP
13067 "langmap",
13068#endif
13069#ifdef FEAT_LIBCALL
13070 "libcall",
13071#endif
13072#ifdef FEAT_LINEBREAK
13073 "linebreak",
13074#endif
13075#ifdef FEAT_LISP
13076 "lispindent",
13077#endif
13078#ifdef FEAT_LISTCMDS
13079 "listcmds",
13080#endif
13081#ifdef FEAT_LOCALMAP
13082 "localmap",
13083#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013084#ifdef FEAT_LUA
13085# ifndef DYNAMIC_LUA
13086 "lua",
13087# endif
13088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089#ifdef FEAT_MENU
13090 "menu",
13091#endif
13092#ifdef FEAT_SESSION
13093 "mksession",
13094#endif
13095#ifdef FEAT_MODIFY_FNAME
13096 "modify_fname",
13097#endif
13098#ifdef FEAT_MOUSE
13099 "mouse",
13100#endif
13101#ifdef FEAT_MOUSESHAPE
13102 "mouseshape",
13103#endif
13104#if defined(UNIX) || defined(VMS)
13105# ifdef FEAT_MOUSE_DEC
13106 "mouse_dec",
13107# endif
13108# ifdef FEAT_MOUSE_GPM
13109 "mouse_gpm",
13110# endif
13111# ifdef FEAT_MOUSE_JSB
13112 "mouse_jsbterm",
13113# endif
13114# ifdef FEAT_MOUSE_NET
13115 "mouse_netterm",
13116# endif
13117# ifdef FEAT_MOUSE_PTERM
13118 "mouse_pterm",
13119# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013120# ifdef FEAT_MOUSE_SGR
13121 "mouse_sgr",
13122# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013123# ifdef FEAT_SYSMOUSE
13124 "mouse_sysmouse",
13125# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013126# ifdef FEAT_MOUSE_URXVT
13127 "mouse_urxvt",
13128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129# ifdef FEAT_MOUSE_XTERM
13130 "mouse_xterm",
13131# endif
13132#endif
13133#ifdef FEAT_MBYTE
13134 "multi_byte",
13135#endif
13136#ifdef FEAT_MBYTE_IME
13137 "multi_byte_ime",
13138#endif
13139#ifdef FEAT_MULTI_LANG
13140 "multi_lang",
13141#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013142#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013143#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013144 "mzscheme",
13145#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147#ifdef FEAT_OLE
13148 "ole",
13149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_PATH_EXTRA
13151 "path_extra",
13152#endif
13153#ifdef FEAT_PERL
13154#ifndef DYNAMIC_PERL
13155 "perl",
13156#endif
13157#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013158#ifdef FEAT_PERSISTENT_UNDO
13159 "persistent_undo",
13160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161#ifdef FEAT_PYTHON
13162#ifndef DYNAMIC_PYTHON
13163 "python",
13164#endif
13165#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013166#ifdef FEAT_PYTHON3
13167#ifndef DYNAMIC_PYTHON3
13168 "python3",
13169#endif
13170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171#ifdef FEAT_POSTSCRIPT
13172 "postscript",
13173#endif
13174#ifdef FEAT_PRINTER
13175 "printer",
13176#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013177#ifdef FEAT_PROFILE
13178 "profile",
13179#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013180#ifdef FEAT_RELTIME
13181 "reltime",
13182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183#ifdef FEAT_QUICKFIX
13184 "quickfix",
13185#endif
13186#ifdef FEAT_RIGHTLEFT
13187 "rightleft",
13188#endif
13189#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13190 "ruby",
13191#endif
13192#ifdef FEAT_SCROLLBIND
13193 "scrollbind",
13194#endif
13195#ifdef FEAT_CMDL_INFO
13196 "showcmd",
13197 "cmdline_info",
13198#endif
13199#ifdef FEAT_SIGNS
13200 "signs",
13201#endif
13202#ifdef FEAT_SMARTINDENT
13203 "smartindent",
13204#endif
13205#ifdef FEAT_SNIFF
13206 "sniff",
13207#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013208#ifdef STARTUPTIME
13209 "startuptime",
13210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211#ifdef FEAT_STL_OPT
13212 "statusline",
13213#endif
13214#ifdef FEAT_SUN_WORKSHOP
13215 "sun_workshop",
13216#endif
13217#ifdef FEAT_NETBEANS_INTG
13218 "netbeans_intg",
13219#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013220#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013221 "spell",
13222#endif
13223#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 "syntax",
13225#endif
13226#if defined(USE_SYSTEM) || !defined(UNIX)
13227 "system",
13228#endif
13229#ifdef FEAT_TAG_BINS
13230 "tag_binary",
13231#endif
13232#ifdef FEAT_TAG_OLDSTATIC
13233 "tag_old_static",
13234#endif
13235#ifdef FEAT_TAG_ANYWHITE
13236 "tag_any_white",
13237#endif
13238#ifdef FEAT_TCL
13239# ifndef DYNAMIC_TCL
13240 "tcl",
13241# endif
13242#endif
13243#ifdef TERMINFO
13244 "terminfo",
13245#endif
13246#ifdef FEAT_TERMRESPONSE
13247 "termresponse",
13248#endif
13249#ifdef FEAT_TEXTOBJ
13250 "textobjects",
13251#endif
13252#ifdef HAVE_TGETENT
13253 "tgetent",
13254#endif
13255#ifdef FEAT_TITLE
13256 "title",
13257#endif
13258#ifdef FEAT_TOOLBAR
13259 "toolbar",
13260#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013261#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13262 "unnamedplus",
13263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264#ifdef FEAT_USR_CMDS
13265 "user-commands", /* was accidentally included in 5.4 */
13266 "user_commands",
13267#endif
13268#ifdef FEAT_VIMINFO
13269 "viminfo",
13270#endif
13271#ifdef FEAT_VERTSPLIT
13272 "vertsplit",
13273#endif
13274#ifdef FEAT_VIRTUALEDIT
13275 "virtualedit",
13276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278#ifdef FEAT_VISUALEXTRA
13279 "visualextra",
13280#endif
13281#ifdef FEAT_VREPLACE
13282 "vreplace",
13283#endif
13284#ifdef FEAT_WILDIGN
13285 "wildignore",
13286#endif
13287#ifdef FEAT_WILDMENU
13288 "wildmenu",
13289#endif
13290#ifdef FEAT_WINDOWS
13291 "windows",
13292#endif
13293#ifdef FEAT_WAK
13294 "winaltkeys",
13295#endif
13296#ifdef FEAT_WRITEBACKUP
13297 "writebackup",
13298#endif
13299#ifdef FEAT_XIM
13300 "xim",
13301#endif
13302#ifdef FEAT_XFONTSET
13303 "xfontset",
13304#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013305#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013306 "xpm",
13307 "xpm_w32", /* for backward compatibility */
13308#else
13309# if defined(HAVE_XPM)
13310 "xpm",
13311# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef USE_XSMP
13314 "xsmp",
13315#endif
13316#ifdef USE_XSMP_INTERACT
13317 "xsmp_interact",
13318#endif
13319#ifdef FEAT_XCLIPBOARD
13320 "xterm_clipboard",
13321#endif
13322#ifdef FEAT_XTERM_SAVE
13323 "xterm_save",
13324#endif
13325#if defined(UNIX) && defined(FEAT_X11)
13326 "X11",
13327#endif
13328 NULL
13329 };
13330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 for (i = 0; has_list[i] != NULL; ++i)
13333 if (STRICMP(name, has_list[i]) == 0)
13334 {
13335 n = TRUE;
13336 break;
13337 }
13338
13339 if (n == FALSE)
13340 {
13341 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013342 {
13343 if (name[5] == '-'
13344 && STRLEN(name) > 11
13345 && vim_isdigit(name[6])
13346 && vim_isdigit(name[8])
13347 && vim_isdigit(name[10]))
13348 {
13349 int major = atoi((char *)name + 6);
13350 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013351
13352 /* Expect "patch-9.9.01234". */
13353 n = (major < VIM_VERSION_MAJOR
13354 || (major == VIM_VERSION_MAJOR
13355 && (minor < VIM_VERSION_MINOR
13356 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013357 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013358 }
13359 else
13360 n = has_patch(atoi((char *)name + 5));
13361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else if (STRICMP(name, "vim_starting") == 0)
13363 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013364#ifdef FEAT_MBYTE
13365 else if (STRICMP(name, "multi_byte_encoding") == 0)
13366 n = has_mbyte;
13367#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013368#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13369 else if (STRICMP(name, "balloon_multiline") == 0)
13370 n = multiline_balloon_available();
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef DYNAMIC_TCL
13373 else if (STRICMP(name, "tcl") == 0)
13374 n = tcl_enabled(FALSE);
13375#endif
13376#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13377 else if (STRICMP(name, "iconv") == 0)
13378 n = iconv_enabled(FALSE);
13379#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013380#ifdef DYNAMIC_LUA
13381 else if (STRICMP(name, "lua") == 0)
13382 n = lua_enabled(FALSE);
13383#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013384#ifdef DYNAMIC_MZSCHEME
13385 else if (STRICMP(name, "mzscheme") == 0)
13386 n = mzscheme_enabled(FALSE);
13387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388#ifdef DYNAMIC_RUBY
13389 else if (STRICMP(name, "ruby") == 0)
13390 n = ruby_enabled(FALSE);
13391#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013392#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef DYNAMIC_PYTHON
13394 else if (STRICMP(name, "python") == 0)
13395 n = python_enabled(FALSE);
13396#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013397#endif
13398#ifdef FEAT_PYTHON3
13399#ifdef DYNAMIC_PYTHON3
13400 else if (STRICMP(name, "python3") == 0)
13401 n = python3_enabled(FALSE);
13402#endif
13403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404#ifdef DYNAMIC_PERL
13405 else if (STRICMP(name, "perl") == 0)
13406 n = perl_enabled(FALSE);
13407#endif
13408#ifdef FEAT_GUI
13409 else if (STRICMP(name, "gui_running") == 0)
13410 n = (gui.in_use || gui.starting);
13411# ifdef FEAT_GUI_W32
13412 else if (STRICMP(name, "gui_win32s") == 0)
13413 n = gui_is_win32s();
13414# endif
13415# ifdef FEAT_BROWSE
13416 else if (STRICMP(name, "browse") == 0)
13417 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13418# endif
13419#endif
13420#ifdef FEAT_SYN_HL
13421 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013422 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#endif
13424#if defined(WIN3264)
13425 else if (STRICMP(name, "win95") == 0)
13426 n = mch_windows95();
13427#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013428#ifdef FEAT_NETBEANS_INTG
13429 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013430 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 }
13433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013438 * "has_key()" function
13439 */
13440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013441f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013442{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013443 if (argvars[0].v_type != VAR_DICT)
13444 {
13445 EMSG(_(e_dictreq));
13446 return;
13447 }
13448 if (argvars[0].vval.v_dict == NULL)
13449 return;
13450
13451 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013452 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013453}
13454
13455/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013456 * "haslocaldir()" function
13457 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013459f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013460{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013461 win_T *wp = NULL;
13462
13463 wp = find_tabwin(&argvars[0], &argvars[1]);
13464 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013465}
13466
13467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 * "hasmapto()" function
13469 */
13470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013471f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472{
13473 char_u *name;
13474 char_u *mode;
13475 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013476 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 mode = (char_u *)"nvo";
13481 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013484 if (argvars[2].v_type != VAR_UNKNOWN)
13485 abbr = get_tv_number(&argvars[2]);
13486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487
Bram Moolenaar2c932302006-03-18 21:42:09 +000013488 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492}
13493
13494/*
13495 * "histadd()" function
13496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013498f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499{
13500#ifdef FEAT_CMDHIST
13501 int histype;
13502 char_u *str;
13503 char_u buf[NUMBUFLEN];
13504#endif
13505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013506 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 if (check_restricted() || check_secure())
13508 return;
13509#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13511 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 if (histype >= 0)
13513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 if (*str != NUL)
13516 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013517 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 return;
13521 }
13522 }
13523#endif
13524}
13525
13526/*
13527 * "histdel()" function
13528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013530f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531{
13532#ifdef FEAT_CMDHIST
13533 int n;
13534 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013535 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13538 if (str == NULL)
13539 n = 0;
13540 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
13548 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 get_tv_string_buf(&argvars[1], buf));
13551 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552#endif
13553}
13554
13555/*
13556 * "histget()" function
13557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013559f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560{
13561#ifdef FEAT_CMDHIST
13562 int type;
13563 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13567 if (str == NULL)
13568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 {
13571 type = get_histtype(str);
13572 if (argvars[1].v_type == VAR_UNKNOWN)
13573 idx = get_history_idx(type);
13574 else
13575 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13576 /* -1 on type error */
13577 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583}
13584
13585/*
13586 * "histnr()" function
13587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013589f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590{
13591 int i;
13592
13593#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013594 char_u *history = get_tv_string_chk(&argvars[0]);
13595
13596 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 if (i >= HIST_CMD && i < HIST_COUNT)
13598 i = get_history_idx(i);
13599 else
13600#endif
13601 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603}
13604
13605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 * "highlightID(name)" function
13607 */
13608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013609f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612}
13613
13614/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615 * "highlight_exists()" function
13616 */
13617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013618f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619{
13620 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13621}
13622
13623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 * "hostname()" function
13625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013627f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
13629 char_u hostname[256];
13630
13631 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->v_type = VAR_STRING;
13633 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
13637 * iconv() function
13638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013640f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641{
13642#ifdef FEAT_MBYTE
13643 char_u buf1[NUMBUFLEN];
13644 char_u buf2[NUMBUFLEN];
13645 char_u *from, *to, *str;
13646 vimconv_T vimconv;
13647#endif
13648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->v_type = VAR_STRING;
13650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651
13652#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 str = get_tv_string(&argvars[0]);
13654 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13655 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 vimconv.vc_type = CONV_NONE;
13657 convert_setup(&vimconv, from, to);
13658
13659 /* If the encodings are equal, no conversion needed. */
13660 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664
13665 convert_setup(&vimconv, NULL, NULL);
13666 vim_free(from);
13667 vim_free(to);
13668#endif
13669}
13670
13671/*
13672 * "indent()" function
13673 */
13674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013675f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676{
13677 linenr_T lnum;
13678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684}
13685
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013686/*
13687 * "index()" function
13688 */
13689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013690f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013691{
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 list_T *l;
13693 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013694 long idx = 0;
13695 int ic = FALSE;
13696
13697 rettv->vval.v_number = -1;
13698 if (argvars[0].v_type != VAR_LIST)
13699 {
13700 EMSG(_(e_listreq));
13701 return;
13702 }
13703 l = argvars[0].vval.v_list;
13704 if (l != NULL)
13705 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013706 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 int error = FALSE;
13710
Bram Moolenaar758711c2005-02-02 23:11:38 +000013711 /* Start at specified item. Use the cached index that list_find()
13712 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013713 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013714 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013715 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 ic = get_tv_number_chk(&argvars[3], &error);
13717 if (error)
13718 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013719 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013720
Bram Moolenaar758711c2005-02-02 23:11:38 +000013721 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013722 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013723 {
13724 rettv->vval.v_number = idx;
13725 break;
13726 }
13727 }
13728}
13729
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730static int inputsecret_flag = 0;
13731
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013732static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013733
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013735 * This function is used by f_input() and f_inputdialog() functions. The third
13736 * argument to f_input() specifies the type of completion to use at the
13737 * prompt. The third argument to f_inputdialog() specifies the value to return
13738 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 */
13740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013741get_user_input(
13742 typval_T *argvars,
13743 typval_T *rettv,
13744 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 char_u *p = NULL;
13748 int c;
13749 char_u buf[NUMBUFLEN];
13750 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013752 int xp_type = EXPAND_NOTHING;
13753 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757
13758#ifdef NO_CONSOLE_INPUT
13759 /* While starting up, there is no place to enter text. */
13760 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762#endif
13763
13764 cmd_silent = FALSE; /* Want to see the prompt. */
13765 if (prompt != NULL)
13766 {
13767 /* Only the part of the message after the last NL is considered as
13768 * prompt for the command line */
13769 p = vim_strrchr(prompt, '\n');
13770 if (p == NULL)
13771 p = prompt;
13772 else
13773 {
13774 ++p;
13775 c = *p;
13776 *p = NUL;
13777 msg_start();
13778 msg_clr_eos();
13779 msg_puts_attr(prompt, echo_attr);
13780 msg_didout = FALSE;
13781 msg_starthere();
13782 *p = c;
13783 }
13784 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 if (argvars[1].v_type != VAR_UNKNOWN)
13787 {
13788 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13789 if (defstr != NULL)
13790 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013792 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013793 {
13794 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013795 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013796 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013797
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013798 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013799 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013800
Bram Moolenaar4463f292005-09-25 22:20:24 +000013801 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13802 if (xp_name == NULL)
13803 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013804
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013805 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013806
Bram Moolenaar4463f292005-09-25 22:20:24 +000013807 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13808 &xp_arg) == FAIL)
13809 return;
13810 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013811 }
13812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013814 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013815 int save_ex_normal_busy = ex_normal_busy;
13816 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013818 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13819 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013820 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013821 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013822 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013823 && argvars[1].v_type != VAR_UNKNOWN
13824 && argvars[2].v_type != VAR_UNKNOWN)
13825 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13826 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013827
13828 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 /* since the user typed this, no need to wait for return */
13831 need_wait_return = FALSE;
13832 msg_didout = FALSE;
13833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 cmd_silent = cmd_silent_save;
13835}
13836
13837/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013838 * "input()" function
13839 * Also handles inputsecret() when inputsecret is set.
13840 */
13841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013842f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013843{
13844 get_user_input(argvars, rettv, FALSE);
13845}
13846
13847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 * "inputdialog()" function
13849 */
13850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013851f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852{
13853#if defined(FEAT_GUI_TEXTDIALOG)
13854 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13855 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13856 {
13857 char_u *message;
13858 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 message = get_tv_string_chk(&argvars[0]);
13862 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013863 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013864 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 else
13866 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 if (message != NULL && defstr != NULL
13868 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013869 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 else
13872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 if (message != NULL && defstr != NULL
13874 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 rettv->vval.v_string = vim_strsave(
13877 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013881 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 }
13883 else
13884#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013885 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886}
13887
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013888/*
13889 * "inputlist()" function
13890 */
13891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013892f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013893{
13894 listitem_T *li;
13895 int selected;
13896 int mouse_used;
13897
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013898#ifdef NO_CONSOLE_INPUT
13899 /* While starting up, there is no place to enter text. */
13900 if (no_console_input())
13901 return;
13902#endif
13903 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13904 {
13905 EMSG2(_(e_listarg), "inputlist()");
13906 return;
13907 }
13908
13909 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013910 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013911 lines_left = Rows; /* avoid more prompt */
13912 msg_scroll = TRUE;
13913 msg_clr_eos();
13914
13915 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13916 {
13917 msg_puts(get_tv_string(&li->li_tv));
13918 msg_putchar('\n');
13919 }
13920
13921 /* Ask for choice. */
13922 selected = prompt_for_number(&mouse_used);
13923 if (mouse_used)
13924 selected -= lines_left;
13925
13926 rettv->vval.v_number = selected;
13927}
13928
13929
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13931
13932/*
13933 * "inputrestore()" function
13934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013936f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
13938 if (ga_userinput.ga_len > 0)
13939 {
13940 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13942 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013943 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 }
13945 else if (p_verbose > 1)
13946 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013947 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 }
13950}
13951
13952/*
13953 * "inputsave()" function
13954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013956f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013958 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 if (ga_grow(&ga_userinput, 1) == OK)
13960 {
13961 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13962 + ga_userinput.ga_len);
13963 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013964 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 }
13966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968}
13969
13970/*
13971 * "inputsecret()" function
13972 */
13973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013974f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975{
13976 ++cmdline_star;
13977 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 --cmdline_star;
13980 --inputsecret_flag;
13981}
13982
13983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984 * "insert()" function
13985 */
13986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013987f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013988{
13989 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013990 listitem_T *item;
13991 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013993
13994 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013996 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013997 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013998 {
13999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 before = get_tv_number_chk(&argvars[2], &error);
14001 if (error)
14002 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014003
Bram Moolenaar758711c2005-02-02 23:11:38 +000014004 if (before == l->lv_len)
14005 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014006 else
14007 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014008 item = list_find(l, before);
14009 if (item == NULL)
14010 {
14011 EMSGN(_(e_listidx), before);
14012 l = NULL;
14013 }
14014 }
14015 if (l != NULL)
14016 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014017 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014018 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014019 }
14020 }
14021}
14022
14023/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014024 * "invert(expr)" function
14025 */
14026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014027f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014028{
14029 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14030}
14031
14032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 * "isdirectory()" function
14034 */
14035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014036f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039}
14040
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014041/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014042 * "islocked()" function
14043 */
14044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014045f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014046{
14047 lval_T lv;
14048 char_u *end;
14049 dictitem_T *di;
14050
14051 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014052 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14053 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014054 if (end != NULL && lv.ll_name != NULL)
14055 {
14056 if (*end != NUL)
14057 EMSG(_(e_trailing));
14058 else
14059 {
14060 if (lv.ll_tv == NULL)
14061 {
14062 if (check_changedtick(lv.ll_name))
14063 rettv->vval.v_number = 1; /* always locked */
14064 else
14065 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014066 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014067 if (di != NULL)
14068 {
14069 /* Consider a variable locked when:
14070 * 1. the variable itself is locked
14071 * 2. the value of the variable is locked.
14072 * 3. the List or Dict value is locked.
14073 */
14074 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14075 || tv_islocked(&di->di_tv));
14076 }
14077 }
14078 }
14079 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014080 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014081 else if (lv.ll_newkey != NULL)
14082 EMSG2(_(e_dictkey), lv.ll_newkey);
14083 else if (lv.ll_list != NULL)
14084 /* List item. */
14085 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14086 else
14087 /* Dictionary item. */
14088 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14089 }
14090 }
14091
14092 clear_lval(&lv);
14093}
14094
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014095static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014096
14097/*
14098 * Turn a dict into a list:
14099 * "what" == 0: list of keys
14100 * "what" == 1: list of values
14101 * "what" == 2: list of items
14102 */
14103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014104dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105{
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 list_T *l2;
14107 dictitem_T *di;
14108 hashitem_T *hi;
14109 listitem_T *li;
14110 listitem_T *li2;
14111 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014112 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014113
Bram Moolenaar8c711452005-01-14 21:53:12 +000014114 if (argvars[0].v_type != VAR_DICT)
14115 {
14116 EMSG(_(e_dictreq));
14117 return;
14118 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014119 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014120 return;
14121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014122 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014124
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014125 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014128 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014129 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014130 --todo;
14131 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014132
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 li = listitem_alloc();
14134 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014135 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014136 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014138 if (what == 0)
14139 {
14140 /* keys() */
14141 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014142 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014143 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14144 }
14145 else if (what == 1)
14146 {
14147 /* values() */
14148 copy_tv(&di->di_tv, &li->li_tv);
14149 }
14150 else
14151 {
14152 /* items() */
14153 l2 = list_alloc();
14154 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014155 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014156 li->li_tv.vval.v_list = l2;
14157 if (l2 == NULL)
14158 break;
14159 ++l2->lv_refcount;
14160
14161 li2 = listitem_alloc();
14162 if (li2 == NULL)
14163 break;
14164 list_append(l2, li2);
14165 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014166 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014167 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14168
14169 li2 = listitem_alloc();
14170 if (li2 == NULL)
14171 break;
14172 list_append(l2, li2);
14173 copy_tv(&di->di_tv, &li2->li_tv);
14174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014175 }
14176 }
14177}
14178
14179/*
14180 * "items(dict)" function
14181 */
14182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014183f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014184{
14185 dict_list(argvars, rettv, 2);
14186}
14187
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014189 * "join()" function
14190 */
14191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014192f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014193{
14194 garray_T ga;
14195 char_u *sep;
14196
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197 if (argvars[0].v_type != VAR_LIST)
14198 {
14199 EMSG(_(e_listreq));
14200 return;
14201 }
14202 if (argvars[0].vval.v_list == NULL)
14203 return;
14204 if (argvars[1].v_type == VAR_UNKNOWN)
14205 sep = (char_u *)" ";
14206 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014208
14209 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210
14211 if (sep != NULL)
14212 {
14213 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014214 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 ga_append(&ga, NUL);
14216 rettv->vval.v_string = (char_u *)ga.ga_data;
14217 }
14218 else
14219 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014220}
14221
14222/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014223 * "jsondecode()" function
14224 */
14225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014226f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014227{
14228 js_read_T reader;
14229
14230 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014231 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014232 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014233 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014234 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014235}
14236
14237/*
14238 * "jsonencode()" function
14239 */
14240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014241f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014242{
14243 rettv->v_type = VAR_STRING;
14244 rettv->vval.v_string = json_encode(&argvars[0]);
14245}
14246
14247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 * "keys()" function
14249 */
14250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252{
14253 dict_list(argvars, rettv, 0);
14254}
14255
14256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 * "last_buffer_nr()" function.
14258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261{
14262 int n = 0;
14263 buf_T *buf;
14264
14265 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14266 if (n < buf->b_fnum)
14267 n = buf->b_fnum;
14268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014269 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014270}
14271
14272/*
14273 * "len()" function
14274 */
14275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014276f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014277{
14278 switch (argvars[0].v_type)
14279 {
14280 case VAR_STRING:
14281 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 rettv->vval.v_number = (varnumber_T)STRLEN(
14283 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284 break;
14285 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014288 case VAR_DICT:
14289 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14290 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014291 case VAR_UNKNOWN:
14292 case VAR_SPECIAL:
14293 case VAR_FLOAT:
14294 case VAR_FUNC:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014295 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296 break;
14297 }
14298}
14299
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014300static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301
14302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014303libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014304{
14305#ifdef FEAT_LIBCALL
14306 char_u *string_in;
14307 char_u **string_result;
14308 int nr_result;
14309#endif
14310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014312 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014314
14315 if (check_restricted() || check_secure())
14316 return;
14317
14318#ifdef FEAT_LIBCALL
14319 /* The first two args must be strings, otherwise its meaningless */
14320 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14321 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014322 string_in = NULL;
14323 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014324 string_in = argvars[2].vval.v_string;
14325 if (type == VAR_NUMBER)
14326 string_result = NULL;
14327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014329 if (mch_libcall(argvars[0].vval.v_string,
14330 argvars[1].vval.v_string,
14331 string_in,
14332 argvars[2].vval.v_number,
14333 string_result,
14334 &nr_result) == OK
14335 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014337 }
14338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014342 * "libcall()" function
14343 */
14344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014345f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346{
14347 libcall_common(argvars, rettv, VAR_STRING);
14348}
14349
14350/*
14351 * "libcallnr()" function
14352 */
14353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014354f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355{
14356 libcall_common(argvars, rettv, VAR_NUMBER);
14357}
14358
14359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 * "line(string)" function
14361 */
14362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014363f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
14365 linenr_T lnum = 0;
14366 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014367 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014369 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 if (fp != NULL)
14371 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373}
14374
14375/*
14376 * "line2byte(lnum)" function
14377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014379f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380{
14381#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383#else
14384 linenr_T lnum;
14385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14391 if (rettv->vval.v_number >= 0)
14392 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393#endif
14394}
14395
14396/*
14397 * "lispindent(lnum)" function
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402#ifdef FEAT_LISP
14403 pos_T pos;
14404 linenr_T lnum;
14405
14406 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14409 {
14410 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 curwin->w_cursor = pos;
14413 }
14414 else
14415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417}
14418
14419/*
14420 * "localtime()" function
14421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014423f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426}
14427
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014428static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429
14430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014431get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
14433 char_u *keys;
14434 char_u *which;
14435 char_u buf[NUMBUFLEN];
14436 char_u *keys_buf = NULL;
14437 char_u *rhs;
14438 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014439 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014440 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014441 mapblock_T *mp;
14442 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
14444 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->v_type = VAR_STRING;
14446 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 if (*keys == NUL)
14450 return;
14451
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014452 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014455 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014456 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014457 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014458 if (argvars[3].v_type != VAR_UNKNOWN)
14459 get_dict = get_tv_number(&argvars[3]);
14460 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 else
14463 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 if (which == NULL)
14465 return;
14466
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 mode = get_map_mode(&which, 0);
14468
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014469 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014470 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014472
14473 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014475 /* Return a string. */
14476 if (rhs != NULL)
14477 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478
Bram Moolenaarbd743252010-10-20 21:23:33 +020014479 }
14480 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14481 {
14482 /* Return a dictionary. */
14483 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14484 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14485 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486
Bram Moolenaarbd743252010-10-20 21:23:33 +020014487 dict_add_nr_str(dict, "lhs", 0L, lhs);
14488 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14489 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14490 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14491 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14492 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14493 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014494 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014495 dict_add_nr_str(dict, "mode", 0L, mapmode);
14496
14497 vim_free(lhs);
14498 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 }
14500}
14501
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014502#ifdef FEAT_FLOAT
14503/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014504 * "log()" function
14505 */
14506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014507f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014508{
14509 float_T f;
14510
14511 rettv->v_type = VAR_FLOAT;
14512 if (get_float_arg(argvars, &f) == OK)
14513 rettv->vval.v_float = log(f);
14514 else
14515 rettv->vval.v_float = 0.0;
14516}
14517
14518/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014519 * "log10()" function
14520 */
14521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014522f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014523{
14524 float_T f;
14525
14526 rettv->v_type = VAR_FLOAT;
14527 if (get_float_arg(argvars, &f) == OK)
14528 rettv->vval.v_float = log10(f);
14529 else
14530 rettv->vval.v_float = 0.0;
14531}
14532#endif
14533
Bram Moolenaar1dced572012-04-05 16:54:08 +020014534#ifdef FEAT_LUA
14535/*
14536 * "luaeval()" function
14537 */
14538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014539f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014540{
14541 char_u *str;
14542 char_u buf[NUMBUFLEN];
14543
14544 str = get_tv_string_buf(&argvars[0], buf);
14545 do_luaeval(str, argvars + 1, rettv);
14546}
14547#endif
14548
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014550 * "map()" function
14551 */
14552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014553f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014554{
14555 filter_map(argvars, rettv, TRUE);
14556}
14557
14558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 */
14561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014562f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565}
14566
14567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 */
14570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014571f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574}
14575
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014576static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577
14578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014579find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014581 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014582 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 char_u *pat;
14585 regmatch_T regmatch;
14586 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014587 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 char_u *save_cpo;
14589 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014590 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014591 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014592 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 list_T *l = NULL;
14594 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014596 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
14598 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14599 save_cpo = p_cpo;
14600 p_cpo = (char_u *)"";
14601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014602 rettv->vval.v_number = -1;
14603 if (type == 3)
14604 {
14605 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014606 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014608 }
14609 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->v_type = VAR_STRING;
14612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 if (argvars[0].v_type == VAR_LIST)
14616 {
14617 if ((l = argvars[0].vval.v_list) == NULL)
14618 goto theend;
14619 li = l->lv_first;
14620 }
14621 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014622 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014623 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014624 len = (long)STRLEN(str);
14625 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14628 if (pat == NULL)
14629 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 int error = FALSE;
14634
14635 start = get_tv_number_chk(&argvars[2], &error);
14636 if (error)
14637 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014638 if (l != NULL)
14639 {
14640 li = list_find(l, start);
14641 if (li == NULL)
14642 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014643 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014644 }
14645 else
14646 {
14647 if (start < 0)
14648 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014649 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014650 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014651 /* When "count" argument is there ignore matches before "start",
14652 * otherwise skip part of the string. Differs when pattern is "^"
14653 * or "\<". */
14654 if (argvars[3].v_type != VAR_UNKNOWN)
14655 startcol = start;
14656 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014657 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014658 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014659 len -= start;
14660 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014661 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 nth = get_tv_number_chk(&argvars[3], &error);
14665 if (error)
14666 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 }
14668
14669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14670 if (regmatch.regprog != NULL)
14671 {
14672 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014674 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014675 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014676 if (l != NULL)
14677 {
14678 if (li == NULL)
14679 {
14680 match = FALSE;
14681 break;
14682 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014683 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014684 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014685 if (str == NULL)
14686 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014687 }
14688
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014689 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014690
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014691 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014693 if (l == NULL && !match)
14694 break;
14695
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014696 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014697 if (l != NULL)
14698 {
14699 li = li->li_next;
14700 ++idx;
14701 }
14702 else
14703 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014704#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014705 startcol = (colnr_T)(regmatch.startp[0]
14706 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014707#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014708 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014710 if (startcol > (colnr_T)len
14711 || str + startcol <= regmatch.startp[0])
14712 {
14713 match = FALSE;
14714 break;
14715 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014716 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014717 }
14718
14719 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 int i;
14724
14725 /* return list with matched string and submatches */
14726 for (i = 0; i < NSUBEXP; ++i)
14727 {
14728 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014729 {
14730 if (list_append_string(rettv->vval.v_list,
14731 (char_u *)"", 0) == FAIL)
14732 break;
14733 }
14734 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014735 regmatch.startp[i],
14736 (int)(regmatch.endp[i] - regmatch.startp[i]))
14737 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 }
14740 }
14741 else if (type == 2)
14742 {
14743 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014744 if (l != NULL)
14745 copy_tv(&li->li_tv, rettv);
14746 else
14747 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014749 }
14750 else if (l != NULL)
14751 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 else
14753 {
14754 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 (varnumber_T)(regmatch.startp[0] - str);
14757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014760 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 }
14762 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014763 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 }
14765
14766theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 p_cpo = save_cpo;
14769}
14770
14771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772 * "match()" function
14773 */
14774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014775f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776{
14777 find_some_match(argvars, rettv, 1);
14778}
14779
14780/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014781 * "matchadd()" function
14782 */
14783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014784f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014785{
14786#ifdef FEAT_SEARCH_EXTRA
14787 char_u buf[NUMBUFLEN];
14788 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14789 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14790 int prio = 10; /* default priority */
14791 int id = -1;
14792 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014793 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014794
14795 rettv->vval.v_number = -1;
14796
14797 if (grp == NULL || pat == NULL)
14798 return;
14799 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014800 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014801 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014802 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014803 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014804 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014805 if (argvars[4].v_type != VAR_UNKNOWN)
14806 {
14807 if (argvars[4].v_type != VAR_DICT)
14808 {
14809 EMSG(_(e_dictreq));
14810 return;
14811 }
14812 if (dict_find(argvars[4].vval.v_dict,
14813 (char_u *)"conceal", -1) != NULL)
14814 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14815 (char_u *)"conceal", FALSE);
14816 }
14817 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014818 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014819 if (error == TRUE)
14820 return;
14821 if (id >= 1 && id <= 3)
14822 {
14823 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14824 return;
14825 }
14826
Bram Moolenaar6561d522015-07-21 15:48:27 +020014827 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14828 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014829#endif
14830}
14831
14832/*
14833 * "matchaddpos()" function
14834 */
14835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014836f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014837{
14838#ifdef FEAT_SEARCH_EXTRA
14839 char_u buf[NUMBUFLEN];
14840 char_u *group;
14841 int prio = 10;
14842 int id = -1;
14843 int error = FALSE;
14844 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014845 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014846
14847 rettv->vval.v_number = -1;
14848
14849 group = get_tv_string_buf_chk(&argvars[0], buf);
14850 if (group == NULL)
14851 return;
14852
14853 if (argvars[1].v_type != VAR_LIST)
14854 {
14855 EMSG2(_(e_listarg), "matchaddpos()");
14856 return;
14857 }
14858 l = argvars[1].vval.v_list;
14859 if (l == NULL)
14860 return;
14861
14862 if (argvars[2].v_type != VAR_UNKNOWN)
14863 {
14864 prio = get_tv_number_chk(&argvars[2], &error);
14865 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014866 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014867 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014868 if (argvars[4].v_type != VAR_UNKNOWN)
14869 {
14870 if (argvars[4].v_type != VAR_DICT)
14871 {
14872 EMSG(_(e_dictreq));
14873 return;
14874 }
14875 if (dict_find(argvars[4].vval.v_dict,
14876 (char_u *)"conceal", -1) != NULL)
14877 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14878 (char_u *)"conceal", FALSE);
14879 }
14880 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014881 }
14882 if (error == TRUE)
14883 return;
14884
14885 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14886 if (id == 1 || id == 2)
14887 {
14888 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14889 return;
14890 }
14891
Bram Moolenaar6561d522015-07-21 15:48:27 +020014892 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14893 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014894#endif
14895}
14896
14897/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014898 * "matcharg()" function
14899 */
14900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014901f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014902{
14903 if (rettv_list_alloc(rettv) == OK)
14904 {
14905#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014906 int id = get_tv_number(&argvars[0]);
14907 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014908
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014909 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014910 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014911 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14912 {
14913 list_append_string(rettv->vval.v_list,
14914 syn_id2name(m->hlg_id), -1);
14915 list_append_string(rettv->vval.v_list, m->pattern, -1);
14916 }
14917 else
14918 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014919 list_append_string(rettv->vval.v_list, NULL, -1);
14920 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014921 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014922 }
14923#endif
14924 }
14925}
14926
14927/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014928 * "matchdelete()" function
14929 */
14930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014931f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014932{
14933#ifdef FEAT_SEARCH_EXTRA
14934 rettv->vval.v_number = match_delete(curwin,
14935 (int)get_tv_number(&argvars[0]), TRUE);
14936#endif
14937}
14938
14939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 * "matchend()" function
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944{
14945 find_some_match(argvars, rettv, 0);
14946}
14947
14948/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014949 * "matchlist()" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014953{
14954 find_some_match(argvars, rettv, 3);
14955}
14956
14957/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958 * "matchstr()" function
14959 */
14960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014961f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962{
14963 find_some_match(argvars, rettv, 2);
14964}
14965
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014966static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014967
14968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014969max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014970{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014971 long n = 0;
14972 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014974
14975 if (argvars[0].v_type == VAR_LIST)
14976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014977 list_T *l;
14978 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014979
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014980 l = argvars[0].vval.v_list;
14981 if (l != NULL)
14982 {
14983 li = l->lv_first;
14984 if (li != NULL)
14985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014986 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014987 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014988 {
14989 li = li->li_next;
14990 if (li == NULL)
14991 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014993 if (domax ? i > n : i < n)
14994 n = i;
14995 }
14996 }
14997 }
14998 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014999 else if (argvars[0].v_type == VAR_DICT)
15000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015001 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015002 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015004 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015005
15006 d = argvars[0].vval.v_dict;
15007 if (d != NULL)
15008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015009 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015011 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015012 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015014 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015016 if (first)
15017 {
15018 n = i;
15019 first = FALSE;
15020 }
15021 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015022 n = i;
15023 }
15024 }
15025 }
15026 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015027 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015028 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015030}
15031
15032/*
15033 * "max()" function
15034 */
15035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015036f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015037{
15038 max_min(argvars, rettv, TRUE);
15039}
15040
15041/*
15042 * "min()" function
15043 */
15044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015045f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015046{
15047 max_min(argvars, rettv, FALSE);
15048}
15049
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015050static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015051
15052/*
15053 * Create the directory in which "dir" is located, and higher levels when
15054 * needed.
15055 */
15056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015057mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015058{
15059 char_u *p;
15060 char_u *updir;
15061 int r = FAIL;
15062
15063 /* Get end of directory name in "dir".
15064 * We're done when it's "/" or "c:/". */
15065 p = gettail_sep(dir);
15066 if (p <= get_past_head(dir))
15067 return OK;
15068
15069 /* If the directory exists we're done. Otherwise: create it.*/
15070 updir = vim_strnsave(dir, (int)(p - dir));
15071 if (updir == NULL)
15072 return FAIL;
15073 if (mch_isdir(updir))
15074 r = OK;
15075 else if (mkdir_recurse(updir, prot) == OK)
15076 r = vim_mkdir_emsg(updir, prot);
15077 vim_free(updir);
15078 return r;
15079}
15080
15081#ifdef vim_mkdir
15082/*
15083 * "mkdir()" function
15084 */
15085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015086f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015087{
15088 char_u *dir;
15089 char_u buf[NUMBUFLEN];
15090 int prot = 0755;
15091
15092 rettv->vval.v_number = FAIL;
15093 if (check_restricted() || check_secure())
15094 return;
15095
15096 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015097 if (*dir == NUL)
15098 rettv->vval.v_number = FAIL;
15099 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015100 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015101 if (*gettail(dir) == NUL)
15102 /* remove trailing slashes */
15103 *gettail_sep(dir) = NUL;
15104
15105 if (argvars[1].v_type != VAR_UNKNOWN)
15106 {
15107 if (argvars[2].v_type != VAR_UNKNOWN)
15108 prot = get_tv_number_chk(&argvars[2], NULL);
15109 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15110 mkdir_recurse(dir, prot);
15111 }
15112 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015113 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015114}
15115#endif
15116
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118 * "mode()" function
15119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015121f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015123 char_u buf[3];
15124
15125 buf[1] = NUL;
15126 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 if (VIsual_active)
15129 {
15130 if (VIsual_select)
15131 buf[0] = VIsual_mode + 's' - 'v';
15132 else
15133 buf[0] = VIsual_mode;
15134 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015135 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015136 || State == CONFIRM)
15137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015139 if (State == ASKMORE)
15140 buf[1] = 'm';
15141 else if (State == CONFIRM)
15142 buf[1] = '?';
15143 }
15144 else if (State == EXTERNCMD)
15145 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146 else if (State & INSERT)
15147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015148#ifdef FEAT_VREPLACE
15149 if (State & VREPLACE_FLAG)
15150 {
15151 buf[0] = 'R';
15152 buf[1] = 'v';
15153 }
15154 else
15155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 if (State & REPLACE_FLAG)
15157 buf[0] = 'R';
15158 else
15159 buf[0] = 'i';
15160 }
15161 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015164 if (exmode_active)
15165 buf[1] = 'v';
15166 }
15167 else if (exmode_active)
15168 {
15169 buf[0] = 'c';
15170 buf[1] = 'e';
15171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015175 if (finish_op)
15176 buf[1] = 'o';
15177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015179 /* Clear out the minor mode when the argument is not a non-zero number or
15180 * non-empty string. */
15181 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015182 buf[1] = NUL;
15183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 rettv->vval.v_string = vim_strsave(buf);
15185 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
Bram Moolenaar429fa852013-04-15 12:27:36 +020015188#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015189/*
15190 * "mzeval()" function
15191 */
15192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015193f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015194{
15195 char_u *str;
15196 char_u buf[NUMBUFLEN];
15197
15198 str = get_tv_string_buf(&argvars[0], buf);
15199 do_mzeval(str, rettv);
15200}
Bram Moolenaar75676462013-01-30 14:55:42 +010015201
15202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015203mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015204{
15205 typval_T argvars[3];
15206
15207 argvars[0].v_type = VAR_STRING;
15208 argvars[0].vval.v_string = name;
15209 copy_tv(args, &argvars[1]);
15210 argvars[2].v_type = VAR_UNKNOWN;
15211 f_call(argvars, rettv);
15212 clear_tv(&argvars[1]);
15213}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015214#endif
15215
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217 * "nextnonblank()" function
15218 */
15219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015220f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221{
15222 linenr_T lnum;
15223
15224 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227 {
15228 lnum = 0;
15229 break;
15230 }
15231 if (*skipwhite(ml_get(lnum)) != NUL)
15232 break;
15233 }
15234 rettv->vval.v_number = lnum;
15235}
15236
15237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 * "nr2char()" function
15239 */
15240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015241f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242{
15243 char_u buf[NUMBUFLEN];
15244
15245#ifdef FEAT_MBYTE
15246 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015247 {
15248 int utf8 = 0;
15249
15250 if (argvars[1].v_type != VAR_UNKNOWN)
15251 utf8 = get_tv_number_chk(&argvars[1], NULL);
15252 if (utf8)
15253 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15254 else
15255 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 else
15258#endif
15259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 buf[1] = NUL;
15262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 rettv->v_type = VAR_STRING;
15264 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015265}
15266
15267/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015268 * "or(expr, expr)" function
15269 */
15270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015271f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015272{
15273 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15274 | get_tv_number_chk(&argvars[1], NULL);
15275}
15276
15277/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015278 * "pathshorten()" function
15279 */
15280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015281f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015282{
15283 char_u *p;
15284
15285 rettv->v_type = VAR_STRING;
15286 p = get_tv_string_chk(&argvars[0]);
15287 if (p == NULL)
15288 rettv->vval.v_string = NULL;
15289 else
15290 {
15291 p = vim_strsave(p);
15292 rettv->vval.v_string = p;
15293 if (p != NULL)
15294 shorten_dir(p);
15295 }
15296}
15297
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015298#ifdef FEAT_PERL
15299/*
15300 * "perleval()" function
15301 */
15302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015303f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015304{
15305 char_u *str;
15306 char_u buf[NUMBUFLEN];
15307
15308 str = get_tv_string_buf(&argvars[0], buf);
15309 do_perleval(str, rettv);
15310}
15311#endif
15312
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015313#ifdef FEAT_FLOAT
15314/*
15315 * "pow()" function
15316 */
15317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015318f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015319{
15320 float_T fx, fy;
15321
15322 rettv->v_type = VAR_FLOAT;
15323 if (get_float_arg(argvars, &fx) == OK
15324 && get_float_arg(&argvars[1], &fy) == OK)
15325 rettv->vval.v_float = pow(fx, fy);
15326 else
15327 rettv->vval.v_float = 0.0;
15328}
15329#endif
15330
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015331/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 * "prevnonblank()" function
15333 */
15334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015335f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015336{
15337 linenr_T lnum;
15338
15339 lnum = get_tv_lnum(argvars);
15340 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15341 lnum = 0;
15342 else
15343 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15344 --lnum;
15345 rettv->vval.v_number = lnum;
15346}
15347
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015348/* This dummy va_list is here because:
15349 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15350 * - locally in the function results in a "used before set" warning
15351 * - using va_start() to initialize it gives "function with fixed args" error */
15352static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015353
Bram Moolenaar8c711452005-01-14 21:53:12 +000015354/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015355 * "printf()" function
15356 */
15357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015358f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015359{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015360 char_u buf[NUMBUFLEN];
15361 int len;
15362 char_u *s;
15363 int saved_did_emsg = did_emsg;
15364 char *fmt;
15365
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015366 rettv->v_type = VAR_STRING;
15367 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015368
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015369 /* Get the required length, allocate the buffer and do it for real. */
15370 did_emsg = FALSE;
15371 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15372 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15373 if (!did_emsg)
15374 {
15375 s = alloc(len + 1);
15376 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015377 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015378 rettv->vval.v_string = s;
15379 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015380 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015381 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015382 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015383}
15384
15385/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015386 * "pumvisible()" function
15387 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015389f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015390{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015391#ifdef FEAT_INS_EXPAND
15392 if (pum_visible())
15393 rettv->vval.v_number = 1;
15394#endif
15395}
15396
Bram Moolenaardb913952012-06-29 12:54:53 +020015397#ifdef FEAT_PYTHON3
15398/*
15399 * "py3eval()" function
15400 */
15401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015402f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015403{
15404 char_u *str;
15405 char_u buf[NUMBUFLEN];
15406
15407 str = get_tv_string_buf(&argvars[0], buf);
15408 do_py3eval(str, rettv);
15409}
15410#endif
15411
15412#ifdef FEAT_PYTHON
15413/*
15414 * "pyeval()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015418{
15419 char_u *str;
15420 char_u buf[NUMBUFLEN];
15421
15422 str = get_tv_string_buf(&argvars[0], buf);
15423 do_pyeval(str, rettv);
15424}
15425#endif
15426
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015428 * "range()" function
15429 */
15430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015431f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015432{
15433 long start;
15434 long end;
15435 long stride = 1;
15436 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015439 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015440 if (argvars[1].v_type == VAR_UNKNOWN)
15441 {
15442 end = start - 1;
15443 start = 0;
15444 }
15445 else
15446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015449 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015450 }
15451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015452 if (error)
15453 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015454 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015455 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015456 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015457 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015458 else
15459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015460 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015461 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015462 if (list_append_number(rettv->vval.v_list,
15463 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015464 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015465 }
15466}
15467
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468/*
15469 * "readfile()" function
15470 */
15471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015472f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015473{
15474 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015475 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015476 char_u *fname;
15477 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015478 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15479 int io_size = sizeof(buf);
15480 int readlen; /* size of last fread() */
15481 char_u *prev = NULL; /* previously read bytes, if any */
15482 long prevlen = 0; /* length of data in prev */
15483 long prevsize = 0; /* size of prev buffer */
15484 long maxline = MAXLNUM;
15485 long cnt = 0;
15486 char_u *p; /* position in buf */
15487 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015489 if (argvars[1].v_type != VAR_UNKNOWN)
15490 {
15491 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15492 binary = TRUE;
15493 if (argvars[2].v_type != VAR_UNKNOWN)
15494 maxline = get_tv_number(&argvars[2]);
15495 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015496
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015497 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015498 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015499
15500 /* Always open the file in binary mode, library functions have a mind of
15501 * their own about CR-LF conversion. */
15502 fname = get_tv_string(&argvars[0]);
15503 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15504 {
15505 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15506 return;
15507 }
15508
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015509 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015510 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015512
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015513 /* This for loop processes what was read, but is also entered at end
15514 * of file so that either:
15515 * - an incomplete line gets written
15516 * - a "binary" file gets an empty line at the end if it ends in a
15517 * newline. */
15518 for (p = buf, start = buf;
15519 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15520 ++p)
15521 {
15522 if (*p == '\n' || readlen <= 0)
15523 {
15524 listitem_T *li;
15525 char_u *s = NULL;
15526 long_u len = p - start;
15527
15528 /* Finished a line. Remove CRs before NL. */
15529 if (readlen > 0 && !binary)
15530 {
15531 while (len > 0 && start[len - 1] == '\r')
15532 --len;
15533 /* removal may cross back to the "prev" string */
15534 if (len == 0)
15535 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15536 --prevlen;
15537 }
15538 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015539 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015540 else
15541 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015542 /* Change "prev" buffer to be the right size. This way
15543 * the bytes are only copied once, and very long lines are
15544 * allocated only once. */
15545 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015547 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015549 prev = NULL; /* the list will own the string */
15550 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551 }
15552 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015553 if (s == NULL)
15554 {
15555 do_outofmem_msg((long_u) prevlen + len + 1);
15556 failed = TRUE;
15557 break;
15558 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015559
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015560 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 {
15562 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015563 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015564 break;
15565 }
15566 li->li_tv.v_type = VAR_STRING;
15567 li->li_tv.v_lock = 0;
15568 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015569 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015570
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015571 start = p + 1; /* step over newline */
15572 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573 break;
15574 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015575 else if (*p == NUL)
15576 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015577#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015578 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15579 * when finding the BF and check the previous two bytes. */
15580 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015581 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015582 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15583 * + 1, these may be in the "prev" string. */
15584 char_u back1 = p >= buf + 1 ? p[-1]
15585 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15586 char_u back2 = p >= buf + 2 ? p[-2]
15587 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15588 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015589
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015590 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015591 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015592 char_u *dest = p - 2;
15593
15594 /* Usually a BOM is at the beginning of a file, and so at
15595 * the beginning of a line; then we can just step over it.
15596 */
15597 if (start == dest)
15598 start = p + 1;
15599 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015600 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015601 /* have to shuffle buf to close gap */
15602 int adjust_prevlen = 0;
15603
15604 if (dest < buf)
15605 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015606 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015607 dest = buf;
15608 }
15609 if (readlen > p - buf + 1)
15610 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15611 readlen -= 3 - adjust_prevlen;
15612 prevlen -= adjust_prevlen;
15613 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015614 }
15615 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015616 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015617#endif
15618 } /* for */
15619
15620 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15621 break;
15622 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015623 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015624 /* There's part of a line in buf, store it in "prev". */
15625 if (p - start + prevlen >= prevsize)
15626 {
15627 /* need bigger "prev" buffer */
15628 char_u *newprev;
15629
15630 /* A common use case is ordinary text files and "prev" gets a
15631 * fragment of a line, so the first allocation is made
15632 * small, to avoid repeatedly 'allocing' large and
15633 * 'reallocing' small. */
15634 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015635 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015636 else
15637 {
15638 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015639 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015640 prevsize = grow50pc > growmin ? grow50pc : growmin;
15641 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015642 newprev = prev == NULL ? alloc(prevsize)
15643 : vim_realloc(prev, prevsize);
15644 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015645 {
15646 do_outofmem_msg((long_u)prevsize);
15647 failed = TRUE;
15648 break;
15649 }
15650 prev = newprev;
15651 }
15652 /* Add the line part to end of "prev". */
15653 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015654 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015655 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015656 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015658 /*
15659 * For a negative line count use only the lines at the end of the file,
15660 * free the rest.
15661 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015662 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015663 while (cnt > -maxline)
15664 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015665 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015666 --cnt;
15667 }
15668
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015669 if (failed)
15670 {
15671 list_free(rettv->vval.v_list, TRUE);
15672 /* readfile doc says an empty list is returned on error */
15673 rettv->vval.v_list = list_alloc();
15674 }
15675
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015676 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015677 fclose(fd);
15678}
15679
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015680#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015681static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015682
15683/*
15684 * Convert a List to proftime_T.
15685 * Return FAIL when there is something wrong.
15686 */
15687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015689{
15690 long n1, n2;
15691 int error = FALSE;
15692
15693 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15694 || arg->vval.v_list->lv_len != 2)
15695 return FAIL;
15696 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15697 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15698# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015699 tm->HighPart = n1;
15700 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015701# else
15702 tm->tv_sec = n1;
15703 tm->tv_usec = n2;
15704# endif
15705 return error ? FAIL : OK;
15706}
15707#endif /* FEAT_RELTIME */
15708
15709/*
15710 * "reltime()" function
15711 */
15712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015714{
15715#ifdef FEAT_RELTIME
15716 proftime_T res;
15717 proftime_T start;
15718
15719 if (argvars[0].v_type == VAR_UNKNOWN)
15720 {
15721 /* No arguments: get current time. */
15722 profile_start(&res);
15723 }
15724 else if (argvars[1].v_type == VAR_UNKNOWN)
15725 {
15726 if (list2proftime(&argvars[0], &res) == FAIL)
15727 return;
15728 profile_end(&res);
15729 }
15730 else
15731 {
15732 /* Two arguments: compute the difference. */
15733 if (list2proftime(&argvars[0], &start) == FAIL
15734 || list2proftime(&argvars[1], &res) == FAIL)
15735 return;
15736 profile_sub(&res, &start);
15737 }
15738
15739 if (rettv_list_alloc(rettv) == OK)
15740 {
15741 long n1, n2;
15742
15743# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015744 n1 = res.HighPart;
15745 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015746# else
15747 n1 = res.tv_sec;
15748 n2 = res.tv_usec;
15749# endif
15750 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15751 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15752 }
15753#endif
15754}
15755
15756/*
15757 * "reltimestr()" function
15758 */
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015761{
15762#ifdef FEAT_RELTIME
15763 proftime_T tm;
15764#endif
15765
15766 rettv->v_type = VAR_STRING;
15767 rettv->vval.v_string = NULL;
15768#ifdef FEAT_RELTIME
15769 if (list2proftime(&argvars[0], &tm) == OK)
15770 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15771#endif
15772}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015773
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015775static void make_connection(void);
15776static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777
15778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015779make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780{
15781 if (X_DISPLAY == NULL
15782# ifdef FEAT_GUI
15783 && !gui.in_use
15784# endif
15785 )
15786 {
15787 x_force_connect = TRUE;
15788 setup_term_clip();
15789 x_force_connect = FALSE;
15790 }
15791}
15792
15793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015794check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795{
15796 make_connection();
15797 if (X_DISPLAY == NULL)
15798 {
15799 EMSG(_("E240: No connection to Vim server"));
15800 return FAIL;
15801 }
15802 return OK;
15803}
15804#endif
15805
15806#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015807static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015808
15809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015810remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811{
15812 char_u *server_name;
15813 char_u *keys;
15814 char_u *r = NULL;
15815 char_u buf[NUMBUFLEN];
15816# ifdef WIN32
15817 HWND w;
15818# else
15819 Window w;
15820# endif
15821
15822 if (check_restricted() || check_secure())
15823 return;
15824
15825# ifdef FEAT_X11
15826 if (check_connection() == FAIL)
15827 return;
15828# endif
15829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015830 server_name = get_tv_string_chk(&argvars[0]);
15831 if (server_name == NULL)
15832 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833 keys = get_tv_string_buf(&argvars[1], buf);
15834# ifdef WIN32
15835 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15836# else
15837 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15838 < 0)
15839# endif
15840 {
15841 if (r != NULL)
15842 EMSG(r); /* sending worked but evaluation failed */
15843 else
15844 EMSG2(_("E241: Unable to send to %s"), server_name);
15845 return;
15846 }
15847
15848 rettv->vval.v_string = r;
15849
15850 if (argvars[2].v_type != VAR_UNKNOWN)
15851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015852 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015853 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015854 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015856 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 v.di_tv.v_type = VAR_STRING;
15858 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 idvar = get_tv_string_chk(&argvars[2]);
15860 if (idvar != NULL)
15861 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 }
15864}
15865#endif
15866
15867/*
15868 * "remote_expr()" function
15869 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015871f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015872{
15873 rettv->v_type = VAR_STRING;
15874 rettv->vval.v_string = NULL;
15875#ifdef FEAT_CLIENTSERVER
15876 remote_common(argvars, rettv, TRUE);
15877#endif
15878}
15879
15880/*
15881 * "remote_foreground()" function
15882 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015884f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015886#ifdef FEAT_CLIENTSERVER
15887# ifdef WIN32
15888 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015889 {
15890 char_u *server_name = get_tv_string_chk(&argvars[0]);
15891
15892 if (server_name != NULL)
15893 serverForeground(server_name);
15894 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895# else
15896 /* Send a foreground() expression to the server. */
15897 argvars[1].v_type = VAR_STRING;
15898 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15899 argvars[2].v_type = VAR_UNKNOWN;
15900 remote_common(argvars, rettv, TRUE);
15901 vim_free(argvars[1].vval.v_string);
15902# endif
15903#endif
15904}
15905
Bram Moolenaar0d660222005-01-07 21:51:51 +000015906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015907f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908{
15909#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911 char_u *s = NULL;
15912# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015913 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916
15917 if (check_restricted() || check_secure())
15918 {
15919 rettv->vval.v_number = -1;
15920 return;
15921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 serverid = get_tv_string_chk(&argvars[0]);
15923 if (serverid == NULL)
15924 {
15925 rettv->vval.v_number = -1;
15926 return; /* type error; errmsg already given */
15927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015928# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015929 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015930 if (n == 0)
15931 rettv->vval.v_number = -1;
15932 else
15933 {
15934 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15935 rettv->vval.v_number = (s != NULL);
15936 }
15937# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015938 if (check_connection() == FAIL)
15939 return;
15940
15941 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015942 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943# endif
15944
15945 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015947 char_u *retvar;
15948
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 v.di_tv.v_type = VAR_STRING;
15950 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015951 retvar = get_tv_string_chk(&argvars[1]);
15952 if (retvar != NULL)
15953 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015954 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 }
15956#else
15957 rettv->vval.v_number = -1;
15958#endif
15959}
15960
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015963{
15964 char_u *r = NULL;
15965
15966#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015967 char_u *serverid = get_tv_string_chk(&argvars[0]);
15968
15969 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015970 {
15971# ifdef WIN32
15972 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015973 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015975 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976 if (n != 0)
15977 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15978 if (r == NULL)
15979# else
15980 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015982# endif
15983 EMSG(_("E277: Unable to read a server reply"));
15984 }
15985#endif
15986 rettv->v_type = VAR_STRING;
15987 rettv->vval.v_string = r;
15988}
15989
15990/*
15991 * "remote_send()" function
15992 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015994f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015995{
15996 rettv->v_type = VAR_STRING;
15997 rettv->vval.v_string = NULL;
15998#ifdef FEAT_CLIENTSERVER
15999 remote_common(argvars, rettv, FALSE);
16000#endif
16001}
16002
16003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016004 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016005 */
16006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016007f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008{
Bram Moolenaar33570922005-01-25 22:26:29 +000016009 list_T *l;
16010 listitem_T *item, *item2;
16011 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016012 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016013 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016014 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 dict_T *d;
16016 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016017 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016018
Bram Moolenaar8c711452005-01-14 21:53:12 +000016019 if (argvars[0].v_type == VAR_DICT)
16020 {
16021 if (argvars[2].v_type != VAR_UNKNOWN)
16022 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016023 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016024 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 key = get_tv_string_chk(&argvars[1]);
16027 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 di = dict_find(d, key, -1);
16030 if (di == NULL)
16031 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016032 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16033 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 {
16035 *rettv = di->di_tv;
16036 init_tv(&di->di_tv);
16037 dictitem_remove(d, di);
16038 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016040 }
16041 }
16042 else if (argvars[0].v_type != VAR_LIST)
16043 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016044 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016045 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 int error = FALSE;
16048
16049 idx = get_tv_number_chk(&argvars[1], &error);
16050 if (error)
16051 ; /* type error: do nothing, errmsg already given */
16052 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016053 EMSGN(_(e_listidx), idx);
16054 else
16055 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016056 if (argvars[2].v_type == VAR_UNKNOWN)
16057 {
16058 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016059 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016060 *rettv = item->li_tv;
16061 vim_free(item);
16062 }
16063 else
16064 {
16065 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 end = get_tv_number_chk(&argvars[2], &error);
16067 if (error)
16068 ; /* type error: do nothing */
16069 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016070 EMSGN(_(e_listidx), end);
16071 else
16072 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016073 int cnt = 0;
16074
16075 for (li = item; li != NULL; li = li->li_next)
16076 {
16077 ++cnt;
16078 if (li == item2)
16079 break;
16080 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016081 if (li == NULL) /* didn't find "item2" after "item" */
16082 EMSG(_(e_invrange));
16083 else
16084 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016085 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016086 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016087 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016088 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016089 l->lv_first = item;
16090 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016091 item->li_prev = NULL;
16092 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016093 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016094 }
16095 }
16096 }
16097 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016098 }
16099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100}
16101
16102/*
16103 * "rename({from}, {to})" function
16104 */
16105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016106f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107{
16108 char_u buf[NUMBUFLEN];
16109
16110 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016113 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16114 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115}
16116
16117/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016118 * "repeat()" function
16119 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016121f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016122{
16123 char_u *p;
16124 int n;
16125 int slen;
16126 int len;
16127 char_u *r;
16128 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016129
16130 n = get_tv_number(&argvars[1]);
16131 if (argvars[0].v_type == VAR_LIST)
16132 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016133 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016134 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016135 if (list_extend(rettv->vval.v_list,
16136 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016137 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016138 }
16139 else
16140 {
16141 p = get_tv_string(&argvars[0]);
16142 rettv->v_type = VAR_STRING;
16143 rettv->vval.v_string = NULL;
16144
16145 slen = (int)STRLEN(p);
16146 len = slen * n;
16147 if (len <= 0)
16148 return;
16149
16150 r = alloc(len + 1);
16151 if (r != NULL)
16152 {
16153 for (i = 0; i < n; i++)
16154 mch_memmove(r + i * slen, p, (size_t)slen);
16155 r[len] = NUL;
16156 }
16157
16158 rettv->vval.v_string = r;
16159 }
16160}
16161
16162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 * "resolve()" function
16164 */
16165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016166f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167{
16168 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016169#ifdef HAVE_READLINK
16170 char_u *buf = NULL;
16171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016173 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174#ifdef FEAT_SHORTCUT
16175 {
16176 char_u *v = NULL;
16177
16178 v = mch_resolve_shortcut(p);
16179 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 }
16184#else
16185# ifdef HAVE_READLINK
16186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 char_u *cpy;
16188 int len;
16189 char_u *remain = NULL;
16190 char_u *q;
16191 int is_relative_to_current = FALSE;
16192 int has_trailing_pathsep = FALSE;
16193 int limit = 100;
16194
16195 p = vim_strsave(p);
16196
16197 if (p[0] == '.' && (vim_ispathsep(p[1])
16198 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16199 is_relative_to_current = TRUE;
16200
16201 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016202 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016205 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
16208 q = getnextcomp(p);
16209 if (*q != NUL)
16210 {
16211 /* Separate the first path component in "p", and keep the
16212 * remainder (beginning with the path separator). */
16213 remain = vim_strsave(q - 1);
16214 q[-1] = NUL;
16215 }
16216
Bram Moolenaard9462e32011-04-11 21:35:11 +020016217 buf = alloc(MAXPATHL + 1);
16218 if (buf == NULL)
16219 goto fail;
16220
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 for (;;)
16222 {
16223 for (;;)
16224 {
16225 len = readlink((char *)p, (char *)buf, MAXPATHL);
16226 if (len <= 0)
16227 break;
16228 buf[len] = NUL;
16229
16230 if (limit-- == 0)
16231 {
16232 vim_free(p);
16233 vim_free(remain);
16234 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 goto fail;
16237 }
16238
16239 /* Ensure that the result will have a trailing path separator
16240 * if the argument has one. */
16241 if (remain == NULL && has_trailing_pathsep)
16242 add_pathsep(buf);
16243
16244 /* Separate the first path component in the link value and
16245 * concatenate the remainders. */
16246 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16247 if (*q != NUL)
16248 {
16249 if (remain == NULL)
16250 remain = vim_strsave(q - 1);
16251 else
16252 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016253 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 if (cpy != NULL)
16255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 vim_free(remain);
16257 remain = cpy;
16258 }
16259 }
16260 q[-1] = NUL;
16261 }
16262
16263 q = gettail(p);
16264 if (q > p && *q == NUL)
16265 {
16266 /* Ignore trailing path separator. */
16267 q[-1] = NUL;
16268 q = gettail(p);
16269 }
16270 if (q > p && !mch_isFullName(buf))
16271 {
16272 /* symlink is relative to directory of argument */
16273 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16274 if (cpy != NULL)
16275 {
16276 STRCPY(cpy, p);
16277 STRCPY(gettail(cpy), buf);
16278 vim_free(p);
16279 p = cpy;
16280 }
16281 }
16282 else
16283 {
16284 vim_free(p);
16285 p = vim_strsave(buf);
16286 }
16287 }
16288
16289 if (remain == NULL)
16290 break;
16291
16292 /* Append the first path component of "remain" to "p". */
16293 q = getnextcomp(remain + 1);
16294 len = q - remain - (*q != NUL);
16295 cpy = vim_strnsave(p, STRLEN(p) + len);
16296 if (cpy != NULL)
16297 {
16298 STRNCAT(cpy, remain, len);
16299 vim_free(p);
16300 p = cpy;
16301 }
16302 /* Shorten "remain". */
16303 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016304 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 else
16306 {
16307 vim_free(remain);
16308 remain = NULL;
16309 }
16310 }
16311
16312 /* If the result is a relative path name, make it explicitly relative to
16313 * the current directory if and only if the argument had this form. */
16314 if (!vim_ispathsep(*p))
16315 {
16316 if (is_relative_to_current
16317 && *p != NUL
16318 && !(p[0] == '.'
16319 && (p[1] == NUL
16320 || vim_ispathsep(p[1])
16321 || (p[1] == '.'
16322 && (p[2] == NUL
16323 || vim_ispathsep(p[2]))))))
16324 {
16325 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016326 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 if (cpy != NULL)
16328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 vim_free(p);
16330 p = cpy;
16331 }
16332 }
16333 else if (!is_relative_to_current)
16334 {
16335 /* Strip leading "./". */
16336 q = p;
16337 while (q[0] == '.' && vim_ispathsep(q[1]))
16338 q += 2;
16339 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016340 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 }
16342 }
16343
16344 /* Ensure that the result will have no trailing path separator
16345 * if the argument had none. But keep "/" or "//". */
16346 if (!has_trailing_pathsep)
16347 {
16348 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016349 if (after_pathsep(p, q))
16350 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 }
16352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016353 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 }
16355# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357# endif
16358#endif
16359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016360 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361
16362#ifdef HAVE_READLINK
16363fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016364 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367}
16368
16369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 */
16372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016373f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374{
Bram Moolenaar33570922005-01-25 22:26:29 +000016375 list_T *l;
16376 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378 if (argvars[0].v_type != VAR_LIST)
16379 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016380 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016381 && !tv_check_lock(l->lv_lock,
16382 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 {
16384 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016385 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016386 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387 while (li != NULL)
16388 {
16389 ni = li->li_prev;
16390 list_append(l, li);
16391 li = ni;
16392 }
16393 rettv->vval.v_list = l;
16394 rettv->v_type = VAR_LIST;
16395 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016396 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398}
16399
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016400#define SP_NOMOVE 0x01 /* don't move cursor */
16401#define SP_REPEAT 0x02 /* repeat to find outer pair */
16402#define SP_RETCOUNT 0x04 /* return matchcount */
16403#define SP_SETPCMARK 0x08 /* set previous context mark */
16404#define SP_START 0x10 /* accept match at start position */
16405#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16406#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016407#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016408
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016409static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410
16411/*
16412 * Get flags for a search function.
16413 * Possibly sets "p_ws".
16414 * Returns BACKWARD, FORWARD or zero (for an error).
16415 */
16416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016417get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016418{
16419 int dir = FORWARD;
16420 char_u *flags;
16421 char_u nbuf[NUMBUFLEN];
16422 int mask;
16423
16424 if (varp->v_type != VAR_UNKNOWN)
16425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016426 flags = get_tv_string_buf_chk(varp, nbuf);
16427 if (flags == NULL)
16428 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429 while (*flags != NUL)
16430 {
16431 switch (*flags)
16432 {
16433 case 'b': dir = BACKWARD; break;
16434 case 'w': p_ws = TRUE; break;
16435 case 'W': p_ws = FALSE; break;
16436 default: mask = 0;
16437 if (flagsp != NULL)
16438 switch (*flags)
16439 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016440 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016441 case 'e': mask = SP_END; break;
16442 case 'm': mask = SP_RETCOUNT; break;
16443 case 'n': mask = SP_NOMOVE; break;
16444 case 'p': mask = SP_SUBPAT; break;
16445 case 'r': mask = SP_REPEAT; break;
16446 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016447 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 }
16449 if (mask == 0)
16450 {
16451 EMSG2(_(e_invarg2), flags);
16452 dir = 0;
16453 }
16454 else
16455 *flagsp |= mask;
16456 }
16457 if (dir == 0)
16458 break;
16459 ++flags;
16460 }
16461 }
16462 return dir;
16463}
16464
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016466 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016471 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472 char_u *pat;
16473 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016474 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 int save_p_ws = p_ws;
16476 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016477 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016478 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016479 proftime_T tm;
16480#ifdef FEAT_RELTIME
16481 long time_limit = 0;
16482#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016483 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016484 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016486 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016487 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016488 if (dir == 0)
16489 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016490 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 if (flags & SP_START)
16492 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016493 if (flags & SP_END)
16494 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016495 if (flags & SP_COLUMN)
16496 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016497
Bram Moolenaar76929292008-01-06 19:07:36 +000016498 /* Optional arguments: line number to stop searching and timeout. */
16499 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016500 {
16501 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16502 if (lnum_stop < 0)
16503 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016504#ifdef FEAT_RELTIME
16505 if (argvars[3].v_type != VAR_UNKNOWN)
16506 {
16507 time_limit = get_tv_number_chk(&argvars[3], NULL);
16508 if (time_limit < 0)
16509 goto theend;
16510 }
16511#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016512 }
16513
Bram Moolenaar76929292008-01-06 19:07:36 +000016514#ifdef FEAT_RELTIME
16515 /* Set the time limit, if there is one. */
16516 profile_setlimit(time_limit, &tm);
16517#endif
16518
Bram Moolenaar231334e2005-07-25 20:46:57 +000016519 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016520 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016521 * Check to make sure only those flags are set.
16522 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16523 * flags cannot be set. Check for that condition also.
16524 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016525 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016526 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016528 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016529 goto theend;
16530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016532 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016533 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016534 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016535 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016537 if (flags & SP_SUBPAT)
16538 retval = subpatnum;
16539 else
16540 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016541 if (flags & SP_SETPCMARK)
16542 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016544 if (match_pos != NULL)
16545 {
16546 /* Store the match cursor position */
16547 match_pos->lnum = pos.lnum;
16548 match_pos->col = pos.col + 1;
16549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 /* "/$" will put the cursor after the end of the line, may need to
16551 * correct that here */
16552 check_cursor();
16553 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016554
16555 /* If 'n' flag is used: restore cursor position. */
16556 if (flags & SP_NOMOVE)
16557 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016558 else
16559 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016560theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016562
16563 return retval;
16564}
16565
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016566#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016567
16568/*
16569 * round() is not in C90, use ceil() or floor() instead.
16570 */
16571 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016572vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016573{
16574 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16575}
16576
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016577/*
16578 * "round({float})" function
16579 */
16580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016581f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016582{
16583 float_T f;
16584
16585 rettv->v_type = VAR_FLOAT;
16586 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016587 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016588 else
16589 rettv->vval.v_float = 0.0;
16590}
16591#endif
16592
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016593/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016594 * "screenattr()" function
16595 */
16596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016598{
16599 int row;
16600 int col;
16601 int c;
16602
16603 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16604 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16605 if (row < 0 || row >= screen_Rows
16606 || col < 0 || col >= screen_Columns)
16607 c = -1;
16608 else
16609 c = ScreenAttrs[LineOffset[row] + col];
16610 rettv->vval.v_number = c;
16611}
16612
16613/*
16614 * "screenchar()" function
16615 */
16616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016617f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016618{
16619 int row;
16620 int col;
16621 int off;
16622 int c;
16623
16624 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16625 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16626 if (row < 0 || row >= screen_Rows
16627 || col < 0 || col >= screen_Columns)
16628 c = -1;
16629 else
16630 {
16631 off = LineOffset[row] + col;
16632#ifdef FEAT_MBYTE
16633 if (enc_utf8 && ScreenLinesUC[off] != 0)
16634 c = ScreenLinesUC[off];
16635 else
16636#endif
16637 c = ScreenLines[off];
16638 }
16639 rettv->vval.v_number = c;
16640}
16641
16642/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016643 * "screencol()" function
16644 *
16645 * First column is 1 to be consistent with virtcol().
16646 */
16647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016648f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016649{
16650 rettv->vval.v_number = screen_screencol() + 1;
16651}
16652
16653/*
16654 * "screenrow()" function
16655 */
16656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016657f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016658{
16659 rettv->vval.v_number = screen_screenrow() + 1;
16660}
16661
16662/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663 * "search()" function
16664 */
16665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016666f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016667{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016668 int flags = 0;
16669
16670 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671}
16672
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016674 * "searchdecl()" function
16675 */
16676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016677f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016678{
16679 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016680 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016681 int error = FALSE;
16682 char_u *name;
16683
16684 rettv->vval.v_number = 1; /* default: FAIL */
16685
16686 name = get_tv_string_chk(&argvars[0]);
16687 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016688 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016689 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016690 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16691 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16692 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016693 if (!error && name != NULL)
16694 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016695 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016696}
16697
16698/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016699 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016702searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703{
16704 char_u *spat, *mpat, *epat;
16705 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 int dir;
16708 int flags = 0;
16709 char_u nbuf1[NUMBUFLEN];
16710 char_u nbuf2[NUMBUFLEN];
16711 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016712 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016713 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016714 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 spat = get_tv_string_chk(&argvars[0]);
16718 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16719 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16720 if (spat == NULL || mpat == NULL || epat == NULL)
16721 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 /* Handle the optional fourth argument: flags */
16724 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016725 if (dir == 0)
16726 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016727
16728 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016729 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16730 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016731 if ((flags & (SP_END | SP_SUBPAT)) != 0
16732 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016733 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016734 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016735 goto theend;
16736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016738 /* Using 'r' implies 'W', otherwise it doesn't work. */
16739 if (flags & SP_REPEAT)
16740 p_ws = FALSE;
16741
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016742 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016743 if (argvars[3].v_type == VAR_UNKNOWN
16744 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 skip = (char_u *)"";
16746 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016748 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 if (argvars[5].v_type != VAR_UNKNOWN)
16750 {
16751 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16752 if (lnum_stop < 0)
16753 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016754#ifdef FEAT_RELTIME
16755 if (argvars[6].v_type != VAR_UNKNOWN)
16756 {
16757 time_limit = get_tv_number_chk(&argvars[6], NULL);
16758 if (time_limit < 0)
16759 goto theend;
16760 }
16761#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016762 }
16763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016764 if (skip == NULL)
16765 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016767 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016768 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016769
16770theend:
16771 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016772
16773 return retval;
16774}
16775
16776/*
16777 * "searchpair()" function
16778 */
16779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016780f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016781{
16782 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16783}
16784
16785/*
16786 * "searchpairpos()" function
16787 */
16788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016789f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016790{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016791 pos_T match_pos;
16792 int lnum = 0;
16793 int col = 0;
16794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016795 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016796 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016797
16798 if (searchpair_cmn(argvars, &match_pos) > 0)
16799 {
16800 lnum = match_pos.lnum;
16801 col = match_pos.col;
16802 }
16803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016804 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16805 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016806}
16807
16808/*
16809 * Search for a start/middle/end thing.
16810 * Used by searchpair(), see its documentation for the details.
16811 * Returns 0 or -1 for no match,
16812 */
16813 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016814do_searchpair(
16815 char_u *spat, /* start pattern */
16816 char_u *mpat, /* middle pattern */
16817 char_u *epat, /* end pattern */
16818 int dir, /* BACKWARD or FORWARD */
16819 char_u *skip, /* skip expression */
16820 int flags, /* SP_SETPCMARK and other SP_ values */
16821 pos_T *match_pos,
16822 linenr_T lnum_stop, /* stop at this line if not zero */
16823 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016824{
16825 char_u *save_cpo;
16826 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16827 long retval = 0;
16828 pos_T pos;
16829 pos_T firstpos;
16830 pos_T foundpos;
16831 pos_T save_cursor;
16832 pos_T save_pos;
16833 int n;
16834 int r;
16835 int nest = 1;
16836 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016837 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016838 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016839
16840 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16841 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016842 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016843
Bram Moolenaar76929292008-01-06 19:07:36 +000016844#ifdef FEAT_RELTIME
16845 /* Set the time limit, if there is one. */
16846 profile_setlimit(time_limit, &tm);
16847#endif
16848
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016849 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16850 * start/middle/end (pat3, for the top pair). */
16851 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16852 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16853 if (pat2 == NULL || pat3 == NULL)
16854 goto theend;
16855 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16856 if (*mpat == NUL)
16857 STRCPY(pat3, pat2);
16858 else
16859 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16860 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016861 if (flags & SP_START)
16862 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016863
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 save_cursor = curwin->w_cursor;
16865 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016866 clearpos(&firstpos);
16867 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 pat = pat3;
16869 for (;;)
16870 {
16871 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016872 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16874 /* didn't find it or found the first match again: FAIL */
16875 break;
16876
16877 if (firstpos.lnum == 0)
16878 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016879 if (equalpos(pos, foundpos))
16880 {
16881 /* Found the same position again. Can happen with a pattern that
16882 * has "\zs" at the end and searching backwards. Advance one
16883 * character and try again. */
16884 if (dir == BACKWARD)
16885 decl(&pos);
16886 else
16887 incl(&pos);
16888 }
16889 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016891 /* clear the start flag to avoid getting stuck here */
16892 options &= ~SEARCH_START;
16893
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 /* If the skip pattern matches, ignore this match. */
16895 if (*skip != NUL)
16896 {
16897 save_pos = curwin->w_cursor;
16898 curwin->w_cursor = pos;
16899 r = eval_to_bool(skip, &err, NULL, FALSE);
16900 curwin->w_cursor = save_pos;
16901 if (err)
16902 {
16903 /* Evaluating {skip} caused an error, break here. */
16904 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016905 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 break;
16907 }
16908 if (r)
16909 continue;
16910 }
16911
16912 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16913 {
16914 /* Found end when searching backwards or start when searching
16915 * forward: nested pair. */
16916 ++nest;
16917 pat = pat2; /* nested, don't search for middle */
16918 }
16919 else
16920 {
16921 /* Found end when searching forward or start when searching
16922 * backward: end of (nested) pair; or found middle in outer pair. */
16923 if (--nest == 1)
16924 pat = pat3; /* outer level, search for middle */
16925 }
16926
16927 if (nest == 0)
16928 {
16929 /* Found the match: return matchcount or line number. */
16930 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016931 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016933 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016934 if (flags & SP_SETPCMARK)
16935 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 curwin->w_cursor = pos;
16937 if (!(flags & SP_REPEAT))
16938 break;
16939 nest = 1; /* search for next unmatched */
16940 }
16941 }
16942
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016943 if (match_pos != NULL)
16944 {
16945 /* Store the match cursor position */
16946 match_pos->lnum = curwin->w_cursor.lnum;
16947 match_pos->col = curwin->w_cursor.col + 1;
16948 }
16949
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016951 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 curwin->w_cursor = save_cursor;
16953
16954theend:
16955 vim_free(pat2);
16956 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016957 if (p_cpo == empty_option)
16958 p_cpo = save_cpo;
16959 else
16960 /* Darn, evaluating the {skip} expression changed the value. */
16961 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016962
16963 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964}
16965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016966/*
16967 * "searchpos()" function
16968 */
16969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016970f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016971{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016972 pos_T match_pos;
16973 int lnum = 0;
16974 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016975 int n;
16976 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016977
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016978 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016979 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016980
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016981 n = search_cmn(argvars, &match_pos, &flags);
16982 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016983 {
16984 lnum = match_pos.lnum;
16985 col = match_pos.col;
16986 }
16987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016988 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16989 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016990 if (flags & SP_SUBPAT)
16991 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016992}
16993
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016995f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997#ifdef FEAT_CLIENTSERVER
16998 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 char_u *server = get_tv_string_chk(&argvars[0]);
17000 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 if (server == NULL || reply == NULL)
17004 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 if (check_restricted() || check_secure())
17006 return;
17007# ifdef FEAT_X11
17008 if (check_connection() == FAIL)
17009 return;
17010# endif
17011
17012 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 EMSG(_("E258: Unable to send to client"));
17015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 rettv->vval.v_number = 0;
17018#else
17019 rettv->vval.v_number = -1;
17020#endif
17021}
17022
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017024f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025{
17026 char_u *r = NULL;
17027
17028#ifdef FEAT_CLIENTSERVER
17029# ifdef WIN32
17030 r = serverGetVimNames();
17031# else
17032 make_connection();
17033 if (X_DISPLAY != NULL)
17034 r = serverGetVimNames(X_DISPLAY);
17035# endif
17036#endif
17037 rettv->v_type = VAR_STRING;
17038 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
17041/*
17042 * "setbufvar()" function
17043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017045f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046{
17047 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017050 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 char_u nbuf[NUMBUFLEN];
17052
17053 if (check_restricted() || check_secure())
17054 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017055 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17056 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017057 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 varp = &argvars[2];
17059
17060 if (buf != NULL && varname != NULL && varp != NULL)
17061 {
17062 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064
17065 if (*varname == '&')
17066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 long numval;
17068 char_u *strval;
17069 int error = FALSE;
17070
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017072 numval = get_tv_number_chk(varp, &error);
17073 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017074 if (!error && strval != NULL)
17075 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 }
17077 else
17078 {
17079 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17080 if (bufvarname != NULL)
17081 {
17082 STRCPY(bufvarname, "b:");
17083 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017084 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 vim_free(bufvarname);
17086 }
17087 }
17088
17089 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092}
17093
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017095f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017096{
17097 dict_T *d;
17098 dictitem_T *di;
17099 char_u *csearch;
17100
17101 if (argvars[0].v_type != VAR_DICT)
17102 {
17103 EMSG(_(e_dictreq));
17104 return;
17105 }
17106
17107 if ((d = argvars[0].vval.v_dict) != NULL)
17108 {
17109 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17110 if (csearch != NULL)
17111 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017112#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017113 if (enc_utf8)
17114 {
17115 int pcc[MAX_MCO];
17116 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017117
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017118 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17119 }
17120 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017121#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017122 set_last_csearch(PTR2CHAR(csearch),
17123 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017124 }
17125
17126 di = dict_find(d, (char_u *)"forward", -1);
17127 if (di != NULL)
17128 set_csearch_direction(get_tv_number(&di->di_tv)
17129 ? FORWARD : BACKWARD);
17130
17131 di = dict_find(d, (char_u *)"until", -1);
17132 if (di != NULL)
17133 set_csearch_until(!!get_tv_number(&di->di_tv));
17134 }
17135}
17136
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137/*
17138 * "setcmdpos()" function
17139 */
17140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017141f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143 int pos = (int)get_tv_number(&argvars[0]) - 1;
17144
17145 if (pos >= 0)
17146 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147}
17148
17149/*
17150 * "setline()" function
17151 */
17152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017153f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154{
17155 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017156 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017157 list_T *l = NULL;
17158 listitem_T *li = NULL;
17159 long added = 0;
17160 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017162 lnum = get_tv_lnum(&argvars[0]);
17163 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017165 l = argvars[1].vval.v_list;
17166 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017170
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017171 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017172 for (;;)
17173 {
17174 if (l != NULL)
17175 {
17176 /* list argument, get next string */
17177 if (li == NULL)
17178 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017180 li = li->li_next;
17181 }
17182
17183 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017184 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017185 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017186
17187 /* When coming here from Insert mode, sync undo, so that this can be
17188 * undone separately from what was previously inserted. */
17189 if (u_sync_once == 2)
17190 {
17191 u_sync_once = 1; /* notify that u_sync() was called */
17192 u_sync(TRUE);
17193 }
17194
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017195 if (lnum <= curbuf->b_ml.ml_line_count)
17196 {
17197 /* existing line, replace it */
17198 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17199 {
17200 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017201 if (lnum == curwin->w_cursor.lnum)
17202 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017203 rettv->vval.v_number = 0; /* OK */
17204 }
17205 }
17206 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17207 {
17208 /* lnum is one past the last line, append the line */
17209 ++added;
17210 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17211 rettv->vval.v_number = 0; /* OK */
17212 }
17213
17214 if (l == NULL) /* only one string argument */
17215 break;
17216 ++lnum;
17217 }
17218
17219 if (added > 0)
17220 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017223static 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 +000017224
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017226 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017227 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017229set_qf_ll_list(
17230 win_T *wp UNUSED,
17231 typval_T *list_arg UNUSED,
17232 typval_T *action_arg UNUSED,
17233 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017234{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017235#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017236 char_u *act;
17237 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017238#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017239
Bram Moolenaar2641f772005-03-25 21:58:17 +000017240 rettv->vval.v_number = -1;
17241
17242#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017243 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017244 EMSG(_(e_listreq));
17245 else
17246 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017247 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017248
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017249 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017250 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017251 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 if (act == NULL)
17253 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017254 if (*act == 'a' || *act == 'r')
17255 action = *act;
17256 }
17257
Bram Moolenaar81484f42012-12-05 15:16:47 +010017258 if (l != NULL && set_errorlist(wp, l, action,
17259 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017260 rettv->vval.v_number = 0;
17261 }
17262#endif
17263}
17264
17265/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017266 * "setloclist()" function
17267 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017270{
17271 win_T *win;
17272
17273 rettv->vval.v_number = -1;
17274
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017275 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017276 if (win != NULL)
17277 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17278}
17279
17280/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017281 * "setmatches()" function
17282 */
17283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017284f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017285{
17286#ifdef FEAT_SEARCH_EXTRA
17287 list_T *l;
17288 listitem_T *li;
17289 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017290 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017291
17292 rettv->vval.v_number = -1;
17293 if (argvars[0].v_type != VAR_LIST)
17294 {
17295 EMSG(_(e_listreq));
17296 return;
17297 }
17298 if ((l = argvars[0].vval.v_list) != NULL)
17299 {
17300
17301 /* To some extent make sure that we are dealing with a list from
17302 * "getmatches()". */
17303 li = l->lv_first;
17304 while (li != NULL)
17305 {
17306 if (li->li_tv.v_type != VAR_DICT
17307 || (d = li->li_tv.vval.v_dict) == NULL)
17308 {
17309 EMSG(_(e_invarg));
17310 return;
17311 }
17312 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017313 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17314 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017315 && dict_find(d, (char_u *)"priority", -1) != NULL
17316 && dict_find(d, (char_u *)"id", -1) != NULL))
17317 {
17318 EMSG(_(e_invarg));
17319 return;
17320 }
17321 li = li->li_next;
17322 }
17323
17324 clear_matches(curwin);
17325 li = l->lv_first;
17326 while (li != NULL)
17327 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017328 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017329 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017330 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017331 char_u *group;
17332 int priority;
17333 int id;
17334 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017335
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017336 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017337 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17338 {
17339 if (s == NULL)
17340 {
17341 s = list_alloc();
17342 if (s == NULL)
17343 return;
17344 }
17345
17346 /* match from matchaddpos() */
17347 for (i = 1; i < 9; i++)
17348 {
17349 sprintf((char *)buf, (char *)"pos%d", i);
17350 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17351 {
17352 if (di->di_tv.v_type != VAR_LIST)
17353 return;
17354
17355 list_append_tv(s, &di->di_tv);
17356 s->lv_refcount++;
17357 }
17358 else
17359 break;
17360 }
17361 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017362
17363 group = get_dict_string(d, (char_u *)"group", FALSE);
17364 priority = (int)get_dict_number(d, (char_u *)"priority");
17365 id = (int)get_dict_number(d, (char_u *)"id");
17366 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17367 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17368 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017369 if (i == 0)
17370 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017371 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017372 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017373 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017374 }
17375 else
17376 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017377 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017378 list_unref(s);
17379 s = NULL;
17380 }
17381
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017382 li = li->li_next;
17383 }
17384 rettv->vval.v_number = 0;
17385 }
17386#endif
17387}
17388
17389/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017390 * "setpos()" function
17391 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017393f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017394{
17395 pos_T pos;
17396 int fnum;
17397 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017398 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017399
Bram Moolenaar08250432008-02-13 11:42:46 +000017400 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017401 name = get_tv_string_chk(argvars);
17402 if (name != NULL)
17403 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017404 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017405 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017406 if (--pos.col < 0)
17407 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017408 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017409 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017410 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017411 if (fnum == curbuf->b_fnum)
17412 {
17413 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017414 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017415 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017416 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017417 curwin->w_set_curswant = FALSE;
17418 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017419 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017420 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017421 }
17422 else
17423 EMSG(_(e_invarg));
17424 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017425 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17426 {
17427 /* set mark */
17428 if (setmark_pos(name[1], &pos, fnum) == OK)
17429 rettv->vval.v_number = 0;
17430 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017431 else
17432 EMSG(_(e_invarg));
17433 }
17434 }
17435}
17436
17437/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017438 * "setqflist()" function
17439 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017441f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017442{
17443 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17444}
17445
17446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 * "setreg()" function
17448 */
17449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017450f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451{
17452 int regname;
17453 char_u *strregname;
17454 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017455 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 int append;
17457 char_u yank_type;
17458 long block_len;
17459
17460 block_len = -1;
17461 yank_type = MAUTO;
17462 append = FALSE;
17463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017464 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017467 if (strregname == NULL)
17468 return; /* type error; errmsg already given */
17469 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 if (regname == 0 || regname == '@')
17471 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017473 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017475 stropt = get_tv_string_chk(&argvars[2]);
17476 if (stropt == NULL)
17477 return; /* type error */
17478 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 switch (*stropt)
17480 {
17481 case 'a': case 'A': /* append */
17482 append = TRUE;
17483 break;
17484 case 'v': case 'c': /* character-wise selection */
17485 yank_type = MCHAR;
17486 break;
17487 case 'V': case 'l': /* line-wise selection */
17488 yank_type = MLINE;
17489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 case 'b': case Ctrl_V: /* block-wise selection */
17491 yank_type = MBLOCK;
17492 if (VIM_ISDIGIT(stropt[1]))
17493 {
17494 ++stropt;
17495 block_len = getdigits(&stropt) - 1;
17496 --stropt;
17497 }
17498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 }
17500 }
17501
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017502 if (argvars[1].v_type == VAR_LIST)
17503 {
17504 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017505 char_u **allocval;
17506 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017507 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017508 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017509 int len = argvars[1].vval.v_list->lv_len;
17510 listitem_T *li;
17511
Bram Moolenaar7d647822014-04-05 21:28:56 +020017512 /* First half: use for pointers to result lines; second half: use for
17513 * pointers to allocated copies. */
17514 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017515 if (lstval == NULL)
17516 return;
17517 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017518 allocval = lstval + len + 2;
17519 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017520
17521 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17522 li = li->li_next)
17523 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017524 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017525 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017526 goto free_lstval;
17527 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017528 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017529 /* Need to make a copy, next get_tv_string_buf_chk() will
17530 * overwrite the string. */
17531 strval = vim_strsave(buf);
17532 if (strval == NULL)
17533 goto free_lstval;
17534 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017535 }
17536 *curval++ = strval;
17537 }
17538 *curval++ = NULL;
17539
17540 write_reg_contents_lst(regname, lstval, -1,
17541 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017542free_lstval:
17543 while (curallocval > allocval)
17544 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017545 vim_free(lstval);
17546 }
17547 else
17548 {
17549 strval = get_tv_string_chk(&argvars[1]);
17550 if (strval == NULL)
17551 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017552 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556}
17557
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017558/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017559 * "settabvar()" function
17560 */
17561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017562f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017563{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017564#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017565 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017566 tabpage_T *tp;
17567#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017568 char_u *varname, *tabvarname;
17569 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017570
17571 rettv->vval.v_number = 0;
17572
17573 if (check_restricted() || check_secure())
17574 return;
17575
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017576#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017577 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017578#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017579 varname = get_tv_string_chk(&argvars[1]);
17580 varp = &argvars[2];
17581
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017582 if (varname != NULL && varp != NULL
17583#ifdef FEAT_WINDOWS
17584 && tp != NULL
17585#endif
17586 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017587 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017588#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017589 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017590 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017591#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017592
17593 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17594 if (tabvarname != NULL)
17595 {
17596 STRCPY(tabvarname, "t:");
17597 STRCPY(tabvarname + 2, varname);
17598 set_var(tabvarname, varp, TRUE);
17599 vim_free(tabvarname);
17600 }
17601
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017602#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017603 /* Restore current tabpage */
17604 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017605 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017606#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017607 }
17608}
17609
17610/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017611 * "settabwinvar()" function
17612 */
17613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017614f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017615{
17616 setwinvar(argvars, rettv, 1);
17617}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
17619/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017620 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017623f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017625 setwinvar(argvars, rettv, 0);
17626}
17627
17628/*
17629 * "setwinvar()" and "settabwinvar()" functions
17630 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017631
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017633setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017634{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 win_T *win;
17636#ifdef FEAT_WINDOWS
17637 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017638 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017639 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640#endif
17641 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017644 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645
17646 if (check_restricted() || check_secure())
17647 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017648
17649#ifdef FEAT_WINDOWS
17650 if (off == 1)
17651 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17652 else
17653 tp = curtab;
17654#endif
17655 win = find_win_by_nr(&argvars[off], tp);
17656 varname = get_tv_string_chk(&argvars[off + 1]);
17657 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658
17659 if (win != NULL && varname != NULL && varp != NULL)
17660 {
17661#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017662 need_switch_win = !(tp == curtab && win == curwin);
17663 if (!need_switch_win
17664 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017667 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017669 long numval;
17670 char_u *strval;
17671 int error = FALSE;
17672
17673 ++varname;
17674 numval = get_tv_number_chk(varp, &error);
17675 strval = get_tv_string_buf_chk(varp, nbuf);
17676 if (!error && strval != NULL)
17677 set_option_value(varname, numval, strval, OPT_LOCAL);
17678 }
17679 else
17680 {
17681 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17682 if (winvarname != NULL)
17683 {
17684 STRCPY(winvarname, "w:");
17685 STRCPY(winvarname + 2, varname);
17686 set_var(winvarname, varp, TRUE);
17687 vim_free(winvarname);
17688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 }
17690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017692 if (need_switch_win)
17693 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694#endif
17695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696}
17697
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017698#ifdef FEAT_CRYPT
17699/*
17700 * "sha256({string})" function
17701 */
17702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017703f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017704{
17705 char_u *p;
17706
17707 p = get_tv_string(&argvars[0]);
17708 rettv->vval.v_string = vim_strsave(
17709 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17710 rettv->v_type = VAR_STRING;
17711}
17712#endif /* FEAT_CRYPT */
17713
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017715 * "shellescape({string})" function
17716 */
17717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017718f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017719{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017720 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017721 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017722 rettv->v_type = VAR_STRING;
17723}
17724
17725/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017726 * shiftwidth() function
17727 */
17728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017729f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017730{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017731 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017732}
17733
17734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017735 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 */
17737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017738f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017740 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742 p = get_tv_string(&argvars[0]);
17743 rettv->vval.v_string = vim_strsave(p);
17744 simplify_filename(rettv->vval.v_string); /* simplify in place */
17745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746}
17747
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017748#ifdef FEAT_FLOAT
17749/*
17750 * "sin()" function
17751 */
17752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017753f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017754{
17755 float_T f;
17756
17757 rettv->v_type = VAR_FLOAT;
17758 if (get_float_arg(argvars, &f) == OK)
17759 rettv->vval.v_float = sin(f);
17760 else
17761 rettv->vval.v_float = 0.0;
17762}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017763
17764/*
17765 * "sinh()" function
17766 */
17767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017768f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017769{
17770 float_T f;
17771
17772 rettv->v_type = VAR_FLOAT;
17773 if (get_float_arg(argvars, &f) == OK)
17774 rettv->vval.v_float = sinh(f);
17775 else
17776 rettv->vval.v_float = 0.0;
17777}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017778#endif
17779
Bram Moolenaar0d660222005-01-07 21:51:51 +000017780static int
17781#ifdef __BORLANDC__
17782 _RTLENTRYF
17783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017784 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017785static int
17786#ifdef __BORLANDC__
17787 _RTLENTRYF
17788#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017789 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017791/* struct used in the array that's given to qsort() */
17792typedef struct
17793{
17794 listitem_T *item;
17795 int idx;
17796} sortItem_T;
17797
Bram Moolenaar0d660222005-01-07 21:51:51 +000017798static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017799static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017800static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017801#ifdef FEAT_FLOAT
17802static int item_compare_float;
17803#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017805static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017806static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017807static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017808static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017809#define ITEM_COMPARE_FAIL 999
17810
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017812 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017814 static int
17815#ifdef __BORLANDC__
17816_RTLENTRYF
17817#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017818item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017820 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017821 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017822 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017823 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017824 int res;
17825 char_u numbuf1[NUMBUFLEN];
17826 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017828 si1 = (sortItem_T *)s1;
17829 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017830 tv1 = &si1->item->li_tv;
17831 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017832
17833 if (item_compare_numbers)
17834 {
17835 long v1 = get_tv_number(tv1);
17836 long v2 = get_tv_number(tv2);
17837
17838 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17839 }
17840
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017841#ifdef FEAT_FLOAT
17842 if (item_compare_float)
17843 {
17844 float_T v1 = get_tv_float(tv1);
17845 float_T v2 = get_tv_float(tv2);
17846
17847 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17848 }
17849#endif
17850
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017851 /* tv2string() puts quotes around a string and allocates memory. Don't do
17852 * that for string variables. Use a single quote when comparing with a
17853 * non-string to do what the docs promise. */
17854 if (tv1->v_type == VAR_STRING)
17855 {
17856 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17857 p1 = (char_u *)"'";
17858 else
17859 p1 = tv1->vval.v_string;
17860 }
17861 else
17862 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17863 if (tv2->v_type == VAR_STRING)
17864 {
17865 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17866 p2 = (char_u *)"'";
17867 else
17868 p2 = tv2->vval.v_string;
17869 }
17870 else
17871 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017872 if (p1 == NULL)
17873 p1 = (char_u *)"";
17874 if (p2 == NULL)
17875 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017876 if (!item_compare_numeric)
17877 {
17878 if (item_compare_ic)
17879 res = STRICMP(p1, p2);
17880 else
17881 res = STRCMP(p1, p2);
17882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017884 {
17885 double n1, n2;
17886 n1 = strtod((char *)p1, (char **)&p1);
17887 n2 = strtod((char *)p2, (char **)&p2);
17888 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17889 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017890
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017891 /* When the result would be zero, compare the item indexes. Makes the
17892 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017893 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017894 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017895
Bram Moolenaar0d660222005-01-07 21:51:51 +000017896 vim_free(tofree1);
17897 vim_free(tofree2);
17898 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899}
17900
17901 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017902#ifdef __BORLANDC__
17903_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017905item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017907 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017910 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017913 /* shortcut after failure in previous call; compare all items equal */
17914 if (item_compare_func_err)
17915 return 0;
17916
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017917 si1 = (sortItem_T *)s1;
17918 si2 = (sortItem_T *)s2;
17919
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017920 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017921 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017922 copy_tv(&si1->item->li_tv, &argv[0]);
17923 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017924
17925 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017926 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017927 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17928 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 clear_tv(&argv[0]);
17930 clear_tv(&argv[1]);
17931
17932 if (res == FAIL)
17933 res = ITEM_COMPARE_FAIL;
17934 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017935 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17936 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017937 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017939
17940 /* When the result would be zero, compare the pointers themselves. Makes
17941 * the sort stable. */
17942 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017943 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017944
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 return res;
17946}
17947
17948/*
17949 * "sort({list})" function
17950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017952do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953{
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 list_T *l;
17955 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017956 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017957 long len;
17958 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017961 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 else
17963 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017965 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017966 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17967 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017968 return;
17969 rettv->vval.v_list = l;
17970 rettv->v_type = VAR_LIST;
17971 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973 len = list_len(l);
17974 if (len <= 1)
17975 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
Bram Moolenaar0d660222005-01-07 21:51:51 +000017977 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017978 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017979 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017980#ifdef FEAT_FLOAT
17981 item_compare_float = FALSE;
17982#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017983 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017984 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017985 if (argvars[1].v_type != VAR_UNKNOWN)
17986 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017987 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017988 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017989 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990 else
17991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017992 int error = FALSE;
17993
17994 i = get_tv_number_chk(&argvars[1], &error);
17995 if (error)
17996 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 if (i == 1)
17998 item_compare_ic = TRUE;
17999 else
18000 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018001 if (item_compare_func != NULL)
18002 {
18003 if (STRCMP(item_compare_func, "n") == 0)
18004 {
18005 item_compare_func = NULL;
18006 item_compare_numeric = TRUE;
18007 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018008 else if (STRCMP(item_compare_func, "N") == 0)
18009 {
18010 item_compare_func = NULL;
18011 item_compare_numbers = TRUE;
18012 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018013#ifdef FEAT_FLOAT
18014 else if (STRCMP(item_compare_func, "f") == 0)
18015 {
18016 item_compare_func = NULL;
18017 item_compare_float = TRUE;
18018 }
18019#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018020 else if (STRCMP(item_compare_func, "i") == 0)
18021 {
18022 item_compare_func = NULL;
18023 item_compare_ic = TRUE;
18024 }
18025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018026 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018027
18028 if (argvars[2].v_type != VAR_UNKNOWN)
18029 {
18030 /* optional third argument: {dict} */
18031 if (argvars[2].v_type != VAR_DICT)
18032 {
18033 EMSG(_(e_dictreq));
18034 return;
18035 }
18036 item_compare_selfdict = argvars[2].vval.v_dict;
18037 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018041 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 if (ptrs == NULL)
18043 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044
Bram Moolenaar327aa022014-03-25 18:24:23 +010018045 i = 0;
18046 if (sort)
18047 {
18048 /* sort(): ptrs will be the list to sort */
18049 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018050 {
18051 ptrs[i].item = li;
18052 ptrs[i].idx = i;
18053 ++i;
18054 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018055
18056 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018057 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018058 /* test the compare function */
18059 if (item_compare_func != NULL
18060 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018062 EMSG(_("E702: Sort compare function failed"));
18063 else
18064 {
18065 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018066 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018067 item_compare_func == NULL ? item_compare : item_compare2);
18068
18069 if (!item_compare_func_err)
18070 {
18071 /* Clear the List and append the items in sorted order. */
18072 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18073 l->lv_len = 0;
18074 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018075 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 }
18077 }
18078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018081 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018082
18083 /* f_uniq(): ptrs will be a stack of items to remove */
18084 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018085 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018086 item_compare_func_ptr = item_compare_func
18087 ? item_compare2 : item_compare;
18088
18089 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18090 li = li->li_next)
18091 {
18092 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18093 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018094 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018095 if (item_compare_func_err)
18096 {
18097 EMSG(_("E882: Uniq compare function failed"));
18098 break;
18099 }
18100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 if (!item_compare_func_err)
18103 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018104 while (--i >= 0)
18105 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018106 li = ptrs[i].item->li_next;
18107 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018108 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018109 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018110 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018111 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018112 list_fix_watch(l, li);
18113 listitem_free(li);
18114 l->lv_len--;
18115 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 }
18118
18119 vim_free(ptrs);
18120 }
18121}
18122
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018123/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018124 * "sort({list})" function
18125 */
18126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018127f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018128{
18129 do_sort_uniq(argvars, rettv, TRUE);
18130}
18131
18132/*
18133 * "uniq({list})" function
18134 */
18135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018136f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018137{
18138 do_sort_uniq(argvars, rettv, FALSE);
18139}
18140
18141/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018142 * "soundfold({word})" function
18143 */
18144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018145f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018146{
18147 char_u *s;
18148
18149 rettv->v_type = VAR_STRING;
18150 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018151#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018152 rettv->vval.v_string = eval_soundfold(s);
18153#else
18154 rettv->vval.v_string = vim_strsave(s);
18155#endif
18156}
18157
18158/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018159 * "spellbadword()" function
18160 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018162f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018163{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018164 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018165 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018166 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018167
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018168 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018169 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018170
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018171#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018172 if (argvars[0].v_type == VAR_UNKNOWN)
18173 {
18174 /* Find the start and length of the badly spelled word. */
18175 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18176 if (len != 0)
18177 word = ml_get_cursor();
18178 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018179 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018180 {
18181 char_u *str = get_tv_string_chk(&argvars[0]);
18182 int capcol = -1;
18183
18184 if (str != NULL)
18185 {
18186 /* Check the argument for spelling. */
18187 while (*str != NUL)
18188 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018189 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018190 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018191 {
18192 word = str;
18193 break;
18194 }
18195 str += len;
18196 }
18197 }
18198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018199#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018201 list_append_string(rettv->vval.v_list, word, len);
18202 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018203 attr == HLF_SPB ? "bad" :
18204 attr == HLF_SPR ? "rare" :
18205 attr == HLF_SPL ? "local" :
18206 attr == HLF_SPC ? "caps" :
18207 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018208}
18209
18210/*
18211 * "spellsuggest()" function
18212 */
18213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018214f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018215{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018216#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018217 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018218 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018219 int maxcount;
18220 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018221 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018222 listitem_T *li;
18223 int need_capital = FALSE;
18224#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018226 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018227 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018228
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018229#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018230 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018231 {
18232 str = get_tv_string(&argvars[0]);
18233 if (argvars[1].v_type != VAR_UNKNOWN)
18234 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018235 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018236 if (maxcount <= 0)
18237 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018238 if (argvars[2].v_type != VAR_UNKNOWN)
18239 {
18240 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18241 if (typeerr)
18242 return;
18243 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018244 }
18245 else
18246 maxcount = 25;
18247
Bram Moolenaar4770d092006-01-12 23:22:24 +000018248 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018249
18250 for (i = 0; i < ga.ga_len; ++i)
18251 {
18252 str = ((char_u **)ga.ga_data)[i];
18253
18254 li = listitem_alloc();
18255 if (li == NULL)
18256 vim_free(str);
18257 else
18258 {
18259 li->li_tv.v_type = VAR_STRING;
18260 li->li_tv.v_lock = 0;
18261 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018262 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018263 }
18264 }
18265 ga_clear(&ga);
18266 }
18267#endif
18268}
18269
Bram Moolenaar0d660222005-01-07 21:51:51 +000018270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018271f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018272{
18273 char_u *str;
18274 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018275 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 regmatch_T regmatch;
18277 char_u patbuf[NUMBUFLEN];
18278 char_u *save_cpo;
18279 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018281 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018282 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283
18284 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18285 save_cpo = p_cpo;
18286 p_cpo = (char_u *)"";
18287
18288 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018289 if (argvars[1].v_type != VAR_UNKNOWN)
18290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18292 if (pat == NULL)
18293 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018295 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018296 }
18297 if (pat == NULL || *pat == NUL)
18298 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018302 if (typeerr)
18303 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304
Bram Moolenaar0d660222005-01-07 21:51:51 +000018305 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18306 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018309 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018310 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018311 if (*str == NUL)
18312 match = FALSE; /* empty item at the end */
18313 else
18314 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315 if (match)
18316 end = regmatch.startp[0];
18317 else
18318 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018319 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18320 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018322 if (list_append_string(rettv->vval.v_list, str,
18323 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018324 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 }
18326 if (!match)
18327 break;
18328 /* Advance to just after the match. */
18329 if (regmatch.endp[0] > str)
18330 col = 0;
18331 else
18332 {
18333 /* Don't get stuck at the same match. */
18334#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018335 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336#else
18337 col = 1;
18338#endif
18339 }
18340 str = regmatch.endp[0];
18341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342
Bram Moolenaar473de612013-06-08 18:19:48 +020018343 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345
Bram Moolenaar0d660222005-01-07 21:51:51 +000018346 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347}
18348
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018349#ifdef FEAT_FLOAT
18350/*
18351 * "sqrt()" function
18352 */
18353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018354f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018355{
18356 float_T f;
18357
18358 rettv->v_type = VAR_FLOAT;
18359 if (get_float_arg(argvars, &f) == OK)
18360 rettv->vval.v_float = sqrt(f);
18361 else
18362 rettv->vval.v_float = 0.0;
18363}
18364
18365/*
18366 * "str2float()" function
18367 */
18368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018369f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018370{
18371 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18372
18373 if (*p == '+')
18374 p = skipwhite(p + 1);
18375 (void)string2float(p, &rettv->vval.v_float);
18376 rettv->v_type = VAR_FLOAT;
18377}
18378#endif
18379
Bram Moolenaar2c932302006-03-18 21:42:09 +000018380/*
18381 * "str2nr()" function
18382 */
18383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018384f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018385{
18386 int base = 10;
18387 char_u *p;
18388 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018389 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018390
18391 if (argvars[1].v_type != VAR_UNKNOWN)
18392 {
18393 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018394 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018395 {
18396 EMSG(_(e_invarg));
18397 return;
18398 }
18399 }
18400
18401 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018402 if (*p == '+')
18403 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018404 switch (base)
18405 {
18406 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18407 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18408 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18409 default: what = 0;
18410 }
18411 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018412 rettv->vval.v_number = n;
18413}
18414
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415#ifdef HAVE_STRFTIME
18416/*
18417 * "strftime({format}[, {time}])" function
18418 */
18419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018420f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421{
18422 char_u result_buf[256];
18423 struct tm *curtime;
18424 time_t seconds;
18425 char_u *p;
18426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018430 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 seconds = time(NULL);
18432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 curtime = localtime(&seconds);
18435 /* MSVC returns NULL for an invalid value of seconds. */
18436 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 else
18439 {
18440# ifdef FEAT_MBYTE
18441 vimconv_T conv;
18442 char_u *enc;
18443
18444 conv.vc_type = CONV_NONE;
18445 enc = enc_locale();
18446 convert_setup(&conv, p_enc, enc);
18447 if (conv.vc_type != CONV_NONE)
18448 p = string_convert(&conv, p, NULL);
18449# endif
18450 if (p != NULL)
18451 (void)strftime((char *)result_buf, sizeof(result_buf),
18452 (char *)p, curtime);
18453 else
18454 result_buf[0] = NUL;
18455
18456# ifdef FEAT_MBYTE
18457 if (conv.vc_type != CONV_NONE)
18458 vim_free(p);
18459 convert_setup(&conv, enc, p_enc);
18460 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 else
18463# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465
18466# ifdef FEAT_MBYTE
18467 /* Release conversion descriptors */
18468 convert_setup(&conv, NULL, NULL);
18469 vim_free(enc);
18470# endif
18471 }
18472}
18473#endif
18474
18475/*
18476 * "stridx()" function
18477 */
18478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480{
18481 char_u buf[NUMBUFLEN];
18482 char_u *needle;
18483 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018484 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018486 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018488 needle = get_tv_string_chk(&argvars[1]);
18489 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018490 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018491 if (needle == NULL || haystack == NULL)
18492 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493
Bram Moolenaar33570922005-01-25 22:26:29 +000018494 if (argvars[2].v_type != VAR_UNKNOWN)
18495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018496 int error = FALSE;
18497
18498 start_idx = get_tv_number_chk(&argvars[2], &error);
18499 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018500 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018501 if (start_idx >= 0)
18502 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018503 }
18504
18505 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18506 if (pos != NULL)
18507 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508}
18509
18510/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018511 * "string()" function
18512 */
18513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018514f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018515{
18516 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018517 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018519 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018520 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018521 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018522 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018523 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524}
18525
18526/*
18527 * "strlen()" function
18528 */
18529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018530f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018532 rettv->vval.v_number = (varnumber_T)(STRLEN(
18533 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534}
18535
18536/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018537 * "strchars()" function
18538 */
18539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018540f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018541{
18542 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018543 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018544#ifdef FEAT_MBYTE
18545 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018546 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018547#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018548
18549 if (argvars[1].v_type != VAR_UNKNOWN)
18550 skipcc = get_tv_number_chk(&argvars[1], NULL);
18551 if (skipcc < 0 || skipcc > 1)
18552 EMSG(_(e_invarg));
18553 else
18554 {
18555#ifdef FEAT_MBYTE
18556 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18557 while (*s != NUL)
18558 {
18559 func_mb_ptr2char_adv(&s);
18560 ++len;
18561 }
18562 rettv->vval.v_number = len;
18563#else
18564 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18565#endif
18566 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018567}
18568
18569/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018570 * "strdisplaywidth()" function
18571 */
18572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018573f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018574{
18575 char_u *s = get_tv_string(&argvars[0]);
18576 int col = 0;
18577
18578 if (argvars[1].v_type != VAR_UNKNOWN)
18579 col = get_tv_number(&argvars[1]);
18580
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018581 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018582}
18583
18584/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018585 * "strwidth()" function
18586 */
18587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018588f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018589{
18590 char_u *s = get_tv_string(&argvars[0]);
18591
18592 rettv->vval.v_number = (varnumber_T)(
18593#ifdef FEAT_MBYTE
18594 mb_string2cells(s, -1)
18595#else
18596 STRLEN(s)
18597#endif
18598 );
18599}
18600
18601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 * "strpart()" function
18603 */
18604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018605f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606{
18607 char_u *p;
18608 int n;
18609 int len;
18610 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018611 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 slen = (int)STRLEN(p);
18615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018616 n = get_tv_number_chk(&argvars[1], &error);
18617 if (error)
18618 len = 0;
18619 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 else
18622 len = slen - n; /* default len: all bytes that are available. */
18623
18624 /*
18625 * Only return the overlap between the specified part and the actual
18626 * string.
18627 */
18628 if (n < 0)
18629 {
18630 len += n;
18631 n = 0;
18632 }
18633 else if (n > slen)
18634 n = slen;
18635 if (len < 0)
18636 len = 0;
18637 else if (n + len > slen)
18638 len = slen - n;
18639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 rettv->v_type = VAR_STRING;
18641 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642}
18643
18644/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 * "strridx()" function
18646 */
18647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018648f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018649{
18650 char_u buf[NUMBUFLEN];
18651 char_u *needle;
18652 char_u *haystack;
18653 char_u *rest;
18654 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018655 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018657 needle = get_tv_string_chk(&argvars[1]);
18658 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018659
18660 rettv->vval.v_number = -1;
18661 if (needle == NULL || haystack == NULL)
18662 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018663
18664 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018665 if (argvars[2].v_type != VAR_UNKNOWN)
18666 {
18667 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018668 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018669 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018670 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018671 }
18672 else
18673 end_idx = haystack_len;
18674
Bram Moolenaar0d660222005-01-07 21:51:51 +000018675 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018676 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018678 lastmatch = haystack + end_idx;
18679 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018681 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 for (rest = haystack; *rest != '\0'; ++rest)
18683 {
18684 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018685 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 break;
18687 lastmatch = rest;
18688 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018690
18691 if (lastmatch == NULL)
18692 rettv->vval.v_number = -1;
18693 else
18694 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18695}
18696
18697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 * "strtrans()" function
18699 */
18700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018701f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 rettv->v_type = VAR_STRING;
18704 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705}
18706
18707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708 * "submatch()" function
18709 */
18710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018711f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712{
Bram Moolenaar41571762014-04-02 19:00:58 +020018713 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018714 int no;
18715 int retList = 0;
18716
18717 no = (int)get_tv_number_chk(&argvars[0], &error);
18718 if (error)
18719 return;
18720 error = FALSE;
18721 if (argvars[1].v_type != VAR_UNKNOWN)
18722 retList = get_tv_number_chk(&argvars[1], &error);
18723 if (error)
18724 return;
18725
18726 if (retList == 0)
18727 {
18728 rettv->v_type = VAR_STRING;
18729 rettv->vval.v_string = reg_submatch(no);
18730 }
18731 else
18732 {
18733 rettv->v_type = VAR_LIST;
18734 rettv->vval.v_list = reg_submatch_list(no);
18735 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018736}
18737
18738/*
18739 * "substitute()" function
18740 */
18741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018742f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018743{
18744 char_u patbuf[NUMBUFLEN];
18745 char_u subbuf[NUMBUFLEN];
18746 char_u flagsbuf[NUMBUFLEN];
18747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018748 char_u *str = get_tv_string_chk(&argvars[0]);
18749 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18750 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18751 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18752
Bram Moolenaar0d660222005-01-07 21:51:51 +000018753 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018754 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18755 rettv->vval.v_string = NULL;
18756 else
18757 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758}
18759
18760/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018761 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018764f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765{
18766 int id = 0;
18767#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018768 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 long col;
18770 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018771 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018773 lnum = get_tv_lnum(argvars); /* -1 on type error */
18774 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18775 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018777 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018778 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018779 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780#endif
18781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783}
18784
18785/*
18786 * "synIDattr(id, what [, mode])" function
18787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018789f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790{
18791 char_u *p = NULL;
18792#ifdef FEAT_SYN_HL
18793 int id;
18794 char_u *what;
18795 char_u *mode;
18796 char_u modebuf[NUMBUFLEN];
18797 int modec;
18798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799 id = get_tv_number(&argvars[0]);
18800 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018805 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 modec = 0; /* replace invalid with current */
18807 }
18808 else
18809 {
18810#ifdef FEAT_GUI
18811 if (gui.in_use)
18812 modec = 'g';
18813 else
18814#endif
18815 if (t_colors > 1)
18816 modec = 'c';
18817 else
18818 modec = 't';
18819 }
18820
18821
18822 switch (TOLOWER_ASC(what[0]))
18823 {
18824 case 'b':
18825 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18826 p = highlight_color(id, what, modec);
18827 else /* bold */
18828 p = highlight_has_attr(id, HL_BOLD, modec);
18829 break;
18830
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018831 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 p = highlight_color(id, what, modec);
18833 break;
18834
18835 case 'i':
18836 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18837 p = highlight_has_attr(id, HL_INVERSE, modec);
18838 else /* italic */
18839 p = highlight_has_attr(id, HL_ITALIC, modec);
18840 break;
18841
18842 case 'n': /* name */
18843 p = get_highlight_name(NULL, id - 1);
18844 break;
18845
18846 case 'r': /* reverse */
18847 p = highlight_has_attr(id, HL_INVERSE, modec);
18848 break;
18849
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018850 case 's':
18851 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18852 p = highlight_color(id, what, modec);
18853 else /* standout */
18854 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 break;
18856
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018857 case 'u':
18858 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18859 /* underline */
18860 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18861 else
18862 /* undercurl */
18863 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 break;
18865 }
18866
18867 if (p != NULL)
18868 p = vim_strsave(p);
18869#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018870 rettv->v_type = VAR_STRING;
18871 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872}
18873
18874/*
18875 * "synIDtrans(id)" function
18876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018878f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879{
18880 int id;
18881
18882#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884
18885 if (id > 0)
18886 id = syn_get_final_id(id);
18887 else
18888#endif
18889 id = 0;
18890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892}
18893
18894/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018895 * "synconcealed(lnum, col)" function
18896 */
18897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018898f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018899{
18900#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18901 long lnum;
18902 long col;
18903 int syntax_flags = 0;
18904 int cchar;
18905 int matchid = 0;
18906 char_u str[NUMBUFLEN];
18907#endif
18908
18909 rettv->v_type = VAR_LIST;
18910 rettv->vval.v_list = NULL;
18911
18912#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18913 lnum = get_tv_lnum(argvars); /* -1 on type error */
18914 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18915
18916 vim_memset(str, NUL, sizeof(str));
18917
18918 if (rettv_list_alloc(rettv) != FAIL)
18919 {
18920 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18921 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18922 && curwin->w_p_cole > 0)
18923 {
18924 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18925 syntax_flags = get_syntax_info(&matchid);
18926
18927 /* get the conceal character */
18928 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18929 {
18930 cchar = syn_get_sub_char();
18931 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18932 cchar = lcs_conceal;
18933 if (cchar != NUL)
18934 {
18935# ifdef FEAT_MBYTE
18936 if (has_mbyte)
18937 (*mb_char2bytes)(cchar, str);
18938 else
18939# endif
18940 str[0] = cchar;
18941 }
18942 }
18943 }
18944
18945 list_append_number(rettv->vval.v_list,
18946 (syntax_flags & HL_CONCEAL) != 0);
18947 /* -1 to auto-determine strlen */
18948 list_append_string(rettv->vval.v_list, str, -1);
18949 list_append_number(rettv->vval.v_list, matchid);
18950 }
18951#endif
18952}
18953
18954/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018955 * "synstack(lnum, col)" function
18956 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018959{
18960#ifdef FEAT_SYN_HL
18961 long lnum;
18962 long col;
18963 int i;
18964 int id;
18965#endif
18966
18967 rettv->v_type = VAR_LIST;
18968 rettv->vval.v_list = NULL;
18969
18970#ifdef FEAT_SYN_HL
18971 lnum = get_tv_lnum(argvars); /* -1 on type error */
18972 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18973
18974 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018975 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018976 && rettv_list_alloc(rettv) != FAIL)
18977 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018978 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018979 for (i = 0; ; ++i)
18980 {
18981 id = syn_get_stack_item(i);
18982 if (id < 0)
18983 break;
18984 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18985 break;
18986 }
18987 }
18988#endif
18989}
18990
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018992get_cmd_output_as_rettv(
18993 typval_T *argvars,
18994 typval_T *rettv,
18995 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018997 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018999 char_u *infile = NULL;
19000 char_u buf[NUMBUFLEN];
19001 int err = FALSE;
19002 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019003 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019004 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019006 rettv->v_type = VAR_STRING;
19007 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019008 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019009 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019010
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019011 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019012 {
19013 /*
19014 * Write the string to a temp file, to be used for input of the shell
19015 * command.
19016 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019017 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019018 {
19019 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019020 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019021 }
19022
19023 fd = mch_fopen((char *)infile, WRITEBIN);
19024 if (fd == NULL)
19025 {
19026 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019027 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019028 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019029 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019030 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019031 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19032 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019033 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019034 else
19035 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019036 size_t len;
19037
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019038 p = get_tv_string_buf_chk(&argvars[1], buf);
19039 if (p == NULL)
19040 {
19041 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019042 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019043 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019044 len = STRLEN(p);
19045 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019046 err = TRUE;
19047 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019048 if (fclose(fd) != 0)
19049 err = TRUE;
19050 if (err)
19051 {
19052 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019054 }
19055 }
19056
Bram Moolenaar52a72462014-08-29 15:53:52 +020019057 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19058 * echoes typeahead, that messes up the display. */
19059 if (!msg_silent)
19060 flags += SHELL_COOKED;
19061
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 int len;
19065 listitem_T *li;
19066 char_u *s = NULL;
19067 char_u *start;
19068 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019069 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070
Bram Moolenaar52a72462014-08-29 15:53:52 +020019071 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019072 if (res == NULL)
19073 goto errret;
19074
19075 list = list_alloc();
19076 if (list == NULL)
19077 goto errret;
19078
19079 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019081 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019082 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019084 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019085
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019086 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019087 if (s == NULL)
19088 goto errret;
19089
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019090 for (p = s; start < end; ++p, ++start)
19091 *p = *start == NUL ? NL : *start;
19092 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019093
19094 li = listitem_alloc();
19095 if (li == NULL)
19096 {
19097 vim_free(s);
19098 goto errret;
19099 }
19100 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019101 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019102 li->li_tv.vval.v_string = s;
19103 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019105
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019106 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019107 rettv->v_type = VAR_LIST;
19108 rettv->vval.v_list = list;
19109 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019111 else
19112 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019113 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019114#ifdef USE_CR
19115 /* translate <CR> into <NL> */
19116 if (res != NULL)
19117 {
19118 char_u *s;
19119
19120 for (s = res; *s; ++s)
19121 {
19122 if (*s == CAR)
19123 *s = NL;
19124 }
19125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126#else
19127# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019128 /* translate <CR><NL> into <NL> */
19129 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019131 char_u *s, *d;
19132
19133 d = res;
19134 for (s = res; *s; ++s)
19135 {
19136 if (s[0] == CAR && s[1] == NL)
19137 ++s;
19138 *d++ = *s;
19139 }
19140 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142# endif
19143#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019144 rettv->vval.v_string = res;
19145 res = NULL;
19146 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019147
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019148errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019149 if (infile != NULL)
19150 {
19151 mch_remove(infile);
19152 vim_free(infile);
19153 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019154 if (res != NULL)
19155 vim_free(res);
19156 if (list != NULL)
19157 list_free(list, TRUE);
19158}
19159
19160/*
19161 * "system()" function
19162 */
19163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019164f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019165{
19166 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19167}
19168
19169/*
19170 * "systemlist()" function
19171 */
19172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019173f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019174{
19175 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176}
19177
19178/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019179 * "tabpagebuflist()" function
19180 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019182f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019183{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019184#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019185 tabpage_T *tp;
19186 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019187
19188 if (argvars[0].v_type == VAR_UNKNOWN)
19189 wp = firstwin;
19190 else
19191 {
19192 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19193 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019194 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019195 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019196 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019197 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019198 for (; wp != NULL; wp = wp->w_next)
19199 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019200 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019201 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019202 }
19203#endif
19204}
19205
19206
19207/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019208 * "tabpagenr()" function
19209 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019211f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019212{
19213 int nr = 1;
19214#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019215 char_u *arg;
19216
19217 if (argvars[0].v_type != VAR_UNKNOWN)
19218 {
19219 arg = get_tv_string_chk(&argvars[0]);
19220 nr = 0;
19221 if (arg != NULL)
19222 {
19223 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019224 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019225 else
19226 EMSG2(_(e_invexpr2), arg);
19227 }
19228 }
19229 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019230 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019231#endif
19232 rettv->vval.v_number = nr;
19233}
19234
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019235
19236#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019237static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019238
19239/*
19240 * Common code for tabpagewinnr() and winnr().
19241 */
19242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019243get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019244{
19245 win_T *twin;
19246 int nr = 1;
19247 win_T *wp;
19248 char_u *arg;
19249
19250 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19251 if (argvar->v_type != VAR_UNKNOWN)
19252 {
19253 arg = get_tv_string_chk(argvar);
19254 if (arg == NULL)
19255 nr = 0; /* type error; errmsg already given */
19256 else if (STRCMP(arg, "$") == 0)
19257 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19258 else if (STRCMP(arg, "#") == 0)
19259 {
19260 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19261 if (twin == NULL)
19262 nr = 0;
19263 }
19264 else
19265 {
19266 EMSG2(_(e_invexpr2), arg);
19267 nr = 0;
19268 }
19269 }
19270
19271 if (nr > 0)
19272 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19273 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019274 {
19275 if (wp == NULL)
19276 {
19277 /* didn't find it in this tabpage */
19278 nr = 0;
19279 break;
19280 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019281 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019282 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019283 return nr;
19284}
19285#endif
19286
19287/*
19288 * "tabpagewinnr()" function
19289 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019291f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019292{
19293 int nr = 1;
19294#ifdef FEAT_WINDOWS
19295 tabpage_T *tp;
19296
19297 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19298 if (tp == NULL)
19299 nr = 0;
19300 else
19301 nr = get_winnr(tp, &argvars[1]);
19302#endif
19303 rettv->vval.v_number = nr;
19304}
19305
19306
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019307/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019308 * "tagfiles()" function
19309 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019311f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019312{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019313 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019314 tagname_T tn;
19315 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019317 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019318 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019319 fname = alloc(MAXPATHL);
19320 if (fname == NULL)
19321 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019322
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019323 for (first = TRUE; ; first = FALSE)
19324 if (get_tagfname(&tn, first, fname) == FAIL
19325 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019326 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019327 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019328 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019329}
19330
19331/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019332 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019333 */
19334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019335f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019336{
19337 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019338
19339 tag_pattern = get_tv_string(&argvars[0]);
19340
19341 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019342 if (*tag_pattern == NUL)
19343 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019345 if (rettv_list_alloc(rettv) == OK)
19346 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019347}
19348
19349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 * "tempname()" function
19351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019353f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354{
19355 static int x = 'A';
19356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019357 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019358 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359
19360 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19361 * names. Skip 'I' and 'O', they are used for shell redirection. */
19362 do
19363 {
19364 if (x == 'Z')
19365 x = '0';
19366 else if (x == '9')
19367 x = 'A';
19368 else
19369 {
19370#ifdef EBCDIC
19371 if (x == 'I')
19372 x = 'J';
19373 else if (x == 'R')
19374 x = 'S';
19375 else
19376#endif
19377 ++x;
19378 }
19379 } while (x == 'I' || x == 'O');
19380}
19381
19382/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019383 * "test(list)" function: Just checking the walls...
19384 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019386f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019387{
19388 /* Used for unit testing. Change the code below to your liking. */
19389#if 0
19390 listitem_T *li;
19391 list_T *l;
19392 char_u *bad, *good;
19393
19394 if (argvars[0].v_type != VAR_LIST)
19395 return;
19396 l = argvars[0].vval.v_list;
19397 if (l == NULL)
19398 return;
19399 li = l->lv_first;
19400 if (li == NULL)
19401 return;
19402 bad = get_tv_string(&li->li_tv);
19403 li = li->li_next;
19404 if (li == NULL)
19405 return;
19406 good = get_tv_string(&li->li_tv);
19407 rettv->vval.v_number = test_edit_score(bad, good);
19408#endif
19409}
19410
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019411#ifdef FEAT_FLOAT
19412/*
19413 * "tan()" function
19414 */
19415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019416f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019417{
19418 float_T f;
19419
19420 rettv->v_type = VAR_FLOAT;
19421 if (get_float_arg(argvars, &f) == OK)
19422 rettv->vval.v_float = tan(f);
19423 else
19424 rettv->vval.v_float = 0.0;
19425}
19426
19427/*
19428 * "tanh()" function
19429 */
19430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019431f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019432{
19433 float_T f;
19434
19435 rettv->v_type = VAR_FLOAT;
19436 if (get_float_arg(argvars, &f) == OK)
19437 rettv->vval.v_float = tanh(f);
19438 else
19439 rettv->vval.v_float = 0.0;
19440}
19441#endif
19442
Bram Moolenaard52d9742005-08-21 22:20:28 +000019443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 * "tolower(string)" function
19445 */
19446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019447f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448{
19449 char_u *p;
19450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019451 p = vim_strsave(get_tv_string(&argvars[0]));
19452 rettv->v_type = VAR_STRING;
19453 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
19455 if (p != NULL)
19456 while (*p != NUL)
19457 {
19458#ifdef FEAT_MBYTE
19459 int l;
19460
19461 if (enc_utf8)
19462 {
19463 int c, lc;
19464
19465 c = utf_ptr2char(p);
19466 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019467 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 /* TODO: reallocate string when byte count changes. */
19469 if (utf_char2len(lc) == l)
19470 utf_char2bytes(lc, p);
19471 p += l;
19472 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019473 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 p += l; /* skip multi-byte character */
19475 else
19476#endif
19477 {
19478 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19479 ++p;
19480 }
19481 }
19482}
19483
19484/*
19485 * "toupper(string)" function
19486 */
19487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019488f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019490 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019491 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492}
19493
19494/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019495 * "tr(string, fromstr, tostr)" function
19496 */
19497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019498f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019499{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019500 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019501 char_u *fromstr;
19502 char_u *tostr;
19503 char_u *p;
19504#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019505 int inlen;
19506 int fromlen;
19507 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019508 int idx;
19509 char_u *cpstr;
19510 int cplen;
19511 int first = TRUE;
19512#endif
19513 char_u buf[NUMBUFLEN];
19514 char_u buf2[NUMBUFLEN];
19515 garray_T ga;
19516
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019517 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019518 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19519 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019520
19521 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019522 rettv->v_type = VAR_STRING;
19523 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019524 if (fromstr == NULL || tostr == NULL)
19525 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019526 ga_init2(&ga, (int)sizeof(char), 80);
19527
19528#ifdef FEAT_MBYTE
19529 if (!has_mbyte)
19530#endif
19531 /* not multi-byte: fromstr and tostr must be the same length */
19532 if (STRLEN(fromstr) != STRLEN(tostr))
19533 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019534#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019535error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019536#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019537 EMSG2(_(e_invarg2), fromstr);
19538 ga_clear(&ga);
19539 return;
19540 }
19541
19542 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019543 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019544 {
19545#ifdef FEAT_MBYTE
19546 if (has_mbyte)
19547 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019548 inlen = (*mb_ptr2len)(in_str);
19549 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019550 cplen = inlen;
19551 idx = 0;
19552 for (p = fromstr; *p != NUL; p += fromlen)
19553 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019554 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019555 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019556 {
19557 for (p = tostr; *p != NUL; p += tolen)
19558 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019559 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560 if (idx-- == 0)
19561 {
19562 cplen = tolen;
19563 cpstr = p;
19564 break;
19565 }
19566 }
19567 if (*p == NUL) /* tostr is shorter than fromstr */
19568 goto error;
19569 break;
19570 }
19571 ++idx;
19572 }
19573
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019574 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019575 {
19576 /* Check that fromstr and tostr have the same number of
19577 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019578 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019579 first = FALSE;
19580 for (p = tostr; *p != NUL; p += tolen)
19581 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019582 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019583 --idx;
19584 }
19585 if (idx != 0)
19586 goto error;
19587 }
19588
Bram Moolenaarcde88542015-08-11 19:14:00 +020019589 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019590 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019591 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019592
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019593 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019594 }
19595 else
19596#endif
19597 {
19598 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019599 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019600 if (p != NULL)
19601 ga_append(&ga, tostr[p - fromstr]);
19602 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019603 ga_append(&ga, *in_str);
19604 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019605 }
19606 }
19607
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019608 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019609 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019610 ga_append(&ga, NUL);
19611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019613}
19614
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019615#ifdef FEAT_FLOAT
19616/*
19617 * "trunc({float})" function
19618 */
19619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019620f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019621{
19622 float_T f;
19623
19624 rettv->v_type = VAR_FLOAT;
19625 if (get_float_arg(argvars, &f) == OK)
19626 /* trunc() is not in C90, use floor() or ceil() instead. */
19627 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19628 else
19629 rettv->vval.v_float = 0.0;
19630}
19631#endif
19632
Bram Moolenaar8299df92004-07-10 09:47:34 +000019633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 * "type(expr)" function
19635 */
19636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019637f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019639 int n;
19640
19641 switch (argvars[0].v_type)
19642 {
19643 case VAR_NUMBER: n = 0; break;
19644 case VAR_STRING: n = 1; break;
19645 case VAR_FUNC: n = 2; break;
19646 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019647 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019648#ifdef FEAT_FLOAT
19649 case VAR_FLOAT: n = 5; break;
19650#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019651 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019652 if (argvars[0].vval.v_number == VVAL_FALSE
19653 || argvars[0].vval.v_number == VVAL_TRUE)
19654 n = 6;
19655 else
19656 n = 7;
19657 break;
19658 case VAR_UNKNOWN:
19659 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19660 n = -1;
19661 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019662 }
19663 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664}
19665
19666/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019667 * "undofile(name)" function
19668 */
19669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019670f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019671{
19672 rettv->v_type = VAR_STRING;
19673#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019674 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019675 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019676
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019677 if (*fname == NUL)
19678 {
19679 /* If there is no file name there will be no undo file. */
19680 rettv->vval.v_string = NULL;
19681 }
19682 else
19683 {
19684 char_u *ffname = FullName_save(fname, FALSE);
19685
19686 if (ffname != NULL)
19687 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19688 vim_free(ffname);
19689 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019690 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019691#else
19692 rettv->vval.v_string = NULL;
19693#endif
19694}
19695
19696/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019697 * "undotree()" function
19698 */
19699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019701{
19702 if (rettv_dict_alloc(rettv) == OK)
19703 {
19704 dict_T *dict = rettv->vval.v_dict;
19705 list_T *list;
19706
Bram Moolenaar730cde92010-06-27 05:18:54 +020019707 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019708 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019709 dict_add_nr_str(dict, "save_last",
19710 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019711 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19712 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019713 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019714
19715 list = list_alloc();
19716 if (list != NULL)
19717 {
19718 u_eval_tree(curbuf->b_u_oldhead, list);
19719 dict_add_list(dict, "entries", list);
19720 }
19721 }
19722}
19723
19724/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019725 * "values(dict)" function
19726 */
19727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019728f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019729{
19730 dict_list(argvars, rettv, 1);
19731}
19732
19733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 * "virtcol(string)" function
19735 */
19736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019737f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738{
19739 colnr_T vcol = 0;
19740 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019741 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019743 fp = var2fpos(&argvars[0], FALSE, &fnum);
19744 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19745 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 {
19747 getvvcol(curwin, fp, NULL, NULL, &vcol);
19748 ++vcol;
19749 }
19750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752}
19753
19754/*
19755 * "visualmode()" function
19756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019758f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 char_u str[2];
19761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 str[0] = curbuf->b_visual_mode_eval;
19764 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766
19767 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019768 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770}
19771
19772/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019773 * "wildmenumode()" function
19774 */
19775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019776f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019777{
19778#ifdef FEAT_WILDMENU
19779 if (wild_menu_showing)
19780 rettv->vval.v_number = 1;
19781#endif
19782}
19783
19784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 * "winbufnr(nr)" function
19786 */
19787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019788f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789{
19790 win_T *wp;
19791
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019792 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797}
19798
19799/*
19800 * "wincol()" function
19801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807}
19808
19809/*
19810 * "winheight(nr)" function
19811 */
19812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019813f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814{
19815 win_T *wp;
19816
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019817 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822}
19823
19824/*
19825 * "winline()" function
19826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019828f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829{
19830 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832}
19833
19834/*
19835 * "winnr()" function
19836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019838f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839{
19840 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019841
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019843 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846}
19847
19848/*
19849 * "winrestcmd()" function
19850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019852f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853{
19854#ifdef FEAT_WINDOWS
19855 win_T *wp;
19856 int winnr = 1;
19857 garray_T ga;
19858 char_u buf[50];
19859
19860 ga_init2(&ga, (int)sizeof(char), 70);
19861 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19862 {
19863 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19864 ga_concat(&ga, buf);
19865# ifdef FEAT_VERTSPLIT
19866 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19867 ga_concat(&ga, buf);
19868# endif
19869 ++winnr;
19870 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019871 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019873 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878}
19879
19880/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019881 * "winrestview()" function
19882 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019884f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019885{
19886 dict_T *dict;
19887
19888 if (argvars[0].v_type != VAR_DICT
19889 || (dict = argvars[0].vval.v_dict) == NULL)
19890 EMSG(_(e_invarg));
19891 else
19892 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019893 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19894 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19895 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19896 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019897#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019898 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19899 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019900#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019901 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19902 {
19903 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19904 curwin->w_set_curswant = FALSE;
19905 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019906
Bram Moolenaar82c25852014-05-28 16:47:16 +020019907 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19908 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019910 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19911 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019912#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019913 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19914 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19915 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19916 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019917
19918 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019919 win_new_height(curwin, curwin->w_height);
19920# ifdef FEAT_VERTSPLIT
19921 win_new_width(curwin, W_WIDTH(curwin));
19922# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019923 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019924
Bram Moolenaarb851a962014-10-31 15:45:52 +010019925 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019926 curwin->w_topline = 1;
19927 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19928 curwin->w_topline = curbuf->b_ml.ml_line_count;
19929#ifdef FEAT_DIFF
19930 check_topfill(curwin, TRUE);
19931#endif
19932 }
19933}
19934
19935/*
19936 * "winsaveview()" function
19937 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019939f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019940{
19941 dict_T *dict;
19942
Bram Moolenaara800b422010-06-27 01:15:55 +020019943 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019944 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019945 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019946
19947 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19948 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19949#ifdef FEAT_VIRTUALEDIT
19950 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19951#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019952 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019953 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19954
19955 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19956#ifdef FEAT_DIFF
19957 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19958#endif
19959 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19960 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19961}
19962
19963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 * "winwidth(nr)" function
19965 */
19966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019967f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968{
19969 win_T *wp;
19970
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019971 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 else
19975#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979#endif
19980}
19981
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019983 * "wordcount()" function
19984 */
19985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019986f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019987{
19988 if (rettv_dict_alloc(rettv) == FAIL)
19989 return;
19990 cursor_pos_info(rettv->vval.v_dict);
19991}
19992
19993/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019994 * Write list of strings to file
19995 */
19996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019997write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019998{
19999 listitem_T *li;
20000 int c;
20001 int ret = OK;
20002 char_u *s;
20003
20004 for (li = list->lv_first; li != NULL; li = li->li_next)
20005 {
20006 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20007 {
20008 if (*s == '\n')
20009 c = putc(NUL, fd);
20010 else
20011 c = putc(*s, fd);
20012 if (c == EOF)
20013 {
20014 ret = FAIL;
20015 break;
20016 }
20017 }
20018 if (!binary || li->li_next != NULL)
20019 if (putc('\n', fd) == EOF)
20020 {
20021 ret = FAIL;
20022 break;
20023 }
20024 if (ret == FAIL)
20025 {
20026 EMSG(_(e_write));
20027 break;
20028 }
20029 }
20030 return ret;
20031}
20032
20033/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020034 * "writefile()" function
20035 */
20036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020037f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020038{
20039 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020040 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020041 char_u *fname;
20042 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020043 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020044
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020045 if (check_restricted() || check_secure())
20046 return;
20047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020048 if (argvars[0].v_type != VAR_LIST)
20049 {
20050 EMSG2(_(e_listarg), "writefile()");
20051 return;
20052 }
20053 if (argvars[0].vval.v_list == NULL)
20054 return;
20055
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020056 if (argvars[2].v_type != VAR_UNKNOWN)
20057 {
20058 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20059 binary = TRUE;
20060 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20061 append = TRUE;
20062 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020063
20064 /* Always open the file in binary mode, library functions have a mind of
20065 * their own about CR-LF conversion. */
20066 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020067 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20068 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020069 {
20070 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20071 ret = -1;
20072 }
20073 else
20074 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020075 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20076 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020077 fclose(fd);
20078 }
20079
20080 rettv->vval.v_number = ret;
20081}
20082
20083/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020084 * "xor(expr, expr)" function
20085 */
20086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020087f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020088{
20089 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20090 ^ get_tv_number_chk(&argvars[1], NULL);
20091}
20092
20093
20094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020096 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 */
20098 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020099var2fpos(
20100 typval_T *varp,
20101 int dollar_lnum, /* TRUE when $ is last line */
20102 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020104 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020106 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107
Bram Moolenaara5525202006-03-02 22:52:09 +000020108 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020109 if (varp->v_type == VAR_LIST)
20110 {
20111 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020112 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020113 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020114 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020115
20116 l = varp->vval.v_list;
20117 if (l == NULL)
20118 return NULL;
20119
20120 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020121 pos.lnum = list_find_nr(l, 0L, &error);
20122 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020123 return NULL; /* invalid line number */
20124
20125 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020126 pos.col = list_find_nr(l, 1L, &error);
20127 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020128 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020129 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020130
20131 /* We accept "$" for the column number: last column. */
20132 li = list_find(l, 1L);
20133 if (li != NULL && li->li_tv.v_type == VAR_STRING
20134 && li->li_tv.vval.v_string != NULL
20135 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20136 pos.col = len + 1;
20137
Bram Moolenaara5525202006-03-02 22:52:09 +000020138 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020139 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020140 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020141 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020142
Bram Moolenaara5525202006-03-02 22:52:09 +000020143#ifdef FEAT_VIRTUALEDIT
20144 /* Get the virtual offset. Defaults to zero. */
20145 pos.coladd = list_find_nr(l, 2L, &error);
20146 if (error)
20147 pos.coladd = 0;
20148#endif
20149
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020150 return &pos;
20151 }
20152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020153 name = get_tv_string_chk(varp);
20154 if (name == NULL)
20155 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020156 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020158 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20159 {
20160 if (VIsual_active)
20161 return &VIsual;
20162 return &curwin->w_cursor;
20163 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020164 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020166 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20168 return NULL;
20169 return pp;
20170 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020171
20172#ifdef FEAT_VIRTUALEDIT
20173 pos.coladd = 0;
20174#endif
20175
Bram Moolenaar477933c2007-07-17 14:32:23 +000020176 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020177 {
20178 pos.col = 0;
20179 if (name[1] == '0') /* "w0": first visible line */
20180 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020181 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020182 pos.lnum = curwin->w_topline;
20183 return &pos;
20184 }
20185 else if (name[1] == '$') /* "w$": last visible line */
20186 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020187 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020188 pos.lnum = curwin->w_botline - 1;
20189 return &pos;
20190 }
20191 }
20192 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020194 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 {
20196 pos.lnum = curbuf->b_ml.ml_line_count;
20197 pos.col = 0;
20198 }
20199 else
20200 {
20201 pos.lnum = curwin->w_cursor.lnum;
20202 pos.col = (colnr_T)STRLEN(ml_get_curline());
20203 }
20204 return &pos;
20205 }
20206 return NULL;
20207}
20208
20209/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020210 * Convert list in "arg" into a position and optional file number.
20211 * When "fnump" is NULL there is no file number, only 3 items.
20212 * Note that the column is passed on as-is, the caller may want to decrement
20213 * it to use 1 for the first column.
20214 * Return FAIL when conversion is not possible, doesn't check the position for
20215 * validity.
20216 */
20217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020218list2fpos(
20219 typval_T *arg,
20220 pos_T *posp,
20221 int *fnump,
20222 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020223{
20224 list_T *l = arg->vval.v_list;
20225 long i = 0;
20226 long n;
20227
Bram Moolenaar493c1782014-05-28 14:34:46 +020020228 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20229 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020230 if (arg->v_type != VAR_LIST
20231 || l == NULL
20232 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020233 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020234 return FAIL;
20235
20236 if (fnump != NULL)
20237 {
20238 n = list_find_nr(l, i++, NULL); /* fnum */
20239 if (n < 0)
20240 return FAIL;
20241 if (n == 0)
20242 n = curbuf->b_fnum; /* current buffer */
20243 *fnump = n;
20244 }
20245
20246 n = list_find_nr(l, i++, NULL); /* lnum */
20247 if (n < 0)
20248 return FAIL;
20249 posp->lnum = n;
20250
20251 n = list_find_nr(l, i++, NULL); /* col */
20252 if (n < 0)
20253 return FAIL;
20254 posp->col = n;
20255
20256#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020257 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020258 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020259 posp->coladd = 0;
20260 else
20261 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020262#endif
20263
Bram Moolenaar493c1782014-05-28 14:34:46 +020020264 if (curswantp != NULL)
20265 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20266
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020267 return OK;
20268}
20269
20270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 * Get the length of an environment variable name.
20272 * Advance "arg" to the first character after the name.
20273 * Return 0 for error.
20274 */
20275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277{
20278 char_u *p;
20279 int len;
20280
20281 for (p = *arg; vim_isIDc(*p); ++p)
20282 ;
20283 if (p == *arg) /* no name found */
20284 return 0;
20285
20286 len = (int)(p - *arg);
20287 *arg = p;
20288 return len;
20289}
20290
20291/*
20292 * Get the length of the name of a function or internal variable.
20293 * "arg" is advanced to the first non-white character after the name.
20294 * Return 0 if something is wrong.
20295 */
20296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020297get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298{
20299 char_u *p;
20300 int len;
20301
20302 /* Find the end of the name. */
20303 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020304 {
20305 if (*p == ':')
20306 {
20307 /* "s:" is start of "s:var", but "n:" is not and can be used in
20308 * slice "[n:]". Also "xx:" is not a namespace. */
20309 len = (int)(p - *arg);
20310 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20311 || len > 1)
20312 break;
20313 }
20314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 if (p == *arg) /* no name found */
20316 return 0;
20317
20318 len = (int)(p - *arg);
20319 *arg = skipwhite(p);
20320
20321 return len;
20322}
20323
20324/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020325 * Get the length of the name of a variable or function.
20326 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020328 * Return -1 if curly braces expansion failed.
20329 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 * If the name contains 'magic' {}'s, expand them and return the
20331 * expanded name in an allocated string via 'alias' - caller must free.
20332 */
20333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020334get_name_len(
20335 char_u **arg,
20336 char_u **alias,
20337 int evaluate,
20338 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339{
20340 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 char_u *p;
20342 char_u *expr_start;
20343 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344
20345 *alias = NULL; /* default to no alias */
20346
20347 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20348 && (*arg)[2] == (int)KE_SNR)
20349 {
20350 /* hard coded <SNR>, already translated */
20351 *arg += 3;
20352 return get_id_len(arg) + 3;
20353 }
20354 len = eval_fname_script(*arg);
20355 if (len > 0)
20356 {
20357 /* literal "<SID>", "s:" or "<SNR>" */
20358 *arg += len;
20359 }
20360
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020364 p = find_name_end(*arg, &expr_start, &expr_end,
20365 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 if (expr_start != NULL)
20367 {
20368 char_u *temp_string;
20369
20370 if (!evaluate)
20371 {
20372 len += (int)(p - *arg);
20373 *arg = skipwhite(p);
20374 return len;
20375 }
20376
20377 /*
20378 * Include any <SID> etc in the expanded string:
20379 * Thus the -len here.
20380 */
20381 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20382 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020383 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 *alias = temp_string;
20385 *arg = skipwhite(p);
20386 return (int)STRLEN(temp_string);
20387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388
20389 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020390 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 EMSG2(_(e_invexpr2), *arg);
20392
20393 return len;
20394}
20395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020396/*
20397 * Find the end of a variable or function name, taking care of magic braces.
20398 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20399 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020400 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 * Return a pointer to just after the name. Equal to "arg" if there is no
20402 * valid name.
20403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020405find_name_end(
20406 char_u *arg,
20407 char_u **expr_start,
20408 char_u **expr_end,
20409 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 int mb_nest = 0;
20412 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020414 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020416 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 *expr_start = NULL;
20419 *expr_end = NULL;
20420 }
20421
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020422 /* Quick check for valid starting character. */
20423 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20424 return arg;
20425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 for (p = arg; *p != NUL
20427 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020428 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020429 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020431 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020432 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020433 if (*p == '\'')
20434 {
20435 /* skip over 'string' to avoid counting [ and ] inside it. */
20436 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20437 ;
20438 if (*p == NUL)
20439 break;
20440 }
20441 else if (*p == '"')
20442 {
20443 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20444 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20445 if (*p == '\\' && p[1] != NUL)
20446 ++p;
20447 if (*p == NUL)
20448 break;
20449 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020450 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20451 {
20452 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020453 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020454 len = (int)(p - arg);
20455 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020456 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020457 break;
20458 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020460 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020462 if (*p == '[')
20463 ++br_nest;
20464 else if (*p == ']')
20465 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020468 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020470 if (*p == '{')
20471 {
20472 mb_nest++;
20473 if (expr_start != NULL && *expr_start == NULL)
20474 *expr_start = p;
20475 }
20476 else if (*p == '}')
20477 {
20478 mb_nest--;
20479 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20480 *expr_end = p;
20481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 }
20484
20485 return p;
20486}
20487
20488/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020489 * Expands out the 'magic' {}'s in a variable/function name.
20490 * Note that this can call itself recursively, to deal with
20491 * constructs like foo{bar}{baz}{bam}
20492 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20493 * "in_start" ^
20494 * "expr_start" ^
20495 * "expr_end" ^
20496 * "in_end" ^
20497 *
20498 * Returns a new allocated string, which the caller must free.
20499 * Returns NULL for failure.
20500 */
20501 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020502make_expanded_name(
20503 char_u *in_start,
20504 char_u *expr_start,
20505 char_u *expr_end,
20506 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020507{
20508 char_u c1;
20509 char_u *retval = NULL;
20510 char_u *temp_result;
20511 char_u *nextcmd = NULL;
20512
20513 if (expr_end == NULL || in_end == NULL)
20514 return NULL;
20515 *expr_start = NUL;
20516 *expr_end = NUL;
20517 c1 = *in_end;
20518 *in_end = NUL;
20519
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020520 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020521 if (temp_result != NULL && nextcmd == NULL)
20522 {
20523 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20524 + (in_end - expr_end) + 1));
20525 if (retval != NULL)
20526 {
20527 STRCPY(retval, in_start);
20528 STRCAT(retval, temp_result);
20529 STRCAT(retval, expr_end + 1);
20530 }
20531 }
20532 vim_free(temp_result);
20533
20534 *in_end = c1; /* put char back for error messages */
20535 *expr_start = '{';
20536 *expr_end = '}';
20537
20538 if (retval != NULL)
20539 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020540 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020541 if (expr_start != NULL)
20542 {
20543 /* Further expansion! */
20544 temp_result = make_expanded_name(retval, expr_start,
20545 expr_end, temp_result);
20546 vim_free(retval);
20547 retval = temp_result;
20548 }
20549 }
20550
20551 return retval;
20552}
20553
20554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020556 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 */
20558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020561 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20562}
20563
20564/*
20565 * Return TRUE if character "c" can be used as the first character in a
20566 * variable or function name (excluding '{' and '}').
20567 */
20568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020569eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020570{
20571 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572}
20573
20574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 * Set number v: variable to "val".
20576 */
20577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020578set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020580 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581}
20582
20583/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020584 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 */
20586 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020589 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590}
20591
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020592/*
20593 * Get string v: variable value. Uses a static buffer, can only be used once.
20594 */
20595 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020596get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020597{
20598 return get_tv_string(&vimvars[idx].vv_tv);
20599}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020600
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020602 * Get List v: variable value. Caller must take care of reference count when
20603 * needed.
20604 */
20605 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020606get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020607{
20608 return vimvars[idx].vv_list;
20609}
20610
20611/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020612 * Set v:char to character "c".
20613 */
20614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020615set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020616{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020617 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020618
20619#ifdef FEAT_MBYTE
20620 if (has_mbyte)
20621 buf[(*mb_char2bytes)(c, buf)] = NUL;
20622 else
20623#endif
20624 {
20625 buf[0] = c;
20626 buf[1] = NUL;
20627 }
20628 set_vim_var_string(VV_CHAR, buf, -1);
20629}
20630
20631/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020632 * Set v:count to "count" and v:count1 to "count1".
20633 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 */
20635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636set_vcount(
20637 long count,
20638 long count1,
20639 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020641 if (set_prevcount)
20642 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020643 vimvars[VV_COUNT].vv_nr = count;
20644 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645}
20646
20647/*
20648 * Set string v: variable to a copy of "val".
20649 */
20650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020651set_vim_var_string(
20652 int idx,
20653 char_u *val,
20654 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655{
Bram Moolenaara542c682016-01-31 16:28:04 +010020656 clear_tv(&vimvars[idx].vv_di.di_tv);
20657 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020659 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020661 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020663 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664}
20665
20666/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020667 * Set List v: variable to "val".
20668 */
20669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020670set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020671{
Bram Moolenaara542c682016-01-31 16:28:04 +010020672 clear_tv(&vimvars[idx].vv_di.di_tv);
20673 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020674 vimvars[idx].vv_list = val;
20675 if (val != NULL)
20676 ++val->lv_refcount;
20677}
20678
20679/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020680 * Set Dictionary v: variable to "val".
20681 */
20682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020683set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020684{
20685 int todo;
20686 hashitem_T *hi;
20687
Bram Moolenaara542c682016-01-31 16:28:04 +010020688 clear_tv(&vimvars[idx].vv_di.di_tv);
20689 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020690 vimvars[idx].vv_dict = val;
20691 if (val != NULL)
20692 {
20693 ++val->dv_refcount;
20694
20695 /* Set readonly */
20696 todo = (int)val->dv_hashtab.ht_used;
20697 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20698 {
20699 if (HASHITEM_EMPTY(hi))
20700 continue;
20701 --todo;
20702 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20703 }
20704 }
20705}
20706
20707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * Set v:register if needed.
20709 */
20710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 char_u regname;
20714
20715 if (c == 0 || c == ' ')
20716 regname = '"';
20717 else
20718 regname = c;
20719 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020720 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 set_vim_var_string(VV_REG, &regname, 1);
20722}
20723
20724/*
20725 * Get or set v:exception. If "oldval" == NULL, return the current value.
20726 * Otherwise, restore the value to "oldval" and return NULL.
20727 * Must always be called in pairs to save and restore v:exception! Does not
20728 * take care of memory allocations.
20729 */
20730 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020731v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732{
20733 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735
Bram Moolenaare9a41262005-01-15 22:18:47 +000020736 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 return NULL;
20738}
20739
20740/*
20741 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20742 * Otherwise, restore the value to "oldval" and return NULL.
20743 * Must always be called in pairs to save and restore v:throwpoint! Does not
20744 * take care of memory allocations.
20745 */
20746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020747v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748{
20749 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020750 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751
Bram Moolenaare9a41262005-01-15 22:18:47 +000020752 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 return NULL;
20754}
20755
20756#if defined(FEAT_AUTOCMD) || defined(PROTO)
20757/*
20758 * Set v:cmdarg.
20759 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20760 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20761 * Must always be called in pairs!
20762 */
20763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020764set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765{
20766 char_u *oldval;
20767 char_u *newval;
20768 unsigned len;
20769
Bram Moolenaare9a41262005-01-15 22:18:47 +000020770 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020771 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020773 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020774 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020775 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 }
20777
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020778 if (eap->force_bin == FORCE_BIN)
20779 len = 6;
20780 else if (eap->force_bin == FORCE_NOBIN)
20781 len = 8;
20782 else
20783 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020784
20785 if (eap->read_edit)
20786 len += 7;
20787
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020788 if (eap->force_ff != 0)
20789 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20790# ifdef FEAT_MBYTE
20791 if (eap->force_enc != 0)
20792 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020793 if (eap->bad_char != 0)
20794 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020795# endif
20796
20797 newval = alloc(len + 1);
20798 if (newval == NULL)
20799 return NULL;
20800
20801 if (eap->force_bin == FORCE_BIN)
20802 sprintf((char *)newval, " ++bin");
20803 else if (eap->force_bin == FORCE_NOBIN)
20804 sprintf((char *)newval, " ++nobin");
20805 else
20806 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020807
20808 if (eap->read_edit)
20809 STRCAT(newval, " ++edit");
20810
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020811 if (eap->force_ff != 0)
20812 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20813 eap->cmd + eap->force_ff);
20814# ifdef FEAT_MBYTE
20815 if (eap->force_enc != 0)
20816 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20817 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020818 if (eap->bad_char == BAD_KEEP)
20819 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20820 else if (eap->bad_char == BAD_DROP)
20821 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20822 else if (eap->bad_char != 0)
20823 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020824# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020825 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020826 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828#endif
20829
20830/*
20831 * Get the value of internal variable "name".
20832 * Return OK or FAIL.
20833 */
20834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020835get_var_tv(
20836 char_u *name,
20837 int len, /* length of "name" */
20838 typval_T *rettv, /* NULL when only checking existence */
20839 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20840 int verbose, /* may give error message */
20841 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842{
20843 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 typval_T *tv = NULL;
20845 typval_T atv;
20846 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848
20849 /* truncate the name, so that we can use strcmp() */
20850 cc = name[len];
20851 name[len] = NUL;
20852
20853 /*
20854 * Check for "b:changedtick".
20855 */
20856 if (STRCMP(name, "b:changedtick") == 0)
20857 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020858 atv.v_type = VAR_NUMBER;
20859 atv.vval.v_number = curbuf->b_changedtick;
20860 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 }
20862
20863 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 * Check for user-defined variables.
20865 */
20866 else
20867 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020868 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020871 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020872 if (dip != NULL)
20873 *dip = v;
20874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 }
20876
Bram Moolenaare9a41262005-01-15 22:18:47 +000020877 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020879 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020880 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 ret = FAIL;
20882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020884 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885
20886 name[len] = cc;
20887
20888 return ret;
20889}
20890
20891/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020892 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20893 * Also handle function call with Funcref variable: func(expr)
20894 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20895 */
20896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020897handle_subscript(
20898 char_u **arg,
20899 typval_T *rettv,
20900 int evaluate, /* do more than finding the end */
20901 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020902{
20903 int ret = OK;
20904 dict_T *selfdict = NULL;
20905 char_u *s;
20906 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020907 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020908
20909 while (ret == OK
20910 && (**arg == '['
20911 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020912 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020913 && !vim_iswhite(*(*arg - 1)))
20914 {
20915 if (**arg == '(')
20916 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020917 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020918 if (evaluate)
20919 {
20920 functv = *rettv;
20921 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020922
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020923 /* Invoke the function. Recursive! */
20924 s = functv.vval.v_string;
20925 }
20926 else
20927 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020928 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020929 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20930 &len, evaluate, selfdict);
20931
20932 /* Clear the funcref afterwards, so that deleting it while
20933 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020934 if (evaluate)
20935 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020936
20937 /* Stop the expression evaluation when immediately aborting on
20938 * error, or when an interrupt occurred or an exception was thrown
20939 * but not caught. */
20940 if (aborting())
20941 {
20942 if (ret == OK)
20943 clear_tv(rettv);
20944 ret = FAIL;
20945 }
20946 dict_unref(selfdict);
20947 selfdict = NULL;
20948 }
20949 else /* **arg == '[' || **arg == '.' */
20950 {
20951 dict_unref(selfdict);
20952 if (rettv->v_type == VAR_DICT)
20953 {
20954 selfdict = rettv->vval.v_dict;
20955 if (selfdict != NULL)
20956 ++selfdict->dv_refcount;
20957 }
20958 else
20959 selfdict = NULL;
20960 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20961 {
20962 clear_tv(rettv);
20963 ret = FAIL;
20964 }
20965 }
20966 }
20967 dict_unref(selfdict);
20968 return ret;
20969}
20970
20971/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020972 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020973 * value).
20974 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020975 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020976alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020977{
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020979}
20980
20981/*
20982 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 * The string "s" must have been allocated, it is consumed.
20984 * Return NULL for out of memory, the variable otherwise.
20985 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020986 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020987alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988{
Bram Moolenaar33570922005-01-25 22:26:29 +000020989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020991 rettv = alloc_tv();
20992 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 rettv->v_type = VAR_STRING;
20995 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 }
20997 else
20998 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000}
21001
21002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021003 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021006free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007{
21008 if (varp != NULL)
21009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021010 switch (varp->v_type)
21011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021012 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021013 func_unref(varp->vval.v_string);
21014 /*FALLTHROUGH*/
21015 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021016 vim_free(varp->vval.v_string);
21017 break;
21018 case VAR_LIST:
21019 list_unref(varp->vval.v_list);
21020 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021021 case VAR_DICT:
21022 dict_unref(varp->vval.v_dict);
21023 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021024 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021025#ifdef FEAT_FLOAT
21026 case VAR_FLOAT:
21027#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021028 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021029 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021030 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 vim_free(varp);
21033 }
21034}
21035
21036/*
21037 * Free the memory for a variable value and set the value to NULL or 0.
21038 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021040clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041{
21042 if (varp != NULL)
21043 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021044 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021046 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021047 func_unref(varp->vval.v_string);
21048 /*FALLTHROUGH*/
21049 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021050 vim_free(varp->vval.v_string);
21051 varp->vval.v_string = NULL;
21052 break;
21053 case VAR_LIST:
21054 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021055 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021056 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021057 case VAR_DICT:
21058 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021059 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021060 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021061 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021062 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021063 varp->vval.v_number = 0;
21064 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021065#ifdef FEAT_FLOAT
21066 case VAR_FLOAT:
21067 varp->vval.v_float = 0.0;
21068 break;
21069#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021070 case VAR_UNKNOWN:
21071 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021073 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 }
21075}
21076
21077/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021078 * Set the value of a variable to NULL without freeing items.
21079 */
21080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082{
21083 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021084 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021085}
21086
21087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 * Get the number value of a variable.
21089 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021090 * For incompatible types, return 0.
21091 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21092 * caller of incompatible types: it sets *denote to TRUE if "denote"
21093 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 */
21095 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021096get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021098 int error = FALSE;
21099
21100 return get_tv_number_chk(varp, &error); /* return 0L on error */
21101}
21102
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021103 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021104get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021106 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021108 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021110 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021111 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021112#ifdef FEAT_FLOAT
21113 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021114 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021115 break;
21116#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021117 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021118 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021119 break;
21120 case VAR_STRING:
21121 if (varp->vval.v_string != NULL)
21122 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021123 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021124 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021125 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021126 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021127 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021128 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021129 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021130 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021131 case VAR_SPECIAL:
21132 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21133 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021134 case VAR_UNKNOWN:
21135 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021138 if (denote == NULL) /* useful for values that must be unsigned */
21139 n = -1;
21140 else
21141 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021142 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143}
21144
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021145#ifdef FEAT_FLOAT
21146 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021148{
21149 switch (varp->v_type)
21150 {
21151 case VAR_NUMBER:
21152 return (float_T)(varp->vval.v_number);
21153#ifdef FEAT_FLOAT
21154 case VAR_FLOAT:
21155 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021156#endif
21157 case VAR_FUNC:
21158 EMSG(_("E891: Using a Funcref as a Float"));
21159 break;
21160 case VAR_STRING:
21161 EMSG(_("E892: Using a String as a Float"));
21162 break;
21163 case VAR_LIST:
21164 EMSG(_("E893: Using a List as a Float"));
21165 break;
21166 case VAR_DICT:
21167 EMSG(_("E894: Using a Dictionary as a Float"));
21168 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021169 case VAR_SPECIAL:
21170 EMSG(_("E907: Using a special value as a Float"));
21171 break;
21172 case VAR_UNKNOWN:
21173 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021174 break;
21175 }
21176 return 0;
21177}
21178#endif
21179
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021181 * Get the lnum from the first argument.
21182 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021183 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 */
21185 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 linenr_T lnum;
21190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021191 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 if (lnum == 0) /* no valid number, try using line() */
21193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021194 rettv.v_type = VAR_NUMBER;
21195 f_line(argvars, &rettv);
21196 lnum = rettv.vval.v_number;
21197 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 }
21199 return lnum;
21200}
21201
21202/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021203 * Get the lnum from the first argument.
21204 * Also accepts "$", then "buf" is used.
21205 * Returns 0 on error.
21206 */
21207 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021208get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021209{
21210 if (argvars[0].v_type == VAR_STRING
21211 && argvars[0].vval.v_string != NULL
21212 && argvars[0].vval.v_string[0] == '$'
21213 && buf != NULL)
21214 return buf->b_ml.ml_line_count;
21215 return get_tv_number_chk(&argvars[0], NULL);
21216}
21217
21218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 * Get the string value of a variable.
21220 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021221 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21222 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 * If the String variable has never been set, return an empty string.
21224 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021225 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21226 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 */
21228 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230{
21231 static char_u mybuf[NUMBUFLEN];
21232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234}
21235
21236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021237get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021239 char_u *res = get_tv_string_buf_chk(varp, buf);
21240
21241 return res != NULL ? res : (char_u *)"";
21242}
21243
Bram Moolenaar7d647822014-04-05 21:28:56 +020021244/*
21245 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21246 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021247 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021249{
21250 static char_u mybuf[NUMBUFLEN];
21251
21252 return get_tv_string_buf_chk(varp, mybuf);
21253}
21254
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021255 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021256get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021257{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021260 case VAR_NUMBER:
21261 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21262 return buf;
21263 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021264 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021265 break;
21266 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021267 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021268 break;
21269 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021270 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021271 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021272#ifdef FEAT_FLOAT
21273 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021274 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021275 break;
21276#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021277 case VAR_STRING:
21278 if (varp->vval.v_string != NULL)
21279 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021280 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021281 case VAR_SPECIAL:
21282 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21283 return buf;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021284 case VAR_UNKNOWN:
21285 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021288 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289}
21290
21291/*
21292 * Find variable "name" in the list of variables.
21293 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021294 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021295 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021296 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303
Bram Moolenaara7043832005-01-21 11:56:39 +000021304 ht = find_var_ht(name, &varname);
21305 if (htp != NULL)
21306 *htp = ht;
21307 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021309 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310}
21311
21312/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021313 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021314 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021316 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317find_var_in_ht(
21318 hashtab_T *ht,
21319 int htname,
21320 char_u *varname,
21321 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021322{
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 hashitem_T *hi;
21324
21325 if (*varname == NUL)
21326 {
21327 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021328 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021329 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021330 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021331 case 'g': return &globvars_var;
21332 case 'v': return &vimvars_var;
21333 case 'b': return &curbuf->b_bufvar;
21334 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021335#ifdef FEAT_WINDOWS
21336 case 't': return &curtab->tp_winvar;
21337#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021338 case 'l': return current_funccal == NULL
21339 ? NULL : &current_funccal->l_vars_var;
21340 case 'a': return current_funccal == NULL
21341 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 }
21343 return NULL;
21344 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021345
21346 hi = hash_find(ht, varname);
21347 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021348 {
21349 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021350 * worked find the variable again. Don't auto-load a script if it was
21351 * loaded already, otherwise it would be loaded every time when
21352 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021353 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021354 {
21355 /* Note: script_autoload() may make "hi" invalid. It must either
21356 * be obtained again or not used. */
21357 if (!script_autoload(varname, FALSE) || aborting())
21358 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021359 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021360 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021361 if (HASHITEM_EMPTY(hi))
21362 return NULL;
21363 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021364 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021365}
21366
21367/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021368 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021369 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021370 * Set "varname" to the start of name without ':'.
21371 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021372 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021373find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021375 hashitem_T *hi;
21376
Bram Moolenaar73627d02015-08-11 15:46:09 +020021377 if (name[0] == NUL)
21378 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 if (name[1] != ':')
21380 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021381 /* The name must not start with a colon or #. */
21382 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 return NULL;
21384 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021385
21386 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021387 hi = hash_find(&compat_hashtab, name);
21388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021389 return &compat_hashtab;
21390
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021392 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021393 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 }
21395 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021396 if (*name == 'g') /* global variable */
21397 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021398 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21399 */
21400 if (vim_strchr(name + 2, ':') != NULL
21401 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021404 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021406 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021407#ifdef FEAT_WINDOWS
21408 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021409 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021410#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021411 if (*name == 'v') /* v: variable */
21412 return &vimvarht;
21413 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021414 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021415 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021416 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 if (*name == 's' /* script variable */
21418 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21419 return &SCRIPT_VARS(current_SID);
21420 return NULL;
21421}
21422
21423/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021424 * Get function call environment based on bactrace debug level
21425 */
21426 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021427get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021428{
21429 int i;
21430 funccall_T *funccal;
21431 funccall_T *temp_funccal;
21432
21433 funccal = current_funccal;
21434 if (debug_backtrace_level > 0)
21435 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021436 for (i = 0; i < debug_backtrace_level; i++)
21437 {
21438 temp_funccal = funccal->caller;
21439 if (temp_funccal)
21440 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021441 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021442 /* backtrace level overflow. reset to max */
21443 debug_backtrace_level = i;
21444 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021445 }
21446 return funccal;
21447}
21448
21449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021451 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 * Returns NULL when it doesn't exist.
21453 */
21454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021455get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
Bram Moolenaar33570922005-01-25 22:26:29 +000021457 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021459 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 if (v == NULL)
21461 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463}
21464
21465/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 * sourcing this script and when executing functions defined in the script.
21468 */
21469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021470new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471{
Bram Moolenaara7043832005-01-21 11:56:39 +000021472 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 hashtab_T *ht;
21474 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021475
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21477 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021478 /* Re-allocating ga_data means that an ht_array pointing to
21479 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021481 for (i = 1; i <= ga_scripts.ga_len; ++i)
21482 {
21483 ht = &SCRIPT_VARS(i);
21484 if (ht->ht_mask == HT_INIT_SIZE - 1)
21485 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021486 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021488 }
21489
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 while (ga_scripts.ga_len < id)
21491 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021492 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021493 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021494 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 }
21497 }
21498}
21499
21500/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021501 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21502 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 */
21504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021505init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506{
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021508 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021509 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021510 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021511 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 dict_var->di_tv.vval.v_dict = dict;
21513 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021514 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21516 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517}
21518
21519/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021520 * Unreference a dictionary initialized by init_var_dict().
21521 */
21522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021523unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021524{
21525 /* Now the dict needs to be freed if no one else is using it, go back to
21526 * normal reference counting. */
21527 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21528 dict_unref(dict);
21529}
21530
21531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 * Frees all allocated variables and the value they contain.
21534 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 */
21536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021538{
21539 vars_clear_ext(ht, TRUE);
21540}
21541
21542/*
21543 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21544 */
21545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
Bram Moolenaara7043832005-01-21 11:56:39 +000021548 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 hashitem_T *hi;
21550 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021553 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021554 for (hi = ht->ht_array; todo > 0; ++hi)
21555 {
21556 if (!HASHITEM_EMPTY(hi))
21557 {
21558 --todo;
21559
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021561 * ht_array might change then. hash_clear() takes care of it
21562 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 v = HI2DI(hi);
21564 if (free_val)
21565 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021566 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021568 }
21569 }
21570 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021571 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572}
21573
Bram Moolenaara7043832005-01-21 11:56:39 +000021574/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 * Delete a variable from hashtab "ht" at item "hi".
21576 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021582
21583 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021584 clear_tv(&di->di_tv);
21585 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586}
21587
21588/*
21589 * List the value of one internal variable.
21590 */
21591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021592list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021594 char_u *tofree;
21595 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021596 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021597
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021598 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021600 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602}
21603
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021605list_one_var_a(
21606 char_u *prefix,
21607 char_u *name,
21608 int type,
21609 char_u *string,
21610 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
Bram Moolenaar31859182007-08-14 20:41:13 +000021612 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21613 msg_start();
21614 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 if (name != NULL) /* "a:" vars don't have a name stored */
21616 msg_puts(name);
21617 msg_putchar(' ');
21618 msg_advance(22);
21619 if (type == VAR_NUMBER)
21620 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621 else if (type == VAR_FUNC)
21622 msg_putchar('*');
21623 else if (type == VAR_LIST)
21624 {
21625 msg_putchar('[');
21626 if (*string == '[')
21627 ++string;
21628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021629 else if (type == VAR_DICT)
21630 {
21631 msg_putchar('{');
21632 if (*string == '{')
21633 ++string;
21634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 else
21636 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021639
21640 if (type == VAR_FUNC)
21641 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021642 if (*first)
21643 {
21644 msg_clr_eos();
21645 *first = FALSE;
21646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647}
21648
21649/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021650 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 * If the variable already exists, the value is updated.
21652 * Otherwise the variable is created.
21653 */
21654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021655set_var(
21656 char_u *name,
21657 typval_T *tv,
21658 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659{
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021664 ht = find_var_ht(name, &varname);
21665 if (ht == NULL || *varname == NUL)
21666 {
21667 EMSG2(_(e_illvar), name);
21668 return;
21669 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021670 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021671
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021672 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21673 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021678 if (var_check_ro(v->di_flags, name, FALSE)
21679 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 return;
21681 if (v->di_tv.v_type != tv->v_type
21682 && !((v->di_tv.v_type == VAR_STRING
21683 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021684 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021685 || tv->v_type == VAR_NUMBER))
21686#ifdef FEAT_FLOAT
21687 && !((v->di_tv.v_type == VAR_NUMBER
21688 || v->di_tv.v_type == VAR_FLOAT)
21689 && (tv->v_type == VAR_NUMBER
21690 || tv->v_type == VAR_FLOAT))
21691#endif
21692 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021693 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021694 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021695 return;
21696 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021697
21698 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021699 * Handle setting internal v: variables separately where needed to
21700 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 */
21702 if (ht == &vimvarht)
21703 {
21704 if (v->di_tv.v_type == VAR_STRING)
21705 {
21706 vim_free(v->di_tv.vval.v_string);
21707 if (copy || tv->v_type != VAR_STRING)
21708 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21709 else
21710 {
21711 /* Take over the string to avoid an extra alloc/free. */
21712 v->di_tv.vval.v_string = tv->vval.v_string;
21713 tv->vval.v_string = NULL;
21714 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021715 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021717 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021718 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021720 if (STRCMP(varname, "searchforward") == 0)
21721 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021722#ifdef FEAT_SEARCH_EXTRA
21723 else if (STRCMP(varname, "hlsearch") == 0)
21724 {
21725 no_hlsearch = !v->di_tv.vval.v_number;
21726 redraw_all_later(SOME_VALID);
21727 }
21728#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021729 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021730 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021731 else if (v->di_tv.v_type != tv->v_type)
21732 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 }
21734
21735 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 }
21737 else /* add a new variable */
21738 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021739 /* Can't add "v:" variable. */
21740 if (ht == &vimvarht)
21741 {
21742 EMSG2(_(e_illvar), name);
21743 return;
21744 }
21745
Bram Moolenaar92124a32005-06-17 22:03:40 +000021746 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021747 if (!valid_varname(varname))
21748 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021750 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21751 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021752 if (v == NULL)
21753 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021757 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 return;
21759 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021760 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021763 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021765 else
21766 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021768 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771}
21772
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021773/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021774 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 * Also give an error message.
21776 */
21777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021779{
21780 if (flags & DI_FLAGS_RO)
21781 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021782 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 return TRUE;
21784 }
21785 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21786 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021787 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 return TRUE;
21789 }
21790 return FALSE;
21791}
21792
21793/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021794 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21795 * Also give an error message.
21796 */
21797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021798var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021799{
21800 if (flags & DI_FLAGS_FIX)
21801 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021802 EMSG2(_("E795: Cannot delete variable %s"),
21803 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021804 return TRUE;
21805 }
21806 return FALSE;
21807}
21808
21809/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021810 * Check if a funcref is assigned to a valid variable name.
21811 * Return TRUE and give an error if not.
21812 */
21813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021814var_check_func_name(
21815 char_u *name, /* points to start of variable name */
21816 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021817{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021818 /* Allow for w: b: s: and t:. */
21819 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021820 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21821 ? name[2] : name[0]))
21822 {
21823 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21824 name);
21825 return TRUE;
21826 }
21827 /* Don't allow hiding a function. When "v" is not NULL we might be
21828 * assigning another function to the same var, the type is checked
21829 * below. */
21830 if (new_var && function_exists(name))
21831 {
21832 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21833 name);
21834 return TRUE;
21835 }
21836 return FALSE;
21837}
21838
21839/*
21840 * Check if a variable name is valid.
21841 * Return FALSE and give an error if not.
21842 */
21843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021844valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021845{
21846 char_u *p;
21847
21848 for (p = varname; *p != NUL; ++p)
21849 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21850 && *p != AUTOLOAD_CHAR)
21851 {
21852 EMSG2(_(e_illvar), varname);
21853 return FALSE;
21854 }
21855 return TRUE;
21856}
21857
21858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021859 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021860 * Also give an error message, using "name" or _("name") when use_gettext is
21861 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 */
21863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021864tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865{
21866 if (lock & VAR_LOCKED)
21867 {
21868 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021869 name == NULL ? (char_u *)_("Unknown")
21870 : use_gettext ? (char_u *)_(name)
21871 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021872 return TRUE;
21873 }
21874 if (lock & VAR_FIXED)
21875 {
21876 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021877 name == NULL ? (char_u *)_("Unknown")
21878 : use_gettext ? (char_u *)_(name)
21879 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021880 return TRUE;
21881 }
21882 return FALSE;
21883}
21884
21885/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021888 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021889 * It is OK for "from" and "to" to point to the same item. This is used to
21890 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021891 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021893copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021895 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021896 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 switch (from->v_type)
21898 {
21899 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021900 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021901 to->vval.v_number = from->vval.v_number;
21902 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021903#ifdef FEAT_FLOAT
21904 case VAR_FLOAT:
21905 to->vval.v_float = from->vval.v_float;
21906 break;
21907#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021908 case VAR_STRING:
21909 case VAR_FUNC:
21910 if (from->vval.v_string == NULL)
21911 to->vval.v_string = NULL;
21912 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021914 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021915 if (from->v_type == VAR_FUNC)
21916 func_ref(to->vval.v_string);
21917 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 break;
21919 case VAR_LIST:
21920 if (from->vval.v_list == NULL)
21921 to->vval.v_list = NULL;
21922 else
21923 {
21924 to->vval.v_list = from->vval.v_list;
21925 ++to->vval.v_list->lv_refcount;
21926 }
21927 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021928 case VAR_DICT:
21929 if (from->vval.v_dict == NULL)
21930 to->vval.v_dict = NULL;
21931 else
21932 {
21933 to->vval.v_dict = from->vval.v_dict;
21934 ++to->vval.v_dict->dv_refcount;
21935 }
21936 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021937 case VAR_UNKNOWN:
21938 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021939 break;
21940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941}
21942
21943/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021944 * Make a copy of an item.
21945 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021946 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21947 * reference to an already copied list/dict can be used.
21948 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021949 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951item_copy(
21952 typval_T *from,
21953 typval_T *to,
21954 int deep,
21955 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021956{
21957 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021958 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021961 {
21962 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021963 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021964 }
21965 ++recurse;
21966
21967 switch (from->v_type)
21968 {
21969 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021970#ifdef FEAT_FLOAT
21971 case VAR_FLOAT:
21972#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021973 case VAR_STRING:
21974 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021975 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021976 copy_tv(from, to);
21977 break;
21978 case VAR_LIST:
21979 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021980 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021981 if (from->vval.v_list == NULL)
21982 to->vval.v_list = NULL;
21983 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21984 {
21985 /* use the copy made earlier */
21986 to->vval.v_list = from->vval.v_list->lv_copylist;
21987 ++to->vval.v_list->lv_refcount;
21988 }
21989 else
21990 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21991 if (to->vval.v_list == NULL)
21992 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021993 break;
21994 case VAR_DICT:
21995 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021996 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021997 if (from->vval.v_dict == NULL)
21998 to->vval.v_dict = NULL;
21999 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22000 {
22001 /* use the copy made earlier */
22002 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22003 ++to->vval.v_dict->dv_refcount;
22004 }
22005 else
22006 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22007 if (to->vval.v_dict == NULL)
22008 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022009 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022010 case VAR_UNKNOWN:
22011 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022012 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022013 }
22014 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022015 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022016}
22017
22018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 * ":echo expr1 ..." print each argument separated with a space, add a
22020 * newline at the end.
22021 * ":echon expr1 ..." print each argument plain.
22022 */
22023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025{
22026 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022028 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 char_u *p;
22030 int needclr = TRUE;
22031 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022032 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033
22034 if (eap->skip)
22035 ++emsg_skip;
22036 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22037 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022038 /* If eval1() causes an error message the text from the command may
22039 * still need to be cleared. E.g., "echo 22,44". */
22040 need_clr_eos = needclr;
22041
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022043 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 {
22045 /*
22046 * Report the invalid expression unless the expression evaluation
22047 * has been cancelled due to an aborting error, an interrupt, or an
22048 * exception.
22049 */
22050 if (!aborting())
22051 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022052 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 break;
22054 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022055 need_clr_eos = FALSE;
22056
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 if (!eap->skip)
22058 {
22059 if (atstart)
22060 {
22061 atstart = FALSE;
22062 /* Call msg_start() after eval1(), evaluating the expression
22063 * may cause a message to appear. */
22064 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022065 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022066 /* Mark the saved text as finishing the line, so that what
22067 * follows is displayed on a new line when scrolling back
22068 * at the more prompt. */
22069 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 }
22073 else if (eap->cmdidx == CMD_echo)
22074 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022075 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022076 if (p != NULL)
22077 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022079 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022081 if (*p != TAB && needclr)
22082 {
22083 /* remove any text still there from the command */
22084 msg_clr_eos();
22085 needclr = FALSE;
22086 }
22087 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022090 {
22091#ifdef FEAT_MBYTE
22092 if (has_mbyte)
22093 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022094 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022095
22096 (void)msg_outtrans_len_attr(p, i, echo_attr);
22097 p += i - 1;
22098 }
22099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022101 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022104 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022106 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 arg = skipwhite(arg);
22108 }
22109 eap->nextcmd = check_nextcmd(arg);
22110
22111 if (eap->skip)
22112 --emsg_skip;
22113 else
22114 {
22115 /* remove text that may still be there from the command */
22116 if (needclr)
22117 msg_clr_eos();
22118 if (eap->cmdidx == CMD_echo)
22119 msg_end();
22120 }
22121}
22122
22123/*
22124 * ":echohl {name}".
22125 */
22126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022127ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128{
22129 int id;
22130
22131 id = syn_name2id(eap->arg);
22132 if (id == 0)
22133 echo_attr = 0;
22134 else
22135 echo_attr = syn_id2attr(id);
22136}
22137
22138/*
22139 * ":execute expr1 ..." execute the result of an expression.
22140 * ":echomsg expr1 ..." Print a message
22141 * ":echoerr expr1 ..." Print an error
22142 * Each gets spaces around each argument and a newline at the end for
22143 * echo commands
22144 */
22145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022146ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147{
22148 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022149 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 int ret = OK;
22151 char_u *p;
22152 garray_T ga;
22153 int len;
22154 int save_did_emsg;
22155
22156 ga_init2(&ga, 1, 80);
22157
22158 if (eap->skip)
22159 ++emsg_skip;
22160 while (*arg != NUL && *arg != '|' && *arg != '\n')
22161 {
22162 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022163 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 {
22165 /*
22166 * Report the invalid expression unless the expression evaluation
22167 * has been cancelled due to an aborting error, an interrupt, or an
22168 * exception.
22169 */
22170 if (!aborting())
22171 EMSG2(_(e_invexpr2), p);
22172 ret = FAIL;
22173 break;
22174 }
22175
22176 if (!eap->skip)
22177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022178 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 len = (int)STRLEN(p);
22180 if (ga_grow(&ga, len + 2) == FAIL)
22181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 ret = FAIL;
22184 break;
22185 }
22186 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 ga.ga_len += len;
22190 }
22191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022192 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 arg = skipwhite(arg);
22194 }
22195
22196 if (ret != FAIL && ga.ga_data != NULL)
22197 {
22198 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022201 out_flush();
22202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 else if (eap->cmdidx == CMD_echoerr)
22204 {
22205 /* We don't want to abort following commands, restore did_emsg. */
22206 save_did_emsg = did_emsg;
22207 EMSG((char_u *)ga.ga_data);
22208 if (!force_abort)
22209 did_emsg = save_did_emsg;
22210 }
22211 else if (eap->cmdidx == CMD_execute)
22212 do_cmdline((char_u *)ga.ga_data,
22213 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22214 }
22215
22216 ga_clear(&ga);
22217
22218 if (eap->skip)
22219 --emsg_skip;
22220
22221 eap->nextcmd = check_nextcmd(arg);
22222}
22223
22224/*
22225 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22226 * "arg" points to the "&" or '+' when called, to "option" when returning.
22227 * Returns NULL when no option name found. Otherwise pointer to the char
22228 * after the option name.
22229 */
22230 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022231find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232{
22233 char_u *p = *arg;
22234
22235 ++p;
22236 if (*p == 'g' && p[1] == ':')
22237 {
22238 *opt_flags = OPT_GLOBAL;
22239 p += 2;
22240 }
22241 else if (*p == 'l' && p[1] == ':')
22242 {
22243 *opt_flags = OPT_LOCAL;
22244 p += 2;
22245 }
22246 else
22247 *opt_flags = 0;
22248
22249 if (!ASCII_ISALPHA(*p))
22250 return NULL;
22251 *arg = p;
22252
22253 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22254 p += 4; /* termcap option */
22255 else
22256 while (ASCII_ISALPHA(*p))
22257 ++p;
22258 return p;
22259}
22260
22261/*
22262 * ":function"
22263 */
22264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022265ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266{
22267 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022268 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 int j;
22270 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022272 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 char_u *name = NULL;
22274 char_u *p;
22275 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022276 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 garray_T newargs;
22278 garray_T newlines;
22279 int varargs = FALSE;
22280 int mustend = FALSE;
22281 int flags = 0;
22282 ufunc_T *fp;
22283 int indent;
22284 int nesting;
22285 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 dictitem_T *v;
22287 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022288 static int func_nr = 0; /* number for nameless function */
22289 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022290 hashtab_T *ht;
22291 int todo;
22292 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022293 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294
22295 /*
22296 * ":function" without argument: list functions.
22297 */
22298 if (ends_excmd(*eap->arg))
22299 {
22300 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022301 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022302 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022303 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022304 {
22305 if (!HASHITEM_EMPTY(hi))
22306 {
22307 --todo;
22308 fp = HI2UF(hi);
22309 if (!isdigit(*fp->uf_name))
22310 list_func_head(fp, FALSE);
22311 }
22312 }
22313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 eap->nextcmd = check_nextcmd(eap->arg);
22315 return;
22316 }
22317
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022318 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022319 * ":function /pat": list functions matching pattern.
22320 */
22321 if (*eap->arg == '/')
22322 {
22323 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22324 if (!eap->skip)
22325 {
22326 regmatch_T regmatch;
22327
22328 c = *p;
22329 *p = NUL;
22330 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22331 *p = c;
22332 if (regmatch.regprog != NULL)
22333 {
22334 regmatch.rm_ic = p_ic;
22335
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022336 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022337 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22338 {
22339 if (!HASHITEM_EMPTY(hi))
22340 {
22341 --todo;
22342 fp = HI2UF(hi);
22343 if (!isdigit(*fp->uf_name)
22344 && vim_regexec(&regmatch, fp->uf_name, 0))
22345 list_func_head(fp, FALSE);
22346 }
22347 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022348 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022349 }
22350 }
22351 if (*p == '/')
22352 ++p;
22353 eap->nextcmd = check_nextcmd(p);
22354 return;
22355 }
22356
22357 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022358 * Get the function name. There are these situations:
22359 * func normal function name
22360 * "name" == func, "fudi.fd_dict" == NULL
22361 * dict.func new dictionary entry
22362 * "name" == NULL, "fudi.fd_dict" set,
22363 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22364 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022365 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022366 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22367 * dict.func existing dict entry that's not a Funcref
22368 * "name" == NULL, "fudi.fd_dict" set,
22369 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022370 * s:func script-local function name
22371 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022374 name = trans_function_name(&p, eap->skip, 0, &fudi);
22375 paren = (vim_strchr(p, '(') != NULL);
22376 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 {
22378 /*
22379 * Return on an invalid expression in braces, unless the expression
22380 * evaluation has been cancelled due to an aborting error, an
22381 * interrupt, or an exception.
22382 */
22383 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022384 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022385 if (!eap->skip && fudi.fd_newkey != NULL)
22386 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 else
22391 eap->skip = TRUE;
22392 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022393
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 /* An error in a function call during evaluation of an expression in magic
22395 * braces should not cause the function not to be defined. */
22396 saved_did_emsg = did_emsg;
22397 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398
22399 /*
22400 * ":function func" with only function name: list function.
22401 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022402 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 {
22404 if (!ends_excmd(*skipwhite(p)))
22405 {
22406 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 }
22409 eap->nextcmd = check_nextcmd(p);
22410 if (eap->nextcmd != NULL)
22411 *p = NUL;
22412 if (!eap->skip && !got_int)
22413 {
22414 fp = find_func(name);
22415 if (fp != NULL)
22416 {
22417 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022418 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022420 if (FUNCLINE(fp, j) == NULL)
22421 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 msg_putchar('\n');
22423 msg_outnum((long)(j + 1));
22424 if (j < 9)
22425 msg_putchar(' ');
22426 if (j < 99)
22427 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022428 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 out_flush(); /* show a line at a time */
22430 ui_breakcheck();
22431 }
22432 if (!got_int)
22433 {
22434 msg_putchar('\n');
22435 msg_puts((char_u *)" endfunction");
22436 }
22437 }
22438 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022439 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022441 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 }
22443
22444 /*
22445 * ":function name(arg1, arg2)" Define function.
22446 */
22447 p = skipwhite(p);
22448 if (*p != '(')
22449 {
22450 if (!eap->skip)
22451 {
22452 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022453 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 }
22455 /* attempt to continue by skipping some text */
22456 if (vim_strchr(p, '(') != NULL)
22457 p = vim_strchr(p, '(');
22458 }
22459 p = skipwhite(p + 1);
22460
22461 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22462 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22463
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022464 if (!eap->skip)
22465 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022466 /* Check the name of the function. Unless it's a dictionary function
22467 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022468 if (name != NULL)
22469 arg = name;
22470 else
22471 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022472 if (arg != NULL && (fudi.fd_di == NULL
22473 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022474 {
22475 if (*arg == K_SPECIAL)
22476 j = 3;
22477 else
22478 j = 0;
22479 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22480 : eval_isnamec(arg[j])))
22481 ++j;
22482 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022483 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022484 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022485 /* Disallow using the g: dict. */
22486 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22487 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022488 }
22489
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 /*
22491 * Isolate the arguments: "arg1, arg2, ...)"
22492 */
22493 while (*p != ')')
22494 {
22495 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22496 {
22497 varargs = TRUE;
22498 p += 3;
22499 mustend = TRUE;
22500 }
22501 else
22502 {
22503 arg = p;
22504 while (ASCII_ISALNUM(*p) || *p == '_')
22505 ++p;
22506 if (arg == p || isdigit(*arg)
22507 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22508 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22509 {
22510 if (!eap->skip)
22511 EMSG2(_("E125: Illegal argument: %s"), arg);
22512 break;
22513 }
22514 if (ga_grow(&newargs, 1) == FAIL)
22515 goto erret;
22516 c = *p;
22517 *p = NUL;
22518 arg = vim_strsave(arg);
22519 if (arg == NULL)
22520 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022521
22522 /* Check for duplicate argument name. */
22523 for (i = 0; i < newargs.ga_len; ++i)
22524 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22525 {
22526 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022527 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022528 goto erret;
22529 }
22530
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22532 *p = c;
22533 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 if (*p == ',')
22535 ++p;
22536 else
22537 mustend = TRUE;
22538 }
22539 p = skipwhite(p);
22540 if (mustend && *p != ')')
22541 {
22542 if (!eap->skip)
22543 EMSG2(_(e_invarg2), eap->arg);
22544 break;
22545 }
22546 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022547 if (*p != ')')
22548 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 ++p; /* skip the ')' */
22550
Bram Moolenaare9a41262005-01-15 22:18:47 +000022551 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 for (;;)
22553 {
22554 p = skipwhite(p);
22555 if (STRNCMP(p, "range", 5) == 0)
22556 {
22557 flags |= FC_RANGE;
22558 p += 5;
22559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022560 else if (STRNCMP(p, "dict", 4) == 0)
22561 {
22562 flags |= FC_DICT;
22563 p += 4;
22564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 else if (STRNCMP(p, "abort", 5) == 0)
22566 {
22567 flags |= FC_ABORT;
22568 p += 5;
22569 }
22570 else
22571 break;
22572 }
22573
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022574 /* When there is a line break use what follows for the function body.
22575 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22576 if (*p == '\n')
22577 line_arg = p + 1;
22578 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 EMSG(_(e_trailing));
22580
22581 /*
22582 * Read the body of the function, until ":endfunction" is found.
22583 */
22584 if (KeyTyped)
22585 {
22586 /* Check if the function already exists, don't let the user type the
22587 * whole function before telling him it doesn't work! For a script we
22588 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022589 if (!eap->skip && !eap->forceit)
22590 {
22591 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22592 EMSG(_(e_funcdict));
22593 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022594 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022597 if (!eap->skip && did_emsg)
22598 goto erret;
22599
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 msg_putchar('\n'); /* don't overwrite the function name */
22601 cmdline_row = msg_row;
22602 }
22603
22604 indent = 2;
22605 nesting = 0;
22606 for (;;)
22607 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022608 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022609 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022610 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022611 saved_wait_return = FALSE;
22612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022614 sourcing_lnum_off = sourcing_lnum;
22615
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022616 if (line_arg != NULL)
22617 {
22618 /* Use eap->arg, split up in parts by line breaks. */
22619 theline = line_arg;
22620 p = vim_strchr(theline, '\n');
22621 if (p == NULL)
22622 line_arg += STRLEN(line_arg);
22623 else
22624 {
22625 *p = NUL;
22626 line_arg = p + 1;
22627 }
22628 }
22629 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 theline = getcmdline(':', 0L, indent);
22631 else
22632 theline = eap->getline(':', eap->cookie, indent);
22633 if (KeyTyped)
22634 lines_left = Rows - 1;
22635 if (theline == NULL)
22636 {
22637 EMSG(_("E126: Missing :endfunction"));
22638 goto erret;
22639 }
22640
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022641 /* Detect line continuation: sourcing_lnum increased more than one. */
22642 if (sourcing_lnum > sourcing_lnum_off + 1)
22643 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22644 else
22645 sourcing_lnum_off = 0;
22646
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 if (skip_until != NULL)
22648 {
22649 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22650 * don't check for ":endfunc". */
22651 if (STRCMP(theline, skip_until) == 0)
22652 {
22653 vim_free(skip_until);
22654 skip_until = NULL;
22655 }
22656 }
22657 else
22658 {
22659 /* skip ':' and blanks*/
22660 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22661 ;
22662
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022663 /* Check for "endfunction". */
22664 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022666 if (line_arg == NULL)
22667 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 break;
22669 }
22670
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022671 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 * at "end". */
22673 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22674 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022675 else if (STRNCMP(p, "if", 2) == 0
22676 || STRNCMP(p, "wh", 2) == 0
22677 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 || STRNCMP(p, "try", 3) == 0)
22679 indent += 2;
22680
22681 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022682 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022684 if (*p == '!')
22685 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022687 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22688 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022690 ++nesting;
22691 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 }
22693 }
22694
22695 /* Check for ":append" or ":insert". */
22696 p = skip_range(p, NULL);
22697 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22698 || (p[0] == 'i'
22699 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22700 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22701 skip_until = vim_strsave((char_u *)".");
22702
22703 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22704 arg = skipwhite(skiptowhite(p));
22705 if (arg[0] == '<' && arg[1] =='<'
22706 && ((p[0] == 'p' && p[1] == 'y'
22707 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22708 || (p[0] == 'p' && p[1] == 'e'
22709 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22710 || (p[0] == 't' && p[1] == 'c'
22711 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022712 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22713 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22715 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022716 || (p[0] == 'm' && p[1] == 'z'
22717 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 ))
22719 {
22720 /* ":python <<" continues until a dot, like ":append" */
22721 p = skipwhite(arg + 2);
22722 if (*p == NUL)
22723 skip_until = vim_strsave((char_u *)".");
22724 else
22725 skip_until = vim_strsave(p);
22726 }
22727 }
22728
22729 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022730 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022731 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022732 if (line_arg == NULL)
22733 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022735 }
22736
22737 /* Copy the line to newly allocated memory. get_one_sourceline()
22738 * allocates 250 bytes per line, this saves 80% on average. The cost
22739 * is an extra alloc/free. */
22740 p = vim_strsave(theline);
22741 if (p != NULL)
22742 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022743 if (line_arg == NULL)
22744 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022745 theline = p;
22746 }
22747
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022748 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22749
22750 /* Add NULL lines for continuation lines, so that the line count is
22751 * equal to the index in the growarray. */
22752 while (sourcing_lnum_off-- > 0)
22753 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022754
22755 /* Check for end of eap->arg. */
22756 if (line_arg != NULL && *line_arg == NUL)
22757 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 }
22759
22760 /* Don't define the function when skipping commands or when an error was
22761 * detected. */
22762 if (eap->skip || did_emsg)
22763 goto erret;
22764
22765 /*
22766 * If there are no errors, add the function
22767 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022768 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022769 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022770 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022772 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022773 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022774 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022775 goto erret;
22776 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022777
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022778 fp = find_func(name);
22779 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 if (!eap->forceit)
22782 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022783 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 goto erret;
22785 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022787 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022788 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 name);
22790 goto erret;
22791 }
22792 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022793 ga_clear_strings(&(fp->uf_args));
22794 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022795 vim_free(name);
22796 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 }
22799 else
22800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022801 char numbuf[20];
22802
22803 fp = NULL;
22804 if (fudi.fd_newkey == NULL && !eap->forceit)
22805 {
22806 EMSG(_(e_funcdict));
22807 goto erret;
22808 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022809 if (fudi.fd_di == NULL)
22810 {
22811 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022812 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022813 goto erret;
22814 }
22815 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022816 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022817 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022818
22819 /* Give the function a sequential number. Can only be used with a
22820 * Funcref! */
22821 vim_free(name);
22822 sprintf(numbuf, "%d", ++func_nr);
22823 name = vim_strsave((char_u *)numbuf);
22824 if (name == NULL)
22825 goto erret;
22826 }
22827
22828 if (fp == NULL)
22829 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022830 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022831 {
22832 int slen, plen;
22833 char_u *scriptname;
22834
22835 /* Check that the autoload name matches the script name. */
22836 j = FAIL;
22837 if (sourcing_name != NULL)
22838 {
22839 scriptname = autoload_name(name);
22840 if (scriptname != NULL)
22841 {
22842 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022843 plen = (int)STRLEN(p);
22844 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022845 if (slen > plen && fnamecmp(p,
22846 sourcing_name + slen - plen) == 0)
22847 j = OK;
22848 vim_free(scriptname);
22849 }
22850 }
22851 if (j == FAIL)
22852 {
22853 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22854 goto erret;
22855 }
22856 }
22857
22858 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 if (fp == NULL)
22860 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022861
22862 if (fudi.fd_dict != NULL)
22863 {
22864 if (fudi.fd_di == NULL)
22865 {
22866 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022867 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022868 if (fudi.fd_di == NULL)
22869 {
22870 vim_free(fp);
22871 goto erret;
22872 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022873 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22874 {
22875 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022876 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022877 goto erret;
22878 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022879 }
22880 else
22881 /* overwrite existing dict entry */
22882 clear_tv(&fudi.fd_di->di_tv);
22883 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022884 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022885 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022886 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022887
22888 /* behave like "dict" was used */
22889 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022890 }
22891
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022893 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022894 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22895 {
22896 vim_free(fp);
22897 goto erret;
22898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022900 fp->uf_args = newargs;
22901 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022902#ifdef FEAT_PROFILE
22903 fp->uf_tml_count = NULL;
22904 fp->uf_tml_total = NULL;
22905 fp->uf_tml_self = NULL;
22906 fp->uf_profiling = FALSE;
22907 if (prof_def_func())
22908 func_do_profile(fp);
22909#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022910 fp->uf_varargs = varargs;
22911 fp->uf_flags = flags;
22912 fp->uf_calls = 0;
22913 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022914 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915
22916erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 ga_clear_strings(&newargs);
22918 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022919ret_free:
22920 vim_free(skip_until);
22921 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022924 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925}
22926
22927/*
22928 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022929 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022932 * TFN_INT: internal function name OK
22933 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022934 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 * Advances "pp" to just after the function name (if no error).
22936 */
22937 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022938trans_function_name(
22939 char_u **pp,
22940 int skip, /* only find the end, don't evaluate */
22941 int flags,
22942 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022944 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 char_u *start;
22946 char_u *end;
22947 int lead;
22948 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022951
22952 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022955
22956 /* Check for hard coded <SNR>: already translated function ID (from a user
22957 * command). */
22958 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22959 && (*pp)[2] == (int)KE_SNR)
22960 {
22961 *pp += 3;
22962 len = get_id_len(pp) + 3;
22963 return vim_strnsave(start, len);
22964 }
22965
22966 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22967 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022969 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022971
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022972 /* Note that TFN_ flags use the same values as GLV_ flags. */
22973 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022974 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022975 if (end == start)
22976 {
22977 if (!skip)
22978 EMSG(_("E129: Function name required"));
22979 goto theend;
22980 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022981 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022982 {
22983 /*
22984 * Report an invalid expression in braces, unless the expression
22985 * evaluation has been cancelled due to an aborting error, an
22986 * interrupt, or an exception.
22987 */
22988 if (!aborting())
22989 {
22990 if (end != NULL)
22991 EMSG2(_(e_invarg2), start);
22992 }
22993 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022994 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022995 goto theend;
22996 }
22997
22998 if (lv.ll_tv != NULL)
22999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023000 if (fdp != NULL)
23001 {
23002 fdp->fd_dict = lv.ll_dict;
23003 fdp->fd_newkey = lv.ll_newkey;
23004 lv.ll_newkey = NULL;
23005 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023007 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23008 {
23009 name = vim_strsave(lv.ll_tv->vval.v_string);
23010 *pp = end;
23011 }
23012 else
23013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023014 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23015 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023016 EMSG(_(e_funcref));
23017 else
23018 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023019 name = NULL;
23020 }
23021 goto theend;
23022 }
23023
23024 if (lv.ll_name == NULL)
23025 {
23026 /* Error found, but continue after the function name. */
23027 *pp = end;
23028 goto theend;
23029 }
23030
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023031 /* Check if the name is a Funcref. If so, use the value. */
23032 if (lv.ll_exp_name != NULL)
23033 {
23034 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023035 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023036 if (name == lv.ll_exp_name)
23037 name = NULL;
23038 }
23039 else
23040 {
23041 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023042 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023043 if (name == *pp)
23044 name = NULL;
23045 }
23046 if (name != NULL)
23047 {
23048 name = vim_strsave(name);
23049 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023050 if (STRNCMP(name, "<SNR>", 5) == 0)
23051 {
23052 /* Change "<SNR>" to the byte sequence. */
23053 name[0] = K_SPECIAL;
23054 name[1] = KS_EXTRA;
23055 name[2] = (int)KE_SNR;
23056 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23057 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023058 goto theend;
23059 }
23060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023061 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023062 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023063 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023064 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23065 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23066 {
23067 /* When there was "s:" already or the name expanded to get a
23068 * leading "s:" then remove it. */
23069 lv.ll_name += 2;
23070 len -= 2;
23071 lead = 2;
23072 }
23073 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023074 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023075 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023076 /* skip over "s:" and "g:" */
23077 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023078 lv.ll_name += 2;
23079 len = (int)(end - lv.ll_name);
23080 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023081
23082 /*
23083 * Copy the function name to allocated memory.
23084 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23085 * Accept <SNR>123_name() outside a script.
23086 */
23087 if (skip)
23088 lead = 0; /* do nothing */
23089 else if (lead > 0)
23090 {
23091 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023092 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23093 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023094 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023095 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023096 if (current_SID <= 0)
23097 {
23098 EMSG(_(e_usingsid));
23099 goto theend;
23100 }
23101 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23102 lead += (int)STRLEN(sid_buf);
23103 }
23104 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023105 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023106 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023107 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023108 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023109 goto theend;
23110 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023111 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023112 {
23113 char_u *cp = vim_strchr(lv.ll_name, ':');
23114
23115 if (cp != NULL && cp < end)
23116 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023117 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023118 goto theend;
23119 }
23120 }
23121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023122 name = alloc((unsigned)(len + lead + 1));
23123 if (name != NULL)
23124 {
23125 if (lead > 0)
23126 {
23127 name[0] = K_SPECIAL;
23128 name[1] = KS_EXTRA;
23129 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023130 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023131 STRCPY(name + 3, sid_buf);
23132 }
23133 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023134 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023135 }
23136 *pp = end;
23137
23138theend:
23139 clear_lval(&lv);
23140 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141}
23142
23143/*
23144 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23145 * Return 2 if "p" starts with "s:".
23146 * Return 0 otherwise.
23147 */
23148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023149eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023151 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23152 * the standard library function. */
23153 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23154 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 return 5;
23156 if (p[0] == 's' && p[1] == ':')
23157 return 2;
23158 return 0;
23159}
23160
23161/*
23162 * Return TRUE if "p" starts with "<SID>" or "s:".
23163 * Only works if eval_fname_script() returned non-zero for "p"!
23164 */
23165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023166eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167{
23168 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23169}
23170
23171/*
23172 * List the head of the function: "name(arg1, arg2)".
23173 */
23174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023175list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176{
23177 int j;
23178
23179 msg_start();
23180 if (indent)
23181 MSG_PUTS(" ");
23182 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023183 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 {
23185 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023186 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 }
23188 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023191 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 {
23193 if (j)
23194 MSG_PUTS(", ");
23195 msg_puts(FUNCARG(fp, j));
23196 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023197 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 {
23199 if (j)
23200 MSG_PUTS(", ");
23201 MSG_PUTS("...");
23202 }
23203 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023204 if (fp->uf_flags & FC_ABORT)
23205 MSG_PUTS(" abort");
23206 if (fp->uf_flags & FC_RANGE)
23207 MSG_PUTS(" range");
23208 if (fp->uf_flags & FC_DICT)
23209 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023210 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023211 if (p_verbose > 0)
23212 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213}
23214
23215/*
23216 * Find a function by name, return pointer to it in ufuncs.
23217 * Return NULL for unknown function.
23218 */
23219 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023220find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023222 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023224 hi = hash_find(&func_hashtab, name);
23225 if (!HASHITEM_EMPTY(hi))
23226 return HI2UF(hi);
23227 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228}
23229
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023230#if defined(EXITFREE) || defined(PROTO)
23231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023233{
23234 hashitem_T *hi;
23235
23236 /* Need to start all over every time, because func_free() may change the
23237 * hash table. */
23238 while (func_hashtab.ht_used > 0)
23239 for (hi = func_hashtab.ht_array; ; ++hi)
23240 if (!HASHITEM_EMPTY(hi))
23241 {
23242 func_free(HI2UF(hi));
23243 break;
23244 }
23245}
23246#endif
23247
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023249translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023250{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023251 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023252 return find_internal_func(name) >= 0;
23253 return find_func(name) != NULL;
23254}
23255
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023256/*
23257 * Return TRUE if a function "name" exists.
23258 */
23259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023260function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023261{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023262 char_u *nm = name;
23263 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023264 int n = FALSE;
23265
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023266 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23267 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023268 nm = skipwhite(nm);
23269
23270 /* Only accept "funcname", "funcname ", "funcname (..." and
23271 * "funcname(...", not "funcname!...". */
23272 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023273 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023274 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023275 return n;
23276}
23277
Bram Moolenaara1544c02013-05-30 12:35:52 +020023278 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023279get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023280{
23281 char_u *nm = name;
23282 char_u *p;
23283
23284 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23285
23286 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023287 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023288 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023289
Bram Moolenaara1544c02013-05-30 12:35:52 +020023290 vim_free(p);
23291 return NULL;
23292}
23293
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023294/*
23295 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023296 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23297 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023298 */
23299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023300builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023301{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023302 char_u *p;
23303
23304 if (!ASCII_ISLOWER(name[0]))
23305 return FALSE;
23306 p = vim_strchr(name, AUTOLOAD_CHAR);
23307 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023308}
23309
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310#if defined(FEAT_PROFILE) || defined(PROTO)
23311/*
23312 * Start profiling function "fp".
23313 */
23314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023315func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023316{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023317 int len = fp->uf_lines.ga_len;
23318
23319 if (len == 0)
23320 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023321 fp->uf_tm_count = 0;
23322 profile_zero(&fp->uf_tm_self);
23323 profile_zero(&fp->uf_tm_total);
23324 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023325 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023326 if (fp->uf_tml_total == NULL)
23327 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023328 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023329 if (fp->uf_tml_self == NULL)
23330 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
23333 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23334 || fp->uf_tml_self == NULL)
23335 return; /* out of memory */
23336
23337 fp->uf_profiling = TRUE;
23338}
23339
23340/*
23341 * Dump the profiling results for all functions in file "fd".
23342 */
23343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023344func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023345{
23346 hashitem_T *hi;
23347 int todo;
23348 ufunc_T *fp;
23349 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023350 ufunc_T **sorttab;
23351 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023353 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023354 if (todo == 0)
23355 return; /* nothing to dump */
23356
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023357 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023358
Bram Moolenaar05159a02005-02-26 23:04:13 +000023359 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23360 {
23361 if (!HASHITEM_EMPTY(hi))
23362 {
23363 --todo;
23364 fp = HI2UF(hi);
23365 if (fp->uf_profiling)
23366 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023367 if (sorttab != NULL)
23368 sorttab[st_len++] = fp;
23369
Bram Moolenaar05159a02005-02-26 23:04:13 +000023370 if (fp->uf_name[0] == K_SPECIAL)
23371 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23372 else
23373 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23374 if (fp->uf_tm_count == 1)
23375 fprintf(fd, "Called 1 time\n");
23376 else
23377 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23378 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23379 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23380 fprintf(fd, "\n");
23381 fprintf(fd, "count total (s) self (s)\n");
23382
23383 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23384 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023385 if (FUNCLINE(fp, i) == NULL)
23386 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023387 prof_func_line(fd, fp->uf_tml_count[i],
23388 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023389 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23390 }
23391 fprintf(fd, "\n");
23392 }
23393 }
23394 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023395
23396 if (sorttab != NULL && st_len > 0)
23397 {
23398 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23399 prof_total_cmp);
23400 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23401 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23402 prof_self_cmp);
23403 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23404 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023405
23406 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023407}
Bram Moolenaar73830342005-02-28 22:48:19 +000023408
23409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023410prof_sort_list(
23411 FILE *fd,
23412 ufunc_T **sorttab,
23413 int st_len,
23414 char *title,
23415 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023416{
23417 int i;
23418 ufunc_T *fp;
23419
23420 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23421 fprintf(fd, "count total (s) self (s) function\n");
23422 for (i = 0; i < 20 && i < st_len; ++i)
23423 {
23424 fp = sorttab[i];
23425 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23426 prefer_self);
23427 if (fp->uf_name[0] == K_SPECIAL)
23428 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23429 else
23430 fprintf(fd, " %s()\n", fp->uf_name);
23431 }
23432 fprintf(fd, "\n");
23433}
23434
23435/*
23436 * Print the count and times for one function or function line.
23437 */
23438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023439prof_func_line(
23440 FILE *fd,
23441 int count,
23442 proftime_T *total,
23443 proftime_T *self,
23444 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023445{
23446 if (count > 0)
23447 {
23448 fprintf(fd, "%5d ", count);
23449 if (prefer_self && profile_equal(total, self))
23450 fprintf(fd, " ");
23451 else
23452 fprintf(fd, "%s ", profile_msg(total));
23453 if (!prefer_self && profile_equal(total, self))
23454 fprintf(fd, " ");
23455 else
23456 fprintf(fd, "%s ", profile_msg(self));
23457 }
23458 else
23459 fprintf(fd, " ");
23460}
23461
23462/*
23463 * Compare function for total time sorting.
23464 */
23465 static int
23466#ifdef __BORLANDC__
23467_RTLENTRYF
23468#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023469prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023470{
23471 ufunc_T *p1, *p2;
23472
23473 p1 = *(ufunc_T **)s1;
23474 p2 = *(ufunc_T **)s2;
23475 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23476}
23477
23478/*
23479 * Compare function for self time sorting.
23480 */
23481 static int
23482#ifdef __BORLANDC__
23483_RTLENTRYF
23484#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023485prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023486{
23487 ufunc_T *p1, *p2;
23488
23489 p1 = *(ufunc_T **)s1;
23490 p2 = *(ufunc_T **)s2;
23491 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23492}
23493
Bram Moolenaar05159a02005-02-26 23:04:13 +000023494#endif
23495
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023496/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023498 * Return TRUE if a package was loaded.
23499 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023501script_autoload(
23502 char_u *name,
23503 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023504{
23505 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023506 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023507 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023508 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023509
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023510 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023511 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023512 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023513 return FALSE;
23514
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023515 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023516
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023517 /* Find the name in the list of previously loaded package names. Skip
23518 * "autoload/", it's always the same. */
23519 for (i = 0; i < ga_loaded.ga_len; ++i)
23520 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23521 break;
23522 if (!reload && i < ga_loaded.ga_len)
23523 ret = FALSE; /* was loaded already */
23524 else
23525 {
23526 /* Remember the name if it wasn't loaded already. */
23527 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23528 {
23529 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23530 tofree = NULL;
23531 }
23532
23533 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023534 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023535 ret = TRUE;
23536 }
23537
23538 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023539 return ret;
23540}
23541
23542/*
23543 * Return the autoload script name for a function or variable name.
23544 * Returns NULL when out of memory.
23545 */
23546 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023547autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548{
23549 char_u *p;
23550 char_u *scriptname;
23551
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023552 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023553 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23554 if (scriptname == NULL)
23555 return FALSE;
23556 STRCPY(scriptname, "autoload/");
23557 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023558 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023559 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023560 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023561 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023563}
23564
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23566
23567/*
23568 * Function given to ExpandGeneric() to obtain the list of user defined
23569 * function names.
23570 */
23571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023572get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 static long_u done;
23575 static hashitem_T *hi;
23576 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577
23578 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023580 done = 0;
23581 hi = func_hashtab.ht_array;
23582 }
23583 if (done < func_hashtab.ht_used)
23584 {
23585 if (done++ > 0)
23586 ++hi;
23587 while (HASHITEM_EMPTY(hi))
23588 ++hi;
23589 fp = HI2UF(hi);
23590
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023591 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023592 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023593
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023594 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23595 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596
23597 cat_func_name(IObuff, fp);
23598 if (xp->xp_context != EXPAND_USER_FUNC)
23599 {
23600 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023601 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 STRCAT(IObuff, ")");
23603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 return IObuff;
23605 }
23606 return NULL;
23607}
23608
23609#endif /* FEAT_CMDL_COMPL */
23610
23611/*
23612 * Copy the function name of "fp" to buffer "buf".
23613 * "buf" must be able to hold the function name plus three bytes.
23614 * Takes care of script-local function names.
23615 */
23616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023617cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023619 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 {
23621 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023622 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 }
23624 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023625 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626}
23627
23628/*
23629 * ":delfunction {name}"
23630 */
23631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023632ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023634 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 char_u *p;
23636 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023637 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638
23639 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 name = trans_function_name(&p, eap->skip, 0, &fudi);
23641 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 {
23644 if (fudi.fd_dict != NULL && !eap->skip)
23645 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 if (!ends_excmd(*skipwhite(p)))
23649 {
23650 vim_free(name);
23651 EMSG(_(e_trailing));
23652 return;
23653 }
23654 eap->nextcmd = check_nextcmd(p);
23655 if (eap->nextcmd != NULL)
23656 *p = NUL;
23657
23658 if (!eap->skip)
23659 fp = find_func(name);
23660 vim_free(name);
23661
23662 if (!eap->skip)
23663 {
23664 if (fp == NULL)
23665 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023666 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 return;
23668 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 {
23671 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23672 return;
23673 }
23674
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 /* Delete the dict item that refers to the function, it will
23678 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023679 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 else
23682 func_free(fp);
23683 }
23684}
23685
23686/*
23687 * Free a function and remove it from the list of functions.
23688 */
23689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023690func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023691{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023692 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023693
23694 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023695 ga_clear_strings(&(fp->uf_args));
23696 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023697#ifdef FEAT_PROFILE
23698 vim_free(fp->uf_tml_count);
23699 vim_free(fp->uf_tml_total);
23700 vim_free(fp->uf_tml_self);
23701#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023702
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023703 /* remove the function from the function hashtable */
23704 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23705 if (HASHITEM_EMPTY(hi))
23706 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023707 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023708 hash_remove(&func_hashtab, hi);
23709
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 vim_free(fp);
23711}
23712
23713/*
23714 * Unreference a Function: decrement the reference count and free it when it
23715 * becomes zero. Only for numbered functions.
23716 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023718func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023719{
23720 ufunc_T *fp;
23721
23722 if (name != NULL && isdigit(*name))
23723 {
23724 fp = find_func(name);
23725 if (fp == NULL)
23726 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023727 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 {
23729 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 * when "uf_calls" becomes zero. */
23731 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023732 func_free(fp);
23733 }
23734 }
23735}
23736
23737/*
23738 * Count a reference to a Function.
23739 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023741func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023742{
23743 ufunc_T *fp;
23744
23745 if (name != NULL && isdigit(*name))
23746 {
23747 fp = find_func(name);
23748 if (fp == NULL)
23749 EMSG2(_(e_intern2), "func_ref()");
23750 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023751 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 }
23753}
23754
23755/*
23756 * Call a user function.
23757 */
23758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023759call_user_func(
23760 ufunc_T *fp, /* pointer to function */
23761 int argcount, /* nr of args */
23762 typval_T *argvars, /* arguments */
23763 typval_T *rettv, /* return value */
23764 linenr_T firstline, /* first line of range */
23765 linenr_T lastline, /* last line of range */
23766 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767{
Bram Moolenaar33570922005-01-25 22:26:29 +000023768 char_u *save_sourcing_name;
23769 linenr_T save_sourcing_lnum;
23770 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023771 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023772 int save_did_emsg;
23773 static int depth = 0;
23774 dictitem_T *v;
23775 int fixvar_idx = 0; /* index in fixvar[] */
23776 int i;
23777 int ai;
23778 char_u numbuf[NUMBUFLEN];
23779 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023780 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023781#ifdef FEAT_PROFILE
23782 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023783 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785
23786 /* If depth of calling is getting too high, don't execute the function */
23787 if (depth >= p_mfd)
23788 {
23789 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023790 rettv->v_type = VAR_NUMBER;
23791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 return;
23793 }
23794 ++depth;
23795
23796 line_breakcheck(); /* check for CTRL-C hit */
23797
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023798 fc = (funccall_T *)alloc(sizeof(funccall_T));
23799 fc->caller = current_funccal;
23800 current_funccal = fc;
23801 fc->func = fp;
23802 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023803 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023804 fc->linenr = 0;
23805 fc->returned = FALSE;
23806 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023808 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23809 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810
Bram Moolenaar33570922005-01-25 22:26:29 +000023811 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023812 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023813 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23814 * each argument variable and saves a lot of time.
23815 */
23816 /*
23817 * Init l: variables.
23818 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023819 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023820 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023821 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023822 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23823 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023824 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023825 name = v->di_key;
23826 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023827 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023828 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023829 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023830 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023831 v->di_tv.vval.v_dict = selfdict;
23832 ++selfdict->dv_refcount;
23833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023834
Bram Moolenaar33570922005-01-25 22:26:29 +000023835 /*
23836 * Init a: variables.
23837 * Set a:0 to "argcount".
23838 * Set a:000 to a list with room for the "..." arguments.
23839 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023840 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023841 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023842 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023843 /* Use "name" to avoid a warning from some compiler that checks the
23844 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023845 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023846 name = v->di_key;
23847 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023849 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023850 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023851 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023852 v->di_tv.vval.v_list = &fc->l_varlist;
23853 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23854 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23855 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023856
23857 /*
23858 * Set a:firstline to "firstline" and a:lastline to "lastline".
23859 * Set a:name to named arguments.
23860 * Set a:N to the "..." arguments.
23861 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023862 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023864 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023865 (varnumber_T)lastline);
23866 for (i = 0; i < argcount; ++i)
23867 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023868 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023869 if (ai < 0)
23870 /* named argument a:name */
23871 name = FUNCARG(fp, i);
23872 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023873 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023874 /* "..." argument a:1, a:2, etc. */
23875 sprintf((char *)numbuf, "%d", ai + 1);
23876 name = numbuf;
23877 }
23878 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23879 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023880 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023881 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23882 }
23883 else
23884 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023885 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23886 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023887 if (v == NULL)
23888 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023889 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023890 }
23891 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023892 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023893
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023894 /* Note: the values are copied directly to avoid alloc/free.
23895 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023896 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023897 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023898
23899 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23900 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023901 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23902 fc->l_listitems[ai].li_tv = argvars[i];
23903 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023904 }
23905 }
23906
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 /* Don't redraw while executing the function. */
23908 ++RedrawingDisabled;
23909 save_sourcing_name = sourcing_name;
23910 save_sourcing_lnum = sourcing_lnum;
23911 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023912 /* need space for function name + ("function " + 3) or "[number]" */
23913 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23914 + STRLEN(fp->uf_name) + 20;
23915 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 if (sourcing_name != NULL)
23917 {
23918 if (save_sourcing_name != NULL
23919 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023920 sprintf((char *)sourcing_name, "%s[%d]..",
23921 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 else
23923 STRCPY(sourcing_name, "function ");
23924 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23925
23926 if (p_verbose >= 12)
23927 {
23928 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023929 verbose_enter_scroll();
23930
Bram Moolenaar555b2802005-05-19 21:08:39 +000023931 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932 if (p_verbose >= 14)
23933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023935 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023936 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023937 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938
23939 msg_puts((char_u *)"(");
23940 for (i = 0; i < argcount; ++i)
23941 {
23942 if (i > 0)
23943 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023944 if (argvars[i].v_type == VAR_NUMBER)
23945 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 else
23947 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023948 /* Do not want errors such as E724 here. */
23949 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023950 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023951 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023952 if (s != NULL)
23953 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023954 if (vim_strsize(s) > MSG_BUF_CLEN)
23955 {
23956 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23957 s = buf;
23958 }
23959 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023960 vim_free(tofree);
23961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 }
23963 }
23964 msg_puts((char_u *)")");
23965 }
23966 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023967
23968 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 --no_wait_return;
23970 }
23971 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023972#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023973 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023974 {
23975 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23976 func_do_profile(fp);
23977 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023978 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023979 {
23980 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023981 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023982 profile_zero(&fp->uf_tm_children);
23983 }
23984 script_prof_save(&wait_start);
23985 }
23986#endif
23987
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023989 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 save_did_emsg = did_emsg;
23991 did_emsg = FALSE;
23992
23993 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023994 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23996
23997 --RedrawingDisabled;
23998
23999 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024000 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024002 clear_tv(rettv);
24003 rettv->v_type = VAR_NUMBER;
24004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 }
24006
Bram Moolenaar05159a02005-02-26 23:04:13 +000024007#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024008 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024009 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024010 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024011 profile_end(&call_start);
24012 profile_sub_wait(&wait_start, &call_start);
24013 profile_add(&fp->uf_tm_total, &call_start);
24014 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024015 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024016 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024017 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24018 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024019 }
24020 }
24021#endif
24022
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 /* when being verbose, mention the return value */
24024 if (p_verbose >= 12)
24025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024027 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024030 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024031 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024032 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024033 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024036 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024037 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024038 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024039 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024040
Bram Moolenaar555b2802005-05-19 21:08:39 +000024041 /* The value may be very long. Skip the middle part, so that we
24042 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024043 * truncate it at the end. Don't want errors such as E724 here. */
24044 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024045 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024046 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024047 if (s != NULL)
24048 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024049 if (vim_strsize(s) > MSG_BUF_CLEN)
24050 {
24051 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24052 s = buf;
24053 }
24054 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024055 vim_free(tofree);
24056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 }
24058 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024059
24060 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 --no_wait_return;
24062 }
24063
24064 vim_free(sourcing_name);
24065 sourcing_name = save_sourcing_name;
24066 sourcing_lnum = save_sourcing_lnum;
24067 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024068#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024069 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024070 script_prof_restore(&wait_start);
24071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072
24073 if (p_verbose >= 12 && sourcing_name != NULL)
24074 {
24075 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024076 verbose_enter_scroll();
24077
Bram Moolenaar555b2802005-05-19 21:08:39 +000024078 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024080
24081 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 --no_wait_return;
24083 }
24084
24085 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024086 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024088
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024089 /* If the a:000 list and the l: and a: dicts are not referenced we can
24090 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24092 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24093 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24094 {
24095 free_funccal(fc, FALSE);
24096 }
24097 else
24098 {
24099 hashitem_T *hi;
24100 listitem_T *li;
24101 int todo;
24102
24103 /* "fc" is still in use. This can happen when returning "a:000" or
24104 * assigning "l:" to a global variable.
24105 * Link "fc" in the list for garbage collection later. */
24106 fc->caller = previous_funccal;
24107 previous_funccal = fc;
24108
24109 /* Make a copy of the a: variables, since we didn't do that above. */
24110 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24111 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24112 {
24113 if (!HASHITEM_EMPTY(hi))
24114 {
24115 --todo;
24116 v = HI2DI(hi);
24117 copy_tv(&v->di_tv, &v->di_tv);
24118 }
24119 }
24120
24121 /* Make a copy of the a:000 items, since we didn't do that above. */
24122 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24123 copy_tv(&li->li_tv, &li->li_tv);
24124 }
24125}
24126
24127/*
24128 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024129 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024130 */
24131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024132can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024133{
24134 return (fc->l_varlist.lv_copyID != copyID
24135 && fc->l_vars.dv_copyID != copyID
24136 && fc->l_avars.dv_copyID != copyID);
24137}
24138
24139/*
24140 * Free "fc" and what it contains.
24141 */
24142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024143free_funccal(
24144 funccall_T *fc,
24145 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024146{
24147 listitem_T *li;
24148
24149 /* The a: variables typevals may not have been allocated, only free the
24150 * allocated variables. */
24151 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24152
24153 /* free all l: variables */
24154 vars_clear(&fc->l_vars.dv_hashtab);
24155
24156 /* Free the a:000 variables if they were allocated. */
24157 if (free_val)
24158 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24159 clear_tv(&li->li_tv);
24160
24161 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162}
24163
24164/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024165 * Add a number variable "name" to dict "dp" with value "nr".
24166 */
24167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024168add_nr_var(
24169 dict_T *dp,
24170 dictitem_T *v,
24171 char *name,
24172 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024173{
24174 STRCPY(v->di_key, name);
24175 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24176 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24177 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024178 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024179 v->di_tv.vval.v_number = nr;
24180}
24181
24182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 * ":return [expr]"
24184 */
24185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024186ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187{
24188 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024189 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 int returning = FALSE;
24191
24192 if (current_funccal == NULL)
24193 {
24194 EMSG(_("E133: :return not inside a function"));
24195 return;
24196 }
24197
24198 if (eap->skip)
24199 ++emsg_skip;
24200
24201 eap->nextcmd = NULL;
24202 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024203 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 {
24205 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024206 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024208 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 }
24210 /* It's safer to return also on error. */
24211 else if (!eap->skip)
24212 {
24213 /*
24214 * Return unless the expression evaluation has been cancelled due to an
24215 * aborting error, an interrupt, or an exception.
24216 */
24217 if (!aborting())
24218 returning = do_return(eap, FALSE, TRUE, NULL);
24219 }
24220
24221 /* When skipping or the return gets pending, advance to the next command
24222 * in this line (!returning). Otherwise, ignore the rest of the line.
24223 * Following lines will be ignored by get_func_line(). */
24224 if (returning)
24225 eap->nextcmd = NULL;
24226 else if (eap->nextcmd == NULL) /* no argument */
24227 eap->nextcmd = check_nextcmd(arg);
24228
24229 if (eap->skip)
24230 --emsg_skip;
24231}
24232
24233/*
24234 * Return from a function. Possibly makes the return pending. Also called
24235 * for a pending return at the ":endtry" or after returning from an extra
24236 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024238 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 * FALSE when the return gets pending.
24240 */
24241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024242do_return(
24243 exarg_T *eap,
24244 int reanimate,
24245 int is_cmd,
24246 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247{
24248 int idx;
24249 struct condstack *cstack = eap->cstack;
24250
24251 if (reanimate)
24252 /* Undo the return. */
24253 current_funccal->returned = FALSE;
24254
24255 /*
24256 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24257 * not in its finally clause (which then is to be executed next) is found.
24258 * In this case, make the ":return" pending for execution at the ":endtry".
24259 * Otherwise, return normally.
24260 */
24261 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24262 if (idx >= 0)
24263 {
24264 cstack->cs_pending[idx] = CSTP_RETURN;
24265
24266 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024267 /* A pending return again gets pending. "rettv" points to an
24268 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024270 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 else
24272 {
24273 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024274 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024276 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024278 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 {
24280 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024281 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024282 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 else
24284 EMSG(_(e_outofmem));
24285 }
24286 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024287 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288
24289 if (reanimate)
24290 {
24291 /* The pending return value could be overwritten by a ":return"
24292 * without argument in a finally clause; reset the default
24293 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024294 current_funccal->rettv->v_type = VAR_NUMBER;
24295 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 }
24297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024298 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 }
24300 else
24301 {
24302 current_funccal->returned = TRUE;
24303
24304 /* If the return is carried out now, store the return value. For
24305 * a return immediately after reanimation, the value is already
24306 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024307 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024309 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024310 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024312 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 }
24314 }
24315
24316 return idx < 0;
24317}
24318
24319/*
24320 * Free the variable with a pending return value.
24321 */
24322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024323discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324{
Bram Moolenaar33570922005-01-25 22:26:29 +000024325 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326}
24327
24328/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024329 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 * is an allocated string. Used by report_pending() for verbose messages.
24331 */
24332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024333get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024335 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024336 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024339 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024340 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024341 if (s == NULL)
24342 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024343
24344 STRCPY(IObuff, ":return ");
24345 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24346 if (STRLEN(s) + 8 >= IOSIZE)
24347 STRCPY(IObuff + IOSIZE - 4, "...");
24348 vim_free(tofree);
24349 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350}
24351
24352/*
24353 * Get next function line.
24354 * Called by do_cmdline() to get the next line.
24355 * Returns allocated string, or NULL for end of function.
24356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024358get_func_line(
24359 int c UNUSED,
24360 void *cookie,
24361 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362{
Bram Moolenaar33570922005-01-25 22:26:29 +000024363 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364 ufunc_T *fp = fcp->func;
24365 char_u *retval;
24366 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367
24368 /* If breakpoints have been added/deleted need to check for it. */
24369 if (fcp->dbg_tick != debug_tick)
24370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024371 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 sourcing_lnum);
24373 fcp->dbg_tick = debug_tick;
24374 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024375#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024376 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024377 func_line_end(cookie);
24378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379
Bram Moolenaar05159a02005-02-26 23:04:13 +000024380 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024381 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24382 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 retval = NULL;
24384 else
24385 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024386 /* Skip NULL lines (continuation lines). */
24387 while (fcp->linenr < gap->ga_len
24388 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24389 ++fcp->linenr;
24390 if (fcp->linenr >= gap->ga_len)
24391 retval = NULL;
24392 else
24393 {
24394 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24395 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024396#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024397 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024398 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024399#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 }
24402
24403 /* Did we encounter a breakpoint? */
24404 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24405 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024406 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024408 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 sourcing_lnum);
24410 fcp->dbg_tick = debug_tick;
24411 }
24412
24413 return retval;
24414}
24415
Bram Moolenaar05159a02005-02-26 23:04:13 +000024416#if defined(FEAT_PROFILE) || defined(PROTO)
24417/*
24418 * Called when starting to read a function line.
24419 * "sourcing_lnum" must be correct!
24420 * When skipping lines it may not actually be executed, but we won't find out
24421 * until later and we need to store the time now.
24422 */
24423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024424func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425{
24426 funccall_T *fcp = (funccall_T *)cookie;
24427 ufunc_T *fp = fcp->func;
24428
24429 if (fp->uf_profiling && sourcing_lnum >= 1
24430 && sourcing_lnum <= fp->uf_lines.ga_len)
24431 {
24432 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024433 /* Skip continuation lines. */
24434 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24435 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024436 fp->uf_tml_execed = FALSE;
24437 profile_start(&fp->uf_tml_start);
24438 profile_zero(&fp->uf_tml_children);
24439 profile_get_wait(&fp->uf_tml_wait);
24440 }
24441}
24442
24443/*
24444 * Called when actually executing a function line.
24445 */
24446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024447func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024448{
24449 funccall_T *fcp = (funccall_T *)cookie;
24450 ufunc_T *fp = fcp->func;
24451
24452 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24453 fp->uf_tml_execed = TRUE;
24454}
24455
24456/*
24457 * Called when done with a function line.
24458 */
24459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024460func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024461{
24462 funccall_T *fcp = (funccall_T *)cookie;
24463 ufunc_T *fp = fcp->func;
24464
24465 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24466 {
24467 if (fp->uf_tml_execed)
24468 {
24469 ++fp->uf_tml_count[fp->uf_tml_idx];
24470 profile_end(&fp->uf_tml_start);
24471 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024472 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024473 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24474 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024475 }
24476 fp->uf_tml_idx = -1;
24477 }
24478}
24479#endif
24480
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481/*
24482 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024483 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 */
24485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024486func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487{
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489
24490 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24491 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024492 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 || fcp->returned);
24494}
24495
24496/*
24497 * return TRUE if cookie indicates a function which "abort"s on errors.
24498 */
24499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024500func_has_abort(
24501 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024503 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504}
24505
24506#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24507typedef enum
24508{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024509 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24510 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24511 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512} var_flavour_T;
24513
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024514static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515
24516 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024517var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518{
24519 char_u *p = varname;
24520
24521 if (ASCII_ISUPPER(*p))
24522 {
24523 while (*(++p))
24524 if (ASCII_ISLOWER(*p))
24525 return VAR_FLAVOUR_SESSION;
24526 return VAR_FLAVOUR_VIMINFO;
24527 }
24528 else
24529 return VAR_FLAVOUR_DEFAULT;
24530}
24531#endif
24532
24533#if defined(FEAT_VIMINFO) || defined(PROTO)
24534/*
24535 * Restore global vars that start with a capital from the viminfo file
24536 */
24537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024538read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539{
24540 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024541 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024542 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024543 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544
24545 if (!writing && (find_viminfo_parameter('!') != NULL))
24546 {
24547 tab = vim_strchr(virp->vir_line + 1, '\t');
24548 if (tab != NULL)
24549 {
24550 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024551 switch (*tab)
24552 {
24553 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024554#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024555 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024556#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024557 case 'D': type = VAR_DICT; break;
24558 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024559 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561
24562 tab = vim_strchr(tab, '\t');
24563 if (tab != NULL)
24564 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024565 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024566 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024567 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024569#ifdef FEAT_FLOAT
24570 else if (type == VAR_FLOAT)
24571 (void)string2float(tab + 1, &tv.vval.v_float);
24572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024574 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024575 if (type == VAR_DICT || type == VAR_LIST)
24576 {
24577 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24578
24579 if (etv == NULL)
24580 /* Failed to parse back the dict or list, use it as a
24581 * string. */
24582 tv.v_type = VAR_STRING;
24583 else
24584 {
24585 vim_free(tv.vval.v_string);
24586 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024587 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024588 }
24589 }
24590
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024591 /* when in a function use global variables */
24592 save_funccal = current_funccal;
24593 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024594 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024595 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024596
24597 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024598 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024599 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24600 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 }
24602 }
24603 }
24604
24605 return viminfo_readline(virp);
24606}
24607
24608/*
24609 * Write global vars that start with a capital to the viminfo file
24610 */
24611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024612write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613{
Bram Moolenaar33570922005-01-25 22:26:29 +000024614 hashitem_T *hi;
24615 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024616 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024617 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024618 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024619 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024620 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621
24622 if (find_viminfo_parameter('!') == NULL)
24623 return;
24624
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024625 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024627 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024628 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024630 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024632 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024633 this_var = HI2DI(hi);
24634 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024636 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024637 {
24638 case VAR_STRING: s = "STR"; break;
24639 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024640#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024641 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024642#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024643 case VAR_DICT: s = "DIC"; break;
24644 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024645 case VAR_SPECIAL: s = "XPL"; break;
24646
24647 case VAR_UNKNOWN:
24648 case VAR_FUNC:
24649 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024650 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024651 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024652 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024653 if (p != NULL)
24654 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024655 vim_free(tofree);
24656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 }
24658 }
24659}
24660#endif
24661
24662#if defined(FEAT_SESSION) || defined(PROTO)
24663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024664store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665{
Bram Moolenaar33570922005-01-25 22:26:29 +000024666 hashitem_T *hi;
24667 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024668 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 char_u *p, *t;
24670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024671 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024672 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024676 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024677 this_var = HI2DI(hi);
24678 if ((this_var->di_tv.v_type == VAR_NUMBER
24679 || this_var->di_tv.v_type == VAR_STRING)
24680 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024681 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024682 /* Escape special characters with a backslash. Turn a LF and
24683 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024684 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024685 (char_u *)"\\\"\n\r");
24686 if (p == NULL) /* out of memory */
24687 break;
24688 for (t = p; *t != NUL; ++t)
24689 if (*t == '\n')
24690 *t = 'n';
24691 else if (*t == '\r')
24692 *t = 'r';
24693 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024694 this_var->di_key,
24695 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24696 : ' ',
24697 p,
24698 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24699 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024700 || put_eol(fd) == FAIL)
24701 {
24702 vim_free(p);
24703 return FAIL;
24704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 vim_free(p);
24706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024707#ifdef FEAT_FLOAT
24708 else if (this_var->di_tv.v_type == VAR_FLOAT
24709 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24710 {
24711 float_T f = this_var->di_tv.vval.v_float;
24712 int sign = ' ';
24713
24714 if (f < 0)
24715 {
24716 f = -f;
24717 sign = '-';
24718 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024719 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024720 this_var->di_key, sign, f) < 0)
24721 || put_eol(fd) == FAIL)
24722 return FAIL;
24723 }
24724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 }
24726 }
24727 return OK;
24728}
24729#endif
24730
Bram Moolenaar661b1822005-07-28 22:36:45 +000024731/*
24732 * Display script name where an item was last set.
24733 * Should only be invoked when 'verbose' is non-zero.
24734 */
24735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024736last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024737{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024738 char_u *p;
24739
Bram Moolenaar661b1822005-07-28 22:36:45 +000024740 if (scriptID != 0)
24741 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024742 p = home_replace_save(NULL, get_scriptname(scriptID));
24743 if (p != NULL)
24744 {
24745 verbose_enter();
24746 MSG_PUTS(_("\n\tLast set from "));
24747 MSG_PUTS(p);
24748 vim_free(p);
24749 verbose_leave();
24750 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024751 }
24752}
24753
Bram Moolenaard812df62008-11-09 12:46:09 +000024754/*
24755 * List v:oldfiles in a nice way.
24756 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024758ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024759{
24760 list_T *l = vimvars[VV_OLDFILES].vv_list;
24761 listitem_T *li;
24762 int nr = 0;
24763
24764 if (l == NULL)
24765 msg((char_u *)_("No old files"));
24766 else
24767 {
24768 msg_start();
24769 msg_scroll = TRUE;
24770 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24771 {
24772 msg_outnum((long)++nr);
24773 MSG_PUTS(": ");
24774 msg_outtrans(get_tv_string(&li->li_tv));
24775 msg_putchar('\n');
24776 out_flush(); /* output one line at a time */
24777 ui_breakcheck();
24778 }
24779 /* Assume "got_int" was set to truncate the listing. */
24780 got_int = FALSE;
24781
24782#ifdef FEAT_BROWSE_CMD
24783 if (cmdmod.browse)
24784 {
24785 quit_more = FALSE;
24786 nr = prompt_for_number(FALSE);
24787 msg_starthere();
24788 if (nr > 0)
24789 {
24790 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24791 (long)nr);
24792
24793 if (p != NULL)
24794 {
24795 p = expand_env_save(p);
24796 eap->arg = p;
24797 eap->cmdidx = CMD_edit;
24798 cmdmod.browse = FALSE;
24799 do_exedit(eap, NULL);
24800 vim_free(p);
24801 }
24802 }
24803 }
24804#endif
24805 }
24806}
24807
Bram Moolenaar53744302015-07-17 17:38:22 +020024808/* reset v:option_new, v:option_old and v:option_type */
24809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024810reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024811{
24812 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24813 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24814 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24815}
24816
24817
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818#endif /* FEAT_EVAL */
24819
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024821#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822
24823#ifdef WIN3264
24824/*
24825 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24826 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024827static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24828static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24829static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830
24831/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024832 * Get the short path (8.3) for the filename in "fnamep".
24833 * Only works for a valid file name.
24834 * When the path gets longer "fnamep" is changed and the allocated buffer
24835 * is put in "bufp".
24836 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24837 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 */
24839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024840get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024842 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 char_u *newbuf;
24844
24845 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 l = GetShortPathName(*fnamep, *fnamep, len);
24847 if (l > len - 1)
24848 {
24849 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024850 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 newbuf = vim_strnsave(*fnamep, l);
24852 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854
24855 vim_free(*bufp);
24856 *fnamep = *bufp = newbuf;
24857
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024858 /* Really should always succeed, as the buffer is big enough. */
24859 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860 }
24861
24862 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864}
24865
24866/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024867 * Get the short path (8.3) for the filename in "fname". The converted
24868 * path is returned in "bufp".
24869 *
24870 * Some of the directories specified in "fname" may not exist. This function
24871 * will shorten the existing directories at the beginning of the path and then
24872 * append the remaining non-existing path.
24873 *
24874 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024875 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024876 * bufp - Pointer to an allocated buffer for the filename.
24877 * fnamelen - Length of the filename pointed to by fname
24878 *
24879 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 */
24881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024882shortpath_for_invalid_fname(
24883 char_u **fname,
24884 char_u **bufp,
24885 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024887 char_u *short_fname, *save_fname, *pbuf_unused;
24888 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024890 int old_len, len;
24891 int new_len, sfx_len;
24892 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893
24894 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024895 old_len = *fnamelen;
24896 save_fname = vim_strnsave(*fname, old_len);
24897 pbuf_unused = NULL;
24898 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024900 endp = save_fname + old_len - 1; /* Find the end of the copy */
24901 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024903 /*
24904 * Try shortening the supplied path till it succeeds by removing one
24905 * directory at a time from the tail of the path.
24906 */
24907 len = 0;
24908 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024910 /* go back one path-separator */
24911 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24912 --endp;
24913 if (endp <= save_fname)
24914 break; /* processed the complete path */
24915
24916 /*
24917 * Replace the path separator with a NUL and try to shorten the
24918 * resulting path.
24919 */
24920 ch = *endp;
24921 *endp = 0;
24922 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024923 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024924 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24925 {
24926 retval = FAIL;
24927 goto theend;
24928 }
24929 *endp = ch; /* preserve the string */
24930
24931 if (len > 0)
24932 break; /* successfully shortened the path */
24933
24934 /* failed to shorten the path. Skip the path separator */
24935 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936 }
24937
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024938 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024940 /*
24941 * Succeeded in shortening the path. Now concatenate the shortened
24942 * path with the remaining path at the tail.
24943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024945 /* Compute the length of the new path. */
24946 sfx_len = (int)(save_endp - endp) + 1;
24947 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024949 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024951 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024953 /* There is not enough space in the currently allocated string,
24954 * copy it to a buffer big enough. */
24955 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024957 {
24958 retval = FAIL;
24959 goto theend;
24960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 }
24962 else
24963 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024964 /* Transfer short_fname to the main buffer (it's big enough),
24965 * unless get_short_pathname() did its work in-place. */
24966 *fname = *bufp = save_fname;
24967 if (short_fname != save_fname)
24968 vim_strncpy(save_fname, short_fname, len);
24969 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024971
24972 /* concat the not-shortened part of the path */
24973 vim_strncpy(*fname + len, endp, sfx_len);
24974 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024976
24977theend:
24978 vim_free(pbuf_unused);
24979 vim_free(save_fname);
24980
24981 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982}
24983
24984/*
24985 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024986 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 */
24988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024989shortpath_for_partial(
24990 char_u **fnamep,
24991 char_u **bufp,
24992 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993{
24994 int sepcount, len, tflen;
24995 char_u *p;
24996 char_u *pbuf, *tfname;
24997 int hasTilde;
24998
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024999 /* Count up the path separators from the RHS.. so we know which part
25000 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025002 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 if (vim_ispathsep(*p))
25004 ++sepcount;
25005
25006 /* Need full path first (use expand_env() to remove a "~/") */
25007 hasTilde = (**fnamep == '~');
25008 if (hasTilde)
25009 pbuf = tfname = expand_env_save(*fnamep);
25010 else
25011 pbuf = tfname = FullName_save(*fnamep, FALSE);
25012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025013 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025015 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017
25018 if (len == 0)
25019 {
25020 /* Don't have a valid filename, so shorten the rest of the
25021 * path if we can. This CAN give us invalid 8.3 filenames, but
25022 * there's not a lot of point in guessing what it might be.
25023 */
25024 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025025 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027 }
25028
25029 /* Count the paths backward to find the beginning of the desired string. */
25030 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025031 {
25032#ifdef FEAT_MBYTE
25033 if (has_mbyte)
25034 p -= mb_head_off(tfname, p);
25035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036 if (vim_ispathsep(*p))
25037 {
25038 if (sepcount == 0 || (hasTilde && sepcount == 1))
25039 break;
25040 else
25041 sepcount --;
25042 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 if (hasTilde)
25045 {
25046 --p;
25047 if (p >= tfname)
25048 *p = '~';
25049 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 }
25052 else
25053 ++p;
25054
25055 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25056 vim_free(*bufp);
25057 *fnamelen = (int)STRLEN(p);
25058 *bufp = pbuf;
25059 *fnamep = p;
25060
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025061 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062}
25063#endif /* WIN3264 */
25064
25065/*
25066 * Adjust a filename, according to a string of modifiers.
25067 * *fnamep must be NUL terminated when called. When returning, the length is
25068 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025069 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 * When there is an error, *fnamep is set to NULL.
25071 */
25072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025073modify_fname(
25074 char_u *src, /* string with modifiers */
25075 int *usedlen, /* characters after src that are used */
25076 char_u **fnamep, /* file name so far */
25077 char_u **bufp, /* buffer for allocated file name or NULL */
25078 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079{
25080 int valid = 0;
25081 char_u *tail;
25082 char_u *s, *p, *pbuf;
25083 char_u dirname[MAXPATHL];
25084 int c;
25085 int has_fullname = 0;
25086#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025087 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 int has_shortname = 0;
25089#endif
25090
25091repeat:
25092 /* ":p" - full path/file_name */
25093 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25094 {
25095 has_fullname = 1;
25096
25097 valid |= VALID_PATH;
25098 *usedlen += 2;
25099
25100 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25101 if ((*fnamep)[0] == '~'
25102#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25103 && ((*fnamep)[1] == '/'
25104# ifdef BACKSLASH_IN_FILENAME
25105 || (*fnamep)[1] == '\\'
25106# endif
25107 || (*fnamep)[1] == NUL)
25108
25109#endif
25110 )
25111 {
25112 *fnamep = expand_env_save(*fnamep);
25113 vim_free(*bufp); /* free any allocated file name */
25114 *bufp = *fnamep;
25115 if (*fnamep == NULL)
25116 return -1;
25117 }
25118
25119 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025120 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 {
25122 if (vim_ispathsep(*p)
25123 && p[1] == '.'
25124 && (p[2] == NUL
25125 || vim_ispathsep(p[2])
25126 || (p[2] == '.'
25127 && (p[3] == NUL || vim_ispathsep(p[3])))))
25128 break;
25129 }
25130
25131 /* FullName_save() is slow, don't use it when not needed. */
25132 if (*p != NUL || !vim_isAbsName(*fnamep))
25133 {
25134 *fnamep = FullName_save(*fnamep, *p != NUL);
25135 vim_free(*bufp); /* free any allocated file name */
25136 *bufp = *fnamep;
25137 if (*fnamep == NULL)
25138 return -1;
25139 }
25140
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025141#ifdef WIN3264
25142# if _WIN32_WINNT >= 0x0500
25143 if (vim_strchr(*fnamep, '~') != NULL)
25144 {
25145 /* Expand 8.3 filename to full path. Needed to make sure the same
25146 * file does not have two different names.
25147 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25148 p = alloc(_MAX_PATH + 1);
25149 if (p != NULL)
25150 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025151 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025152 {
25153 vim_free(*bufp);
25154 *bufp = *fnamep = p;
25155 }
25156 else
25157 vim_free(p);
25158 }
25159 }
25160# endif
25161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 /* Append a path separator to a directory. */
25163 if (mch_isdir(*fnamep))
25164 {
25165 /* Make room for one or two extra characters. */
25166 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25167 vim_free(*bufp); /* free any allocated file name */
25168 *bufp = *fnamep;
25169 if (*fnamep == NULL)
25170 return -1;
25171 add_pathsep(*fnamep);
25172 }
25173 }
25174
25175 /* ":." - path relative to the current directory */
25176 /* ":~" - path relative to the home directory */
25177 /* ":8" - shortname path - postponed till after */
25178 while (src[*usedlen] == ':'
25179 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25180 {
25181 *usedlen += 2;
25182 if (c == '8')
25183 {
25184#ifdef WIN3264
25185 has_shortname = 1; /* Postpone this. */
25186#endif
25187 continue;
25188 }
25189 pbuf = NULL;
25190 /* Need full path first (use expand_env() to remove a "~/") */
25191 if (!has_fullname)
25192 {
25193 if (c == '.' && **fnamep == '~')
25194 p = pbuf = expand_env_save(*fnamep);
25195 else
25196 p = pbuf = FullName_save(*fnamep, FALSE);
25197 }
25198 else
25199 p = *fnamep;
25200
25201 has_fullname = 0;
25202
25203 if (p != NULL)
25204 {
25205 if (c == '.')
25206 {
25207 mch_dirname(dirname, MAXPATHL);
25208 s = shorten_fname(p, dirname);
25209 if (s != NULL)
25210 {
25211 *fnamep = s;
25212 if (pbuf != NULL)
25213 {
25214 vim_free(*bufp); /* free any allocated file name */
25215 *bufp = pbuf;
25216 pbuf = NULL;
25217 }
25218 }
25219 }
25220 else
25221 {
25222 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25223 /* Only replace it when it starts with '~' */
25224 if (*dirname == '~')
25225 {
25226 s = vim_strsave(dirname);
25227 if (s != NULL)
25228 {
25229 *fnamep = s;
25230 vim_free(*bufp);
25231 *bufp = s;
25232 }
25233 }
25234 }
25235 vim_free(pbuf);
25236 }
25237 }
25238
25239 tail = gettail(*fnamep);
25240 *fnamelen = (int)STRLEN(*fnamep);
25241
25242 /* ":h" - head, remove "/file_name", can be repeated */
25243 /* Don't remove the first "/" or "c:\" */
25244 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25245 {
25246 valid |= VALID_HEAD;
25247 *usedlen += 2;
25248 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025249 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025250 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 *fnamelen = (int)(tail - *fnamep);
25252#ifdef VMS
25253 if (*fnamelen > 0)
25254 *fnamelen += 1; /* the path separator is part of the path */
25255#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025256 if (*fnamelen == 0)
25257 {
25258 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25259 p = vim_strsave((char_u *)".");
25260 if (p == NULL)
25261 return -1;
25262 vim_free(*bufp);
25263 *bufp = *fnamep = tail = p;
25264 *fnamelen = 1;
25265 }
25266 else
25267 {
25268 while (tail > s && !after_pathsep(s, tail))
25269 mb_ptr_back(*fnamep, tail);
25270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 }
25272
25273 /* ":8" - shortname */
25274 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25275 {
25276 *usedlen += 2;
25277#ifdef WIN3264
25278 has_shortname = 1;
25279#endif
25280 }
25281
25282#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025283 /*
25284 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285 */
25286 if (has_shortname)
25287 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025288 /* Copy the string if it is shortened by :h and when it wasn't copied
25289 * yet, because we are going to change it in place. Avoids changing
25290 * the buffer name for "%:8". */
25291 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 {
25293 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025294 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 return -1;
25296 vim_free(*bufp);
25297 *bufp = *fnamep = p;
25298 }
25299
25300 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025301 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (!has_fullname && !vim_isAbsName(*fnamep))
25303 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025304 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 return -1;
25306 }
25307 else
25308 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025309 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310
Bram Moolenaardc935552011-08-17 15:23:23 +020025311 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025313 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 return -1;
25315
25316 if (l == 0)
25317 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025318 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321 return -1;
25322 }
25323 *fnamelen = l;
25324 }
25325 }
25326#endif /* WIN3264 */
25327
25328 /* ":t" - tail, just the basename */
25329 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25330 {
25331 *usedlen += 2;
25332 *fnamelen -= (int)(tail - *fnamep);
25333 *fnamep = tail;
25334 }
25335
25336 /* ":e" - extension, can be repeated */
25337 /* ":r" - root, without extension, can be repeated */
25338 while (src[*usedlen] == ':'
25339 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25340 {
25341 /* find a '.' in the tail:
25342 * - for second :e: before the current fname
25343 * - otherwise: The last '.'
25344 */
25345 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25346 s = *fnamep - 2;
25347 else
25348 s = *fnamep + *fnamelen - 1;
25349 for ( ; s > tail; --s)
25350 if (s[0] == '.')
25351 break;
25352 if (src[*usedlen + 1] == 'e') /* :e */
25353 {
25354 if (s > tail)
25355 {
25356 *fnamelen += (int)(*fnamep - (s + 1));
25357 *fnamep = s + 1;
25358#ifdef VMS
25359 /* cut version from the extension */
25360 s = *fnamep + *fnamelen - 1;
25361 for ( ; s > *fnamep; --s)
25362 if (s[0] == ';')
25363 break;
25364 if (s > *fnamep)
25365 *fnamelen = s - *fnamep;
25366#endif
25367 }
25368 else if (*fnamep <= tail)
25369 *fnamelen = 0;
25370 }
25371 else /* :r */
25372 {
25373 if (s > tail) /* remove one extension */
25374 *fnamelen = (int)(s - *fnamep);
25375 }
25376 *usedlen += 2;
25377 }
25378
25379 /* ":s?pat?foo?" - substitute */
25380 /* ":gs?pat?foo?" - global substitute */
25381 if (src[*usedlen] == ':'
25382 && (src[*usedlen + 1] == 's'
25383 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25384 {
25385 char_u *str;
25386 char_u *pat;
25387 char_u *sub;
25388 int sep;
25389 char_u *flags;
25390 int didit = FALSE;
25391
25392 flags = (char_u *)"";
25393 s = src + *usedlen + 2;
25394 if (src[*usedlen + 1] == 'g')
25395 {
25396 flags = (char_u *)"g";
25397 ++s;
25398 }
25399
25400 sep = *s++;
25401 if (sep)
25402 {
25403 /* find end of pattern */
25404 p = vim_strchr(s, sep);
25405 if (p != NULL)
25406 {
25407 pat = vim_strnsave(s, (int)(p - s));
25408 if (pat != NULL)
25409 {
25410 s = p + 1;
25411 /* find end of substitution */
25412 p = vim_strchr(s, sep);
25413 if (p != NULL)
25414 {
25415 sub = vim_strnsave(s, (int)(p - s));
25416 str = vim_strnsave(*fnamep, *fnamelen);
25417 if (sub != NULL && str != NULL)
25418 {
25419 *usedlen = (int)(p + 1 - src);
25420 s = do_string_sub(str, pat, sub, flags);
25421 if (s != NULL)
25422 {
25423 *fnamep = s;
25424 *fnamelen = (int)STRLEN(s);
25425 vim_free(*bufp);
25426 *bufp = s;
25427 didit = TRUE;
25428 }
25429 }
25430 vim_free(sub);
25431 vim_free(str);
25432 }
25433 vim_free(pat);
25434 }
25435 }
25436 /* after using ":s", repeat all the modifiers */
25437 if (didit)
25438 goto repeat;
25439 }
25440 }
25441
Bram Moolenaar26df0922014-02-23 23:39:13 +010025442 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25443 {
25444 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25445 if (p == NULL)
25446 return -1;
25447 vim_free(*bufp);
25448 *bufp = *fnamep = p;
25449 *fnamelen = (int)STRLEN(p);
25450 *usedlen += 2;
25451 }
25452
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 return valid;
25454}
25455
25456/*
25457 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25458 * "flags" can be "g" to do a global substitute.
25459 * Returns an allocated string, NULL for error.
25460 */
25461 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025462do_string_sub(
25463 char_u *str,
25464 char_u *pat,
25465 char_u *sub,
25466 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467{
25468 int sublen;
25469 regmatch_T regmatch;
25470 int i;
25471 int do_all;
25472 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025473 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 garray_T ga;
25475 char_u *ret;
25476 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025477 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478
25479 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25480 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025481 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482
25483 ga_init2(&ga, 1, 200);
25484
25485 do_all = (flags[0] == 'g');
25486
25487 regmatch.rm_ic = p_ic;
25488 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25489 if (regmatch.regprog != NULL)
25490 {
25491 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025492 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25494 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025495 /* Skip empty match except for first match. */
25496 if (regmatch.startp[0] == regmatch.endp[0])
25497 {
25498 if (zero_width == regmatch.startp[0])
25499 {
25500 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025501 i = MB_PTR2LEN(tail);
25502 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25503 (size_t)i);
25504 ga.ga_len += i;
25505 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025506 continue;
25507 }
25508 zero_width = regmatch.startp[0];
25509 }
25510
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 /*
25512 * Get some space for a temporary buffer to do the substitution
25513 * into. It will contain:
25514 * - The text up to where the match is.
25515 * - The substituted text.
25516 * - The text after the match.
25517 */
25518 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025519 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25521 {
25522 ga_clear(&ga);
25523 break;
25524 }
25525
25526 /* copy the text up to where the match is */
25527 i = (int)(regmatch.startp[0] - tail);
25528 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25529 /* add the substituted text */
25530 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25531 + ga.ga_len + i, TRUE, TRUE, FALSE);
25532 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025533 tail = regmatch.endp[0];
25534 if (*tail == NUL)
25535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 if (!do_all)
25537 break;
25538 }
25539
25540 if (ga.ga_data != NULL)
25541 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25542
Bram Moolenaar473de612013-06-08 18:19:48 +020025543 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 }
25545
25546 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25547 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025548 if (p_cpo == empty_option)
25549 p_cpo = save_cpo;
25550 else
25551 /* Darn, evaluating {sub} expression changed the value. */
25552 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553
25554 return ret;
25555}
25556
25557#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */