blob: bd0040e66a2fd30314c7e811cb19bd3263636bfe [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
507static void f_ch_close(typval_T *argvars, typval_T *rettv);
508static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
509static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
510#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100511static void f_changenr(typval_T *argvars, typval_T *rettv);
512static void f_char2nr(typval_T *argvars, typval_T *rettv);
513static void f_cindent(typval_T *argvars, typval_T *rettv);
514static void f_clearmatches(typval_T *argvars, typval_T *rettv);
515static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000516#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_complete(typval_T *argvars, typval_T *rettv);
518static void f_complete_add(typval_T *argvars, typval_T *rettv);
519static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_confirm(typval_T *argvars, typval_T *rettv);
522static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_cos(typval_T *argvars, typval_T *rettv);
525static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_count(typval_T *argvars, typval_T *rettv);
528static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
529static void f_cursor(typval_T *argsvars, typval_T *rettv);
530static void f_deepcopy(typval_T *argvars, typval_T *rettv);
531static void f_delete(typval_T *argvars, typval_T *rettv);
532static void f_did_filetype(typval_T *argvars, typval_T *rettv);
533static void f_diff_filler(typval_T *argvars, typval_T *rettv);
534static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
535static void f_empty(typval_T *argvars, typval_T *rettv);
536static void f_escape(typval_T *argvars, typval_T *rettv);
537static void f_eval(typval_T *argvars, typval_T *rettv);
538static void f_eventhandler(typval_T *argvars, typval_T *rettv);
539static void f_executable(typval_T *argvars, typval_T *rettv);
540static void f_exepath(typval_T *argvars, typval_T *rettv);
541static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200542#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200544#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_expand(typval_T *argvars, typval_T *rettv);
546static void f_extend(typval_T *argvars, typval_T *rettv);
547static void f_feedkeys(typval_T *argvars, typval_T *rettv);
548static void f_filereadable(typval_T *argvars, typval_T *rettv);
549static void f_filewritable(typval_T *argvars, typval_T *rettv);
550static void f_filter(typval_T *argvars, typval_T *rettv);
551static void f_finddir(typval_T *argvars, typval_T *rettv);
552static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000553#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100554static void f_float2nr(typval_T *argvars, typval_T *rettv);
555static void f_floor(typval_T *argvars, typval_T *rettv);
556static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_fnameescape(typval_T *argvars, typval_T *rettv);
559static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
560static void f_foldclosed(typval_T *argvars, typval_T *rettv);
561static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
562static void f_foldlevel(typval_T *argvars, typval_T *rettv);
563static void f_foldtext(typval_T *argvars, typval_T *rettv);
564static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
565static void f_foreground(typval_T *argvars, typval_T *rettv);
566static void f_function(typval_T *argvars, typval_T *rettv);
567static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
568static void f_get(typval_T *argvars, typval_T *rettv);
569static void f_getbufline(typval_T *argvars, typval_T *rettv);
570static void f_getbufvar(typval_T *argvars, typval_T *rettv);
571static void f_getchar(typval_T *argvars, typval_T *rettv);
572static void f_getcharmod(typval_T *argvars, typval_T *rettv);
573static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
574static void f_getcmdline(typval_T *argvars, typval_T *rettv);
575static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
576static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
577static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
578static void f_getcwd(typval_T *argvars, typval_T *rettv);
579static void f_getfontname(typval_T *argvars, typval_T *rettv);
580static void f_getfperm(typval_T *argvars, typval_T *rettv);
581static void f_getfsize(typval_T *argvars, typval_T *rettv);
582static void f_getftime(typval_T *argvars, typval_T *rettv);
583static void f_getftype(typval_T *argvars, typval_T *rettv);
584static void f_getline(typval_T *argvars, typval_T *rettv);
585static void f_getmatches(typval_T *argvars, typval_T *rettv);
586static void f_getpid(typval_T *argvars, typval_T *rettv);
587static void f_getcurpos(typval_T *argvars, typval_T *rettv);
588static void f_getpos(typval_T *argvars, typval_T *rettv);
589static void f_getqflist(typval_T *argvars, typval_T *rettv);
590static void f_getreg(typval_T *argvars, typval_T *rettv);
591static void f_getregtype(typval_T *argvars, typval_T *rettv);
592static void f_gettabvar(typval_T *argvars, typval_T *rettv);
593static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
594static void f_getwinposx(typval_T *argvars, typval_T *rettv);
595static void f_getwinposy(typval_T *argvars, typval_T *rettv);
596static void f_getwinvar(typval_T *argvars, typval_T *rettv);
597static void f_glob(typval_T *argvars, typval_T *rettv);
598static void f_globpath(typval_T *argvars, typval_T *rettv);
599static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
600static void f_has(typval_T *argvars, typval_T *rettv);
601static void f_has_key(typval_T *argvars, typval_T *rettv);
602static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
603static void f_hasmapto(typval_T *argvars, typval_T *rettv);
604static void f_histadd(typval_T *argvars, typval_T *rettv);
605static void f_histdel(typval_T *argvars, typval_T *rettv);
606static void f_histget(typval_T *argvars, typval_T *rettv);
607static void f_histnr(typval_T *argvars, typval_T *rettv);
608static void f_hlID(typval_T *argvars, typval_T *rettv);
609static void f_hlexists(typval_T *argvars, typval_T *rettv);
610static void f_hostname(typval_T *argvars, typval_T *rettv);
611static void f_iconv(typval_T *argvars, typval_T *rettv);
612static void f_indent(typval_T *argvars, typval_T *rettv);
613static void f_index(typval_T *argvars, typval_T *rettv);
614static void f_input(typval_T *argvars, typval_T *rettv);
615static void f_inputdialog(typval_T *argvars, typval_T *rettv);
616static void f_inputlist(typval_T *argvars, typval_T *rettv);
617static void f_inputrestore(typval_T *argvars, typval_T *rettv);
618static void f_inputsave(typval_T *argvars, typval_T *rettv);
619static void f_inputsecret(typval_T *argvars, typval_T *rettv);
620static void f_insert(typval_T *argvars, typval_T *rettv);
621static void f_invert(typval_T *argvars, typval_T *rettv);
622static void f_isdirectory(typval_T *argvars, typval_T *rettv);
623static void f_islocked(typval_T *argvars, typval_T *rettv);
624static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100625#ifdef FEAT_JOB
626static void f_job_start(typval_T *argvars, typval_T *rettv);
627static void f_job_stop(typval_T *argvars, typval_T *rettv);
628static void f_job_status(typval_T *argvars, typval_T *rettv);
629#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100630static void f_join(typval_T *argvars, typval_T *rettv);
631static void f_jsondecode(typval_T *argvars, typval_T *rettv);
632static void f_jsonencode(typval_T *argvars, typval_T *rettv);
633static void f_keys(typval_T *argvars, typval_T *rettv);
634static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
635static void f_len(typval_T *argvars, typval_T *rettv);
636static void f_libcall(typval_T *argvars, typval_T *rettv);
637static void f_libcallnr(typval_T *argvars, typval_T *rettv);
638static void f_line(typval_T *argvars, typval_T *rettv);
639static void f_line2byte(typval_T *argvars, typval_T *rettv);
640static void f_lispindent(typval_T *argvars, typval_T *rettv);
641static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_log(typval_T *argvars, typval_T *rettv);
644static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200646#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200648#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_map(typval_T *argvars, typval_T *rettv);
650static void f_maparg(typval_T *argvars, typval_T *rettv);
651static void f_mapcheck(typval_T *argvars, typval_T *rettv);
652static void f_match(typval_T *argvars, typval_T *rettv);
653static void f_matchadd(typval_T *argvars, typval_T *rettv);
654static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
655static void f_matcharg(typval_T *argvars, typval_T *rettv);
656static void f_matchdelete(typval_T *argvars, typval_T *rettv);
657static void f_matchend(typval_T *argvars, typval_T *rettv);
658static void f_matchlist(typval_T *argvars, typval_T *rettv);
659static void f_matchstr(typval_T *argvars, typval_T *rettv);
660static void f_max(typval_T *argvars, typval_T *rettv);
661static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000662#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100666#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100668#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
670static void f_nr2char(typval_T *argvars, typval_T *rettv);
671static void f_or(typval_T *argvars, typval_T *rettv);
672static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100673#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100675#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000678#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
680static void f_printf(typval_T *argvars, typval_T *rettv);
681static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200682#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200684#endif
685#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_range(typval_T *argvars, typval_T *rettv);
689static void f_readfile(typval_T *argvars, typval_T *rettv);
690static void f_reltime(typval_T *argvars, typval_T *rettv);
691static void f_reltimestr(typval_T *argvars, typval_T *rettv);
692static void f_remote_expr(typval_T *argvars, typval_T *rettv);
693static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
694static void f_remote_peek(typval_T *argvars, typval_T *rettv);
695static void f_remote_read(typval_T *argvars, typval_T *rettv);
696static void f_remote_send(typval_T *argvars, typval_T *rettv);
697static void f_remove(typval_T *argvars, typval_T *rettv);
698static void f_rename(typval_T *argvars, typval_T *rettv);
699static void f_repeat(typval_T *argvars, typval_T *rettv);
700static void f_resolve(typval_T *argvars, typval_T *rettv);
701static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_screenattr(typval_T *argvars, typval_T *rettv);
706static void f_screenchar(typval_T *argvars, typval_T *rettv);
707static void f_screencol(typval_T *argvars, typval_T *rettv);
708static void f_screenrow(typval_T *argvars, typval_T *rettv);
709static void f_search(typval_T *argvars, typval_T *rettv);
710static void f_searchdecl(typval_T *argvars, typval_T *rettv);
711static void f_searchpair(typval_T *argvars, typval_T *rettv);
712static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
713static void f_searchpos(typval_T *argvars, typval_T *rettv);
714static void f_server2client(typval_T *argvars, typval_T *rettv);
715static void f_serverlist(typval_T *argvars, typval_T *rettv);
716static void f_setbufvar(typval_T *argvars, typval_T *rettv);
717static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
718static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
719static void f_setline(typval_T *argvars, typval_T *rettv);
720static void f_setloclist(typval_T *argvars, typval_T *rettv);
721static void f_setmatches(typval_T *argvars, typval_T *rettv);
722static void f_setpos(typval_T *argvars, typval_T *rettv);
723static void f_setqflist(typval_T *argvars, typval_T *rettv);
724static void f_setreg(typval_T *argvars, typval_T *rettv);
725static void f_settabvar(typval_T *argvars, typval_T *rettv);
726static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
727static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100728#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100729static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100730#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_shellescape(typval_T *argvars, typval_T *rettv);
732static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
733static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_sin(typval_T *argvars, typval_T *rettv);
736static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_sort(typval_T *argvars, typval_T *rettv);
739static void f_soundfold(typval_T *argvars, typval_T *rettv);
740static void f_spellbadword(typval_T *argvars, typval_T *rettv);
741static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
742static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_sqrt(typval_T *argvars, typval_T *rettv);
745static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_str2nr(typval_T *argvars, typval_T *rettv);
748static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000749#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_stridx(typval_T *argvars, typval_T *rettv);
753static void f_string(typval_T *argvars, typval_T *rettv);
754static void f_strlen(typval_T *argvars, typval_T *rettv);
755static void f_strpart(typval_T *argvars, typval_T *rettv);
756static void f_strridx(typval_T *argvars, typval_T *rettv);
757static void f_strtrans(typval_T *argvars, typval_T *rettv);
758static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
759static void f_strwidth(typval_T *argvars, typval_T *rettv);
760static void f_submatch(typval_T *argvars, typval_T *rettv);
761static void f_substitute(typval_T *argvars, typval_T *rettv);
762static void f_synID(typval_T *argvars, typval_T *rettv);
763static void f_synIDattr(typval_T *argvars, typval_T *rettv);
764static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
765static void f_synstack(typval_T *argvars, typval_T *rettv);
766static void f_synconcealed(typval_T *argvars, typval_T *rettv);
767static void f_system(typval_T *argvars, typval_T *rettv);
768static void f_systemlist(typval_T *argvars, typval_T *rettv);
769static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
770static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
771static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
772static void f_taglist(typval_T *argvars, typval_T *rettv);
773static void f_tagfiles(typval_T *argvars, typval_T *rettv);
774static void f_tempname(typval_T *argvars, typval_T *rettv);
775static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200776#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_tan(typval_T *argvars, typval_T *rettv);
778static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200779#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_tolower(typval_T *argvars, typval_T *rettv);
781static void f_toupper(typval_T *argvars, typval_T *rettv);
782static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000785#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_type(typval_T *argvars, typval_T *rettv);
787static void f_undofile(typval_T *argvars, typval_T *rettv);
788static void f_undotree(typval_T *argvars, typval_T *rettv);
789static void f_uniq(typval_T *argvars, typval_T *rettv);
790static void f_values(typval_T *argvars, typval_T *rettv);
791static void f_virtcol(typval_T *argvars, typval_T *rettv);
792static void f_visualmode(typval_T *argvars, typval_T *rettv);
793static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
794static void f_winbufnr(typval_T *argvars, typval_T *rettv);
795static void f_wincol(typval_T *argvars, typval_T *rettv);
796static void f_winheight(typval_T *argvars, typval_T *rettv);
797static void f_winline(typval_T *argvars, typval_T *rettv);
798static void f_winnr(typval_T *argvars, typval_T *rettv);
799static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
800static void f_winrestview(typval_T *argvars, typval_T *rettv);
801static void f_winsaveview(typval_T *argvars, typval_T *rettv);
802static void f_winwidth(typval_T *argvars, typval_T *rettv);
803static void f_writefile(typval_T *argvars, typval_T *rettv);
804static void f_wordcount(typval_T *argvars, typval_T *rettv);
805static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000806
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
808static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
809static int get_env_len(char_u **arg);
810static int get_id_len(char_u **arg);
811static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
812static 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 +0000813#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
814#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
815 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100816static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
817static int eval_isnamec(int c);
818static int eval_isnamec1(int c);
819static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
820static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static typval_T *alloc_string_tv(char_u *string);
822static void init_tv(typval_T *varp);
823static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100824#ifdef FEAT_FLOAT
825static float_T get_tv_float(typval_T *varp);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static linenr_T get_tv_lnum(typval_T *argvars);
828static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
829static char_u *get_tv_string(typval_T *varp);
830static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
831static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
832static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
833static hashtab_T *find_var_ht(char_u *name, char_u **varname);
834static funccall_T *get_funccal(void);
835static void vars_clear_ext(hashtab_T *ht, int free_val);
836static void delete_var(hashtab_T *ht, hashitem_T *hi);
837static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
838static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
839static void set_var(char_u *name, typval_T *varp, int copy);
840static int var_check_ro(int flags, char_u *name, int use_gettext);
841static int var_check_fixed(int flags, char_u *name, int use_gettext);
842static int var_check_func_name(char_u *name, int new_var);
843static int valid_varname(char_u *varname);
844static int tv_check_lock(int lock, char_u *name, int use_gettext);
845static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
846static char_u *find_option_end(char_u **arg, int *opt_flags);
847static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
848static int eval_fname_script(char_u *p);
849static int eval_fname_sid(char_u *p);
850static void list_func_head(ufunc_T *fp, int indent);
851static ufunc_T *find_func(char_u *name);
852static int function_exists(char_u *name);
853static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855static void func_do_profile(ufunc_T *fp);
856static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
857static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000858static int
859# ifdef __BORLANDC__
860 _RTLENTRYF
861# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100862 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000863static int
864# ifdef __BORLANDC__
865 _RTLENTRYF
866# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static int script_autoload(char_u *name, int reload);
870static char_u *autoload_name(char_u *name);
871static void cat_func_name(char_u *buf, ufunc_T *fp);
872static void func_free(ufunc_T *fp);
873static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
874static int can_free_funccal(funccall_T *fc, int copyID) ;
875static void free_funccal(funccall_T *fc, int free_val);
876static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
877static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
878static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
879static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
880static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
881static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
882static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
883static int write_list(FILE *fd, list_T *list, int binary);
884static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int compare_func_name(const void *s1, const void *s2);
889static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890#endif
891
Bram Moolenaar33570922005-01-25 22:26:29 +0000892/*
893 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000894 */
895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100896eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000897{
Bram Moolenaar33570922005-01-25 22:26:29 +0000898 int i;
899 struct vimvar *p;
900
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200901 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
902 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200903 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000904 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000906
907 for (i = 0; i < VV_LEN; ++i)
908 {
909 p = &vimvars[i];
910 STRCPY(p->vv_di.di_key, p->vv_name);
911 if (p->vv_flags & VV_RO)
912 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
913 else if (p->vv_flags & VV_RO_SBX)
914 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
915 else
916 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000917
918 /* add to v: scope dict, unless the value is not always available */
919 if (p->vv_type != VAR_UNKNOWN)
920 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000922 /* add to compat scope dict */
923 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000924 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100925 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
926
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000927 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100928 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200929 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100930 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100931
932 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
933 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
934 set_vim_var_nr(VV_NONE, VVAL_NONE);
935 set_vim_var_nr(VV_NULL, VVAL_NULL);
936
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200937 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200938
939#ifdef EBCDIC
940 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100941 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200942 */
943 sortFunctions();
944#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000945}
946
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000947#if defined(EXITFREE) || defined(PROTO)
948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100949eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000950{
951 int i;
952 struct vimvar *p;
953
954 for (i = 0; i < VV_LEN; ++i)
955 {
956 p = &vimvars[i];
957 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000959 vim_free(p->vv_str);
960 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000961 }
962 else if (p->vv_di.di_tv.v_type == VAR_LIST)
963 {
964 list_unref(p->vv_list);
965 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000966 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967 }
968 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000969 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970 hash_clear(&compat_hashtab);
971
Bram Moolenaard9fba312005-06-26 22:34:35 +0000972 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100973# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200974 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100975# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976
977 /* global variables */
978 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000979
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000980 /* autoloaded script names */
981 ga_clear_strings(&ga_loaded);
982
Bram Moolenaarcca74132013-09-25 21:00:28 +0200983 /* Script-local variables. First clear all the variables and in a second
984 * loop free the scriptvar_T, because a variable in one script might hold
985 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200986 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200987 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200988 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200989 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200990 ga_clear(&ga_scripts);
991
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000992 /* unreferenced lists and dicts */
993 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000994
995 /* functions */
996 free_all_functions();
997 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000998}
999#endif
1000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Return the name of the executed function.
1003 */
1004 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001005func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/*
1011 * Return the address holding the next breakpoint line for a funccall cookie.
1012 */
1013 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001014func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar33570922005-01-25 22:26:29 +00001016 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017}
1018
1019/*
1020 * Return the address holding the debug tick for a funccall cookie.
1021 */
1022 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001023func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaar33570922005-01-25 22:26:29 +00001025 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026}
1027
1028/*
1029 * Return the nesting level for a funccall cookie.
1030 */
1031 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001032func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001038funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001040/* pointer to list of previously used funccal, still around because some
1041 * item in it is still being used. */
1042funccall_T *previous_funccal = NULL;
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044/*
1045 * Return TRUE when a function was ended by a ":return" command.
1046 */
1047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 return current_funccal->returned;
1051}
1052
1053
1054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 * Set an internal variable to a string value. Creates the variable if it does
1056 * not already exist.
1057 */
1058 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001061 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001062 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064 val = vim_strsave(value);
1065 if (val != NULL)
1066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001068 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001070 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001071 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073 }
1074}
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001077static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078static char_u *redir_endp = NULL;
1079static char_u *redir_varname = NULL;
1080
1081/*
1082 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 * Returns OK if successfully completed the setup. FAIL otherwise.
1085 */
1086 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088{
1089 int save_emsg;
1090 int err;
1091 typval_T tv;
1092
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001093 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001094 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 {
1096 EMSG(_(e_invarg));
1097 return FAIL;
1098 }
1099
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 redir_varname = vim_strsave(name);
1102 if (redir_varname == NULL)
1103 return FAIL;
1104
1105 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1106 if (redir_lval == NULL)
1107 {
1108 var_redir_stop();
1109 return FAIL;
1110 }
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 /* The output is stored in growarray "redir_ga" until redirection ends. */
1113 ga_init2(&redir_ga, (int)sizeof(char), 500);
1114
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001116 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001117 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1119 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001120 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 if (redir_endp != NULL && *redir_endp != NUL)
1122 /* Trailing characters are present after the variable name */
1123 EMSG(_(e_trailing));
1124 else
1125 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001126 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
1128 return FAIL;
1129 }
1130
1131 /* check if we can write to the variable: set it to or append an empty
1132 * string */
1133 save_emsg = did_emsg;
1134 did_emsg = FALSE;
1135 tv.v_type = VAR_STRING;
1136 tv.vval.v_string = (char_u *)"";
1137 if (append)
1138 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1139 else
1140 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001141 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001143 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (err)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 var_redir_stop();
1148 return FAIL;
1149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150
1151 return OK;
1152}
1153
1154/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 * Append "value[value_len]" to the variable set by var_redir_start().
1156 * The actual appending is postponed until redirection ends, because the value
1157 * appended may in fact be the string we write to, changing it may cause freed
1158 * memory to be used:
1159 * :redir => foo
1160 * :let foo
1161 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 */
1163 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001164var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
1168 if (redir_lval == NULL)
1169 return;
1170
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001172 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001174 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001176 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 {
1178 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001179 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 }
1181 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183}
1184
1185/*
1186 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 */
1189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001190var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192 typval_T tv;
1193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 if (redir_lval != NULL)
1195 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001196 /* If there was no error: assign the text to the variable. */
1197 if (redir_endp != NULL)
1198 {
1199 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1200 tv.v_type = VAR_STRING;
1201 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 /* Call get_lval() again, if it's inside a Dict or List it may
1203 * have changed. */
1204 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001205 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001206 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1207 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1208 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001209 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 /* free the collected output */
1212 vim_free(redir_ga.ga_data);
1213 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215 vim_free(redir_lval);
1216 redir_lval = NULL;
1217 }
1218 vim_free(redir_varname);
1219 redir_varname = NULL;
1220}
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222# if defined(FEAT_MBYTE) || defined(PROTO)
1223 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001224eval_charconvert(
1225 char_u *enc_from,
1226 char_u *enc_to,
1227 char_u *fname_from,
1228 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1233 set_vim_var_string(VV_CC_TO, enc_to, -1);
1234 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1235 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1236 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1237 err = TRUE;
1238 set_vim_var_string(VV_CC_FROM, NULL, -1);
1239 set_vim_var_string(VV_CC_TO, NULL, -1);
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242
1243 if (err)
1244 return FAIL;
1245 return OK;
1246}
1247# endif
1248
1249# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 int err = FALSE;
1254
1255 set_vim_var_string(VV_FNAME_IN, fname, -1);
1256 set_vim_var_string(VV_CMDARG, args, -1);
1257 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1258 err = TRUE;
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_CMDARG, NULL, -1);
1261
1262 if (err)
1263 {
1264 mch_remove(fname);
1265 return FAIL;
1266 }
1267 return OK;
1268}
1269# endif
1270
1271# if defined(FEAT_DIFF) || defined(PROTO)
1272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001273eval_diff(
1274 char_u *origfile,
1275 char_u *newfile,
1276 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int err = FALSE;
1279
1280 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1281 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1282 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1283 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1284 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1285 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1286 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1287}
1288
1289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001290eval_patch(
1291 char_u *origfile,
1292 char_u *difffile,
1293 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 int err;
1296
1297 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1298 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1299 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1300 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1301 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1302 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1303 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1304}
1305# endif
1306
1307/*
1308 * Top level evaluation function, returning a boolean.
1309 * Sets "error" to TRUE if there was an error.
1310 * Return TRUE or FALSE.
1311 */
1312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001313eval_to_bool(
1314 char_u *arg,
1315 int *error,
1316 char_u **nextcmd,
1317 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
Bram Moolenaar33570922005-01-25 22:26:29 +00001319 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 int retval = FALSE;
1321
1322 if (skip)
1323 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 {
1328 *error = FALSE;
1329 if (!skip)
1330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001331 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334 }
1335 if (skip)
1336 --emsg_skip;
1337
1338 return retval;
1339}
1340
1341/*
1342 * Top level evaluation function, returning a string. If "skip" is TRUE,
1343 * only parsing to "nextcmd" is done, without reporting errors. Return
1344 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1345 */
1346 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347eval_to_string_skip(
1348 char_u *arg,
1349 char_u **nextcmd,
1350 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 char_u *retval;
1354
1355 if (skip)
1356 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 retval = NULL;
1359 else
1360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 retval = vim_strsave(get_tv_string(&tv));
1362 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 if (skip)
1365 --emsg_skip;
1366
1367 return retval;
1368}
1369
1370/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001371 * Skip over an expression at "*pp".
1372 * Return FAIL for an error, OK otherwise.
1373 */
1374 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001375skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001378
1379 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001381}
1382
1383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001385 * When "convert" is TRUE convert a List into a sequence of lines and convert
1386 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 * Return pointer to allocated memory, or NULL for failure.
1388 */
1389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001390eval_to_string(
1391 char_u *arg,
1392 char_u **nextcmd,
1393 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001398#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001399 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 retval = NULL;
1404 else
1405 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001406 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 {
1408 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001409 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001410 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001411 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001412 if (tv.vval.v_list->lv_len > 0)
1413 ga_append(&ga, NL);
1414 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 ga_append(&ga, NUL);
1416 retval = (char_u *)ga.ga_data;
1417 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418#ifdef FEAT_FLOAT
1419 else if (convert && tv.v_type == VAR_FLOAT)
1420 {
1421 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1422 retval = vim_strsave(numbuf);
1423 }
1424#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425 else
1426 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429
1430 return retval;
1431}
1432
1433/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001434 * Call eval_to_string() without using current local variables and using
1435 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 */
1437 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001438eval_to_string_safe(
1439 char_u *arg,
1440 char_u **nextcmd,
1441 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 char_u *retval;
1444 void *save_funccalp;
1445
1446 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001447 if (use_sandbox)
1448 ++sandbox;
1449 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001451 if (use_sandbox)
1452 --sandbox;
1453 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 restore_funccal(save_funccalp);
1455 return retval;
1456}
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458/*
1459 * Top level evaluation function, returning a number.
1460 * Evaluates "expr" silently.
1461 * Returns -1 for an error.
1462 */
1463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001468 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 ++emsg_off;
1471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 retval = -1;
1474 else
1475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001476 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479 --emsg_off;
1480
1481 return retval;
1482}
1483
Bram Moolenaara40058a2005-07-11 22:42:07 +00001484/*
1485 * Prepare v: variable "idx" to be used.
1486 * Save the current typeval in "save_tv".
1487 * When not used yet add the variable to the v: hashtable.
1488 */
1489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001490prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001491{
1492 *save_tv = vimvars[idx].vv_tv;
1493 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1494 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1495}
1496
1497/*
1498 * Restore v: variable "idx" to typeval "save_tv".
1499 * When no longer defined, remove the variable from the v: hashtable.
1500 */
1501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503{
1504 hashitem_T *hi;
1505
Bram Moolenaara40058a2005-07-11 22:42:07 +00001506 vimvars[idx].vv_tv = *save_tv;
1507 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1508 {
1509 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1510 if (HASHITEM_EMPTY(hi))
1511 EMSG2(_(e_intern2), "restore_vimvar()");
1512 else
1513 hash_remove(&vimvarht, hi);
1514 }
1515}
1516
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001517#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001518/*
1519 * Evaluate an expression to a list with suggestions.
1520 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001521 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001522 */
1523 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001525{
1526 typval_T save_val;
1527 typval_T rettv;
1528 list_T *list = NULL;
1529 char_u *p = skipwhite(expr);
1530
1531 /* Set "v:val" to the bad word. */
1532 prepare_vimvar(VV_VAL, &save_val);
1533 vimvars[VV_VAL].vv_type = VAR_STRING;
1534 vimvars[VV_VAL].vv_str = badword;
1535 if (p_verbose == 0)
1536 ++emsg_off;
1537
1538 if (eval1(&p, &rettv, TRUE) == OK)
1539 {
1540 if (rettv.v_type != VAR_LIST)
1541 clear_tv(&rettv);
1542 else
1543 list = rettv.vval.v_list;
1544 }
1545
1546 if (p_verbose == 0)
1547 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001548 restore_vimvar(VV_VAL, &save_val);
1549
1550 return list;
1551}
1552
1553/*
1554 * "list" is supposed to contain two items: a word and a number. Return the
1555 * word in "pp" and the number as the return value.
1556 * Return -1 if anything isn't right.
1557 * Used to get the good word and score from the eval_spell_expr() result.
1558 */
1559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001561{
1562 listitem_T *li;
1563
1564 li = list->lv_first;
1565 if (li == NULL)
1566 return -1;
1567 *pp = get_tv_string(&li->li_tv);
1568
1569 li = li->li_next;
1570 if (li == NULL)
1571 return -1;
1572 return get_tv_number(&li->li_tv);
1573}
1574#endif
1575
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001576/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001577 * Top level evaluation function.
1578 * Returns an allocated typval_T with the result.
1579 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 */
1581 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001582eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583{
1584 typval_T *tv;
1585
1586 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001587 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588 {
1589 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001590 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001591 }
1592
1593 return tv;
1594}
1595
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599 * Uses argv[argc] for the function arguments. Only Number and String
1600 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604call_vim_function(
1605 char_u *func,
1606 int argc,
1607 char_u **argv,
1608 int safe, /* use the sandbox */
1609 int str_arg_only, /* all arguments are strings */
1610 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar33570922005-01-25 22:26:29 +00001612 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 long n;
1614 int len;
1615 int i;
1616 int doesrange;
1617 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001620 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 for (i = 0; i < argc; i++)
1625 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001626 /* Pass a NULL or empty argument as an empty string */
1627 if (argv[i] == NULL || *argv[i] == NUL)
1628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001629 argvars[i].v_type = VAR_STRING;
1630 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001631 continue;
1632 }
1633
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001634 if (str_arg_only)
1635 len = 0;
1636 else
1637 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001638 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (len != 0 && len == (int)STRLEN(argv[i]))
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_NUMBER;
1642 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 else
1645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001646 argvars[i].v_type = VAR_STRING;
1647 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
1649 }
1650
1651 if (safe)
1652 {
1653 save_funccalp = save_funccal();
1654 ++sandbox;
1655 }
1656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1658 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (safe)
1662 {
1663 --sandbox;
1664 restore_funccal(save_funccalp);
1665 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666 vim_free(argvars);
1667
1668 if (ret == FAIL)
1669 clear_tv(rettv);
1670
1671 return ret;
1672}
1673
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001674/*
1675 * Call vimL function "func" and return the result as a number.
1676 * Returns -1 when calling the function fails.
1677 * Uses argv[argc] for the function arguments.
1678 */
1679 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680call_func_retnr(
1681 char_u *func,
1682 int argc,
1683 char_u **argv,
1684 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001685{
1686 typval_T rettv;
1687 long retval;
1688
1689 /* All arguments are passed as strings, no conversion to number. */
1690 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1691 return -1;
1692
1693 retval = get_tv_number_chk(&rettv, NULL);
1694 clear_tv(&rettv);
1695 return retval;
1696}
1697
1698#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1699 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1700
Bram Moolenaar4f688582007-07-24 12:34:30 +00001701# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001703 * Call vimL function "func" and return the result as a string.
1704 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 * Uses argv[argc] for the function arguments.
1706 */
1707 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708call_func_retstr(
1709 char_u *func,
1710 int argc,
1711 char_u **argv,
1712 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713{
1714 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001715 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001717 /* All arguments are passed as strings, no conversion to number. */
1718 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 return NULL;
1720
1721 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 return retval;
1724}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001725# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001727/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001728 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001730 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 */
1732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001733call_func_retlist(
1734 char_u *func,
1735 int argc,
1736 char_u **argv,
1737 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738{
1739 typval_T rettv;
1740
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001741 /* All arguments are passed as strings, no conversion to number. */
1742 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 return NULL;
1744
1745 if (rettv.v_type != VAR_LIST)
1746 {
1747 clear_tv(&rettv);
1748 return NULL;
1749 }
1750
1751 return rettv.vval.v_list;
1752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#endif
1754
1755/*
1756 * Save the current function call pointer, and set it to NULL.
1757 * Used when executing autocommands and for ":source".
1758 */
1759 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 current_funccal = NULL;
1765 return (void *)fc;
1766}
1767
1768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771 funccall_T *fc = (funccall_T *)vfc;
1772
1773 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774}
1775
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776#if defined(FEAT_PROFILE) || defined(PROTO)
1777/*
1778 * Prepare profiling for entering a child or something else that is not
1779 * counted for the script/function itself.
1780 * Should always be called in pair with prof_child_exit().
1781 */
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783prof_child_enter(
1784 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785{
1786 funccall_T *fc = current_funccal;
1787
1788 if (fc != NULL && fc->func->uf_profiling)
1789 profile_start(&fc->prof_child);
1790 script_prof_save(tm);
1791}
1792
1793/*
1794 * Take care of time spent in a child.
1795 * Should always be called after prof_child_enter().
1796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798prof_child_exit(
1799 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001800{
1801 funccall_T *fc = current_funccal;
1802
1803 if (fc != NULL && fc->func->uf_profiling)
1804 {
1805 profile_end(&fc->prof_child);
1806 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1807 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1808 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1809 }
1810 script_prof_restore(tm);
1811}
1812#endif
1813
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#ifdef FEAT_FOLDING
1816/*
1817 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1818 * it in "*cp". Doesn't give error messages.
1819 */
1820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
Bram Moolenaar33570922005-01-25 22:26:29 +00001823 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 int retval;
1825 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001826 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1827 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001830 if (use_sandbox)
1831 ++sandbox;
1832 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 retval = 0;
1836 else
1837 {
1838 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 if (tv.v_type == VAR_NUMBER)
1840 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001841 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 retval = 0;
1843 else
1844 {
1845 /* If the result is a string, check if there is a non-digit before
1846 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (!VIM_ISDIGIT(*s) && *s != '-')
1849 *cp = *s++;
1850 retval = atol((char *)s);
1851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001855 if (use_sandbox)
1856 --sandbox;
1857 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859 return retval;
1860}
1861#endif
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 * ":let" list all variable values
1865 * ":let var1 var2" list variable values
1866 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 * ":let var += expr" assignment command.
1868 * ":let var -= expr" assignment command.
1869 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 */
1872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001873ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 int var_count = 0;
1880 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
Bram Moolenaardb552d602006-03-23 22:59:57 +00001885 argend = skip_var_list(arg, &var_count, &semicolon);
1886 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001888 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1889 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001890 expr = skipwhite(argend);
1891 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1892 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001894 /*
1895 * ":let" without "=": list variables
1896 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 if (*arg == '[')
1898 EMSG(_(e_invarg));
1899 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_glob_vars(&first);
1906 list_buf_vars(&first);
1907 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001908#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001909 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001910#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001911 list_script_vars(&first);
1912 list_func_vars(&first);
1913 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 eap->nextcmd = check_nextcmd(arg);
1916 }
1917 else
1918 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 op[0] = '=';
1920 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001921 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001923 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1924 op[0] = *expr; /* +=, -= or .= */
1925 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001927 else
1928 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (eap->skip)
1934 {
1935 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 --emsg_skip;
1938 }
1939 else if (i != FAIL)
1940 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
1946}
1947
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948/*
1949 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1950 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 * When "nextchars" is not NULL it points to a string with characters that
1952 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1953 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 * Returns OK or FAIL;
1955 */
1956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001957ex_let_vars(
1958 char_u *arg_start,
1959 typval_T *tv,
1960 int copy, /* copy values from "tv", don't move */
1961 int semicolon, /* from skip_var_list() */
1962 int var_count, /* from skip_var_list() */
1963 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964{
1965 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 listitem_T *item;
1969 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970
1971 if (*arg != '[')
1972 {
1973 /*
1974 * ":let var = expr" or ":for var in list"
1975 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 return FAIL;
1978 return OK;
1979 }
1980
1981 /*
1982 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1983 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001984 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 {
1986 EMSG(_(e_listreq));
1987 return FAIL;
1988 }
1989
1990 i = list_len(l);
1991 if (semicolon == 0 && var_count < i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996 if (var_count - semicolon > i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001
2002 item = l->lv_first;
2003 while (*arg != ']')
2004 {
2005 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 item = item->li_next;
2008 if (arg == NULL)
2009 return FAIL;
2010
2011 arg = skipwhite(arg);
2012 if (*arg == ';')
2013 {
2014 /* Put the rest of the list (may be empty) in the var after ';'.
2015 * Create a new list for this. */
2016 l = list_alloc();
2017 if (l == NULL)
2018 return FAIL;
2019 while (item != NULL)
2020 {
2021 list_append_tv(l, &item->li_tv);
2022 item = item->li_next;
2023 }
2024
2025 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002026 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 ltv.vval.v_list = l;
2028 l->lv_refcount = 1;
2029
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2031 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 clear_tv(&ltv);
2033 if (arg == NULL)
2034 return FAIL;
2035 break;
2036 }
2037 else if (*arg != ',' && *arg != ']')
2038 {
2039 EMSG2(_(e_intern2), "ex_let_vars()");
2040 return FAIL;
2041 }
2042 }
2043
2044 return OK;
2045}
2046
2047/*
2048 * Skip over assignable variable "var" or list of variables "[var, var]".
2049 * Used for ":let varvar = expr" and ":for varvar in expr".
2050 * For "[var, var]" increment "*var_count" for each variable.
2051 * for "[var, var; var]" set "semicolon".
2052 * Return NULL for an error.
2053 */
2054 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002055skip_var_list(
2056 char_u *arg,
2057 int *var_count,
2058 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059{
2060 char_u *p, *s;
2061
2062 if (*arg == '[')
2063 {
2064 /* "[var, var]": find the matching ']'. */
2065 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002066 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 {
2068 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2069 s = skip_var_one(p);
2070 if (s == p)
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 ++*var_count;
2076
2077 p = skipwhite(s);
2078 if (*p == ']')
2079 break;
2080 else if (*p == ';')
2081 {
2082 if (*semicolon == 1)
2083 {
2084 EMSG(_("Double ; in list of variables"));
2085 return NULL;
2086 }
2087 *semicolon = 1;
2088 }
2089 else if (*p != ',')
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 }
2095 return p + 1;
2096 }
2097 else
2098 return skip_var_one(arg);
2099}
2100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002102 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002105 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002106skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002108 if (*arg == '@' && arg[1] != NUL)
2109 return arg + 2;
2110 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2111 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112}
2113
Bram Moolenaara7043832005-01-21 11:56:39 +00002114/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 * List variables for hashtab "ht" with prefix "prefix".
2116 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002119list_hashtable_vars(
2120 hashtab_T *ht,
2121 char_u *prefix,
2122 int empty,
2123 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 hashitem_T *hi;
2126 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 int todo;
2128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002129 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2131 {
2132 if (!HASHITEM_EMPTY(hi))
2133 {
2134 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 di = HI2DI(hi);
2136 if (empty || di->di_tv.v_type != VAR_STRING
2137 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 }
2140 }
2141}
2142
2143/*
2144 * List global variables.
2145 */
2146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002147list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List buffer variables.
2154 */
2155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 char_u numbuf[NUMBUFLEN];
2159
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162
2163 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2165 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List window variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002176}
2177
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178#ifdef FEAT_WINDOWS
2179/*
2180 * List tab page variables.
2181 */
2182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002183list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002184{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187}
2188#endif
2189
Bram Moolenaara7043832005-01-21 11:56:39 +00002190/*
2191 * List Vim variables.
2192 */
2193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002194list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197}
2198
2199/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200 * List script-local variables, if there is a script.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2207 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
2211 * List function variables, if there is a function.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002215{
2216 if (current_funccal != NULL)
2217 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219}
2220
2221/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 * List variables in "arg".
2223 */
2224 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002225list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226{
2227 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 char_u *name_start;
2231 char_u *arg_subsc;
2232 char_u *tofree;
2233 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234
2235 while (!ends_excmd(*arg) && !got_int)
2236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2241 {
2242 emsg_severe = TRUE;
2243 EMSG(_(e_trailing));
2244 break;
2245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* get_name_len() takes care of expanding curly braces */
2250 name_start = name = arg;
2251 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2252 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* This is mainly to keep test 49 working: when expanding
2255 * curly braces fails overrule the exception error message. */
2256 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 emsg_severe = TRUE;
2259 EMSG2(_(e_invarg2), arg);
2260 break;
2261 }
2262 error = TRUE;
2263 }
2264 else
2265 {
2266 if (tofree != NULL)
2267 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002268 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 else
2271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* handle d.key, l[idx], f(expr) */
2273 arg_subsc = arg;
2274 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002282 case 'g': list_glob_vars(first); break;
2283 case 'b': list_buf_vars(first); break;
2284 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'v': list_vim_vars(first); break;
2289 case 's': list_script_vars(first); break;
2290 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 default:
2292 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
2295 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 {
2297 char_u numbuf[NUMBUFLEN];
2298 char_u *tf;
2299 int c;
2300 char_u *s;
2301
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002302 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 c = *arg;
2304 *arg = NUL;
2305 list_one_var_a((char_u *)"",
2306 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 tv.v_type,
2308 s == NULL ? (char_u *)"" : s,
2309 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 *arg = c;
2311 vim_free(tf);
2312 }
2313 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320
2321 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323
2324 return arg;
2325}
2326
2327/*
2328 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2329 * Returns a pointer to the char just after the var name.
2330 * Returns NULL if there is an error.
2331 */
2332 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002333ex_let_one(
2334 char_u *arg, /* points to variable name */
2335 typval_T *tv, /* value to assign to variable */
2336 int copy, /* copy value from "tv" */
2337 char_u *endchars, /* valid chars after variable name or NULL */
2338 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
2340 int c1;
2341 char_u *name;
2342 char_u *p;
2343 char_u *arg_end = NULL;
2344 int len;
2345 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347
2348 /*
2349 * ":let $VAR = expr": Set environment variable.
2350 */
2351 if (*arg == '$')
2352 {
2353 /* Find the end of the name. */
2354 ++arg;
2355 name = arg;
2356 len = get_env_len(&arg);
2357 if (len == 0)
2358 EMSG2(_(e_invarg2), name - 1);
2359 else
2360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 if (op != NULL && (*op == '+' || *op == '-'))
2362 EMSG2(_(e_letwrong), op);
2363 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002366 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 {
2368 c1 = name[len];
2369 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 p = get_tv_string_chk(tv);
2371 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 {
2373 int mustfree = FALSE;
2374 char_u *s = vim_getenv(name, &mustfree);
2375
2376 if (s != NULL)
2377 {
2378 p = tofree = concat_str(s, p);
2379 if (mustfree)
2380 vim_free(s);
2381 }
2382 }
2383 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (STRICMP(name, "HOME") == 0)
2387 init_homedir();
2388 else if (didset_vim && STRICMP(name, "VIM") == 0)
2389 didset_vim = FALSE;
2390 else if (didset_vimruntime
2391 && STRICMP(name, "VIMRUNTIME") == 0)
2392 didset_vimruntime = FALSE;
2393 arg_end = arg;
2394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
2398 }
2399 }
2400
2401 /*
2402 * ":let &option = expr": Set option value.
2403 * ":let &l:option = expr": Set local option value.
2404 * ":let &g:option = expr": Set global option value.
2405 */
2406 else if (*arg == '&')
2407 {
2408 /* Find the end of the name. */
2409 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002410 if (p == NULL || (endchars != NULL
2411 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 EMSG(_(e_letunexp));
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 long n;
2416 int opt_type;
2417 long numval;
2418 char_u *stringval = NULL;
2419 char_u *s;
2420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 c1 = *p;
2422 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423
2424 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 s = get_tv_string_chk(tv); /* != NULL if number or string */
2426 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 opt_type = get_option_value(arg, &numval,
2429 &stringval, opt_flags);
2430 if ((opt_type == 1 && *op == '.')
2431 || (opt_type == 0 && *op != '.'))
2432 EMSG2(_(e_letwrong), op);
2433 else
2434 {
2435 if (opt_type == 1) /* number */
2436 {
2437 if (*op == '+')
2438 n = numval + n;
2439 else
2440 n = numval - n;
2441 }
2442 else if (opt_type == 0 && stringval != NULL) /* string */
2443 {
2444 s = concat_str(stringval, s);
2445 vim_free(stringval);
2446 stringval = s;
2447 }
2448 }
2449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 if (s != NULL)
2451 {
2452 set_option_value(arg, n, s, opt_flags);
2453 arg_end = p;
2454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 }
2458 }
2459
2460 /*
2461 * ":let @r = expr": Set register contents.
2462 */
2463 else if (*arg == '@')
2464 {
2465 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (op != NULL && (*op == '+' || *op == '-'))
2467 EMSG2(_(e_letwrong), op);
2468 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 char_u *s;
2475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 p = get_tv_string_chk(tv);
2477 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002479 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (s != NULL)
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(s);
2484 }
2485 }
2486 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 arg_end = arg + 1;
2490 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 }
2493 }
2494
2495 /*
2496 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002499 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002503 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2507 EMSG(_(e_letunexp));
2508 else
2509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516
2517 else
2518 EMSG2(_(e_invarg2), arg);
2519
2520 return arg_end;
2521}
2522
2523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2525 */
2526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002527check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528{
2529 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2530 {
2531 EMSG2(_(e_readonlyvar), arg);
2532 return TRUE;
2533 }
2534 return FALSE;
2535}
2536
2537/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 * Get an lval: variable, Dict item or List item that can be assigned a value
2539 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2540 * "name.key", "name.key[expr]" etc.
2541 * Indexing only works if "name" is an existing List or Dictionary.
2542 * "name" points to the start of the name.
2543 * If "rettv" is not NULL it points to the value to be assigned.
2544 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2545 * wrong; must end in space or cmd separator.
2546 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002547 * flags:
2548 * GLV_QUIET: do not give error messages
2549 * GLV_NO_AUTOLOAD: do not use script autoloading
2550 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002552 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 */
2555 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002556get_lval(
2557 char_u *name,
2558 typval_T *rettv,
2559 lval_T *lp,
2560 int unlet,
2561 int skip,
2562 int flags, /* GLV_ values */
2563 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 char_u *expr_start, *expr_end;
2567 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 dictitem_T *v;
2569 typval_T var1;
2570 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002576 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580
2581 if (skip)
2582 {
2583 /* When skipping just find the end of the name. */
2584 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002585 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 }
2587
2588 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002589 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (expr_start != NULL)
2591 {
2592 /* Don't expand the name when we already know there is an error. */
2593 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2594 && *p != '[' && *p != '.')
2595 {
2596 EMSG(_(e_trailing));
2597 return NULL;
2598 }
2599
2600 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2601 if (lp->ll_exp_name == NULL)
2602 {
2603 /* Report an invalid expression in braces, unless the
2604 * expression evaluation has been cancelled due to an
2605 * aborting error, an interrupt, or an exception. */
2606 if (!aborting() && !quiet)
2607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002608 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 EMSG2(_(e_invarg2), name);
2610 return NULL;
2611 }
2612 }
2613 lp->ll_name = lp->ll_exp_name;
2614 }
2615 else
2616 lp->ll_name = name;
2617
2618 /* Without [idx] or .key we are done. */
2619 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2620 return p;
2621
2622 cc = *p;
2623 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002624 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (v == NULL && !quiet)
2626 EMSG2(_(e_undefvar), lp->ll_name);
2627 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 if (v == NULL)
2629 return NULL;
2630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 /*
2632 * Loop until no more [idx] or .key is following.
2633 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2638 && !(lp->ll_tv->v_type == VAR_DICT
2639 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (!quiet)
2642 EMSG(_("E689: Can only index a List or Dictionary"));
2643 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!quiet)
2648 EMSG(_("E708: [:] must come last"));
2649 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 len = -1;
2653 if (*p == '.')
2654 {
2655 key = p + 1;
2656 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2657 ;
2658 if (len == 0)
2659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_(e_emptykey));
2662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = key + len;
2665 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 else
2667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (*p == ':')
2671 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 else
2673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 empty1 = FALSE;
2675 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var1) == NULL)
2678 {
2679 /* not a number or string */
2680 clear_tv(&var1);
2681 return NULL;
2682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
2684
2685 /* Optionally get the second index [ :expr]. */
2686 if (*p == ':')
2687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 if (!empty1)
2693 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (rettv != NULL && (rettv->v_type != VAR_LIST
2697 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705 p = skipwhite(p + 1);
2706 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 else
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 if (get_tv_string_chk(&var2) == NULL)
2718 {
2719 /* not a number or string */
2720 if (!empty1)
2721 clear_tv(&var1);
2722 clear_tv(&var2);
2723 return NULL;
2724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002730
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!quiet)
2734 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
2741
2742 /* Skip to past ']'. */
2743 ++p;
2744 }
2745
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 {
2748 if (len == -1)
2749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (*key == NUL)
2753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (!quiet)
2755 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 lp->ll_list = NULL;
2761 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002762 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002764 /* When assigning to a scope dictionary check that a function and
2765 * variable name is valid (only variable name unless it is l: or
2766 * g: dictionary). Disallow overwriting a builtin function. */
2767 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 int prevval;
2770 int wrong;
2771
2772 if (len != -1)
2773 {
2774 prevval = key[len];
2775 key[len] = NUL;
2776 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002777 else
2778 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002779 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2780 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002782 || !valid_varname(key);
2783 if (len != -1)
2784 key[len] = prevval;
2785 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 return NULL;
2787 }
2788
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 /* Can't add "v:" variable. */
2792 if (lp->ll_dict == &vimvardict)
2793 {
2794 EMSG2(_(e_illvar), name);
2795 return NULL;
2796 }
2797
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002798 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 }
2807 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 if (len == -1)
2812 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 p = NULL;
2815 break;
2816 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002817 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002818 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002819 return NULL;
2820
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (len == -1)
2822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 }
2825 else
2826 {
2827 /*
2828 * Get the number and item for the only or first index of the List.
2829 */
2830 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 else
2833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002834 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 clear_tv(&var1);
2836 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_list = lp->ll_tv->vval.v_list;
2839 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2840 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002842 if (lp->ll_n1 < 0)
2843 {
2844 lp->ll_n1 = 0;
2845 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2846 }
2847 }
2848 if (lp->ll_li == NULL)
2849 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 if (!quiet)
2853 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 }
2856
2857 /*
2858 * May need to find the item or absolute index for the second
2859 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 * When no index given: "lp->ll_empty2" is TRUE.
2861 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002865 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 {
2872 if (!quiet)
2873 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2880 if (lp->ll_n1 < 0)
2881 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2882 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 {
2884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002888 }
2889
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 }
2893
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return p;
2895}
2896
2897/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 */
2900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902{
2903 vim_free(lp->ll_exp_name);
2904 vim_free(lp->ll_newkey);
2905}
2906
2907/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002908 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913set_var_lval(
2914 lval_T *lp,
2915 char_u *endp,
2916 typval_T *rettv,
2917 int copy,
2918 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919{
2920 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 listitem_T *ri;
2922 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923
2924 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 cc = *endp;
2929 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002934 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002935 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002936 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 if ((di == NULL
2940 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2941 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2942 FALSE)))
2943 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 set_var(lp->ll_name, &tv, FALSE);
2945 clear_tv(&tv);
2946 }
2947 }
2948 else
2949 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 else if (tv_check_lock(lp->ll_newkey == NULL
2954 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002955 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 else if (lp->ll_range)
2958 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002959 listitem_T *ll_li = lp->ll_li;
2960 int ll_n1 = lp->ll_n1;
2961
2962 /*
2963 * Check whether any of the list items is locked
2964 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002965 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 return;
2969 ri = ri->li_next;
2970 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2971 break;
2972 ll_li = ll_li->li_next;
2973 ++ll_n1;
2974 }
2975
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 /*
2977 * Assign the List values to the list items.
2978 */
2979 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002980 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 if (op != NULL && *op != '=')
2982 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2983 else
2984 {
2985 clear_tv(&lp->ll_li->li_tv);
2986 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2990 break;
2991 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002994 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 ri = NULL;
2997 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 lp->ll_li = lp->ll_li->li_next;
3001 ++lp->ll_n1;
3002 }
3003 if (ri != NULL)
3004 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 else if (lp->ll_empty2
3006 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 : lp->ll_n1 != lp->ll_n2)
3008 EMSG(_("E711: List value has not enough items"));
3009 }
3010 else
3011 {
3012 /*
3013 * Assign to a List or Dictionary item.
3014 */
3015 if (lp->ll_newkey != NULL)
3016 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 if (op != NULL && *op != '=')
3018 {
3019 EMSG2(_(e_letwrong), op);
3020 return;
3021 }
3022
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 if (di == NULL)
3026 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3028 {
3029 vim_free(di);
3030 return;
3031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 else if (op != NULL && *op != '=')
3035 {
3036 tv_op(lp->ll_tv, rettv, op);
3037 return;
3038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /*
3043 * Assign the value to the variable or list item.
3044 */
3045 if (copy)
3046 copy_tv(rettv, lp->ll_tv);
3047 else
3048 {
3049 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003050 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 }
3053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054}
3055
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3058 * Returns OK or FAIL.
3059 */
3060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003061tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062{
3063 long n;
3064 char_u numbuf[NUMBUFLEN];
3065 char_u *s;
3066
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003067 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3068 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3069 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 {
3071 switch (tv1->v_type)
3072 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003073 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 case VAR_DICT:
3075 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003076 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003077 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078 break;
3079
3080 case VAR_LIST:
3081 if (*op != '+' || tv2->v_type != VAR_LIST)
3082 break;
3083 /* List += List */
3084 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3085 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3086 return OK;
3087
3088 case VAR_NUMBER:
3089 case VAR_STRING:
3090 if (tv2->v_type == VAR_LIST)
3091 break;
3092 if (*op == '+' || *op == '-')
3093 {
3094 /* nr += nr or nr -= nr*/
3095 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003096#ifdef FEAT_FLOAT
3097 if (tv2->v_type == VAR_FLOAT)
3098 {
3099 float_T f = n;
3100
3101 if (*op == '+')
3102 f += tv2->vval.v_float;
3103 else
3104 f -= tv2->vval.v_float;
3105 clear_tv(tv1);
3106 tv1->v_type = VAR_FLOAT;
3107 tv1->vval.v_float = f;
3108 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110#endif
3111 {
3112 if (*op == '+')
3113 n += get_tv_number(tv2);
3114 else
3115 n -= get_tv_number(tv2);
3116 clear_tv(tv1);
3117 tv1->v_type = VAR_NUMBER;
3118 tv1->vval.v_number = n;
3119 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 }
3121 else
3122 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123 if (tv2->v_type == VAR_FLOAT)
3124 break;
3125
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 /* str .= str */
3127 s = get_tv_string(tv1);
3128 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_STRING;
3131 tv1->vval.v_string = s;
3132 }
3133 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134
3135#ifdef FEAT_FLOAT
3136 case VAR_FLOAT:
3137 {
3138 float_T f;
3139
3140 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3141 && tv2->v_type != VAR_NUMBER
3142 && tv2->v_type != VAR_STRING))
3143 break;
3144 if (tv2->v_type == VAR_FLOAT)
3145 f = tv2->vval.v_float;
3146 else
3147 f = get_tv_number(tv2);
3148 if (*op == '+')
3149 tv1->vval.v_float += f;
3150 else
3151 tv1->vval.v_float -= f;
3152 }
3153 return OK;
3154#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003155 }
3156 }
3157
3158 EMSG2(_(e_letwrong), op);
3159 return FAIL;
3160}
3161
3162/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163 * Add a watcher to a list.
3164 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003166list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167{
3168 lw->lw_next = l->lv_watch;
3169 l->lv_watch = lw;
3170}
3171
3172/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003173 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 * No warning when it isn't found...
3175 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 lwp = &l->lv_watch;
3182 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3183 {
3184 if (lw == lwrem)
3185 {
3186 *lwp = lw->lw_next;
3187 break;
3188 }
3189 lwp = &lw->lw_next;
3190 }
3191}
3192
3193/*
3194 * Just before removing an item from a list: advance watchers to the next
3195 * item.
3196 */
3197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 if (lw->lw_item == item)
3204 lw->lw_item = item->li_next;
3205}
3206
3207/*
3208 * Evaluate the expression used in a ":for var in expr" command.
3209 * "arg" points to "var".
3210 * Set "*errp" to TRUE for an error, FALSE otherwise;
3211 * Return a pointer that holds the info. Null when there is an error.
3212 */
3213 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214eval_for_line(
3215 char_u *arg,
3216 int *errp,
3217 char_u **nextcmdp,
3218 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 typval_T tv;
3223 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224
3225 *errp = TRUE; /* default: there is an error */
3226
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 if (fi == NULL)
3229 return NULL;
3230
3231 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3232 if (expr == NULL)
3233 return fi;
3234
3235 expr = skipwhite(expr);
3236 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3237 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003238 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 return fi;
3240 }
3241
3242 if (skip)
3243 ++emsg_skip;
3244 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3245 {
3246 *errp = FALSE;
3247 if (!skip)
3248 {
3249 l = tv.vval.v_list;
3250 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003251 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 clear_tv(&tv);
3254 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 else
3256 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003257 /* No need to increment the refcount, it's already set for the
3258 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 fi->fi_list = l;
3260 list_add_watch(l, &fi->fi_lw);
3261 fi->fi_lw.lw_item = l->lv_first;
3262 }
3263 }
3264 }
3265 if (skip)
3266 --emsg_skip;
3267
3268 return fi;
3269}
3270
3271/*
3272 * Use the first item in a ":for" list. Advance to the next.
3273 * Assign the values to the variable (list). "arg" points to the first one.
3274 * Return TRUE when a valid item was found, FALSE when at end of list or
3275 * something wrong.
3276 */
3277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003278next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003280 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283
3284 item = fi->fi_lw.lw_item;
3285 if (item == NULL)
3286 result = FALSE;
3287 else
3288 {
3289 fi->fi_lw.lw_item = item->li_next;
3290 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3291 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3292 }
3293 return result;
3294}
3295
3296/*
3297 * Free the structure used to store info used by ":for".
3298 */
3299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003300free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301{
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003304 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 list_unref(fi->fi_list);
3308 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 vim_free(fi);
3310}
3311
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3313
3314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315set_context_for_expression(
3316 expand_T *xp,
3317 char_u *arg,
3318 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 int got_eq = FALSE;
3321 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 if (cmdidx == CMD_let)
3325 {
3326 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003327 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 {
3329 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003330 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 {
3332 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003333 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 if (vim_iswhite(*p))
3335 break;
3336 }
3337 return;
3338 }
3339 }
3340 else
3341 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3342 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 while ((xp->xp_pattern = vim_strpbrk(arg,
3344 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3345 {
3346 c = *xp->xp_pattern;
3347 if (c == '&')
3348 {
3349 c = xp->xp_pattern[1];
3350 if (c == '&')
3351 {
3352 ++xp->xp_pattern;
3353 xp->xp_context = cmdidx != CMD_let || got_eq
3354 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3355 }
3356 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003359 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3360 xp->xp_pattern += 2;
3361
3362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364 else if (c == '$')
3365 {
3366 /* environment variable */
3367 xp->xp_context = EXPAND_ENV_VARS;
3368 }
3369 else if (c == '=')
3370 {
3371 got_eq = TRUE;
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003374 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 && xp->xp_context == EXPAND_FUNCTIONS
3376 && vim_strchr(xp->xp_pattern, '(') == NULL)
3377 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003378 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 break;
3380 }
3381 else if (cmdidx != CMD_let || got_eq)
3382 {
3383 if (c == '"') /* string */
3384 {
3385 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3386 if (c == '\\' && xp->xp_pattern[1] != NUL)
3387 ++xp->xp_pattern;
3388 xp->xp_context = EXPAND_NOTHING;
3389 }
3390 else if (c == '\'') /* literal string */
3391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003392 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3394 /* skip */ ;
3395 xp->xp_context = EXPAND_NOTHING;
3396 }
3397 else if (c == '|')
3398 {
3399 if (xp->xp_pattern[1] == '|')
3400 {
3401 ++xp->xp_pattern;
3402 xp->xp_context = EXPAND_EXPRESSION;
3403 }
3404 else
3405 xp->xp_context = EXPAND_COMMANDS;
3406 }
3407 else
3408 xp->xp_context = EXPAND_EXPRESSION;
3409 }
3410 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003411 /* Doesn't look like something valid, expand as an expression
3412 * anyway. */
3413 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 arg = xp->xp_pattern;
3415 if (*arg != NUL)
3416 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3417 /* skip */ ;
3418 }
3419 xp->xp_pattern = arg;
3420}
3421
3422#endif /* FEAT_CMDL_COMPL */
3423
3424/*
3425 * ":1,25call func(arg1, arg2)" function call.
3426 */
3427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003428ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 char_u *arg = eap->arg;
3431 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003435 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 linenr_T lnum;
3437 int doesrange;
3438 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003439 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003441 if (eap->skip)
3442 {
3443 /* trans_function_name() doesn't work well when skipping, use eval0()
3444 * instead to skip to any following command, e.g. for:
3445 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003446 ++emsg_skip;
3447 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3448 clear_tv(&rettv);
3449 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003450 return;
3451 }
3452
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003453 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003454 if (fudi.fd_newkey != NULL)
3455 {
3456 /* Still need to give an error message for missing key. */
3457 EMSG2(_(e_dictkey), fudi.fd_newkey);
3458 vim_free(fudi.fd_newkey);
3459 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003460 if (tofree == NULL)
3461 return;
3462
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003463 /* Increase refcount on dictionary, it could get deleted when evaluating
3464 * the arguments. */
3465 if (fudi.fd_dict != NULL)
3466 ++fudi.fd_dict->dv_refcount;
3467
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003468 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003469 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003470 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar532c7802005-01-27 14:44:31 +00003472 /* Skip white space to allow ":call func ()". Not good, but required for
3473 * backward compatibility. */
3474 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 if (*startarg != '(')
3478 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003479 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 goto end;
3481 }
3482
3483 /*
3484 * When skipping, evaluate the function once, to find the end of the
3485 * arguments.
3486 * When the function takes a range, this is discovered after the first
3487 * call, and the loop is broken.
3488 */
3489 if (eap->skip)
3490 {
3491 ++emsg_skip;
3492 lnum = eap->line2; /* do it once, also with an invalid range */
3493 }
3494 else
3495 lnum = eap->line1;
3496 for ( ; lnum <= eap->line2; ++lnum)
3497 {
3498 if (!eap->skip && eap->addr_count > 0)
3499 {
3500 curwin->w_cursor.lnum = lnum;
3501 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003502#ifdef FEAT_VIRTUALEDIT
3503 curwin->w_cursor.coladd = 0;
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003507 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003508 eap->line1, eap->line2, &doesrange,
3509 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 failed = TRUE;
3512 break;
3513 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003514
3515 /* Handle a function returning a Funcref, Dictionary or List. */
3516 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3517 {
3518 failed = TRUE;
3519 break;
3520 }
3521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (doesrange || eap->skip)
3524 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003529 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (aborting())
3531 break;
3532 }
3533 if (eap->skip)
3534 --emsg_skip;
3535
3536 if (!failed)
3537 {
3538 /* Check for trailing illegal characters and a following command. */
3539 if (!ends_excmd(*arg))
3540 {
3541 emsg_severe = TRUE;
3542 EMSG(_(e_trailing));
3543 }
3544 else
3545 eap->nextcmd = check_nextcmd(arg);
3546 }
3547
3548end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003549 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003550 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553/*
3554 * ":unlet[!] var1 ... " command.
3555 */
3556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003559 ex_unletlock(eap, eap->arg, 0);
3560}
3561
3562/*
3563 * ":lockvar" and ":unlockvar" commands
3564 */
3565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int deep = 2;
3570
3571 if (eap->forceit)
3572 deep = -1;
3573 else if (vim_isdigit(*arg))
3574 {
3575 deep = getdigits(&arg);
3576 arg = skipwhite(arg);
3577 }
3578
3579 ex_unletlock(eap, arg, deep);
3580}
3581
3582/*
3583 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3584 */
3585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_unletlock(
3587 exarg_T *eap,
3588 char_u *argstart,
3589 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590{
3591 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 do
3597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003599 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003600 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (lv.ll_name == NULL)
3602 error = TRUE; /* error but continue parsing */
3603 if (name_end == NULL || (!vim_iswhite(*name_end)
3604 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (name_end != NULL)
3607 {
3608 emsg_severe = TRUE;
3609 EMSG(_(e_trailing));
3610 }
3611 if (!(eap->skip || error))
3612 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
3615
3616 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 {
3618 if (eap->cmdidx == CMD_unlet)
3619 {
3620 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3621 error = TRUE;
3622 }
3623 else
3624 {
3625 if (do_lock_var(&lv, name_end, deep,
3626 eap->cmdidx == CMD_lockvar) == FAIL)
3627 error = TRUE;
3628 }
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (!eap->skip)
3632 clear_lval(&lv);
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 arg = skipwhite(name_end);
3635 } while (!ends_excmd(*arg));
3636
3637 eap->nextcmd = check_nextcmd(arg);
3638}
3639
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641do_unlet_var(
3642 lval_T *lp,
3643 char_u *name_end,
3644 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 int ret = OK;
3647 int cc;
3648
3649 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 cc = *name_end;
3652 *name_end = NUL;
3653
3654 /* Normal name or expanded name. */
3655 if (check_changedtick(lp->ll_name))
3656 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003661 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003662 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003663 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003664 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 else if (lp->ll_range)
3667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003668 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003669 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003670 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003671
3672 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3673 {
3674 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003676 return FAIL;
3677 ll_li = li;
3678 ++ll_n1;
3679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680
3681 /* Delete a range of List items. */
3682 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3683 {
3684 li = lp->ll_li->li_next;
3685 listitem_remove(lp->ll_list, lp->ll_li);
3686 lp->ll_li = li;
3687 ++lp->ll_n1;
3688 }
3689 }
3690 else
3691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 /* unlet a List item. */
3694 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003696 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003697 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 }
3699
3700 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
3704 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
3707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
Bram Moolenaar33570922005-01-25 22:26:29 +00003710 hashtab_T *ht;
3711 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003712 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003713 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003714 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 ht = find_var_ht(name, &varname);
3717 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003719 if (ht == &globvarht)
3720 d = &globvardict;
3721 else if (current_funccal != NULL
3722 && ht == &current_funccal->l_vars.dv_hashtab)
3723 d = &current_funccal->l_vars;
3724 else if (ht == &compat_hashtab)
3725 d = &vimvardict;
3726 else
3727 {
3728 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3729 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3730 }
3731 if (d == NULL)
3732 {
3733 EMSG2(_(e_intern2), "do_unlet()");
3734 return FAIL;
3735 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hi = hash_find(ht, varname);
3737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003739 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003740 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003741 || var_check_ro(di->di_flags, name, FALSE)
3742 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003743 return FAIL;
3744
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745 delete_var(ht, hi);
3746 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 if (forceit)
3750 return OK;
3751 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return FAIL;
3753}
3754
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003755/*
3756 * Lock or unlock variable indicated by "lp".
3757 * "deep" is the levels to go (-1 for unlimited);
3758 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3759 */
3760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761do_lock_var(
3762 lval_T *lp,
3763 char_u *name_end,
3764 int deep,
3765 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766{
3767 int ret = OK;
3768 int cc;
3769 dictitem_T *di;
3770
3771 if (deep == 0) /* nothing to do */
3772 return OK;
3773
3774 if (lp->ll_tv == NULL)
3775 {
3776 cc = *name_end;
3777 *name_end = NUL;
3778
3779 /* Normal name or expanded name. */
3780 if (check_changedtick(lp->ll_name))
3781 ret = FAIL;
3782 else
3783 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003784 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003785 if (di == NULL)
3786 ret = FAIL;
3787 else
3788 {
3789 if (lock)
3790 di->di_flags |= DI_FLAGS_LOCK;
3791 else
3792 di->di_flags &= ~DI_FLAGS_LOCK;
3793 item_lock(&di->di_tv, deep, lock);
3794 }
3795 }
3796 *name_end = cc;
3797 }
3798 else if (lp->ll_range)
3799 {
3800 listitem_T *li = lp->ll_li;
3801
3802 /* (un)lock a range of List items. */
3803 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3804 {
3805 item_lock(&li->li_tv, deep, lock);
3806 li = li->li_next;
3807 ++lp->ll_n1;
3808 }
3809 }
3810 else if (lp->ll_list != NULL)
3811 /* (un)lock a List item. */
3812 item_lock(&lp->ll_li->li_tv, deep, lock);
3813 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003814 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 item_lock(&lp->ll_di->di_tv, deep, lock);
3816
3817 return ret;
3818}
3819
3820/*
3821 * Lock or unlock an item. "deep" is nr of levels to go.
3822 */
3823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003824item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003825{
3826 static int recurse = 0;
3827 list_T *l;
3828 listitem_T *li;
3829 dict_T *d;
3830 hashitem_T *hi;
3831 int todo;
3832
3833 if (recurse >= DICT_MAXNEST)
3834 {
3835 EMSG(_("E743: variable nested too deep for (un)lock"));
3836 return;
3837 }
3838 if (deep == 0)
3839 return;
3840 ++recurse;
3841
3842 /* lock/unlock the item itself */
3843 if (lock)
3844 tv->v_lock |= VAR_LOCKED;
3845 else
3846 tv->v_lock &= ~VAR_LOCKED;
3847
3848 switch (tv->v_type)
3849 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003850 case VAR_UNKNOWN:
3851 case VAR_NUMBER:
3852 case VAR_STRING:
3853 case VAR_FUNC:
3854 case VAR_FLOAT:
3855 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003856 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003857 break;
3858
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859 case VAR_LIST:
3860 if ((l = tv->vval.v_list) != NULL)
3861 {
3862 if (lock)
3863 l->lv_lock |= VAR_LOCKED;
3864 else
3865 l->lv_lock &= ~VAR_LOCKED;
3866 if (deep < 0 || deep > 1)
3867 /* recursive: lock/unlock the items the List contains */
3868 for (li = l->lv_first; li != NULL; li = li->li_next)
3869 item_lock(&li->li_tv, deep - 1, lock);
3870 }
3871 break;
3872 case VAR_DICT:
3873 if ((d = tv->vval.v_dict) != NULL)
3874 {
3875 if (lock)
3876 d->dv_lock |= VAR_LOCKED;
3877 else
3878 d->dv_lock &= ~VAR_LOCKED;
3879 if (deep < 0 || deep > 1)
3880 {
3881 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003882 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003883 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3884 {
3885 if (!HASHITEM_EMPTY(hi))
3886 {
3887 --todo;
3888 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3889 }
3890 }
3891 }
3892 }
3893 }
3894 --recurse;
3895}
3896
Bram Moolenaara40058a2005-07-11 22:42:07 +00003897/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003898 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3899 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003900 */
3901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003902tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003903{
3904 return (tv->v_lock & VAR_LOCKED)
3905 || (tv->v_type == VAR_LIST
3906 && tv->vval.v_list != NULL
3907 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3908 || (tv->v_type == VAR_DICT
3909 && tv->vval.v_dict != NULL
3910 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3911}
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3914/*
3915 * Delete all "menutrans_" variables.
3916 */
3917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003924 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 {
3927 if (!HASHITEM_EMPTY(hi))
3928 {
3929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3931 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 }
3933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936#endif
3937
3938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3939
3940/*
3941 * Local string buffer for the next two functions to store a variable name
3942 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3943 * get_user_var_name().
3944 */
3945
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003946static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
3948static char_u *varnamebuf = NULL;
3949static int varnamebuflen = 0;
3950
3951/*
3952 * Function to concatenate a prefix and a variable name.
3953 */
3954 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003955cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 int len;
3958
3959 len = (int)STRLEN(name) + 3;
3960 if (len > varnamebuflen)
3961 {
3962 vim_free(varnamebuf);
3963 len += 10; /* some additional space */
3964 varnamebuf = alloc(len);
3965 if (varnamebuf == NULL)
3966 {
3967 varnamebuflen = 0;
3968 return NULL;
3969 }
3970 varnamebuflen = len;
3971 }
3972 *varnamebuf = prefix;
3973 varnamebuf[1] = ':';
3974 STRCPY(varnamebuf + 2, name);
3975 return varnamebuf;
3976}
3977
3978/*
3979 * Function given to ExpandGeneric() to obtain the list of user defined
3980 * (global/buffer/window/built-in) variable names.
3981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003985 static long_u gdone;
3986 static long_u bdone;
3987 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988#ifdef FEAT_WINDOWS
3989 static long_u tdone;
3990#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 static int vidx;
3992 static hashitem_T *hi;
3993 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003996 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998#ifdef FEAT_WINDOWS
3999 tdone = 0;
4000#endif
4001 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004002
4003 /* Global variables */
4004 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004008 else
4009 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 while (HASHITEM_EMPTY(hi))
4011 ++hi;
4012 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4013 return cat_prefix_varname('g', hi->hi_key);
4014 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004016
4017 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004018 ht = &curbuf->b_vars->dv_hashtab;
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 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 else
4024 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 while (HASHITEM_EMPTY(hi))
4026 ++hi;
4027 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 return (char_u *)"b:changedtick";
4033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004039 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004048#ifdef FEAT_WINDOWS
4049 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 if (tdone < ht->ht_used)
4052 {
4053 if (tdone++ == 0)
4054 hi = ht->ht_array;
4055 else
4056 ++hi;
4057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('t', hi->hi_key);
4060 }
4061#endif
4062
Bram Moolenaar33570922005-01-25 22:26:29 +00004063 /* v: variables */
4064 if (vidx < VV_LEN)
4065 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 vim_free(varnamebuf);
4068 varnamebuf = NULL;
4069 varnamebuflen = 0;
4070 return NULL;
4071}
4072
4073#endif /* FEAT_CMDL_COMPL */
4074
4075/*
4076 * types for expressions.
4077 */
4078typedef enum
4079{
4080 TYPE_UNKNOWN = 0
4081 , TYPE_EQUAL /* == */
4082 , TYPE_NEQUAL /* != */
4083 , TYPE_GREATER /* > */
4084 , TYPE_GEQUAL /* >= */
4085 , TYPE_SMALLER /* < */
4086 , TYPE_SEQUAL /* <= */
4087 , TYPE_MATCH /* =~ */
4088 , TYPE_NOMATCH /* !~ */
4089} exptype_T;
4090
4091/*
4092 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4095 */
4096
4097/*
4098 * Handle zero level expression.
4099 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004101 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 * Return OK or FAIL.
4103 */
4104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004105eval0(
4106 char_u *arg,
4107 typval_T *rettv,
4108 char_u **nextcmd,
4109 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 int ret;
4112 char_u *p;
4113
4114 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (ret == FAIL || !ends_excmd(*p))
4117 {
4118 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /*
4121 * Report the invalid expression unless the expression evaluation has
4122 * been cancelled due to an aborting error, an interrupt, or an
4123 * exception.
4124 */
4125 if (!aborting())
4126 EMSG2(_(e_invexpr2), arg);
4127 ret = FAIL;
4128 }
4129 if (nextcmd != NULL)
4130 *nextcmd = check_nextcmd(p);
4131
4132 return ret;
4133}
4134
4135/*
4136 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004137 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 *
4139 * "arg" must point to the first non-white of the expression.
4140 * "arg" is advanced to the next non-white after the recognized expression.
4141 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004142 * Note: "rettv.v_lock" is not set.
4143 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 * Return OK or FAIL.
4145 */
4146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004147eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
4149 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 if ((*arg)[0] == '?')
4159 {
4160 result = FALSE;
4161 if (evaluate)
4162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 int error = FALSE;
4164
4165 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (error)
4169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Check for the ":".
4181 */
4182 if ((*arg)[0] != ':')
4183 {
4184 EMSG(_("E109: Missing ':' after '?'"));
4185 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return FAIL;
4188 }
4189
4190 /*
4191 * Get the third variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
4194 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4195 {
4196 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199 }
4200 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203
4204 return OK;
4205}
4206
4207/*
4208 * Handle first level expression:
4209 * expr2 || expr2 || expr2 logical OR
4210 *
4211 * "arg" must point to the first non-white of the expression.
4212 * "arg" is advanced to the next non-white after the recognized expression.
4213 *
4214 * Return OK or FAIL.
4215 */
4216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004217eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
Bram Moolenaar33570922005-01-25 22:26:29 +00004219 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 long result;
4221 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /*
4225 * Get the first variable.
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229
4230 /*
4231 * Repeat until there is no following "||".
4232 */
4233 first = TRUE;
4234 result = FALSE;
4235 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4236 {
4237 if (evaluate && first)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (error)
4243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 first = FALSE;
4245 }
4246
4247 /*
4248 * Get the second variable.
4249 */
4250 *arg = skipwhite(*arg + 2);
4251 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4252 return FAIL;
4253
4254 /*
4255 * Compute the result.
4256 */
4257 if (evaluate && !result)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 if (evaluate)
4266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_NUMBER;
4268 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271
4272 return OK;
4273}
4274
4275/*
4276 * Handle second level expression:
4277 * expr3 && expr3 && expr3 logical AND
4278 *
4279 * "arg" must point to the first non-white of the expression.
4280 * "arg" is advanced to the next non-white after the recognized expression.
4281 *
4282 * Return OK or FAIL.
4283 */
4284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
Bram Moolenaar33570922005-01-25 22:26:29 +00004287 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 long result;
4289 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /*
4293 * Get the first variable.
4294 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297
4298 /*
4299 * Repeat until there is no following "&&".
4300 */
4301 first = TRUE;
4302 result = TRUE;
4303 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4304 {
4305 if (evaluate && first)
4306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004307 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 if (error)
4311 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 first = FALSE;
4313 }
4314
4315 /*
4316 * Get the second variable.
4317 */
4318 *arg = skipwhite(*arg + 2);
4319 if (eval4(arg, &var2, evaluate && result) == FAIL)
4320 return FAIL;
4321
4322 /*
4323 * Compute the result.
4324 */
4325 if (evaluate && result)
4326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (error)
4331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333 if (evaluate)
4334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 rettv->v_type = VAR_NUMBER;
4336 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 }
4339
4340 return OK;
4341}
4342
4343/*
4344 * Handle third level expression:
4345 * var1 == var2
4346 * var1 =~ var2
4347 * var1 != var2
4348 * var1 !~ var2
4349 * var1 > var2
4350 * var1 >= var2
4351 * var1 < var2
4352 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 * var1 is var2
4354 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 *
4356 * "arg" must point to the first non-white of the expression.
4357 * "arg" is advanced to the next non-white after the recognized expression.
4358 *
4359 * Return OK or FAIL.
4360 */
4361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004362eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
Bram Moolenaar33570922005-01-25 22:26:29 +00004364 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 char_u *p;
4366 int i;
4367 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 int len = 2;
4370 long n1, n2;
4371 char_u *s1, *s2;
4372 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4373 regmatch_T regmatch;
4374 int ic;
4375 char_u *save_cpo;
4376
4377 /*
4378 * Get the first variable.
4379 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 return FAIL;
4382
4383 p = *arg;
4384 switch (p[0])
4385 {
4386 case '=': if (p[1] == '=')
4387 type = TYPE_EQUAL;
4388 else if (p[1] == '~')
4389 type = TYPE_MATCH;
4390 break;
4391 case '!': if (p[1] == '=')
4392 type = TYPE_NEQUAL;
4393 else if (p[1] == '~')
4394 type = TYPE_NOMATCH;
4395 break;
4396 case '>': if (p[1] != '=')
4397 {
4398 type = TYPE_GREATER;
4399 len = 1;
4400 }
4401 else
4402 type = TYPE_GEQUAL;
4403 break;
4404 case '<': if (p[1] != '=')
4405 {
4406 type = TYPE_SMALLER;
4407 len = 1;
4408 }
4409 else
4410 type = TYPE_SEQUAL;
4411 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 case 'i': if (p[1] == 's')
4413 {
4414 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4415 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004416 i = p[len];
4417 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 {
4419 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4420 type_is = TRUE;
4421 }
4422 }
4423 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
4426 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004427 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
4429 if (type != TYPE_UNKNOWN)
4430 {
4431 /* extra question mark appended: ignore case */
4432 if (p[len] == '?')
4433 {
4434 ic = TRUE;
4435 ++len;
4436 }
4437 /* extra '#' appended: match case */
4438 else if (p[len] == '#')
4439 {
4440 ic = FALSE;
4441 ++len;
4442 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 else
4445 ic = p_ic;
4446
4447 /*
4448 * Get the second variable.
4449 */
4450 *arg = skipwhite(p + len);
4451 if (eval5(arg, &var2, evaluate) == FAIL)
4452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004453 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 return FAIL;
4455 }
4456
4457 if (evaluate)
4458 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004459 if (type_is && rettv->v_type != var2.v_type)
4460 {
4461 /* For "is" a different type always means FALSE, for "notis"
4462 * it means TRUE. */
4463 n1 = (type == TYPE_NEQUAL);
4464 }
4465 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4466 {
4467 if (type_is)
4468 {
4469 n1 = (rettv->v_type == var2.v_type
4470 && rettv->vval.v_list == var2.vval.v_list);
4471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 else if (rettv->v_type != var2.v_type
4475 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4476 {
4477 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004480 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 clear_tv(rettv);
4482 clear_tv(&var2);
4483 return FAIL;
4484 }
4485 else
4486 {
4487 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004488 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4489 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 }
4494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004495 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4496 {
4497 if (type_is)
4498 {
4499 n1 = (rettv->v_type == var2.v_type
4500 && rettv->vval.v_dict == var2.vval.v_dict);
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 else if (rettv->v_type != var2.v_type
4505 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4506 {
4507 if (rettv->v_type != var2.v_type)
4508 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4509 else
4510 EMSG(_("E736: Invalid operation for Dictionary"));
4511 clear_tv(rettv);
4512 clear_tv(&var2);
4513 return FAIL;
4514 }
4515 else
4516 {
4517 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004518 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4519 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 }
4524
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4526 {
4527 if (rettv->v_type != var2.v_type
4528 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4529 {
4530 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004531 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004532 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004533 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 else
4539 {
4540 /* Compare two Funcrefs for being equal or unequal. */
4541 if (rettv->vval.v_string == NULL
4542 || var2.vval.v_string == NULL)
4543 n1 = FALSE;
4544 else
4545 n1 = STRCMP(rettv->vval.v_string,
4546 var2.vval.v_string) == 0;
4547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 }
4551
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004552#ifdef FEAT_FLOAT
4553 /*
4554 * If one of the two variables is a float, compare as a float.
4555 * When using "=~" or "!~", always compare as string.
4556 */
4557 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4558 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4559 {
4560 float_T f1, f2;
4561
4562 if (rettv->v_type == VAR_FLOAT)
4563 f1 = rettv->vval.v_float;
4564 else
4565 f1 = get_tv_number(rettv);
4566 if (var2.v_type == VAR_FLOAT)
4567 f2 = var2.vval.v_float;
4568 else
4569 f2 = get_tv_number(&var2);
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (f1 == f2); break;
4574 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4575 case TYPE_GREATER: n1 = (f1 > f2); break;
4576 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4577 case TYPE_SMALLER: n1 = (f1 < f2); break;
4578 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4579 case TYPE_UNKNOWN:
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH: break; /* avoid gcc warning */
4582 }
4583 }
4584#endif
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /*
4587 * If one of the two variables is a number, compare as a number.
4588 * When using "=~" or "!~", always compare as string.
4589 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 n1 = get_tv_number(rettv);
4594 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 switch (type)
4596 {
4597 case TYPE_EQUAL: n1 = (n1 == n2); break;
4598 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4599 case TYPE_GREATER: n1 = (n1 > n2); break;
4600 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4601 case TYPE_SMALLER: n1 = (n1 < n2); break;
4602 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4603 case TYPE_UNKNOWN:
4604 case TYPE_MATCH:
4605 case TYPE_NOMATCH: break; /* avoid gcc warning */
4606 }
4607 }
4608 else
4609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 s1 = get_tv_string_buf(rettv, buf1);
4611 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4614 else
4615 i = 0;
4616 n1 = FALSE;
4617 switch (type)
4618 {
4619 case TYPE_EQUAL: n1 = (i == 0); break;
4620 case TYPE_NEQUAL: n1 = (i != 0); break;
4621 case TYPE_GREATER: n1 = (i > 0); break;
4622 case TYPE_GEQUAL: n1 = (i >= 0); break;
4623 case TYPE_SMALLER: n1 = (i < 0); break;
4624 case TYPE_SEQUAL: n1 = (i <= 0); break;
4625
4626 case TYPE_MATCH:
4627 case TYPE_NOMATCH:
4628 /* avoid 'l' flag in 'cpoptions' */
4629 save_cpo = p_cpo;
4630 p_cpo = (char_u *)"";
4631 regmatch.regprog = vim_regcomp(s2,
4632 RE_MAGIC + RE_STRING);
4633 regmatch.rm_ic = ic;
4634 if (regmatch.regprog != NULL)
4635 {
4636 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004637 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (type == TYPE_NOMATCH)
4639 n1 = !n1;
4640 }
4641 p_cpo = save_cpo;
4642 break;
4643
4644 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4645 }
4646 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 clear_tv(rettv);
4648 clear_tv(&var2);
4649 rettv->v_type = VAR_NUMBER;
4650 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 }
4653
4654 return OK;
4655}
4656
4657/*
4658 * Handle fourth level expression:
4659 * + number addition
4660 * - number subtraction
4661 * . string concatenation
4662 *
4663 * "arg" must point to the first non-white of the expression.
4664 * "arg" is advanced to the next non-white after the recognized expression.
4665 *
4666 * Return OK or FAIL.
4667 */
4668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004669eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
Bram Moolenaar33570922005-01-25 22:26:29 +00004671 typval_T var2;
4672 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 int op;
4674 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675#ifdef FEAT_FLOAT
4676 float_T f1 = 0, f2 = 0;
4677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 char_u *s1, *s2;
4679 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4680 char_u *p;
4681
4682 /*
4683 * Get the first variable.
4684 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004685 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 return FAIL;
4687
4688 /*
4689 * Repeat computing, until no '+', '-' or '.' is following.
4690 */
4691 for (;;)
4692 {
4693 op = **arg;
4694 if (op != '+' && op != '-' && op != '.')
4695 break;
4696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697 if ((op != '+' || rettv->v_type != VAR_LIST)
4698#ifdef FEAT_FLOAT
4699 && (op == '.' || rettv->v_type != VAR_FLOAT)
4700#endif
4701 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 {
4703 /* For "list + ...", an illegal use of the first operand as
4704 * a number cannot be determined before evaluating the 2nd
4705 * operand: if this is also a list, all is ok.
4706 * For "something . ...", "something - ..." or "non-list + ...",
4707 * we know that the first operand needs to be a string or number
4708 * without evaluating the 2nd operand. So check before to avoid
4709 * side effects after an error. */
4710 if (evaluate && get_tv_string_chk(rettv) == NULL)
4711 {
4712 clear_tv(rettv);
4713 return FAIL;
4714 }
4715 }
4716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 /*
4718 * Get the second variable.
4719 */
4720 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004721 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return FAIL;
4725 }
4726
4727 if (evaluate)
4728 {
4729 /*
4730 * Compute the result.
4731 */
4732 if (op == '.')
4733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4735 s2 = get_tv_string_buf_chk(&var2, buf2);
4736 if (s2 == NULL) /* type error ? */
4737 {
4738 clear_tv(rettv);
4739 clear_tv(&var2);
4740 return FAIL;
4741 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004742 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
4744 rettv->v_type = VAR_STRING;
4745 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004747 else if (op == '+' && rettv->v_type == VAR_LIST
4748 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004749 {
4750 /* concatenate Lists */
4751 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4752 &var3) == FAIL)
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
4758 clear_tv(rettv);
4759 *rettv = var3;
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 else
4762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 int error = FALSE;
4764
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765#ifdef FEAT_FLOAT
4766 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 f1 = rettv->vval.v_float;
4769 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771 else
4772#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774 n1 = get_tv_number_chk(rettv, &error);
4775 if (error)
4776 {
4777 /* This can only happen for "list + non-list". For
4778 * "non-list + ..." or "something - ...", we returned
4779 * before evaluating the 2nd operand. */
4780 clear_tv(rettv);
4781 return FAIL;
4782 }
4783#ifdef FEAT_FLOAT
4784 if (var2.v_type == VAR_FLOAT)
4785 f1 = n1;
4786#endif
4787 }
4788#ifdef FEAT_FLOAT
4789 if (var2.v_type == VAR_FLOAT)
4790 {
4791 f2 = var2.vval.v_float;
4792 n2 = 0;
4793 }
4794 else
4795#endif
4796 {
4797 n2 = get_tv_number_chk(&var2, &error);
4798 if (error)
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (rettv->v_type == VAR_FLOAT)
4806 f2 = n2;
4807#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810
4811#ifdef FEAT_FLOAT
4812 /* If there is a float on either side the result is a float. */
4813 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4814 {
4815 if (op == '+')
4816 f1 = f1 + f2;
4817 else
4818 f1 = f1 - f2;
4819 rettv->v_type = VAR_FLOAT;
4820 rettv->vval.v_float = f1;
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823#endif
4824 {
4825 if (op == '+')
4826 n1 = n1 + n2;
4827 else
4828 n1 = n1 - n2;
4829 rettv->v_type = VAR_NUMBER;
4830 rettv->vval.v_number = n1;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 }
4836 return OK;
4837}
4838
4839/*
4840 * Handle fifth level expression:
4841 * * number multiplication
4842 * / number division
4843 * % number modulo
4844 *
4845 * "arg" must point to the first non-white of the expression.
4846 * "arg" is advanced to the next non-white after the recognized expression.
4847 *
4848 * Return OK or FAIL.
4849 */
4850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004851eval6(
4852 char_u **arg,
4853 typval_T *rettv,
4854 int evaluate,
4855 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar33570922005-01-25 22:26:29 +00004857 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 int op;
4859 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#ifdef FEAT_FLOAT
4861 int use_float = FALSE;
4862 float_T f1 = 0, f2;
4863#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 /*
4867 * Get the first variable.
4868 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004869 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return FAIL;
4871
4872 /*
4873 * Repeat computing, until no '*', '/' or '%' is following.
4874 */
4875 for (;;)
4876 {
4877 op = **arg;
4878 if (op != '*' && op != '/' && op != '%')
4879 break;
4880
4881 if (evaluate)
4882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883#ifdef FEAT_FLOAT
4884 if (rettv->v_type == VAR_FLOAT)
4885 {
4886 f1 = rettv->vval.v_float;
4887 use_float = TRUE;
4888 n1 = 0;
4889 }
4890 else
4891#endif
4892 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 if (error)
4895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 else
4898 n1 = 0;
4899
4900 /*
4901 * Get the second variable.
4902 */
4903 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004904 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return FAIL;
4906
4907 if (evaluate)
4908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#ifdef FEAT_FLOAT
4910 if (var2.v_type == VAR_FLOAT)
4911 {
4912 if (!use_float)
4913 {
4914 f1 = n1;
4915 use_float = TRUE;
4916 }
4917 f2 = var2.vval.v_float;
4918 n2 = 0;
4919 }
4920 else
4921#endif
4922 {
4923 n2 = get_tv_number_chk(&var2, &error);
4924 clear_tv(&var2);
4925 if (error)
4926 return FAIL;
4927#ifdef FEAT_FLOAT
4928 if (use_float)
4929 f2 = n2;
4930#endif
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 /*
4934 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004935 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937#ifdef FEAT_FLOAT
4938 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940 if (op == '*')
4941 f1 = f1 * f2;
4942 else if (op == '/')
4943 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004944# ifdef VMS
4945 /* VMS crashes on divide by zero, work around it */
4946 if (f2 == 0.0)
4947 {
4948 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004949 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004950 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004951 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004952 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004953 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004954 }
4955 else
4956 f1 = f1 / f2;
4957# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 /* We rely on the floating point library to handle divide
4959 * by zero to result in "inf" and not a crash. */
4960 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004961# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004965 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 return FAIL;
4967 }
4968 rettv->v_type = VAR_FLOAT;
4969 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 if (op == '*')
4975 n1 = n1 * n2;
4976 else if (op == '/')
4977 {
4978 if (n2 == 0) /* give an error message? */
4979 {
4980 if (n1 == 0)
4981 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4982 else if (n1 < 0)
4983 n1 = -0x7fffffffL;
4984 else
4985 n1 = 0x7fffffffL;
4986 }
4987 else
4988 n1 = n1 / n2;
4989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 {
4992 if (n2 == 0) /* give an error message? */
4993 n1 = 0;
4994 else
4995 n1 = n1 % n2;
4996 }
4997 rettv->v_type = VAR_NUMBER;
4998 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 }
5002
5003 return OK;
5004}
5005
5006/*
5007 * Handle sixth level expression:
5008 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005009 * "string" string constant
5010 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * &option-name option value
5012 * @r register contents
5013 * identifier variable value
5014 * function() function call
5015 * $VAR environment variable
5016 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005017 * [expr, expr] List
5018 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 *
5020 * Also handle:
5021 * ! in front logical NOT
5022 * - in front unary minus
5023 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 * trailing [] subscript in String or List
5025 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *
5027 * "arg" must point to the first non-white of the expression.
5028 * "arg" is advanced to the next non-white after the recognized expression.
5029 *
5030 * Return OK or FAIL.
5031 */
5032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033eval7(
5034 char_u **arg,
5035 typval_T *rettv,
5036 int evaluate,
5037 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 long n;
5040 int len;
5041 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 char_u *start_leader, *end_leader;
5043 int ret = OK;
5044 char_u *alias;
5045
5046 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005048 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
5052 /*
5053 * Skip '!' and '-' characters. They are handled later.
5054 */
5055 start_leader = *arg;
5056 while (**arg == '!' || **arg == '-' || **arg == '+')
5057 *arg = skipwhite(*arg + 1);
5058 end_leader = *arg;
5059
5060 switch (**arg)
5061 {
5062 /*
5063 * Number constant.
5064 */
5065 case '0':
5066 case '1':
5067 case '2':
5068 case '3':
5069 case '4':
5070 case '5':
5071 case '6':
5072 case '7':
5073 case '8':
5074 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 {
5076#ifdef FEAT_FLOAT
5077 char_u *p = skipdigits(*arg + 1);
5078 int get_float = FALSE;
5079
5080 /* We accept a float when the format matches
5081 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005082 * strict to avoid backwards compatibility problems.
5083 * Don't look for a float after the "." operator, so that
5084 * ":let vers = 1.2.3" doesn't fail. */
5085 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 get_float = TRUE;
5088 p = skipdigits(p + 2);
5089 if (*p == 'e' || *p == 'E')
5090 {
5091 ++p;
5092 if (*p == '-' || *p == '+')
5093 ++p;
5094 if (!vim_isdigit(*p))
5095 get_float = FALSE;
5096 else
5097 p = skipdigits(p + 1);
5098 }
5099 if (ASCII_ISALPHA(*p) || *p == '.')
5100 get_float = FALSE;
5101 }
5102 if (get_float)
5103 {
5104 float_T f;
5105
5106 *arg += string2float(*arg, &f);
5107 if (evaluate)
5108 {
5109 rettv->v_type = VAR_FLOAT;
5110 rettv->vval.v_float = f;
5111 }
5112 }
5113 else
5114#endif
5115 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005116 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 *arg += len;
5118 if (evaluate)
5119 {
5120 rettv->v_type = VAR_NUMBER;
5121 rettv->vval.v_number = n;
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 /*
5128 * String constant: "string".
5129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005130 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 break;
5132
5133 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137 break;
5138
5139 /*
5140 * List: [expr, expr]
5141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144
5145 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 * Dictionary: {key: val, key: val}
5147 */
5148 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5149 break;
5150
5151 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005154 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156
5157 /*
5158 * Environment variable: $VAR.
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
5164 * Register contents: @r.
5165 */
5166 case '@': ++*arg;
5167 if (evaluate)
5168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005170 rettv->vval.v_string = get_reg_contents(**arg,
5171 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 if (**arg != NUL)
5174 ++*arg;
5175 break;
5176
5177 /*
5178 * nested expression: (expression).
5179 */
5180 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (**arg == ')')
5183 ++*arg;
5184 else if (ret == OK)
5185 {
5186 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 ret = FAIL;
5189 }
5190 break;
5191
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195
5196 if (ret == NOTDONE)
5197 {
5198 /*
5199 * Must be a variable or function name.
5200 * Can also be a curly-braces kind of name: {expr}.
5201 */
5202 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005203 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 if (alias != NULL)
5205 s = alias;
5206
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005207 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 ret = FAIL;
5209 else
5210 {
5211 if (**arg == '(') /* recursive! */
5212 {
5213 /* If "s" is the name of a variable of type VAR_FUNC
5214 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005215 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 /* Invoke the function. */
5218 ret = get_func_tv(s, len, rettv, arg,
5219 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005220 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005221
5222 /* If evaluate is FALSE rettv->v_type was not set in
5223 * get_func_tv, but it's needed in handle_subscript() to parse
5224 * what follows. So set it here. */
5225 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5226 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005227 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005228 rettv->v_type = VAR_FUNC;
5229 }
5230
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 /* Stop the expression evaluation when immediately
5232 * aborting on error, or when an interrupt occurred or
5233 * an exception was thrown but not caught. */
5234 if (aborting())
5235 {
5236 if (ret == OK)
5237 clear_tv(rettv);
5238 ret = FAIL;
5239 }
5240 }
5241 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005242 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005243 else
5244 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005246 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 }
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 *arg = skipwhite(*arg);
5250
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5252 * expr(expr). */
5253 if (ret == OK)
5254 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 /*
5257 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5258 */
5259 if (ret == OK && evaluate && end_leader > start_leader)
5260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005261 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005262 int val = 0;
5263#ifdef FEAT_FLOAT
5264 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005266 if (rettv->v_type == VAR_FLOAT)
5267 f = rettv->vval.v_float;
5268 else
5269#endif
5270 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 clear_tv(rettv);
5274 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 else
5277 {
5278 while (end_leader > start_leader)
5279 {
5280 --end_leader;
5281 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 {
5283#ifdef FEAT_FLOAT
5284 if (rettv->v_type == VAR_FLOAT)
5285 f = !f;
5286 else
5287#endif
5288 val = !val;
5289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005291 {
5292#ifdef FEAT_FLOAT
5293 if (rettv->v_type == VAR_FLOAT)
5294 f = -f;
5295 else
5296#endif
5297 val = -val;
5298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300#ifdef FEAT_FLOAT
5301 if (rettv->v_type == VAR_FLOAT)
5302 {
5303 clear_tv(rettv);
5304 rettv->vval.v_float = f;
5305 }
5306 else
5307#endif
5308 {
5309 clear_tv(rettv);
5310 rettv->v_type = VAR_NUMBER;
5311 rettv->vval.v_number = val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315
5316 return ret;
5317}
5318
5319/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005320 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5321 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5323 */
5324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005325eval_index(
5326 char_u **arg,
5327 typval_T *rettv,
5328 int evaluate,
5329 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330{
5331 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 long len = -1;
5335 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338
Bram Moolenaara03f2332016-02-06 18:09:59 +01005339 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 case VAR_FUNC:
5342 if (verbose)
5343 EMSG(_("E695: Cannot index a Funcref"));
5344 return FAIL;
5345 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005346#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005347 if (verbose)
5348 EMSG(_(e_float_as_string));
5349 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005350#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005351 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005352 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 if (verbose)
5354 EMSG(_("E909: Cannot index a special variable"));
5355 return FAIL;
5356 case VAR_UNKNOWN:
5357 if (evaluate)
5358 return FAIL;
5359 /* FALLTHROUGH */
5360
5361 case VAR_STRING:
5362 case VAR_NUMBER:
5363 case VAR_LIST:
5364 case VAR_DICT:
5365 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005366 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005368 init_tv(&var1);
5369 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 /*
5373 * dict.name
5374 */
5375 key = *arg + 1;
5376 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5377 ;
5378 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 /*
5385 * something[idx]
5386 *
5387 * Get the (first) variable from inside the [].
5388 */
5389 *arg = skipwhite(*arg + 1);
5390 if (**arg == ':')
5391 empty1 = TRUE;
5392 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5393 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005394 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5395 {
5396 /* not a number or string */
5397 clear_tv(&var1);
5398 return FAIL;
5399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400
5401 /*
5402 * Get the second variable from inside the [:].
5403 */
5404 if (**arg == ':')
5405 {
5406 range = TRUE;
5407 *arg = skipwhite(*arg + 1);
5408 if (**arg == ']')
5409 empty2 = TRUE;
5410 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005412 if (!empty1)
5413 clear_tv(&var1);
5414 return FAIL;
5415 }
5416 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5417 {
5418 /* not a number or string */
5419 if (!empty1)
5420 clear_tv(&var1);
5421 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 return FAIL;
5423 }
5424 }
5425
5426 /* Check for the ']'. */
5427 if (**arg != ']')
5428 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005429 if (verbose)
5430 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 clear_tv(&var1);
5432 if (range)
5433 clear_tv(&var2);
5434 return FAIL;
5435 }
5436 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438
5439 if (evaluate)
5440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 n1 = 0;
5442 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 n1 = get_tv_number(&var1);
5445 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 if (range)
5448 {
5449 if (empty2)
5450 n2 = -1;
5451 else
5452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 n2 = get_tv_number(&var2);
5454 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 }
5456 }
5457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005460 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005461 case VAR_FUNC:
5462 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005463 case VAR_SPECIAL:
5464 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005465 break; /* not evaluating, skipping over subscript */
5466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 case VAR_NUMBER:
5468 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 len = (long)STRLEN(s);
5471 if (range)
5472 {
5473 /* The resulting variable is a substring. If the indexes
5474 * are out of range the result is empty. */
5475 if (n1 < 0)
5476 {
5477 n1 = len + n1;
5478 if (n1 < 0)
5479 n1 = 0;
5480 }
5481 if (n2 < 0)
5482 n2 = len + n2;
5483 else if (n2 >= len)
5484 n2 = len;
5485 if (n1 >= len || n2 < 0 || n1 > n2)
5486 s = NULL;
5487 else
5488 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5489 }
5490 else
5491 {
5492 /* The resulting variable is a string of a single
5493 * character. If the index is too big or negative the
5494 * result is empty. */
5495 if (n1 >= len || n1 < 0)
5496 s = NULL;
5497 else
5498 s = vim_strnsave(s + n1, 1);
5499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 clear_tv(rettv);
5501 rettv->v_type = VAR_STRING;
5502 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 break;
5504
5505 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 if (n1 < 0)
5508 n1 = len + n1;
5509 if (!empty1 && (n1 < 0 || n1 >= len))
5510 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005511 /* For a range we allow invalid values and return an empty
5512 * list. A list index out of range is an error. */
5513 if (!range)
5514 {
5515 if (verbose)
5516 EMSGN(_(e_listidx), n1);
5517 return FAIL;
5518 }
5519 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 if (range)
5522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 list_T *l;
5524 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525
5526 if (n2 < 0)
5527 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005528 else if (n2 >= len)
5529 n2 = len - 1;
5530 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 l = list_alloc();
5533 if (l == NULL)
5534 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 n1 <= n2; ++n1)
5537 {
5538 if (list_append_tv(l, &item->li_tv) == FAIL)
5539 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005540 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 return FAIL;
5542 }
5543 item = item->li_next;
5544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 clear_tv(rettv);
5546 rettv->v_type = VAR_LIST;
5547 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005548 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 }
5550 else
5551 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 clear_tv(rettv);
5554 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557
5558 case VAR_DICT:
5559 if (range)
5560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005561 if (verbose)
5562 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 if (len == -1)
5564 clear_tv(&var1);
5565 return FAIL;
5566 }
5567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569
5570 if (len == -1)
5571 {
5572 key = get_tv_string(&var1);
5573 if (*key == NUL)
5574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005575 if (verbose)
5576 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 clear_tv(&var1);
5578 return FAIL;
5579 }
5580 }
5581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005582 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005585 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 if (item == NULL)
5589 return FAIL;
5590
5591 copy_tv(&item->di_tv, &var1);
5592 clear_tv(rettv);
5593 *rettv = var1;
5594 }
5595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 }
5597 }
5598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 return OK;
5600}
5601
5602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 * Get an option value.
5604 * "arg" points to the '&' or '+' before the option name.
5605 * "arg" is advanced to character after the option name.
5606 * Return OK or FAIL.
5607 */
5608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005609get_option_tv(
5610 char_u **arg,
5611 typval_T *rettv, /* when NULL, only check if option exists */
5612 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 char_u *option_end;
5615 long numval;
5616 char_u *stringval;
5617 int opt_type;
5618 int c;
5619 int working = (**arg == '+'); /* has("+option") */
5620 int ret = OK;
5621 int opt_flags;
5622
5623 /*
5624 * Isolate the option name and find its value.
5625 */
5626 option_end = find_option_end(arg, &opt_flags);
5627 if (option_end == NULL)
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 EMSG2(_("E112: Option name missing: %s"), *arg);
5631 return FAIL;
5632 }
5633
5634 if (!evaluate)
5635 {
5636 *arg = option_end;
5637 return OK;
5638 }
5639
5640 c = *option_end;
5641 *option_end = NUL;
5642 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 if (opt_type == -3) /* invalid name */
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E113: Unknown option: %s"), *arg);
5649 ret = FAIL;
5650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 if (opt_type == -2) /* hidden string option */
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv->v_type = VAR_STRING;
5656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (opt_type == -1) /* hidden number option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_NUMBER;
5661 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == 1) /* number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else /* string option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_STRING;
5671 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 }
5674 else if (working && (opt_type == -2 || opt_type == -1))
5675 ret = FAIL;
5676
5677 *option_end = c; /* put back for error messages */
5678 *arg = option_end;
5679
5680 return ret;
5681}
5682
5683/*
5684 * Allocate a variable for a string constant.
5685 * Return OK or FAIL.
5686 */
5687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005688get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 char_u *p;
5691 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 int extra = 0;
5693
5694 /*
5695 * Find the end of the string, skipping backslashed characters.
5696 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 if (*p == '\\' && p[1] != NUL)
5700 {
5701 ++p;
5702 /* A "\<x>" form occupies at least 4 characters, and produces up
5703 * to 6 characters: reserve space for 2 extra */
5704 if (*p == '<')
5705 extra += 2;
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708
5709 if (*p != '"')
5710 {
5711 EMSG2(_("E114: Missing quote: %s"), *arg);
5712 return FAIL;
5713 }
5714
5715 /* If only parsing, set *arg and return here */
5716 if (!evaluate)
5717 {
5718 *arg = p + 1;
5719 return OK;
5720 }
5721
5722 /*
5723 * Copy the string into allocated memory, handling backslashed
5724 * characters.
5725 */
5726 name = alloc((unsigned)(p - *arg + extra));
5727 if (name == NULL)
5728 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 rettv->v_type = VAR_STRING;
5730 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 if (*p == '\\')
5735 {
5736 switch (*++p)
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 case 'b': *name++ = BS; ++p; break;
5739 case 'e': *name++ = ESC; ++p; break;
5740 case 'f': *name++ = FF; ++p; break;
5741 case 'n': *name++ = NL; ++p; break;
5742 case 'r': *name++ = CAR; ++p; break;
5743 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 case 'X': /* hex: "\x1", "\x12" */
5746 case 'x':
5747 case 'u': /* Unicode: "\u0023" */
5748 case 'U':
5749 if (vim_isxdigit(p[1]))
5750 {
5751 int n, nr;
5752 int c = toupper(*p);
5753
5754 if (c == 'X')
5755 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005756 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005758 else
5759 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 nr = 0;
5761 while (--n >= 0 && vim_isxdigit(p[1]))
5762 {
5763 ++p;
5764 nr = (nr << 4) + hex2nr(*p);
5765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#ifdef FEAT_MBYTE
5768 /* For "\u" store the number according to
5769 * 'encoding'. */
5770 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 break;
5777
5778 /* octal: "\1", "\12", "\123" */
5779 case '0':
5780 case '1':
5781 case '2':
5782 case '3':
5783 case '4':
5784 case '5':
5785 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 case '7': *name = *p++ - '0';
5787 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *name = (*name << 3) + *p++ - '0';
5790 if (*p >= '0' && *p <= '7')
5791 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (extra != 0)
5799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802 }
5803 /* FALLTHROUGH */
5804
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 }
5809 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 *arg = p + 1;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 return OK;
5817}
5818
5819/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Return OK or FAIL.
5822 */
5823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005824get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005828 int reduce = 0;
5829
5830 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 */
5833 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 break;
5839 ++reduce;
5840 ++p;
5841 }
5842 }
5843
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 return FAIL;
5848 }
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 if (!evaluate)
5852 {
5853 *arg = p + 1;
5854 return OK;
5855 }
5856
5857 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 */
5860 str = alloc((unsigned)((p - *arg) - reduce));
5861 if (str == NULL)
5862 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 rettv->v_type = VAR_STRING;
5864 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 break;
5872 ++p;
5873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 *arg = p + 1;
5878
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 return OK;
5880}
5881
5882/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 * Allocate a variable for a List and fill it from "*arg".
5884 * Return OK or FAIL.
5885 */
5886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005887get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888{
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l = NULL;
5890 typval_T tv;
5891 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892
5893 if (evaluate)
5894 {
5895 l = list_alloc();
5896 if (l == NULL)
5897 return FAIL;
5898 }
5899
5900 *arg = skipwhite(*arg + 1);
5901 while (**arg != ']' && **arg != NUL)
5902 {
5903 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5904 goto failret;
5905 if (evaluate)
5906 {
5907 item = listitem_alloc();
5908 if (item != NULL)
5909 {
5910 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005911 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 list_append(l, item);
5913 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005914 else
5915 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 }
5917
5918 if (**arg == ']')
5919 break;
5920 if (**arg != ',')
5921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 goto failret;
5924 }
5925 *arg = skipwhite(*arg + 1);
5926 }
5927
5928 if (**arg != ']')
5929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931failret:
5932 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005933 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 return FAIL;
5935 }
5936
5937 *arg = skipwhite(*arg + 1);
5938 if (evaluate)
5939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005940 rettv->v_type = VAR_LIST;
5941 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 ++l->lv_refcount;
5943 }
5944
5945 return OK;
5946}
5947
5948/*
5949 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005950 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005952 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005955 list_T *l;
5956
5957 l = (list_T *)alloc_clear(sizeof(list_T));
5958 if (l != NULL)
5959 {
5960 /* Prepend the list to the list of lists for garbage collection. */
5961 if (first_list != NULL)
5962 first_list->lv_used_prev = l;
5963 l->lv_used_prev = NULL;
5964 l->lv_used_next = first_list;
5965 first_list = l;
5966 }
5967 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968}
5969
5970/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005971 * Allocate an empty list for a return value.
5972 * Returns OK or FAIL.
5973 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005976{
5977 list_T *l = list_alloc();
5978
5979 if (l == NULL)
5980 return FAIL;
5981
5982 rettv->vval.v_list = l;
5983 rettv->v_type = VAR_LIST;
5984 ++l->lv_refcount;
5985 return OK;
5986}
5987
5988/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 * Unreference a list: decrement the reference count and free it when it
5990 * becomes zero.
5991 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005995 if (l != NULL && --l->lv_refcount <= 0)
5996 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997}
5998
5999/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006000 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 * Ignores the reference count.
6002 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004list_free(
6005 list_T *l,
6006 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007{
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 /* Remove the list from the list of lists for garbage collection. */
6011 if (l->lv_used_prev == NULL)
6012 first_list = l->lv_used_next;
6013 else
6014 l->lv_used_prev->lv_used_next = l->lv_used_next;
6015 if (l->lv_used_next != NULL)
6016 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6017
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006020 /* Remove the item before deleting it. */
6021 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (recurse || (item->li_tv.v_type != VAR_LIST
6023 && item->li_tv.v_type != VAR_DICT))
6024 clear_tv(&item->li_tv);
6025 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 }
6027 vim_free(l);
6028}
6029
6030/*
6031 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006032 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006034 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006035listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006041 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006046 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 vim_free(item);
6048}
6049
6050/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006051 * Remove a list item from a List and free it. Also clears the value.
6052 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006055{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006056 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006057 listitem_free(item);
6058}
6059
6060/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061 * Get the number of items in a list.
6062 */
6063 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 if (l == NULL)
6067 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006068 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069}
6070
6071/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006072 * Return TRUE when two lists have exactly the same values.
6073 */
6074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075list_equal(
6076 list_T *l1,
6077 list_T *l2,
6078 int ic, /* ignore case for strings */
6079 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006083 if (l1 == NULL || l2 == NULL)
6084 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006085 if (l1 == l2)
6086 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087 if (list_len(l1) != list_len(l2))
6088 return FALSE;
6089
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 for (item1 = l1->lv_first, item2 = l2->lv_first;
6091 item1 != NULL && item2 != NULL;
6092 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094 return FALSE;
6095 return item1 == NULL && item2 == NULL;
6096}
6097
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006098/*
6099 * Return the dictitem that an entry in a hashtable points to.
6100 */
6101 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006103{
6104 return HI2DI(hi);
6105}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006106
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108 * Return TRUE when two dictionaries have exactly the same key/values.
6109 */
6110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111dict_equal(
6112 dict_T *d1,
6113 dict_T *d2,
6114 int ic, /* ignore case for strings */
6115 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116{
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 hashitem_T *hi;
6118 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006119 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006120
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006121 if (d1 == NULL || d2 == NULL)
6122 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006123 if (d1 == d2)
6124 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125 if (dict_len(d1) != dict_len(d2))
6126 return FALSE;
6127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006128 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131 if (!HASHITEM_EMPTY(hi))
6132 {
6133 item2 = dict_find(d2, hi->hi_key, -1);
6134 if (item2 == NULL)
6135 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006136 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 return FALSE;
6138 --todo;
6139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140 }
6141 return TRUE;
6142}
6143
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006144static int tv_equal_recurse_limit;
6145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006147 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006148 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006149 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006150 */
6151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152tv_equal(
6153 typval_T *tv1,
6154 typval_T *tv2,
6155 int ic, /* ignore case */
6156 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006157{
6158 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006159 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006161 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006163 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006166 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167 * recursiveness to a limit. We guess they are equal then.
6168 * A fixed limit has the problem of still taking an awful long time.
6169 * Reduce the limit every time running into it. That should work fine for
6170 * deeply linked structures that are not recursively linked and catch
6171 * recursiveness quickly. */
6172 if (!recursive)
6173 tv_equal_recurse_limit = 1000;
6174 if (recursive_cnt >= tv_equal_recurse_limit)
6175 {
6176 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 ++recursive_cnt;
6184 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6185 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006186 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187
6188 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 ++recursive_cnt;
6190 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6191 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006192 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 case VAR_FUNC:
6195 return (tv1->vval.v_string != NULL
6196 && tv2->vval.v_string != NULL
6197 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6198
6199 case VAR_NUMBER:
6200 return tv1->vval.v_number == tv2->vval.v_number;
6201
6202 case VAR_STRING:
6203 s1 = get_tv_string_buf(tv1, buf1);
6204 s2 = get_tv_string_buf(tv2, buf2);
6205 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006206
6207 case VAR_SPECIAL:
6208 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006209
6210 case VAR_FLOAT:
6211#ifdef FEAT_FLOAT
6212 return tv1->vval.v_float == tv2->vval.v_float;
6213#endif
6214 case VAR_JOB:
6215#ifdef FEAT_JOB
6216 return tv1->vval.v_job == tv2->vval.v_job;
6217#endif
6218 case VAR_UNKNOWN:
6219 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221
Bram Moolenaara03f2332016-02-06 18:09:59 +01006222 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6223 * does not equal anything, not even itself. */
6224 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225}
6226
6227/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 * Locate item with index "n" in list "l" and return it.
6229 * A negative index is counted from the end; -1 is the last item.
6230 * Returns NULL when "n" is out of range.
6231 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006233list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234{
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 long idx;
6237
6238 if (l == NULL)
6239 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006240
6241 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243 n = l->lv_len + n;
6244
6245 /* Check for index out of range. */
6246 if (n < 0 || n >= l->lv_len)
6247 return NULL;
6248
6249 /* When there is a cached index may start search from there. */
6250 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006252 if (n < l->lv_idx / 2)
6253 {
6254 /* closest to the start of the list */
6255 item = l->lv_first;
6256 idx = 0;
6257 }
6258 else if (n > (l->lv_idx + l->lv_len) / 2)
6259 {
6260 /* closest to the end of the list */
6261 item = l->lv_last;
6262 idx = l->lv_len - 1;
6263 }
6264 else
6265 {
6266 /* closest to the cached index */
6267 item = l->lv_idx_item;
6268 idx = l->lv_idx;
6269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 }
6271 else
6272 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273 if (n < l->lv_len / 2)
6274 {
6275 /* closest to the start of the list */
6276 item = l->lv_first;
6277 idx = 0;
6278 }
6279 else
6280 {
6281 /* closest to the end of the list */
6282 item = l->lv_last;
6283 idx = l->lv_len - 1;
6284 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006286
6287 while (n > idx)
6288 {
6289 /* search forward */
6290 item = item->li_next;
6291 ++idx;
6292 }
6293 while (n < idx)
6294 {
6295 /* search backward */
6296 item = item->li_prev;
6297 --idx;
6298 }
6299
6300 /* cache the used index */
6301 l->lv_idx = idx;
6302 l->lv_idx_item = item;
6303
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 return item;
6305}
6306
6307/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006308 * Get list item "l[idx]" as a number.
6309 */
6310 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006311list_find_nr(
6312 list_T *l,
6313 long idx,
6314 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006315{
6316 listitem_T *li;
6317
6318 li = list_find(l, idx);
6319 if (li == NULL)
6320 {
6321 if (errorp != NULL)
6322 *errorp = TRUE;
6323 return -1L;
6324 }
6325 return get_tv_number_chk(&li->li_tv, errorp);
6326}
6327
6328/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006329 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6330 */
6331 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006332list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006333{
6334 listitem_T *li;
6335
6336 li = list_find(l, idx - 1);
6337 if (li == NULL)
6338 {
6339 EMSGN(_(e_listidx), idx);
6340 return NULL;
6341 }
6342 return get_tv_string(&li->li_tv);
6343}
6344
6345/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006346 * Locate "item" list "l" and return its index.
6347 * Returns -1 when "item" is not in the list.
6348 */
6349 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351{
6352 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006354
6355 if (l == NULL)
6356 return -1;
6357 idx = 0;
6358 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6359 ++idx;
6360 if (li == NULL)
6361 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006362 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006363}
6364
6365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 * Append item "item" to the end of list "l".
6367 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370{
6371 if (l->lv_last == NULL)
6372 {
6373 /* empty list */
6374 l->lv_first = item;
6375 l->lv_last = item;
6376 item->li_prev = NULL;
6377 }
6378 else
6379 {
6380 l->lv_last->li_next = item;
6381 item->li_prev = l->lv_last;
6382 l->lv_last = item;
6383 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006384 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 item->li_next = NULL;
6386}
6387
6388/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006395 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396
Bram Moolenaar05159a02005-02-26 23:04:13 +00006397 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006399 copy_tv(tv, &li->li_tv);
6400 list_append(l, li);
6401 return OK;
6402}
6403
6404/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006405 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006406 * Return FAIL when out of memory.
6407 */
6408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006410{
6411 listitem_T *li = listitem_alloc();
6412
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_DICT;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_dict = dict;
6418 list_append(list, li);
6419 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 return OK;
6421}
6422
6423/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006424 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006425 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006426 * Returns FAIL when out of memory.
6427 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006430{
6431 listitem_T *li = listitem_alloc();
6432
6433 if (li == NULL)
6434 return FAIL;
6435 list_append(l, li);
6436 li->li_tv.v_type = VAR_STRING;
6437 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006438 if (str == NULL)
6439 li->li_tv.vval.v_string = NULL;
6440 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006441 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 return FAIL;
6443 return OK;
6444}
6445
6446/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447 * Append "n" to list "l".
6448 * Returns FAIL when out of memory.
6449 */
6450 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452{
6453 listitem_T *li;
6454
6455 li = listitem_alloc();
6456 if (li == NULL)
6457 return FAIL;
6458 li->li_tv.v_type = VAR_NUMBER;
6459 li->li_tv.v_lock = 0;
6460 li->li_tv.vval.v_number = n;
6461 list_append(l, li);
6462 return OK;
6463}
6464
6465/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 * If "item" is NULL append at the end.
6468 * Return FAIL when out of memory.
6469 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
6475 if (ni == NULL)
6476 return FAIL;
6477 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006478 list_insert(l, ni, item);
6479 return OK;
6480}
6481
6482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006484{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485 if (item == NULL)
6486 /* Append new item at end of list. */
6487 list_append(l, ni);
6488 else
6489 {
6490 /* Insert new item before existing item. */
6491 ni->li_prev = item->li_prev;
6492 ni->li_next = item;
6493 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006494 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 ++l->lv_idx;
6497 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 l->lv_idx_item = NULL;
6502 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006504 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506}
6507
6508/*
6509 * Extend "l1" with "l2".
6510 * If "bef" is NULL append at the end, otherwise insert before this item.
6511 * Returns FAIL when out of memory.
6512 */
6513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006514list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006517 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006519 /* We also quit the loop when we have inserted the original item count of
6520 * the list, avoid a hang when we extend a list with itself. */
6521 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6523 return FAIL;
6524 return OK;
6525}
6526
6527/*
6528 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6529 * Return FAIL when out of memory.
6530 */
6531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533{
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006536 if (l1 == NULL || l2 == NULL)
6537 return FAIL;
6538
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 if (l == NULL)
6542 return FAIL;
6543 tv->v_type = VAR_LIST;
6544 tv->vval.v_list = l;
6545
6546 /* append all items from the second list */
6547 return list_extend(l, l2, NULL);
6548}
6549
6550/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006553 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 * Returns NULL when out of memory.
6555 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558{
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *copy;
6560 listitem_T *item;
6561 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562
6563 if (orig == NULL)
6564 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565
6566 copy = list_alloc();
6567 if (copy != NULL)
6568 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006569 if (copyID != 0)
6570 {
6571 /* Do this before adding the items, because one of the items may
6572 * refer back to this list. */
6573 orig->lv_copyID = copyID;
6574 orig->lv_copylist = copy;
6575 }
6576 for (item = orig->lv_first; item != NULL && !got_int;
6577 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 {
6579 ni = listitem_alloc();
6580 if (ni == NULL)
6581 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 {
6584 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6585 {
6586 vim_free(ni);
6587 break;
6588 }
6589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006591 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 list_append(copy, ni);
6593 }
6594 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 if (item != NULL)
6596 {
6597 list_unref(copy);
6598 copy = NULL;
6599 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 }
6601
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 return copy;
6603}
6604
6605/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006607 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006608 * This used to be called list_remove, but that conflicts with a Sun header
6609 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613{
Bram Moolenaar33570922005-01-25 22:26:29 +00006614 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006616 /* notify watchers */
6617 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006619 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620 list_fix_watch(l, ip);
6621 if (ip == item2)
6622 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624
6625 if (item2->li_next == NULL)
6626 l->lv_last = item->li_prev;
6627 else
6628 item2->li_next->li_prev = item->li_prev;
6629 if (item->li_prev == NULL)
6630 l->lv_first = item2->li_next;
6631 else
6632 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006633 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634}
6635
6636/*
6637 * Return an allocated string with the string representation of a list.
6638 * May return NULL.
6639 */
6640 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642{
6643 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644
6645 if (tv->vval.v_list == NULL)
6646 return NULL;
6647 ga_init2(&ga, (int)sizeof(char), 80);
6648 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006649 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 {
6651 vim_free(ga.ga_data);
6652 return NULL;
6653 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654 ga_append(&ga, ']');
6655 ga_append(&ga, NUL);
6656 return (char_u *)ga.ga_data;
6657}
6658
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006659typedef struct join_S {
6660 char_u *s;
6661 char_u *tofree;
6662} join_T;
6663
6664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665list_join_inner(
6666 garray_T *gap, /* to store the result in */
6667 list_T *l,
6668 char_u *sep,
6669 int echo_style,
6670 int copyID,
6671 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672{
6673 int i;
6674 join_T *p;
6675 int len;
6676 int sumlen = 0;
6677 int first = TRUE;
6678 char_u *tofree;
6679 char_u numbuf[NUMBUFLEN];
6680 listitem_T *item;
6681 char_u *s;
6682
6683 /* Stringify each item in the list. */
6684 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6685 {
6686 if (echo_style)
6687 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6688 else
6689 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6690 if (s == NULL)
6691 return FAIL;
6692
6693 len = (int)STRLEN(s);
6694 sumlen += len;
6695
Bram Moolenaarcde88542015-08-11 19:14:00 +02006696 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6698 if (tofree != NULL || s != numbuf)
6699 {
6700 p->s = s;
6701 p->tofree = tofree;
6702 }
6703 else
6704 {
6705 p->s = vim_strnsave(s, len);
6706 p->tofree = p->s;
6707 }
6708
6709 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006710 if (did_echo_string_emsg) /* recursion error, bail out */
6711 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006712 }
6713
6714 /* Allocate result buffer with its total size, avoid re-allocation and
6715 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6716 if (join_gap->ga_len >= 2)
6717 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6718 if (ga_grow(gap, sumlen + 2) == FAIL)
6719 return FAIL;
6720
6721 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6722 {
6723 if (first)
6724 first = FALSE;
6725 else
6726 ga_concat(gap, sep);
6727 p = ((join_T *)join_gap->ga_data) + i;
6728
6729 if (p->s != NULL)
6730 ga_concat(gap, p->s);
6731 line_breakcheck();
6732 }
6733
6734 return OK;
6735}
6736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006739 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006740 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743list_join(
6744 garray_T *gap,
6745 list_T *l,
6746 char_u *sep,
6747 int echo_style,
6748 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006749{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006750 garray_T join_ga;
6751 int retval;
6752 join_T *p;
6753 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754
Bram Moolenaard39a7512015-04-16 22:51:22 +02006755 if (l->lv_len < 1)
6756 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006757 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6758 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6759
6760 /* Dispose each item in join_ga. */
6761 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 p = (join_T *)join_ga.ga_data;
6764 for (i = 0; i < join_ga.ga_len; ++i)
6765 {
6766 vim_free(p->tofree);
6767 ++p;
6768 }
6769 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006771
6772 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773}
6774
6775/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006776 * Return the next (unique) copy ID.
6777 * Used for serializing nested structures.
6778 */
6779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006781{
6782 current_copyID += COPYID_INC;
6783 return current_copyID;
6784}
6785
6786/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 * Garbage collection for lists and dictionaries.
6788 *
6789 * We use reference counts to be able to free most items right away when they
6790 * are no longer used. But for composite items it's possible that it becomes
6791 * unused while the reference count is > 0: When there is a recursive
6792 * reference. Example:
6793 * :let l = [1, 2, 3]
6794 * :let d = {9: l}
6795 * :let l[1] = d
6796 *
6797 * Since this is quite unusual we handle this with garbage collection: every
6798 * once in a while find out which lists and dicts are not referenced from any
6799 * variable.
6800 *
6801 * Here is a good reference text about garbage collection (refers to Python
6802 * but it applies to all reference-counting mechanisms):
6803 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Do garbage collection for lists and dicts.
6808 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006814 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 buf_T *buf;
6816 win_T *wp;
6817 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006818 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006819 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006820 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006821#ifdef FEAT_WINDOWS
6822 tabpage_T *tp;
6823#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006825 /* Only do this once. */
6826 want_garbage_collect = FALSE;
6827 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006828 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006829
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 /* We advance by two because we add one for items referenced through
6831 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006832 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 /*
6835 * 1. Go through all accessible variables and mark all lists and dicts
6836 * with copyID.
6837 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838
6839 /* Don't free variables in the previous_funccal list unless they are only
6840 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006841 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6843 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6845 NULL);
6846 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6847 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 }
6849
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 /* script-local variables */
6851 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006852 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853
6854 /* buffer-local variables */
6855 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006856 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6857 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858
6859 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006860 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6862 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006863#ifdef FEAT_AUTOCMD
6864 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6866 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006867#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006869#ifdef FEAT_WINDOWS
6870 /* tabpage-local variables */
6871 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6873 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006874#endif
6875
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* function-local variables */
6880 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6881 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6883 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 }
6885
Bram Moolenaard812df62008-11-09 12:46:09 +00006886 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006888
Bram Moolenaar1dced572012-04-05 16:54:08 +02006889#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006891#endif
6892
Bram Moolenaardb913952012-06-29 12:54:53 +02006893#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006895#endif
6896
6897#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006899#endif
6900
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006901#ifdef FEAT_CHANNEL
6902 abort = abort || set_ref_in_channel(copyID);
6903#endif
6904
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 /*
6908 * 2. Free lists and dictionaries that are not referenced.
6909 */
6910 did_free = free_unref_items(copyID);
6911
6912 /*
6913 * 3. Check if any funccal can be freed now.
6914 */
6915 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006916 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 if (can_free_funccal(*pfc, copyID))
6918 {
6919 fc = *pfc;
6920 *pfc = fc->caller;
6921 free_funccal(fc, TRUE);
6922 did_free = TRUE;
6923 did_free_funccal = TRUE;
6924 }
6925 else
6926 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 if (did_free_funccal)
6929 /* When a funccal was freed some more items might be garbage
6930 * collected, so run again. */
6931 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 else if (p_verbose > 0)
6934 {
6935 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6936 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937
6938 return did_free;
6939}
6940
6941/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006942 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 */
6944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006945free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006947 dict_T *dd, *dd_next;
6948 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 int did_free = FALSE;
6950
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 */
6954 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006955 {
6956 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006959 /* Free the Dictionary and ordinary items it contains, but don't
6960 * recurse into Lists and Dictionaries, they will be in the list
6961 * of dicts or list of lists. */
6962 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006965 dd = dd_next;
6966 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967
6968 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 * Go through the list of lists and free items without the copyID.
6970 * But don't free a list that has a watcher (used in a for loop), these
6971 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 */
6973 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 {
6975 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6977 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 /* Free the List and ordinary items it contains, but don't recurse
6980 * into Lists and Dictionaries, they will be in the list of dicts
6981 * or list of lists. */
6982 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 ll = ll_next;
6986 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 return did_free;
6989}
6990
6991/*
6992 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 * "list_stack" is used to add lists to be marked. Can be NULL.
6994 *
6995 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006998set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999{
7000 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 hashtab_T *cur_ht;
7004 ht_stack_T *ht_stack = NULL;
7005 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 cur_ht = ht;
7008 for (;;)
7009 {
7010 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 /* Mark each item in the hashtab. If the item contains a hashtab
7013 * it is added to ht_stack, if it contains a list it is added to
7014 * list_stack. */
7015 todo = (int)cur_ht->ht_used;
7016 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7017 if (!HASHITEM_EMPTY(hi))
7018 {
7019 --todo;
7020 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7021 &ht_stack, list_stack);
7022 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024
7025 if (ht_stack == NULL)
7026 break;
7027
7028 /* take an item from the stack */
7029 cur_ht = ht_stack->ht;
7030 tempitem = ht_stack;
7031 ht_stack = ht_stack->prev;
7032 free(tempitem);
7033 }
7034
7035 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036}
7037
7038/*
7039 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7041 *
7042 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 listitem_T *li;
7048 int abort = FALSE;
7049 list_T *cur_l;
7050 list_stack_T *list_stack = NULL;
7051 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 cur_l = l;
7054 for (;;)
7055 {
7056 if (!abort)
7057 /* Mark each item in the list. If the item contains a hashtab
7058 * it is added to ht_stack, if it contains a list it is added to
7059 * list_stack. */
7060 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7061 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7062 ht_stack, &list_stack);
7063 if (list_stack == NULL)
7064 break;
7065
7066 /* take an item from the stack */
7067 cur_l = list_stack->list;
7068 tempitem = list_stack;
7069 list_stack = list_stack->prev;
7070 free(tempitem);
7071 }
7072
7073 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074}
7075
7076/*
7077 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 * "list_stack" is used to add lists to be marked. Can be NULL.
7079 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7080 *
7081 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084set_ref_in_item(
7085 typval_T *tv,
7086 int copyID,
7087 ht_stack_T **ht_stack,
7088 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089{
7090 dict_T *dd;
7091 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007093
Bram Moolenaara03f2332016-02-06 18:09:59 +01007094 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007095 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007096 dd = tv->vval.v_dict;
7097 if (dd != NULL && dd->dv_copyID != copyID)
7098 {
7099 /* Didn't see this dict yet. */
7100 dd->dv_copyID = copyID;
7101 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007103 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7104 }
7105 else
7106 {
7107 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7108 if (newitem == NULL)
7109 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 else
7111 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007112 newitem->ht = &dd->dv_hashtab;
7113 newitem->prev = *ht_stack;
7114 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007115 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007116 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007117 }
7118 }
7119 else if (tv->v_type == VAR_LIST)
7120 {
7121 ll = tv->vval.v_list;
7122 if (ll != NULL && ll->lv_copyID != copyID)
7123 {
7124 /* Didn't see this list yet. */
7125 ll->lv_copyID = copyID;
7126 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007127 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007128 abort = set_ref_in_list(ll, copyID, ht_stack);
7129 }
7130 else
7131 {
7132 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 if (newitem == NULL)
7135 abort = TRUE;
7136 else
7137 {
7138 newitem->list = ll;
7139 newitem->prev = *list_stack;
7140 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007142 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146}
7147
7148/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007149 * Allocate an empty header for a dictionary.
7150 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007151 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007152dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153{
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007158 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007159 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007160 if (first_dict != NULL)
7161 first_dict->dv_used_prev = d;
7162 d->dv_used_next = first_dict;
7163 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007164 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007165
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007167 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007168 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007169 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007171 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173}
7174
7175/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007176 * Allocate an empty dict for a return value.
7177 * Returns OK or FAIL.
7178 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007181{
7182 dict_T *d = dict_alloc();
7183
7184 if (d == NULL)
7185 return FAIL;
7186
7187 rettv->vval.v_dict = d;
7188 rettv->v_type = VAR_DICT;
7189 ++d->dv_refcount;
7190 return OK;
7191}
7192
7193
7194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 * Unreference a Dictionary: decrement the reference count and free it when it
7196 * becomes zero.
7197 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007199dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007201 if (d != NULL && --d->dv_refcount <= 0)
7202 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203}
7204
7205/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007206 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 * Ignores the reference count.
7208 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007210dict_free(
7211 dict_T *d,
7212 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007216 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218 /* Remove the dict from the list of dicts for garbage collection. */
7219 if (d->dv_used_prev == NULL)
7220 first_dict = d->dv_used_next;
7221 else
7222 d->dv_used_prev->dv_used_next = d->dv_used_next;
7223 if (d->dv_used_next != NULL)
7224 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7225
7226 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007227 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007228 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231 if (!HASHITEM_EMPTY(hi))
7232 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007233 /* Remove the item before deleting it, just in case there is
7234 * something recursive causing trouble. */
7235 di = HI2DI(hi);
7236 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007237 if (recurse || (di->di_tv.v_type != VAR_LIST
7238 && di->di_tv.v_type != VAR_DICT))
7239 clear_tv(&di->di_tv);
7240 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 --todo;
7242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 vim_free(d);
7246}
7247
7248/*
7249 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250 * The "key" is copied to the new item.
7251 * Note that the value of the item "di_tv" still needs to be initialized!
7252 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007254 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007255dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256{
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007259 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007261 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007263 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007264 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266}
7267
7268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007269 * Make a copy of a Dictionary item.
7270 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273{
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7277 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 if (di != NULL)
7279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007281 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 copy_tv(&org->di_tv, &di->di_tv);
7283 }
7284 return di;
7285}
7286
7287/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 * Remove item "item" from Dictionary "dict" and free it.
7289 */
7290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292{
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 if (HASHITEM_EMPTY(hi))
7297 EMSG2(_(e_intern2), "dictitem_remove()");
7298 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 dictitem_free(item);
7301}
7302
7303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Free a dict item. Also clears the value.
7305 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007307dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007310 if (item->di_flags & DI_FLAGS_ALLOC)
7311 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312}
7313
7314/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7316 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007317 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Returns NULL when out of memory.
7319 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322{
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dict_T *copy;
7324 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327
7328 if (orig == NULL)
7329 return NULL;
7330
7331 copy = dict_alloc();
7332 if (copy != NULL)
7333 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007334 if (copyID != 0)
7335 {
7336 orig->dv_copyID = copyID;
7337 orig->dv_copydict = copy;
7338 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007339 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007340 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 --todo;
7345
7346 di = dictitem_alloc(hi->hi_key);
7347 if (di == NULL)
7348 break;
7349 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007350 {
7351 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7352 copyID) == FAIL)
7353 {
7354 vim_free(di);
7355 break;
7356 }
7357 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 else
7359 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7360 if (dict_add(copy, di) == FAIL)
7361 {
7362 dictitem_free(di);
7363 break;
7364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 if (todo > 0)
7370 {
7371 dict_unref(copy);
7372 copy = NULL;
7373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 }
7375
7376 return copy;
7377}
7378
7379/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007381 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007384dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385{
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387}
7388
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007390 * Add a number or string entry to dictionary "d".
7391 * When "str" is NULL use number "nr", otherwise use "str".
7392 * Returns FAIL when out of memory and when key already exists.
7393 */
7394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007395dict_add_nr_str(
7396 dict_T *d,
7397 char *key,
7398 long nr,
7399 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007400{
7401 dictitem_T *item;
7402
7403 item = dictitem_alloc((char_u *)key);
7404 if (item == NULL)
7405 return FAIL;
7406 item->di_tv.v_lock = 0;
7407 if (str == NULL)
7408 {
7409 item->di_tv.v_type = VAR_NUMBER;
7410 item->di_tv.vval.v_number = nr;
7411 }
7412 else
7413 {
7414 item->di_tv.v_type = VAR_STRING;
7415 item->di_tv.vval.v_string = vim_strsave(str);
7416 }
7417 if (dict_add(d, item) == FAIL)
7418 {
7419 dictitem_free(item);
7420 return FAIL;
7421 }
7422 return OK;
7423}
7424
7425/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007426 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007427 * Returns FAIL when out of memory and when key already exists.
7428 */
7429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007430dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007431{
7432 dictitem_T *item;
7433
7434 item = dictitem_alloc((char_u *)key);
7435 if (item == NULL)
7436 return FAIL;
7437 item->di_tv.v_lock = 0;
7438 item->di_tv.v_type = VAR_LIST;
7439 item->di_tv.vval.v_list = list;
7440 if (dict_add(d, item) == FAIL)
7441 {
7442 dictitem_free(item);
7443 return FAIL;
7444 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007445 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007446 return OK;
7447}
7448
7449/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007450 * Get the number of items in a Dictionary.
7451 */
7452 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 if (d == NULL)
7456 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007457 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007458}
7459
7460/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 * Find item "key[len]" in Dictionary "d".
7462 * If "len" is negative use strlen(key).
7463 * Returns NULL when not found.
7464 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007465 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007468#define AKEYLEN 200
7469 char_u buf[AKEYLEN];
7470 char_u *akey;
7471 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 if (len < 0)
7475 akey = key;
7476 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007478 tofree = akey = vim_strnsave(key, len);
7479 if (akey == NULL)
7480 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 else
7483 {
7484 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007485 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 akey = buf;
7487 }
7488
Bram Moolenaar33570922005-01-25 22:26:29 +00007489 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 vim_free(tofree);
7491 if (HASHITEM_EMPTY(hi))
7492 return NULL;
7493 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494}
7495
7496/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007497 * Get a string item from a dictionary.
7498 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007499 * Returns NULL if the entry doesn't exist or out of memory.
7500 */
7501 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007502get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007503{
7504 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007505 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007506
7507 di = dict_find(d, key, -1);
7508 if (di == NULL)
7509 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007510 s = get_tv_string(&di->di_tv);
7511 if (save && s != NULL)
7512 s = vim_strsave(s);
7513 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007514}
7515
7516/*
7517 * Get a number item from a dictionary.
7518 * Returns 0 if the entry doesn't exist or out of memory.
7519 */
7520 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007521get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007522{
7523 dictitem_T *di;
7524
7525 di = dict_find(d, key, -1);
7526 if (di == NULL)
7527 return 0;
7528 return get_tv_number(&di->di_tv);
7529}
7530
7531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 * Return an allocated string with the string representation of a Dictionary.
7533 * May return NULL.
7534 */
7535 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537{
7538 garray_T ga;
7539 int first = TRUE;
7540 char_u *tofree;
7541 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 return NULL;
7549 ga_init2(&ga, (int)sizeof(char), 80);
7550 ga_append(&ga, '{');
7551
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007552 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007553 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 --todo;
7558
7559 if (first)
7560 first = FALSE;
7561 else
7562 ga_concat(&ga, (char_u *)", ");
7563
7564 tofree = string_quote(hi->hi_key, FALSE);
7565 if (tofree != NULL)
7566 {
7567 ga_concat(&ga, tofree);
7568 vim_free(tofree);
7569 }
7570 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007571 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 if (s != NULL)
7573 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007575 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007576 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007577 line_breakcheck();
7578
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 if (todo > 0)
7582 {
7583 vim_free(ga.ga_data);
7584 return NULL;
7585 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586
7587 ga_append(&ga, '}');
7588 ga_append(&ga, NUL);
7589 return (char_u *)ga.ga_data;
7590}
7591
7592/*
7593 * Allocate a variable for a Dictionary and fill it from "*arg".
7594 * Return OK or FAIL. Returns NOTDONE for {expr}.
7595 */
7596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007597get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598{
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 dict_T *d = NULL;
7600 typval_T tvkey;
7601 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007602 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606
7607 /*
7608 * First check if it's not a curly-braces thing: {expr}.
7609 * Must do this without evaluating, otherwise a function may be called
7610 * twice. Unfortunately this means we need to call eval1() twice for the
7611 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 if (*start != '}')
7615 {
7616 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7617 return FAIL;
7618 if (*start == '}')
7619 return NOTDONE;
7620 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621
7622 if (evaluate)
7623 {
7624 d = dict_alloc();
7625 if (d == NULL)
7626 return FAIL;
7627 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 tvkey.v_type = VAR_UNKNOWN;
7629 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 *arg = skipwhite(*arg + 1);
7632 while (**arg != '}' && **arg != NUL)
7633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 goto failret;
7636 if (**arg != ':')
7637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007638 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 goto failret;
7641 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007642 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007644 key = get_tv_string_buf_chk(&tvkey, buf);
7645 if (key == NULL || *key == NUL)
7646 {
7647 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7648 if (key != NULL)
7649 EMSG(_(e_emptykey));
7650 clear_tv(&tvkey);
7651 goto failret;
7652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 *arg = skipwhite(*arg + 1);
7656 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7657 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007658 if (evaluate)
7659 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 }
7662 if (evaluate)
7663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 if (item != NULL)
7666 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007667 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 clear_tv(&tv);
7670 goto failret;
7671 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 item = dictitem_alloc(key);
7673 clear_tv(&tvkey);
7674 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007677 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 if (dict_add(d, item) == FAIL)
7679 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
7681 }
7682
7683 if (**arg == '}')
7684 break;
7685 if (**arg != ',')
7686 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007687 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 goto failret;
7689 }
7690 *arg = skipwhite(*arg + 1);
7691 }
7692
7693 if (**arg != '}')
7694 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007695 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696failret:
7697 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007698 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 return FAIL;
7700 }
7701
7702 *arg = skipwhite(*arg + 1);
7703 if (evaluate)
7704 {
7705 rettv->v_type = VAR_DICT;
7706 rettv->vval.v_dict = d;
7707 ++d->dv_refcount;
7708 }
7709
7710 return OK;
7711}
7712
Bram Moolenaar835dc632016-02-07 14:27:38 +01007713#ifdef FEAT_JOB
7714 static void
7715job_free(job_T *job)
7716{
7717 /* TODO: free any handles */
7718
7719 vim_free(job);
7720}
7721
7722 static void
7723job_unref(job_T *job)
7724{
7725 if (job != NULL && --job->jv_refcount <= 0)
7726 job_free(job);
7727}
7728
7729/*
7730 * Allocate a job. Sets the refcount to one.
7731 */
7732 static job_T *
7733job_alloc(void)
7734{
7735 job_T *job;
7736
7737 job = (job_T *)alloc_clear(sizeof(job_T));
7738 if (job != NULL)
7739 {
7740 job->jv_refcount = 1;
7741 }
7742 return job;
7743}
7744
7745#endif
7746
Bram Moolenaar17a13432016-01-24 14:22:10 +01007747 static char *
7748get_var_special_name(int nr)
7749{
7750 switch (nr)
7751 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007752 case VVAL_FALSE: return "v:false";
7753 case VVAL_TRUE: return "v:true";
7754 case VVAL_NONE: return "v:none";
7755 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007756 }
7757 EMSG2(_(e_intern2), "get_var_special_name()");
7758 return "42";
7759}
7760
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 * Return a string with the string representation of a variable.
7763 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007764 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007765 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007766 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007767 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 */
7769 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007770echo_string(
7771 typval_T *tv,
7772 char_u **tofree,
7773 char_u *numbuf,
7774 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007776 static int recurse = 0;
7777 char_u *r = NULL;
7778
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007780 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007781 if (!did_echo_string_emsg)
7782 {
7783 /* Only give this message once for a recursive call to avoid
7784 * flooding the user with errors. And stop iterating over lists
7785 * and dicts. */
7786 did_echo_string_emsg = TRUE;
7787 EMSG(_("E724: variable nested too deep for displaying"));
7788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007790 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 }
7792 ++recurse;
7793
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 switch (tv->v_type)
7795 {
7796 case VAR_FUNC:
7797 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 r = tv->vval.v_string;
7799 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802 if (tv->vval.v_list == NULL)
7803 {
7804 *tofree = NULL;
7805 r = NULL;
7806 }
7807 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7808 {
7809 *tofree = NULL;
7810 r = (char_u *)"[...]";
7811 }
7812 else
7813 {
7814 tv->vval.v_list->lv_copyID = copyID;
7815 *tofree = list2string(tv, copyID);
7816 r = *tofree;
7817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007819
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821 if (tv->vval.v_dict == NULL)
7822 {
7823 *tofree = NULL;
7824 r = NULL;
7825 }
7826 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7827 {
7828 *tofree = NULL;
7829 r = (char_u *)"{...}";
7830 }
7831 else
7832 {
7833 tv->vval.v_dict->dv_copyID = copyID;
7834 *tofree = dict2string(tv, copyID);
7835 r = *tofree;
7836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 case VAR_STRING:
7840 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007841 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007842 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 *tofree = NULL;
7844 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007848#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849 *tofree = NULL;
7850 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7851 r = numbuf;
7852 break;
7853#endif
7854
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007855 case VAR_SPECIAL:
7856 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007857 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007858 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860
Bram Moolenaar8502c702014-06-17 12:51:16 +02007861 if (--recurse == 0)
7862 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864}
7865
7866/*
7867 * Return a string with the string representation of a variable.
7868 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7869 * "numbuf" is used for a number.
7870 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007871 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 */
7873 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007874tv2string(
7875 typval_T *tv,
7876 char_u **tofree,
7877 char_u *numbuf,
7878 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879{
7880 switch (tv->v_type)
7881 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 case VAR_FUNC:
7883 *tofree = string_quote(tv->vval.v_string, TRUE);
7884 return *tofree;
7885 case VAR_STRING:
7886 *tofree = string_quote(tv->vval.v_string, FALSE);
7887 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#ifdef FEAT_FLOAT
7889 case VAR_FLOAT:
7890 *tofree = NULL;
7891 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7892 return numbuf;
7893#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007897 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007898 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007899 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903}
7904
7905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 * Return string "str" in ' quotes, doubling ' characters.
7907 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 */
7910 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007911string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912{
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 char_u *p, *r, *s;
7915
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 len = (function ? 13 : 3);
7917 if (str != NULL)
7918 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007919 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 for (p = str; *p != NUL; mb_ptr_adv(p))
7921 if (*p == '\'')
7922 ++len;
7923 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 s = r = alloc(len);
7925 if (r != NULL)
7926 {
7927 if (function)
7928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 r += 10;
7931 }
7932 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 if (str != NULL)
7935 for (p = str; *p != NUL; )
7936 {
7937 if (*p == '\'')
7938 *r++ = '\'';
7939 MB_COPY_CHAR(p, r);
7940 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 if (function)
7943 *r++ = ')';
7944 *r++ = NUL;
7945 }
7946 return s;
7947}
7948
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007949#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007950/*
7951 * Convert the string "text" to a floating point number.
7952 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7953 * this always uses a decimal point.
7954 * Returns the length of the text that was consumed.
7955 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007957string2float(
7958 char_u *text,
7959 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007960{
7961 char *s = (char *)text;
7962 float_T f;
7963
7964 f = strtod(s, &s);
7965 *value = f;
7966 return (int)((char_u *)s - text);
7967}
7968#endif
7969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 * Get the value of an environment variable.
7972 * "arg" is pointing to the '$'. It is advanced to after the name.
7973 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007974 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 */
7976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007977get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 char_u *string = NULL;
7980 int len;
7981 int cc;
7982 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007983 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
7985 ++*arg;
7986 name = *arg;
7987 len = get_env_len(arg);
7988 if (evaluate)
7989 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007990 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007991 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007992
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007993 cc = name[len];
7994 name[len] = NUL;
7995 /* first try vim_getenv(), fast for normal environment vars */
7996 string = vim_getenv(name, &mustfree);
7997 if (string != NULL && *string != NUL)
7998 {
7999 if (!mustfree)
8000 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008002 else
8003 {
8004 if (mustfree)
8005 vim_free(string);
8006
8007 /* next try expanding things like $VIM and ${HOME} */
8008 string = expand_env_save(name - 1);
8009 if (string != NULL && *string == '$')
8010 {
8011 vim_free(string);
8012 string = NULL;
8013 }
8014 }
8015 name[len] = cc;
8016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017 rettv->v_type = VAR_STRING;
8018 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020
8021 return OK;
8022}
8023
8024/*
8025 * Array with names and number of arguments of all internal functions
8026 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8027 */
8028static struct fst
8029{
8030 char *f_name; /* function name */
8031 char f_min_argc; /* minimal number of arguments */
8032 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008033 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008034 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035} functions[] =
8036{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037#ifdef FEAT_FLOAT
8038 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008039 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008040#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008041 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008042 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008043 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"append", 2, 2, f_append},
8045 {"argc", 0, 0, f_argc},
8046 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008047 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008048 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008049#ifdef FEAT_FLOAT
8050 {"asin", 1, 1, f_asin}, /* WJMc */
8051#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008052 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008053 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008054 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008055 {"assert_false", 1, 2, f_assert_false},
8056 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008062 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"bufexists", 1, 1, f_bufexists},
8064 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8065 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8066 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8067 {"buflisted", 1, 1, f_buflisted},
8068 {"bufloaded", 1, 1, f_bufloaded},
8069 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008070 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"bufwinnr", 1, 1, f_bufwinnr},
8072 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008073 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008074 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"ceil", 1, 1, f_ceil},
8078#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008079#ifdef FEAT_CHANNEL
8080 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008081 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008082 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8083 {"ch_sendraw", 2, 3, f_ch_sendraw},
8084#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008085 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008086 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008088 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008090#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008091 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008092 {"complete_add", 1, 1, f_complete_add},
8093 {"complete_check", 0, 0, f_complete_check},
8094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008096 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#ifdef FEAT_FLOAT
8098 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008099 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008101 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008103 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008104 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008105 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008107 {"diff_filler", 1, 1, f_diff_filler},
8108 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008109 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008111 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"eventhandler", 0, 0, f_eventhandler},
8113 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008114 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116#ifdef FEAT_FLOAT
8117 {"exp", 1, 1, f_exp},
8118#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008119 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008120 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008121 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8123 {"filereadable", 1, 1, f_filereadable},
8124 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008125 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008126 {"finddir", 1, 3, f_finddir},
8127 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008128#ifdef FEAT_FLOAT
8129 {"float2nr", 1, 1, f_float2nr},
8130 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008131 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008133 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"fnamemodify", 2, 2, f_fnamemodify},
8135 {"foldclosed", 1, 1, f_foldclosed},
8136 {"foldclosedend", 1, 1, f_foldclosedend},
8137 {"foldlevel", 1, 1, f_foldlevel},
8138 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008139 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008142 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008143 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008144 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008145 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"getchar", 0, 1, f_getchar},
8147 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008148 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"getcmdline", 0, 0, f_getcmdline},
8150 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008151 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008152 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008153 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008154 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008155 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008156 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getfsize", 1, 1, f_getfsize},
8158 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008159 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008160 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008161 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008162 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008163 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008164 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008165 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008166 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008168 {"gettabvar", 2, 3, f_gettabvar},
8169 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getwinposx", 0, 0, f_getwinposx},
8171 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008172 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008173 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008174 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008175 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008178 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008179 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8181 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8182 {"histadd", 2, 2, f_histadd},
8183 {"histdel", 1, 2, f_histdel},
8184 {"histget", 1, 2, f_histget},
8185 {"histnr", 1, 1, f_histnr},
8186 {"hlID", 1, 1, f_hlID},
8187 {"hlexists", 1, 1, f_hlexists},
8188 {"hostname", 0, 0, f_hostname},
8189 {"iconv", 3, 3, f_iconv},
8190 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008191 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008192 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008194 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"inputrestore", 0, 0, f_inputrestore},
8196 {"inputsave", 0, 0, f_inputsave},
8197 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008198 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008199 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008201 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008202 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008203#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +01008204 {"job_start", 1, 2, f_job_start},
8205 {"job_status", 1, 1, f_job_status},
8206 {"job_stop", 1, 1, f_job_stop},
8207#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008209 {"jsondecode", 1, 1, f_jsondecode},
8210 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008211 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008213 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"libcall", 3, 3, f_libcall},
8215 {"libcallnr", 3, 3, f_libcallnr},
8216 {"line", 1, 1, f_line},
8217 {"line2byte", 1, 1, f_line2byte},
8218 {"lispindent", 1, 1, f_lispindent},
8219 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008220#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008221 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008222 {"log10", 1, 1, f_log10},
8223#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008224#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008225 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008226#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008228 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008229 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008230 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008231 {"matchadd", 2, 5, f_matchadd},
8232 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008233 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008234 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008235 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008236 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008237 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008238 {"max", 1, 1, f_max},
8239 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008240#ifdef vim_mkdir
8241 {"mkdir", 1, 3, f_mkdir},
8242#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008244#ifdef FEAT_MZSCHEME
8245 {"mzeval", 1, 1, f_mzeval},
8246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008248 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008249 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008250 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008251#ifdef FEAT_PERL
8252 {"perleval", 1, 1, f_perleval},
8253#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008254#ifdef FEAT_FLOAT
8255 {"pow", 2, 2, f_pow},
8256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008258 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008259 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008260#ifdef FEAT_PYTHON3
8261 {"py3eval", 1, 1, f_py3eval},
8262#endif
8263#ifdef FEAT_PYTHON
8264 {"pyeval", 1, 1, f_pyeval},
8265#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008266 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008267 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008268 {"reltime", 0, 2, f_reltime},
8269 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"remote_expr", 2, 3, f_remote_expr},
8271 {"remote_foreground", 1, 1, f_remote_foreground},
8272 {"remote_peek", 1, 2, f_remote_peek},
8273 {"remote_read", 1, 1, f_remote_read},
8274 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008275 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008277 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008279 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008280#ifdef FEAT_FLOAT
8281 {"round", 1, 1, f_round},
8282#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008283 {"screenattr", 2, 2, f_screenattr},
8284 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008285 {"screencol", 0, 0, f_screencol},
8286 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008287 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008288 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008289 {"searchpair", 3, 7, f_searchpair},
8290 {"searchpairpos", 3, 7, f_searchpairpos},
8291 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"server2client", 2, 2, f_server2client},
8293 {"serverlist", 0, 0, f_serverlist},
8294 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008295 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"setcmdpos", 1, 1, f_setcmdpos},
8297 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008298 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008299 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008300 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008301 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008303 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008304 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008306#ifdef FEAT_CRYPT
8307 {"sha256", 1, 1, f_sha256},
8308#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008309 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008310 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008312#ifdef FEAT_FLOAT
8313 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008314 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008315#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008316 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008317 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008318 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008319 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008320 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#ifdef FEAT_FLOAT
8322 {"sqrt", 1, 1, f_sqrt},
8323 {"str2float", 1, 1, f_str2float},
8324#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008325 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008326 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008327 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#ifdef HAVE_STRFTIME
8329 {"strftime", 1, 2, f_strftime},
8330#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008331 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008332 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"strlen", 1, 1, f_strlen},
8334 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008335 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008337 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008338 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"substitute", 4, 4, f_substitute},
8340 {"synID", 3, 3, f_synID},
8341 {"synIDattr", 2, 3, f_synIDattr},
8342 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008343 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008344 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008345 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008346 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008347 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008348 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008349 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008350 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008351 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008352#ifdef FEAT_FLOAT
8353 {"tan", 1, 1, f_tan},
8354 {"tanh", 1, 1, f_tanh},
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008357 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"tolower", 1, 1, f_tolower},
8359 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008360 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361#ifdef FEAT_FLOAT
8362 {"trunc", 1, 1, f_trunc},
8363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008365 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008366 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008367 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008368 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"virtcol", 1, 1, f_virtcol},
8370 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008371 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"winbufnr", 1, 1, f_winbufnr},
8373 {"wincol", 0, 0, f_wincol},
8374 {"winheight", 1, 1, f_winheight},
8375 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008376 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008378 {"winrestview", 1, 1, f_winrestview},
8379 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008381 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008382 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008383 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384};
8385
8386#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8387
8388/*
8389 * Function given to ExpandGeneric() to obtain the list of internal
8390 * or user defined function names.
8391 */
8392 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008393get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
8395 static int intidx = -1;
8396 char_u *name;
8397
8398 if (idx == 0)
8399 intidx = -1;
8400 if (intidx < 0)
8401 {
8402 name = get_user_func_name(xp, idx);
8403 if (name != NULL)
8404 return name;
8405 }
8406 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8407 {
8408 STRCPY(IObuff, functions[intidx].f_name);
8409 STRCAT(IObuff, "(");
8410 if (functions[intidx].f_max_argc == 0)
8411 STRCAT(IObuff, ")");
8412 return IObuff;
8413 }
8414
8415 return NULL;
8416}
8417
8418/*
8419 * Function given to ExpandGeneric() to obtain the list of internal or
8420 * user defined variable or function names.
8421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008423get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 static int intidx = -1;
8426 char_u *name;
8427
8428 if (idx == 0)
8429 intidx = -1;
8430 if (intidx < 0)
8431 {
8432 name = get_function_name(xp, idx);
8433 if (name != NULL)
8434 return name;
8435 }
8436 return get_user_var_name(xp, ++intidx);
8437}
8438
8439#endif /* FEAT_CMDL_COMPL */
8440
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008441#if defined(EBCDIC) || defined(PROTO)
8442/*
8443 * Compare struct fst by function name.
8444 */
8445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008447{
8448 struct fst *p1 = (struct fst *)s1;
8449 struct fst *p2 = (struct fst *)s2;
8450
8451 return STRCMP(p1->f_name, p2->f_name);
8452}
8453
8454/*
8455 * Sort the function table by function name.
8456 * The sorting of the table above is ASCII dependant.
8457 * On machines using EBCDIC we have to sort it.
8458 */
8459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008460sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008461{
8462 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8463
8464 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8465}
8466#endif
8467
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469/*
8470 * Find internal function in table above.
8471 * Return index, or -1 if not found
8472 */
8473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008474find_internal_func(
8475 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 int first = 0;
8478 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8479 int cmp;
8480 int x;
8481
8482 /*
8483 * Find the function name in the table. Binary search.
8484 */
8485 while (first <= last)
8486 {
8487 x = first + ((unsigned)(last - first) >> 1);
8488 cmp = STRCMP(name, functions[x].f_name);
8489 if (cmp < 0)
8490 last = x - 1;
8491 else if (cmp > 0)
8492 first = x + 1;
8493 else
8494 return x;
8495 }
8496 return -1;
8497}
8498
8499/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008500 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8501 * name it contains, otherwise return "name".
8502 */
8503 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008504deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505{
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 int cc;
8508
8509 cc = name[*lenp];
8510 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008511 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 {
8517 *lenp = 0;
8518 return (char_u *)""; /* just in case */
8519 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008520 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 }
8523
8524 return name;
8525}
8526
8527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * Allocate a variable for the result of a function.
8529 * Return OK or FAIL.
8530 */
8531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008532get_func_tv(
8533 char_u *name, /* name of the function */
8534 int len, /* length of "name" */
8535 typval_T *rettv,
8536 char_u **arg, /* argument, pointing to the '(' */
8537 linenr_T firstline, /* first line of range */
8538 linenr_T lastline, /* last line of range */
8539 int *doesrange, /* return: function handled range */
8540 int evaluate,
8541 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 char_u *argp;
8544 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008545 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 int argcount = 0; /* number of arguments found */
8547
8548 /*
8549 * Get the arguments.
8550 */
8551 argp = *arg;
8552 while (argcount < MAX_FUNC_ARGS)
8553 {
8554 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8555 if (*argp == ')' || *argp == ',' || *argp == NUL)
8556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8558 {
8559 ret = FAIL;
8560 break;
8561 }
8562 ++argcount;
8563 if (*argp != ',')
8564 break;
8565 }
8566 if (*argp == ')')
8567 ++argp;
8568 else
8569 ret = FAIL;
8570
8571 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 {
8576 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008577 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008579 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
8582 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
8585 *arg = skipwhite(argp);
8586 return ret;
8587}
8588
8589
8590/*
8591 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008592 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008593 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008596call_func(
8597 char_u *funcname, /* name of the function */
8598 int len, /* length of "name" */
8599 typval_T *rettv, /* return value goes here */
8600 int argcount, /* number of "argvars" */
8601 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008602 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008603 linenr_T firstline, /* first line of range */
8604 linenr_T lastline, /* last line of range */
8605 int *doesrange, /* return: function handled range */
8606 int evaluate,
8607 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610#define ERROR_UNKNOWN 0
8611#define ERROR_TOOMANY 1
8612#define ERROR_TOOFEW 2
8613#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008614#define ERROR_DICT 4
8615#define ERROR_NONE 5
8616#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 int error = ERROR_NONE;
8618 int i;
8619 int llen;
8620 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621#define FLEN_FIXED 40
8622 char_u fname_buf[FLEN_FIXED + 1];
8623 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008624 char_u *name;
8625
8626 /* Make a copy of the name, if it comes from a funcref variable it could
8627 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008628 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008629 if (name == NULL)
8630 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
8632 /*
8633 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8634 * Change <SNR>123_name() to K_SNR 123_name().
8635 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 llen = eval_fname_script(name);
8638 if (llen > 0)
8639 {
8640 fname_buf[0] = K_SPECIAL;
8641 fname_buf[1] = KS_EXTRA;
8642 fname_buf[2] = (int)KE_SNR;
8643 i = 3;
8644 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8645 {
8646 if (current_SID <= 0)
8647 error = ERROR_SCRIPT;
8648 else
8649 {
8650 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8651 i = (int)STRLEN(fname_buf);
8652 }
8653 }
8654 if (i + STRLEN(name + llen) < FLEN_FIXED)
8655 {
8656 STRCPY(fname_buf + i, name + llen);
8657 fname = fname_buf;
8658 }
8659 else
8660 {
8661 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8662 if (fname == NULL)
8663 error = ERROR_OTHER;
8664 else
8665 {
8666 mch_memmove(fname, fname_buf, (size_t)i);
8667 STRCPY(fname + i, name + llen);
8668 }
8669 }
8670 }
8671 else
8672 fname = name;
8673
8674 *doesrange = FALSE;
8675
8676
8677 /* execute the function if no errors detected and executing */
8678 if (evaluate && error == ERROR_NONE)
8679 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008680 char_u *rfname = fname;
8681
8682 /* Ignore "g:" before a function name. */
8683 if (fname[0] == 'g' && fname[1] == ':')
8684 rfname = fname + 2;
8685
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008686 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8687 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 error = ERROR_UNKNOWN;
8689
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008690 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 /*
8693 * User defined function.
8694 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008695 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698 /* Trigger FuncUndefined event, may load the function. */
8699 if (fp == NULL
8700 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008705 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
8707#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 {
8711 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008712 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008713 }
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 if (fp != NULL)
8716 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008717 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008719 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008721 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008724 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 else
8726 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008727 int did_save_redo = FALSE;
8728
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 /*
8730 * Call the user function.
8731 * Save and restore search patterns, script variables and
8732 * redo buffer.
8733 */
8734 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008735#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008736 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008737#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008738 {
8739 saveRedobuff();
8740 did_save_redo = TRUE;
8741 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008745 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8746 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8747 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008748 /* Function was unreferenced while being used, free it
8749 * now. */
8750 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008751 if (did_save_redo)
8752 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 restore_search_patterns();
8754 error = ERROR_NONE;
8755 }
8756 }
8757 }
8758 else
8759 {
8760 /*
8761 * Find the function name in the table, call its implementation.
8762 */
8763 i = find_internal_func(fname);
8764 if (i >= 0)
8765 {
8766 if (argcount < functions[i].f_min_argc)
8767 error = ERROR_TOOFEW;
8768 else if (argcount > functions[i].f_max_argc)
8769 error = ERROR_TOOMANY;
8770 else
8771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008772 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 error = ERROR_NONE;
8775 }
8776 }
8777 }
8778 /*
8779 * The function call (or "FuncUndefined" autocommand sequence) might
8780 * have been aborted by an error, an interrupt, or an explicitly thrown
8781 * exception that has not been caught so far. This situation can be
8782 * tested for by calling aborting(). For an error in an internal
8783 * function or for the "E132" error in call_user_func(), however, the
8784 * throw point at which the "force_abort" flag (temporarily reset by
8785 * emsg()) is normally updated has not been reached yet. We need to
8786 * update that flag first to make aborting() reliable.
8787 */
8788 update_force_abort();
8789 }
8790 if (error == ERROR_NONE)
8791 ret = OK;
8792
8793 /*
8794 * Report an error unless the argument evaluation or function call has been
8795 * cancelled due to an aborting error, an interrupt, or an exception.
8796 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008797 if (!aborting())
8798 {
8799 switch (error)
8800 {
8801 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008802 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008803 break;
8804 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008805 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 break;
8807 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008808 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008809 name);
8810 break;
8811 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008815 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008817 name);
8818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008819 }
8820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (fname != name && fname != fname_buf)
8823 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008824 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 return ret;
8827}
8828
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829/*
8830 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008831 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 */
8833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008834emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008835{
8836 char_u *p;
8837
8838 if (*name == K_SPECIAL)
8839 p = concat_str((char_u *)"<SNR>", name + 3);
8840 else
8841 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008842 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843 if (p != name)
8844 vim_free(p);
8845}
8846
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008847/*
8848 * Return TRUE for a non-zero Number and a non-empty String.
8849 */
8850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008851non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008852{
8853 return ((argvars[0].v_type == VAR_NUMBER
8854 && argvars[0].vval.v_number != 0)
8855 || (argvars[0].v_type == VAR_STRING
8856 && argvars[0].vval.v_string != NULL
8857 && *argvars[0].vval.v_string != NUL));
8858}
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860/*********************************************
8861 * Implementation of the built-in functions
8862 */
8863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008865static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008866
8867/*
8868 * Get the float value of "argvars[0]" into "f".
8869 * Returns FAIL when the argument is not a Number or Float.
8870 */
8871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008872get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008873{
8874 if (argvars[0].v_type == VAR_FLOAT)
8875 {
8876 *f = argvars[0].vval.v_float;
8877 return OK;
8878 }
8879 if (argvars[0].v_type == VAR_NUMBER)
8880 {
8881 *f = (float_T)argvars[0].vval.v_number;
8882 return OK;
8883 }
8884 EMSG(_("E808: Number or Float required"));
8885 return FAIL;
8886}
8887
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008888/*
8889 * "abs(expr)" function
8890 */
8891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893{
8894 if (argvars[0].v_type == VAR_FLOAT)
8895 {
8896 rettv->v_type = VAR_FLOAT;
8897 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8898 }
8899 else
8900 {
8901 varnumber_T n;
8902 int error = FALSE;
8903
8904 n = get_tv_number_chk(&argvars[0], &error);
8905 if (error)
8906 rettv->vval.v_number = -1;
8907 else if (n > 0)
8908 rettv->vval.v_number = n;
8909 else
8910 rettv->vval.v_number = -n;
8911 }
8912}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008913
8914/*
8915 * "acos()" function
8916 */
8917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008918f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008919{
8920 float_T f;
8921
8922 rettv->v_type = VAR_FLOAT;
8923 if (get_float_arg(argvars, &f) == OK)
8924 rettv->vval.v_float = acos(f);
8925 else
8926 rettv->vval.v_float = 0.0;
8927}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008928#endif
8929
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008931 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 */
8933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008934f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008939 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008941 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008942 && !tv_check_lock(l->lv_lock,
8943 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008944 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008946 }
8947 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008948 EMSG(_(e_listreq));
8949}
8950
8951/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008952 * "alloc_fail(id, countdown, repeat)" function
8953 */
8954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008955f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008956{
8957 if (argvars[0].v_type != VAR_NUMBER
8958 || argvars[0].vval.v_number <= 0
8959 || argvars[1].v_type != VAR_NUMBER
8960 || argvars[1].vval.v_number < 0
8961 || argvars[2].v_type != VAR_NUMBER)
8962 EMSG(_(e_invarg));
8963 else
8964 {
8965 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008966 if (alloc_fail_id >= aid_last)
8967 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008968 alloc_fail_countdown = argvars[1].vval.v_number;
8969 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008970 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008971 }
8972}
8973
8974/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008975 * "and(expr, expr)" function
8976 */
8977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008979{
8980 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8981 & get_tv_number_chk(&argvars[1], NULL);
8982}
8983
8984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985 * "append(lnum, string/list)" function
8986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989{
8990 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 list_T *l = NULL;
8993 listitem_T *li = NULL;
8994 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995 long added = 0;
8996
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008997 /* When coming here from Insert mode, sync undo, so that this can be
8998 * undone separately from what was previously inserted. */
8999 if (u_sync_once == 2)
9000 {
9001 u_sync_once = 1; /* notify that u_sync() was called */
9002 u_sync(TRUE);
9003 }
9004
Bram Moolenaar0d660222005-01-07 21:51:51 +00009005 lnum = get_tv_lnum(argvars);
9006 if (lnum >= 0
9007 && lnum <= curbuf->b_ml.ml_line_count
9008 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009009 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009010 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009011 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009012 l = argvars[1].vval.v_list;
9013 if (l == NULL)
9014 return;
9015 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009017 for (;;)
9018 {
9019 if (l == NULL)
9020 tv = &argvars[1]; /* append a string */
9021 else if (li == NULL)
9022 break; /* end of list */
9023 else
9024 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 line = get_tv_string_chk(tv);
9026 if (line == NULL) /* type error */
9027 {
9028 rettv->vval.v_number = 1; /* Failed */
9029 break;
9030 }
9031 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009032 ++added;
9033 if (l == NULL)
9034 break;
9035 li = li->li_next;
9036 }
9037
9038 appended_lines_mark(lnum, added);
9039 if (curwin->w_cursor.lnum > lnum)
9040 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042 else
9043 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "argc()" function
9048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
9055/*
9056 * "argidx()" function
9057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009059f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062}
9063
9064/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009065 * "arglistid()" function
9066 */
9067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009069{
9070 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009071
9072 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009073 wp = find_tabwin(&argvars[0], &argvars[1]);
9074 if (wp != NULL)
9075 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009076}
9077
9078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 * "argv(nr)" function
9080 */
9081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009082f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 int idx;
9085
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009086 if (argvars[0].v_type != VAR_UNKNOWN)
9087 {
9088 idx = get_tv_number_chk(&argvars[0], NULL);
9089 if (idx >= 0 && idx < ARGCOUNT)
9090 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9091 else
9092 rettv->vval.v_string = NULL;
9093 rettv->v_type = VAR_STRING;
9094 }
9095 else if (rettv_list_alloc(rettv) == OK)
9096 for (idx = 0; idx < ARGCOUNT; ++idx)
9097 list_append_string(rettv->vval.v_list,
9098 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
9100
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009101static void prepare_assert_error(garray_T*gap);
9102static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9103static void assert_error(garray_T *gap);
9104static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009105
9106/*
9107 * Prepare "gap" for an assert error and add the sourcing position.
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009111{
9112 char buf[NUMBUFLEN];
9113
9114 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009115 if (sourcing_name != NULL)
9116 {
9117 ga_concat(gap, sourcing_name);
9118 if (sourcing_lnum > 0)
9119 ga_concat(gap, (char_u *)" ");
9120 }
9121 if (sourcing_lnum > 0)
9122 {
9123 sprintf(buf, "line %ld", (long)sourcing_lnum);
9124 ga_concat(gap, (char_u *)buf);
9125 }
9126 if (sourcing_name != NULL || sourcing_lnum > 0)
9127 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009128}
9129
9130/*
9131 * Fill "gap" with information about an assert error.
9132 */
9133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009134fill_assert_error(
9135 garray_T *gap,
9136 typval_T *opt_msg_tv,
9137 char_u *exp_str,
9138 typval_T *exp_tv,
9139 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009140{
9141 char_u numbuf[NUMBUFLEN];
9142 char_u *tofree;
9143
9144 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9145 {
9146 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9147 vim_free(tofree);
9148 }
9149 else
9150 {
9151 ga_concat(gap, (char_u *)"Expected ");
9152 if (exp_str == NULL)
9153 {
9154 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9155 vim_free(tofree);
9156 }
9157 else
9158 ga_concat(gap, exp_str);
9159 ga_concat(gap, (char_u *)" but got ");
9160 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9161 vim_free(tofree);
9162 }
9163}
Bram Moolenaar43345542015-11-29 17:35:35 +01009164
9165/*
9166 * Add an assert error to v:errors.
9167 */
9168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009169assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009170{
9171 struct vimvar *vp = &vimvars[VV_ERRORS];
9172
9173 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9174 /* Make sure v:errors is a list. */
9175 set_vim_var_list(VV_ERRORS, list_alloc());
9176 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9177}
9178
Bram Moolenaar43345542015-11-29 17:35:35 +01009179/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009180 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009181 */
9182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009183f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009184{
9185 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009186
9187 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9188 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009189 prepare_assert_error(&ga);
9190 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9191 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009192 ga_clear(&ga);
9193 }
9194}
9195
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009196/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009197 * "assert_exception(string[, msg])" function
9198 */
9199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009200f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009201{
9202 garray_T ga;
9203 char *error;
9204
9205 error = (char *)get_tv_string_chk(&argvars[0]);
9206 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9207 {
9208 prepare_assert_error(&ga);
9209 ga_concat(&ga, (char_u *)"v:exception is not set");
9210 assert_error(&ga);
9211 ga_clear(&ga);
9212 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009213 else if (error != NULL
9214 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009215 {
9216 prepare_assert_error(&ga);
9217 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9218 &vimvars[VV_EXCEPTION].vv_tv);
9219 assert_error(&ga);
9220 ga_clear(&ga);
9221 }
9222}
9223
9224/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009225 * "assert_fails(cmd [, error])" function
9226 */
9227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009228f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009229{
9230 char_u *cmd = get_tv_string_chk(&argvars[0]);
9231 garray_T ga;
9232
9233 called_emsg = FALSE;
9234 suppress_errthrow = TRUE;
9235 emsg_silent = TRUE;
9236 do_cmdline_cmd(cmd);
9237 if (!called_emsg)
9238 {
9239 prepare_assert_error(&ga);
9240 ga_concat(&ga, (char_u *)"command did not fail: ");
9241 ga_concat(&ga, cmd);
9242 assert_error(&ga);
9243 ga_clear(&ga);
9244 }
9245 else if (argvars[1].v_type != VAR_UNKNOWN)
9246 {
9247 char_u buf[NUMBUFLEN];
9248 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9249
9250 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9251 {
9252 prepare_assert_error(&ga);
9253 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9254 &vimvars[VV_ERRMSG].vv_tv);
9255 assert_error(&ga);
9256 ga_clear(&ga);
9257 }
9258 }
9259
9260 called_emsg = FALSE;
9261 suppress_errthrow = FALSE;
9262 emsg_silent = FALSE;
9263 emsg_on_display = FALSE;
9264 set_vim_var_string(VV_ERRMSG, NULL, 0);
9265}
9266
9267/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268 * Common for assert_true() and assert_false().
9269 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009271assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009272{
9273 int error = FALSE;
9274 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009275
Bram Moolenaar37127922016-02-06 20:29:28 +01009276 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009277 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009278 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009279 if (argvars[0].v_type != VAR_NUMBER
9280 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9281 || error)
9282 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009283 prepare_assert_error(&ga);
9284 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009285 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009286 NULL, &argvars[0]);
9287 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009288 ga_clear(&ga);
9289 }
9290}
9291
9292/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009293 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009294 */
9295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009296f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009297{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009298 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009299}
9300
9301/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009302 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009303 */
9304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009305f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009306{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009307 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009308}
9309
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009310#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009311/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009312 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009315f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009316{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009317 float_T f;
9318
9319 rettv->v_type = VAR_FLOAT;
9320 if (get_float_arg(argvars, &f) == OK)
9321 rettv->vval.v_float = asin(f);
9322 else
9323 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009324}
9325
9326/*
9327 * "atan()" function
9328 */
9329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009330f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009331{
9332 float_T f;
9333
9334 rettv->v_type = VAR_FLOAT;
9335 if (get_float_arg(argvars, &f) == OK)
9336 rettv->vval.v_float = atan(f);
9337 else
9338 rettv->vval.v_float = 0.0;
9339}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009340
9341/*
9342 * "atan2()" function
9343 */
9344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009345f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009346{
9347 float_T fx, fy;
9348
9349 rettv->v_type = VAR_FLOAT;
9350 if (get_float_arg(argvars, &fx) == OK
9351 && get_float_arg(&argvars[1], &fy) == OK)
9352 rettv->vval.v_float = atan2(fx, fy);
9353 else
9354 rettv->vval.v_float = 0.0;
9355}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009356#endif
9357
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358/*
9359 * "browse(save, title, initdir, default)" function
9360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364#ifdef FEAT_BROWSE
9365 int save;
9366 char_u *title;
9367 char_u *initdir;
9368 char_u *defname;
9369 char_u buf[NUMBUFLEN];
9370 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 save = get_tv_number_chk(&argvars[0], &error);
9374 title = get_tv_string_chk(&argvars[1]);
9375 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9376 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 if (error || title == NULL || initdir == NULL || defname == NULL)
9379 rettv->vval.v_string = NULL;
9380 else
9381 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009382 do_browse(save ? BROWSE_SAVE : 0,
9383 title, defname, NULL, initdir, NULL, curbuf);
9384#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009388}
9389
9390/*
9391 * "browsedir(title, initdir)" function
9392 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009394f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009395{
9396#ifdef FEAT_BROWSE
9397 char_u *title;
9398 char_u *initdir;
9399 char_u buf[NUMBUFLEN];
9400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401 title = get_tv_string_chk(&argvars[0]);
9402 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 if (title == NULL || initdir == NULL)
9405 rettv->vval.v_string = NULL;
9406 else
9407 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009408 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009415static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417/*
9418 * Find a buffer by number or exact name.
9419 */
9420 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009421find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009425 if (avar->v_type == VAR_NUMBER)
9426 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009427 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009430 if (buf == NULL)
9431 {
9432 /* No full path name match, try a match with a URL or a "nofile"
9433 * buffer, these don't use the full path. */
9434 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9435 if (buf->b_fname != NULL
9436 && (path_with_url(buf->b_fname)
9437#ifdef FEAT_QUICKFIX
9438 || bt_nofile(buf)
9439#endif
9440 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009441 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009442 break;
9443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 }
9445 return buf;
9446}
9447
9448/*
9449 * "bufexists(expr)" function
9450 */
9451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009452f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455}
9456
9457/*
9458 * "buflisted(expr)" function
9459 */
9460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009461f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
9463 buf_T *buf;
9464
9465 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * "bufloaded(expr)" function
9471 */
9472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009473f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 buf_T *buf;
9476
9477 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479}
9480
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009481static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483/*
9484 * Get buffer by number or pattern.
9485 */
9486 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 int save_magic;
9491 char_u *save_cpo;
9492 buf_T *buf;
9493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 if (tv->v_type == VAR_NUMBER)
9495 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009496 if (tv->v_type != VAR_STRING)
9497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (name == NULL || *name == NUL)
9499 return curbuf;
9500 if (name[0] == '$' && name[1] == NUL)
9501 return lastbuf;
9502
9503 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9504 save_magic = p_magic;
9505 p_magic = TRUE;
9506 save_cpo = p_cpo;
9507 p_cpo = (char_u *)"";
9508
9509 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009510 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
9512 p_magic = save_magic;
9513 p_cpo = save_cpo;
9514
9515 /* If not found, try expanding the name, like done for bufexists(). */
9516 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
9519 return buf;
9520}
9521
9522/*
9523 * "bufname(expr)" function
9524 */
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 buf_T *buf;
9529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009532 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 --emsg_off;
9539}
9540
9541/*
9542 * "bufnr(expr)" function
9543 */
9544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009548 int error = FALSE;
9549 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009553 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009554 --emsg_off;
9555
9556 /* If the buffer isn't found and the second argument is not zero create a
9557 * new buffer. */
9558 if (buf == NULL
9559 && argvars[1].v_type != VAR_UNKNOWN
9560 && get_tv_number_chk(&argvars[1], &error) != 0
9561 && !error
9562 && (name = get_tv_string_chk(&argvars[0])) != NULL
9563 && !error)
9564 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9565
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
9573 * "bufwinnr(nr)" function
9574 */
9575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009576f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578#ifdef FEAT_WINDOWS
9579 win_T *wp;
9580 int winnr = 0;
9581#endif
9582 buf_T *buf;
9583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009586 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587#ifdef FEAT_WINDOWS
9588 for (wp = firstwin; wp; wp = wp->w_next)
9589 {
9590 ++winnr;
9591 if (wp->w_buffer == buf)
9592 break;
9593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#endif
9598 --emsg_off;
9599}
9600
9601/*
9602 * "byte2line(byte)" function
9603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009605f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
9607#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609#else
9610 long boff = 0;
9611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 (linenr_T)0, &boff);
9618#endif
9619}
9620
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009622byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009623{
9624#ifdef FEAT_MBYTE
9625 char_u *t;
9626#endif
9627 char_u *str;
9628 long idx;
9629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 str = get_tv_string_chk(&argvars[0]);
9631 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009634 return;
9635
9636#ifdef FEAT_MBYTE
9637 t = str;
9638 for ( ; idx > 0; idx--)
9639 {
9640 if (*t == NUL) /* EOL reached */
9641 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009642 if (enc_utf8 && comp)
9643 t += utf_ptr2len(t);
9644 else
9645 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009646 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009647 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009648#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009649 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009651#endif
9652}
9653
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009654/*
9655 * "byteidx()" function
9656 */
9657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009659{
9660 byteidx(argvars, rettv, FALSE);
9661}
9662
9663/*
9664 * "byteidxcomp()" function
9665 */
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009668{
9669 byteidx(argvars, rettv, TRUE);
9670}
9671
Bram Moolenaardb913952012-06-29 12:54:53 +02009672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009673func_call(
9674 char_u *name,
9675 typval_T *args,
9676 dict_T *selfdict,
9677 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009678{
9679 listitem_T *item;
9680 typval_T argv[MAX_FUNC_ARGS + 1];
9681 int argc = 0;
9682 int dummy;
9683 int r = 0;
9684
9685 for (item = args->vval.v_list->lv_first; item != NULL;
9686 item = item->li_next)
9687 {
9688 if (argc == MAX_FUNC_ARGS)
9689 {
9690 EMSG(_("E699: Too many arguments"));
9691 break;
9692 }
9693 /* Make a copy of each argument. This is needed to be able to set
9694 * v_lock to VAR_FIXED in the copy without changing the original list.
9695 */
9696 copy_tv(&item->li_tv, &argv[argc++]);
9697 }
9698
9699 if (item == NULL)
9700 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9701 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9702 &dummy, TRUE, selfdict);
9703
9704 /* Free the arguments. */
9705 while (argc > 0)
9706 clear_tv(&argv[--argc]);
9707
9708 return r;
9709}
9710
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009711/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009712 * "call(func, arglist)" function
9713 */
9714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009715f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009716{
9717 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009719
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 if (argvars[1].v_type != VAR_LIST)
9721 {
9722 EMSG(_(e_listreq));
9723 return;
9724 }
9725 if (argvars[1].vval.v_list == NULL)
9726 return;
9727
9728 if (argvars[0].v_type == VAR_FUNC)
9729 func = argvars[0].vval.v_string;
9730 else
9731 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009732 if (*func == NUL)
9733 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009734
Bram Moolenaare9a41262005-01-15 22:18:47 +00009735 if (argvars[2].v_type != VAR_UNKNOWN)
9736 {
9737 if (argvars[2].v_type != VAR_DICT)
9738 {
9739 EMSG(_(e_dictreq));
9740 return;
9741 }
9742 selfdict = argvars[2].vval.v_dict;
9743 }
9744
Bram Moolenaardb913952012-06-29 12:54:53 +02009745 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009746}
9747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009748#ifdef FEAT_FLOAT
9749/*
9750 * "ceil({float})" function
9751 */
9752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009753f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009754{
9755 float_T f;
9756
9757 rettv->v_type = VAR_FLOAT;
9758 if (get_float_arg(argvars, &f) == OK)
9759 rettv->vval.v_float = ceil(f);
9760 else
9761 rettv->vval.v_float = 0.0;
9762}
9763#endif
9764
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009765#ifdef FEAT_CHANNEL
9766/*
9767 * Get the channel index from the handle argument.
9768 * Returns -1 if the handle is invalid or the channel is closed.
9769 */
9770 static int
9771get_channel_arg(typval_T *tv)
9772{
9773 int ch_idx;
9774
9775 if (tv->v_type != VAR_NUMBER)
9776 {
9777 EMSG2(_(e_invarg2), get_tv_string(tv));
9778 return -1;
9779 }
9780 ch_idx = tv->vval.v_number;
9781
9782 if (!channel_is_open(ch_idx))
9783 {
9784 EMSGN(_("E906: not an open channel"), ch_idx);
9785 return -1;
9786 }
9787 return ch_idx;
9788}
9789
9790/*
9791 * "ch_close()" function
9792 */
9793 static void
9794f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9795{
9796 int ch_idx = get_channel_arg(&argvars[0]);
9797
9798 if (ch_idx >= 0)
9799 channel_close(ch_idx);
9800}
9801
9802/*
9803 * Get a callback from "arg". It can be a Funcref or a function name.
9804 * When "arg" is zero return an empty string.
9805 * Return NULL for an invalid argument.
9806 */
9807 static char_u *
9808get_callback(typval_T *arg)
9809{
9810 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9811 return arg->vval.v_string;
9812 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9813 return (char_u *)"";
9814 EMSG(_("E999: Invalid callback argument"));
9815 return NULL;
9816}
9817
9818/*
9819 * "ch_open()" function
9820 */
9821 static void
9822f_ch_open(typval_T *argvars, typval_T *rettv)
9823{
9824 char_u *address;
9825 char_u *mode;
9826 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009827 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009828 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009829 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009830 int waittime = 0;
9831 int timeout = 2000;
9832 int json_mode = TRUE;
9833 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009834
9835 /* default: fail */
9836 rettv->vval.v_number = -1;
9837
9838 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009839 if (argvars[1].v_type != VAR_UNKNOWN
9840 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009841 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009842 EMSG(_(e_invarg));
9843 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009844 }
9845
9846 /* parse address */
9847 p = vim_strchr(address, ':');
9848 if (p == NULL)
9849 {
9850 EMSG2(_(e_invarg2), address);
9851 return;
9852 }
9853 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009854 port = strtol((char *)p, &rest, 10);
9855 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009856 {
9857 p[-1] = ':';
9858 EMSG2(_(e_invarg2), address);
9859 return;
9860 }
9861
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009862 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009863 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009864 /* parse argdict */
9865 dict_T *dict = argvars[1].vval.v_dict;
9866
9867 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9868 {
9869 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9870 if (STRCMP(mode, "raw") == 0)
9871 json_mode = FALSE;
9872 else if (STRCMP(mode, "json") != 0)
9873 {
9874 EMSG2(_(e_invarg2), mode);
9875 return;
9876 }
9877 }
9878 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9879 waittime = get_dict_number(dict, (char_u *)"waittime");
9880 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9881 timeout = get_dict_number(dict, (char_u *)"timeout");
9882 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9883 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9884 }
9885 if (waittime < 0 || timeout < 0)
9886 {
9887 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009888 return;
9889 }
9890
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009891 ch_idx = channel_open((char *)address, port, waittime, NULL);
9892 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009893 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009894 channel_set_json_mode(ch_idx, json_mode);
9895 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009896 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009897 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009898 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009899 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009900}
9901
9902/*
9903 * common for "sendexpr()" and "sendraw()"
9904 * Returns the channel index if the caller should read the response.
9905 * Otherwise returns -1.
9906 */
9907 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009908send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009909{
9910 int ch_idx;
9911 char_u *callback = NULL;
9912
9913 ch_idx = get_channel_arg(&argvars[0]);
9914 if (ch_idx < 0)
9915 return -1;
9916
9917 if (argvars[2].v_type != VAR_UNKNOWN)
9918 {
9919 callback = get_callback(&argvars[2]);
9920 if (callback == NULL)
9921 return -1;
9922 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009923 /* Set the callback. An empty callback means no callback and not reading
9924 * the response. */
9925 if (callback != NULL && *callback != NUL)
9926 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009927
9928 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9929 return ch_idx;
9930 return -1;
9931}
9932
9933/*
9934 * "ch_sendexpr()" function
9935 */
9936 static void
9937f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9938{
9939 char_u *text;
9940 typval_T *listtv;
9941 int ch_idx;
9942 int id;
9943
9944 /* return an empty string by default */
9945 rettv->v_type = VAR_STRING;
9946 rettv->vval.v_string = NULL;
9947
9948 id = channel_get_id();
9949 text = json_encode_nr_expr(id, &argvars[1]);
9950 if (text == NULL)
9951 return;
9952
Bram Moolenaara07fec92016-02-05 21:04:08 +01009953 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009954 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009955 if (ch_idx >= 0)
9956 {
9957 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9958 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009959 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009960
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009961 /* Move the item from the list and then change the type to
9962 * avoid the value being freed. */
9963 *rettv = list->lv_last->li_tv;
9964 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009965 clear_tv(listtv);
9966 }
9967 }
9968}
9969
9970/*
9971 * "ch_sendraw()" function
9972 */
9973 static void
9974f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9975{
9976 char_u buf[NUMBUFLEN];
9977 char_u *text;
9978 int ch_idx;
9979
9980 /* return an empty string by default */
9981 rettv->v_type = VAR_STRING;
9982 rettv->vval.v_string = NULL;
9983
9984 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009985 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009986 if (ch_idx >= 0)
9987 rettv->vval.v_string = channel_read_block(ch_idx);
9988}
9989#endif
9990
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009991/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009992 * "changenr()" function
9993 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009996{
9997 rettv->vval.v_number = curbuf->b_u_seq_cur;
9998}
9999
10000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 * "char2nr(string)" function
10002 */
10003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010004f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006#ifdef FEAT_MBYTE
10007 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010008 {
10009 int utf8 = 0;
10010
10011 if (argvars[1].v_type != VAR_UNKNOWN)
10012 utf8 = get_tv_number_chk(&argvars[1], NULL);
10013
10014 if (utf8)
10015 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10016 else
10017 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022}
10023
10024/*
10025 * "cindent(lnum)" function
10026 */
10027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010028f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030#ifdef FEAT_CINDENT
10031 pos_T pos;
10032 linenr_T lnum;
10033
10034 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10037 {
10038 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 curwin->w_cursor = pos;
10041 }
10042 else
10043#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
10047/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010048 * "clearmatches()" function
10049 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010051f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010052{
10053#ifdef FEAT_SEARCH_EXTRA
10054 clear_matches(curwin);
10055#endif
10056}
10057
10058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 * "col(string)" function
10060 */
10061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010062f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
10064 colnr_T col = 0;
10065 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010066 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010068 fp = var2fpos(&argvars[0], FALSE, &fnum);
10069 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 if (fp->col == MAXCOL)
10072 {
10073 /* '> can be MAXCOL, get the length of the line then */
10074 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010075 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 else
10077 col = MAXCOL;
10078 }
10079 else
10080 {
10081 col = fp->col + 1;
10082#ifdef FEAT_VIRTUALEDIT
10083 /* col(".") when the cursor is on the NUL at the end of the line
10084 * because of "coladd" can be seen as an extra column. */
10085 if (virtual_active() && fp == &curwin->w_cursor)
10086 {
10087 char_u *p = ml_get_cursor();
10088
10089 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10090 curwin->w_virtcol - curwin->w_cursor.coladd))
10091 {
10092# ifdef FEAT_MBYTE
10093 int l;
10094
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010095 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 col += l;
10097# else
10098 if (*p != NUL && p[1] == NUL)
10099 ++col;
10100# endif
10101 }
10102 }
10103#endif
10104 }
10105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107}
10108
Bram Moolenaar572cb562005-08-05 21:35:02 +000010109#if defined(FEAT_INS_EXPAND)
10110/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010111 * "complete()" function
10112 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010114f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010115{
10116 int startcol;
10117
10118 if ((State & INSERT) == 0)
10119 {
10120 EMSG(_("E785: complete() can only be used in Insert mode"));
10121 return;
10122 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010123
10124 /* Check for undo allowed here, because if something was already inserted
10125 * the line was already saved for undo and this check isn't done. */
10126 if (!undo_allowed())
10127 return;
10128
Bram Moolenaarade00832006-03-10 21:46:58 +000010129 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10130 {
10131 EMSG(_(e_invarg));
10132 return;
10133 }
10134
10135 startcol = get_tv_number_chk(&argvars[0], NULL);
10136 if (startcol <= 0)
10137 return;
10138
10139 set_completion(startcol - 1, argvars[1].vval.v_list);
10140}
10141
10142/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010143 * "complete_add()" function
10144 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010146f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010147{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010148 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010149}
10150
10151/*
10152 * "complete_check()" function
10153 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010155f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010156{
10157 int saved = RedrawingDisabled;
10158
10159 RedrawingDisabled = 0;
10160 ins_compl_check_keys(0);
10161 rettv->vval.v_number = compl_interrupted;
10162 RedrawingDisabled = saved;
10163}
10164#endif
10165
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166/*
10167 * "confirm(message, buttons[, default [, type]])" function
10168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010170f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10173 char_u *message;
10174 char_u *buttons = NULL;
10175 char_u buf[NUMBUFLEN];
10176 char_u buf2[NUMBUFLEN];
10177 int def = 1;
10178 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 char_u *typestr;
10180 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 message = get_tv_string_chk(&argvars[0]);
10183 if (message == NULL)
10184 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010185 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10188 if (buttons == NULL)
10189 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010190 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010193 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10196 if (typestr == NULL)
10197 error = TRUE;
10198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 switch (TOUPPER_ASC(*typestr))
10201 {
10202 case 'E': type = VIM_ERROR; break;
10203 case 'Q': type = VIM_QUESTION; break;
10204 case 'I': type = VIM_INFO; break;
10205 case 'W': type = VIM_WARNING; break;
10206 case 'G': type = VIM_GENERIC; break;
10207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 }
10209 }
10210 }
10211 }
10212
10213 if (buttons == NULL || *buttons == NUL)
10214 buttons = (char_u *)_("&Ok");
10215
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010216 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010218 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219#endif
10220}
10221
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010222/*
10223 * "copy()" function
10224 */
10225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010226f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010227{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010228 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010229}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010231#ifdef FEAT_FLOAT
10232/*
10233 * "cos()" function
10234 */
10235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010236f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010237{
10238 float_T f;
10239
10240 rettv->v_type = VAR_FLOAT;
10241 if (get_float_arg(argvars, &f) == OK)
10242 rettv->vval.v_float = cos(f);
10243 else
10244 rettv->vval.v_float = 0.0;
10245}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010246
10247/*
10248 * "cosh()" function
10249 */
10250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010251f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010252{
10253 float_T f;
10254
10255 rettv->v_type = VAR_FLOAT;
10256 if (get_float_arg(argvars, &f) == OK)
10257 rettv->vval.v_float = cosh(f);
10258 else
10259 rettv->vval.v_float = 0.0;
10260}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010261#endif
10262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010264 * "count()" function
10265 */
10266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010269 long n = 0;
10270 int ic = FALSE;
10271
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 listitem_T *li;
10275 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 if ((l = argvars[0].vval.v_list) != NULL)
10279 {
10280 li = l->lv_first;
10281 if (argvars[2].v_type != VAR_UNKNOWN)
10282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 int error = FALSE;
10284
10285 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 if (argvars[3].v_type != VAR_UNKNOWN)
10287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 idx = get_tv_number_chk(&argvars[3], &error);
10289 if (!error)
10290 {
10291 li = list_find(l, idx);
10292 if (li == NULL)
10293 EMSGN(_(e_listidx), idx);
10294 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 if (error)
10297 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 }
10299
10300 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010301 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010302 ++n;
10303 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 else if (argvars[0].v_type == VAR_DICT)
10306 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010307 int todo;
10308 dict_T *d;
10309 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010311 if ((d = argvars[0].vval.v_dict) != NULL)
10312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010313 int error = FALSE;
10314
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 if (argvars[2].v_type != VAR_UNKNOWN)
10316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 if (argvars[3].v_type != VAR_UNKNOWN)
10319 EMSG(_(e_invarg));
10320 }
10321
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010322 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010324 {
10325 if (!HASHITEM_EMPTY(hi))
10326 {
10327 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010328 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010329 ++n;
10330 }
10331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 }
10333 }
10334 else
10335 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010336 rettv->vval.v_number = n;
10337}
10338
10339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10341 *
10342 * Checks the existence of a cscope connection.
10343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010345f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
10347#ifdef FEAT_CSCOPE
10348 int num = 0;
10349 char_u *dbpath = NULL;
10350 char_u *prepend = NULL;
10351 char_u buf[NUMBUFLEN];
10352
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010353 if (argvars[0].v_type != VAR_UNKNOWN
10354 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 num = (int)get_tv_number(&argvars[0]);
10357 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 }
10361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363#endif
10364}
10365
10366/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010367 * "cursor(lnum, col)" function, or
10368 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010370 * Moves the cursor to the specified line and column.
10371 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010374f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375{
10376 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010377#ifdef FEAT_VIRTUALEDIT
10378 long coladd = 0;
10379#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010380 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010382 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010383 if (argvars[1].v_type == VAR_UNKNOWN)
10384 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010385 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010386 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010387
Bram Moolenaar493c1782014-05-28 14:34:46 +020010388 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010389 {
10390 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010391 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010392 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010393 line = pos.lnum;
10394 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010395#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010396 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010397#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010398 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010399 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010400 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010401 set_curswant = FALSE;
10402 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010403 }
10404 else
10405 {
10406 line = get_tv_lnum(argvars);
10407 col = get_tv_number_chk(&argvars[1], NULL);
10408#ifdef FEAT_VIRTUALEDIT
10409 if (argvars[2].v_type != VAR_UNKNOWN)
10410 coladd = get_tv_number_chk(&argvars[2], NULL);
10411#endif
10412 }
10413 if (line < 0 || col < 0
10414#ifdef FEAT_VIRTUALEDIT
10415 || coladd < 0
10416#endif
10417 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 if (line > 0)
10420 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 if (col > 0)
10422 curwin->w_cursor.col = col - 1;
10423#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010424 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425#endif
10426
10427 /* Make sure the cursor is in a valid position. */
10428 check_cursor();
10429#ifdef FEAT_MBYTE
10430 /* Correct cursor for multi-byte character. */
10431 if (has_mbyte)
10432 mb_adjust_cursor();
10433#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010434
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010435 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437}
10438
10439/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010440 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 */
10442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010443f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010445 int noref = 0;
10446
10447 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010449 if (noref < 0 || noref > 1)
10450 EMSG(_(e_invarg));
10451 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010452 {
10453 current_copyID += COPYID_INC;
10454 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456}
10457
10458/*
10459 * "delete()" function
10460 */
10461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010462f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010464 char_u nbuf[NUMBUFLEN];
10465 char_u *name;
10466 char_u *flags;
10467
10468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010470 return;
10471
10472 name = get_tv_string(&argvars[0]);
10473 if (name == NULL || *name == NUL)
10474 {
10475 EMSG(_(e_invarg));
10476 return;
10477 }
10478
10479 if (argvars[1].v_type != VAR_UNKNOWN)
10480 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010482 flags = (char_u *)"";
10483
10484 if (*flags == NUL)
10485 /* delete a file */
10486 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10487 else if (STRCMP(flags, "d") == 0)
10488 /* delete an empty directory */
10489 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10490 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010491 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010492 rettv->vval.v_number = delete_recursive(name);
10493 else
10494 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495}
10496
10497/*
10498 * "did_filetype()" function
10499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505#endif
10506}
10507
10508/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010509 * "diff_filler()" function
10510 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010512f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010513{
10514#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010516#endif
10517}
10518
10519/*
10520 * "diff_hlID()" function
10521 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010524{
10525#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010527 static linenr_T prev_lnum = 0;
10528 static int changedtick = 0;
10529 static int fnum = 0;
10530 static int change_start = 0;
10531 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010532 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010533 int filler_lines;
10534 int col;
10535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 if (lnum < 0) /* ignore type error in {lnum} arg */
10537 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010538 if (lnum != prev_lnum
10539 || changedtick != curbuf->b_changedtick
10540 || fnum != curbuf->b_fnum)
10541 {
10542 /* New line, buffer, change: need to get the values. */
10543 filler_lines = diff_check(curwin, lnum);
10544 if (filler_lines < 0)
10545 {
10546 if (filler_lines == -1)
10547 {
10548 change_start = MAXCOL;
10549 change_end = -1;
10550 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10551 hlID = HLF_ADD; /* added line */
10552 else
10553 hlID = HLF_CHD; /* changed line */
10554 }
10555 else
10556 hlID = HLF_ADD; /* added line */
10557 }
10558 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010559 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010560 prev_lnum = lnum;
10561 changedtick = curbuf->b_changedtick;
10562 fnum = curbuf->b_fnum;
10563 }
10564
10565 if (hlID == HLF_CHD || hlID == HLF_TXD)
10566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010568 if (col >= change_start && col <= change_end)
10569 hlID = HLF_TXD; /* changed text */
10570 else
10571 hlID = HLF_CHD; /* changed line */
10572 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010573 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010574#endif
10575}
10576
10577/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010578 * "empty({expr})" function
10579 */
10580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010581f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010582{
10583 int n;
10584
10585 switch (argvars[0].v_type)
10586 {
10587 case VAR_STRING:
10588 case VAR_FUNC:
10589 n = argvars[0].vval.v_string == NULL
10590 || *argvars[0].vval.v_string == NUL;
10591 break;
10592 case VAR_NUMBER:
10593 n = argvars[0].vval.v_number == 0;
10594 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010595 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010596#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010597 n = argvars[0].vval.v_float == 0.0;
10598 break;
10599#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010600 case VAR_LIST:
10601 n = argvars[0].vval.v_list == NULL
10602 || argvars[0].vval.v_list->lv_first == NULL;
10603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 case VAR_DICT:
10605 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010608 case VAR_SPECIAL:
10609 n = argvars[0].vval.v_number != VVAL_TRUE;
10610 break;
10611
Bram Moolenaar835dc632016-02-07 14:27:38 +010010612 case VAR_JOB:
10613#ifdef FEAT_JOB
10614 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10615 break;
10616#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010617 case VAR_UNKNOWN:
10618 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10619 n = TRUE;
10620 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010621 }
10622
10623 rettv->vval.v_number = n;
10624}
10625
10626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 * "escape({string}, {chars})" function
10628 */
10629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010630f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631{
10632 char_u buf[NUMBUFLEN];
10633
Bram Moolenaar758711c2005-02-02 23:11:38 +000010634 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10635 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637}
10638
10639/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010640 * "eval()" function
10641 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010643f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010644{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010645 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 s = get_tv_string_chk(&argvars[0]);
10648 if (s != NULL)
10649 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650
Bram Moolenaar615b9972015-01-14 17:15:05 +010010651 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10653 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010654 if (p != NULL && !aborting())
10655 EMSG2(_(e_invexpr2), p);
10656 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010657 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010659 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010660 else if (*s != NUL)
10661 EMSG(_(e_trailing));
10662}
10663
10664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 * "eventhandler()" function
10666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
10672
10673/*
10674 * "executable()" function
10675 */
10676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010679 char_u *name = get_tv_string(&argvars[0]);
10680
10681 /* Check in $PATH and also check directly if there is a directory name. */
10682 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10683 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010684}
10685
10686/*
10687 * "exepath()" function
10688 */
10689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010690f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010691{
10692 char_u *p = NULL;
10693
Bram Moolenaarb5971142015-03-21 17:32:19 +010010694 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010695 rettv->v_type = VAR_STRING;
10696 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697}
10698
10699/*
10700 * "exists()" function
10701 */
10702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010703f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 char_u *p;
10706 char_u *name;
10707 int n = FALSE;
10708 int len = 0;
10709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 if (*p == '$') /* environment variable */
10712 {
10713 /* first try "normal" environment variables (fast) */
10714 if (mch_getenv(p + 1) != NULL)
10715 n = TRUE;
10716 else
10717 {
10718 /* try expanding things like $VIM and ${HOME} */
10719 p = expand_env_save(p);
10720 if (p != NULL && *p != '$')
10721 n = TRUE;
10722 vim_free(p);
10723 }
10724 }
10725 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010728 if (*skipwhite(p) != NUL)
10729 n = FALSE; /* trailing garbage */
10730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 else if (*p == '*') /* internal or user defined function */
10732 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 else if (*p == ':')
10736 {
10737 n = cmd_exists(p + 1);
10738 }
10739 else if (*p == '#')
10740 {
10741#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010742 if (p[1] == '#')
10743 n = autocmd_supported(p + 2);
10744 else
10745 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746#endif
10747 }
10748 else /* internal variable */
10749 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010750 char_u *tofree;
10751 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010753 /* get_name_len() takes care of expanding curly braces */
10754 name = p;
10755 len = get_name_len(&p, &tofree, TRUE, FALSE);
10756 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010758 if (tofree != NULL)
10759 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010760 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010761 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010763 /* handle d.key, l[idx], f(expr) */
10764 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10765 if (n)
10766 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
10768 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010769 if (*p != NUL)
10770 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010772 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 }
10774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776}
10777
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010778#ifdef FEAT_FLOAT
10779/*
10780 * "exp()" function
10781 */
10782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010783f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010784{
10785 float_T f;
10786
10787 rettv->v_type = VAR_FLOAT;
10788 if (get_float_arg(argvars, &f) == OK)
10789 rettv->vval.v_float = exp(f);
10790 else
10791 rettv->vval.v_float = 0.0;
10792}
10793#endif
10794
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795/*
10796 * "expand()" function
10797 */
10798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
10801 char_u *s;
10802 int len;
10803 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010804 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010807 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010810 if (argvars[1].v_type != VAR_UNKNOWN
10811 && argvars[2].v_type != VAR_UNKNOWN
10812 && get_tv_number_chk(&argvars[2], &error)
10813 && !error)
10814 {
10815 rettv->v_type = VAR_LIST;
10816 rettv->vval.v_list = NULL;
10817 }
10818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (*s == '%' || *s == '#' || *s == '<')
10821 {
10822 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010823 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010825 if (rettv->v_type == VAR_LIST)
10826 {
10827 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10828 list_append_string(rettv->vval.v_list, result, -1);
10829 else
10830 vim_free(result);
10831 }
10832 else
10833 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 }
10835 else
10836 {
10837 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010838 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 if (argvars[1].v_type != VAR_UNKNOWN
10840 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010841 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 if (!error)
10843 {
10844 ExpandInit(&xpc);
10845 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010846 if (p_wic)
10847 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010848 if (rettv->v_type == VAR_STRING)
10849 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10850 options, WILD_ALL);
10851 else if (rettv_list_alloc(rettv) != FAIL)
10852 {
10853 int i;
10854
10855 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10856 for (i = 0; i < xpc.xp_numfiles; i++)
10857 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10858 ExpandCleanup(&xpc);
10859 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 }
10861 else
10862 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 }
10864}
10865
10866/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010867 * Go over all entries in "d2" and add them to "d1".
10868 * When "action" is "error" then a duplicate key is an error.
10869 * When "action" is "force" then a duplicate key is overwritten.
10870 * Otherwise duplicate keys are ignored ("action" is "keep").
10871 */
10872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010873dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010874{
10875 dictitem_T *di1;
10876 hashitem_T *hi2;
10877 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010878 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010879
10880 todo = (int)d2->dv_hashtab.ht_used;
10881 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10882 {
10883 if (!HASHITEM_EMPTY(hi2))
10884 {
10885 --todo;
10886 di1 = dict_find(d1, hi2->hi_key, -1);
10887 if (d1->dv_scope != 0)
10888 {
10889 /* Disallow replacing a builtin function in l: and g:.
10890 * Check the key to be valid when adding to any
10891 * scope. */
10892 if (d1->dv_scope == VAR_DEF_SCOPE
10893 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10894 && var_check_func_name(hi2->hi_key,
10895 di1 == NULL))
10896 break;
10897 if (!valid_varname(hi2->hi_key))
10898 break;
10899 }
10900 if (di1 == NULL)
10901 {
10902 di1 = dictitem_copy(HI2DI(hi2));
10903 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10904 dictitem_free(di1);
10905 }
10906 else if (*action == 'e')
10907 {
10908 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10909 break;
10910 }
10911 else if (*action == 'f' && HI2DI(hi2) != di1)
10912 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010913 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10914 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010915 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010916 clear_tv(&di1->di_tv);
10917 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10918 }
10919 }
10920 }
10921}
10922
10923/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010924 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010926 */
10927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010928f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010929{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010930 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010931
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010933 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010934 list_T *l1, *l2;
10935 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939 l1 = argvars[0].vval.v_list;
10940 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010941 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010942 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 {
10944 if (argvars[2].v_type != VAR_UNKNOWN)
10945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 before = get_tv_number_chk(&argvars[2], &error);
10947 if (error)
10948 return; /* type error; errmsg already given */
10949
Bram Moolenaar758711c2005-02-02 23:11:38 +000010950 if (before == l1->lv_len)
10951 item = NULL;
10952 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010954 item = list_find(l1, before);
10955 if (item == NULL)
10956 {
10957 EMSGN(_(e_listidx), before);
10958 return;
10959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 }
10961 }
10962 else
10963 item = NULL;
10964 list_extend(l1, l2, item);
10965
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 copy_tv(&argvars[0], rettv);
10967 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10970 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010971 dict_T *d1, *d2;
10972 char_u *action;
10973 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974
10975 d1 = argvars[0].vval.v_dict;
10976 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010977 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010978 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 {
10980 /* Check the third argument. */
10981 if (argvars[2].v_type != VAR_UNKNOWN)
10982 {
10983 static char *(av[]) = {"keep", "force", "error"};
10984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010985 action = get_tv_string_chk(&argvars[2]);
10986 if (action == NULL)
10987 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 for (i = 0; i < 3; ++i)
10989 if (STRCMP(action, av[i]) == 0)
10990 break;
10991 if (i == 3)
10992 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010993 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 return;
10995 }
10996 }
10997 else
10998 action = (char_u *)"force";
10999
Bram Moolenaara9922d62013-05-30 13:01:18 +020011000 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001
Bram Moolenaare9a41262005-01-15 22:18:47 +000011002 copy_tv(&argvars[0], rettv);
11003 }
11004 }
11005 else
11006 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011007}
11008
11009/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011010 * "feedkeys()" function
11011 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011013f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011014{
11015 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011016 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011017 char_u *keys, *flags;
11018 char_u nbuf[NUMBUFLEN];
11019 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011020 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011021 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011022
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011023 /* This is not allowed in the sandbox. If the commands would still be
11024 * executed in the sandbox it would be OK, but it probably happens later,
11025 * when "sandbox" is no longer set. */
11026 if (check_secure())
11027 return;
11028
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011029 keys = get_tv_string(&argvars[0]);
11030 if (*keys != NUL)
11031 {
11032 if (argvars[1].v_type != VAR_UNKNOWN)
11033 {
11034 flags = get_tv_string_buf(&argvars[1], nbuf);
11035 for ( ; *flags != NUL; ++flags)
11036 {
11037 switch (*flags)
11038 {
11039 case 'n': remap = FALSE; break;
11040 case 'm': remap = TRUE; break;
11041 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011042 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011043 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011044 }
11045 }
11046 }
11047
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011048 /* Need to escape K_SPECIAL and CSI before putting the string in the
11049 * typeahead buffer. */
11050 keys_esc = vim_strsave_escape_csi(keys);
11051 if (keys_esc != NULL)
11052 {
11053 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011054 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011055 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011056 if (vgetc_busy)
11057 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011058 if (execute)
11059 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011060 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011061 }
11062}
11063
11064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 * "filereadable()" function
11066 */
11067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011068f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011070 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 char_u *p;
11072 int n;
11073
Bram Moolenaarc236c162008-07-13 17:41:49 +000011074#ifndef O_NONBLOCK
11075# define O_NONBLOCK 0
11076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011078 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11079 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 {
11081 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011082 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 }
11084 else
11085 n = FALSE;
11086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011091 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * rights to write into.
11093 */
11094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011095f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011097 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098}
11099
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011100 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011101findfilendir(
11102 typval_T *argvars UNUSED,
11103 typval_T *rettv,
11104 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011105{
11106#ifdef FEAT_SEARCHPATH
11107 char_u *fname;
11108 char_u *fresult = NULL;
11109 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11110 char_u *p;
11111 char_u pathbuf[NUMBUFLEN];
11112 int count = 1;
11113 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011114 int error = FALSE;
11115#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011116
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011117 rettv->vval.v_string = NULL;
11118 rettv->v_type = VAR_STRING;
11119
11120#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11126 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011127 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 else
11129 {
11130 if (*p != NUL)
11131 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011134 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011136 }
11137
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011138 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11139 error = TRUE;
11140
11141 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 do
11144 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011145 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011146 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 fresult = find_file_in_path_option(first ? fname : NULL,
11148 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011149 0, first, path,
11150 find_what,
11151 curbuf->b_ffname,
11152 find_what == FINDFILE_DIR
11153 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011155
11156 if (fresult != NULL && rettv->v_type == VAR_LIST)
11157 list_append_string(rettv->vval.v_list, fresult, -1);
11158
11159 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011161
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011162 if (rettv->v_type == VAR_STRING)
11163 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011164#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011165}
11166
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011167static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11168static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169
11170/*
11171 * Implementation of map() and filter().
11172 */
11173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011174filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011175{
11176 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011177 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011178 listitem_T *li, *nli;
11179 list_T *l = NULL;
11180 dictitem_T *di;
11181 hashtab_T *ht;
11182 hashitem_T *hi;
11183 dict_T *d = NULL;
11184 typval_T save_val;
11185 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011186 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011187 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011188 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011189 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011190 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011191 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011192 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011193
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011195 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011196 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011197 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 return;
11199 }
11200 else if (argvars[0].v_type == VAR_DICT)
11201 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011202 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011203 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 return;
11205 }
11206 else
11207 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011208 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 return;
11210 }
11211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 expr = get_tv_string_buf_chk(&argvars[1], buf);
11213 /* On type errors, the preceding call has already displayed an error
11214 * message. Avoid a misleading error message for an empty string that
11215 * was not passed as argument. */
11216 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 prepare_vimvar(VV_VAL, &save_val);
11219 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011220
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011221 /* We reset "did_emsg" to be able to detect whether an error
11222 * occurred during evaluation of the expression. */
11223 save_did_emsg = did_emsg;
11224 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011225
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011226 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 vimvars[VV_KEY].vv_type = VAR_STRING;
11230
11231 ht = &d->dv_hashtab;
11232 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011233 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236 if (!HASHITEM_EMPTY(hi))
11237 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011238 int r;
11239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 --todo;
11241 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011242 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011243 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11244 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 break;
11246 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011247 r = filter_map_one(&di->di_tv, expr, map, &rem);
11248 clear_tv(&vimvars[VV_KEY].vv_tv);
11249 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 break;
11251 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011252 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011253 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11254 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011255 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011258 }
11259 }
11260 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 }
11262 else
11263 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011264 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 for (li = l->lv_first; li != NULL; li = nli)
11267 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011268 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011269 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011271 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011272 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011273 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011274 break;
11275 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011277 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011280
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011281 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011283
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011284 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011286
11287 copy_tv(&argvars[0], rettv);
11288}
11289
11290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011291filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011292{
Bram Moolenaar33570922005-01-25 22:26:29 +000011293 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011294 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011295 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011296
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011298 s = expr;
11299 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011300 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011301 if (*s != NUL) /* check for trailing chars after expr */
11302 {
11303 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011304 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011305 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011306 }
11307 if (map)
11308 {
11309 /* map(): replace the list item value */
11310 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011311 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011312 *tv = rettv;
11313 }
11314 else
11315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 int error = FALSE;
11317
Bram Moolenaare9a41262005-01-15 22:18:47 +000011318 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011319 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011320 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 /* On type error, nothing has been removed; return FAIL to stop the
11322 * loop. The error message was given by get_tv_number_chk(). */
11323 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011324 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011325 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011326 retval = OK;
11327theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011329 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330}
11331
11332/*
11333 * "filter()" function
11334 */
11335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337{
11338 filter_map(argvars, rettv, FALSE);
11339}
11340
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011341/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011342 * "finddir({fname}[, {path}[, {count}]])" function
11343 */
11344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011345f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011346{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011347 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011348}
11349
11350/*
11351 * "findfile({fname}[, {path}[, {count}]])" function
11352 */
11353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011354f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011355{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011356 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011357}
11358
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011359#ifdef FEAT_FLOAT
11360/*
11361 * "float2nr({float})" function
11362 */
11363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011364f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011365{
11366 float_T f;
11367
11368 if (get_float_arg(argvars, &f) == OK)
11369 {
11370 if (f < -0x7fffffff)
11371 rettv->vval.v_number = -0x7fffffff;
11372 else if (f > 0x7fffffff)
11373 rettv->vval.v_number = 0x7fffffff;
11374 else
11375 rettv->vval.v_number = (varnumber_T)f;
11376 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011377}
11378
11379/*
11380 * "floor({float})" function
11381 */
11382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011383f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011384{
11385 float_T f;
11386
11387 rettv->v_type = VAR_FLOAT;
11388 if (get_float_arg(argvars, &f) == OK)
11389 rettv->vval.v_float = floor(f);
11390 else
11391 rettv->vval.v_float = 0.0;
11392}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011393
11394/*
11395 * "fmod()" function
11396 */
11397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011398f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011399{
11400 float_T fx, fy;
11401
11402 rettv->v_type = VAR_FLOAT;
11403 if (get_float_arg(argvars, &fx) == OK
11404 && get_float_arg(&argvars[1], &fy) == OK)
11405 rettv->vval.v_float = fmod(fx, fy);
11406 else
11407 rettv->vval.v_float = 0.0;
11408}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011409#endif
11410
Bram Moolenaar0d660222005-01-07 21:51:51 +000011411/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011412 * "fnameescape({string})" function
11413 */
11414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011415f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011416{
11417 rettv->vval.v_string = vim_strsave_fnameescape(
11418 get_tv_string(&argvars[0]), FALSE);
11419 rettv->v_type = VAR_STRING;
11420}
11421
11422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * "fnamemodify({fname}, {mods})" function
11424 */
11425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
11428 char_u *fname;
11429 char_u *mods;
11430 int usedlen = 0;
11431 int len;
11432 char_u *fbuf = NULL;
11433 char_u buf[NUMBUFLEN];
11434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 fname = get_tv_string_chk(&argvars[0]);
11436 mods = get_tv_string_buf_chk(&argvars[1], buf);
11437 if (fname == NULL || mods == NULL)
11438 fname = NULL;
11439 else
11440 {
11441 len = (int)STRLEN(fname);
11442 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 vim_free(fbuf);
11451}
11452
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011453static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454
11455/*
11456 * "foldclosed()" function
11457 */
11458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011459foldclosed_both(
11460 typval_T *argvars UNUSED,
11461 typval_T *rettv,
11462 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
11464#ifdef FEAT_FOLDING
11465 linenr_T lnum;
11466 linenr_T first, last;
11467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11470 {
11471 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11472 {
11473 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 return;
11478 }
11479 }
11480#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482}
11483
11484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011485 * "foldclosed()" function
11486 */
11487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011488f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489{
11490 foldclosed_both(argvars, rettv, FALSE);
11491}
11492
11493/*
11494 * "foldclosedend()" function
11495 */
11496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011497f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498{
11499 foldclosed_both(argvars, rettv, TRUE);
11500}
11501
11502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 * "foldlevel()" function
11504 */
11505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011506f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507{
11508#ifdef FEAT_FOLDING
11509 linenr_T lnum;
11510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
11517/*
11518 * "foldtext()" function
11519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522{
11523#ifdef FEAT_FOLDING
11524 linenr_T lnum;
11525 char_u *s;
11526 char_u *r;
11527 int len;
11528 char *txt;
11529#endif
11530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->v_type = VAR_STRING;
11532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011534 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11535 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11536 <= curbuf->b_ml.ml_line_count
11537 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 {
11539 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011540 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11541 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 {
11543 if (!linewhite(lnum))
11544 break;
11545 ++lnum;
11546 }
11547
11548 /* Find interesting text in this line. */
11549 s = skipwhite(ml_get(lnum));
11550 /* skip C comment-start */
11551 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011554 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011555 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011556 {
11557 s = skipwhite(ml_get(lnum + 1));
11558 if (*s == '*')
11559 s = skipwhite(s + 1);
11560 }
11561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 txt = _("+-%s%3ld lines: ");
11563 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011564 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 + 20 /* for %3ld */
11566 + STRLEN(s))); /* concatenated */
11567 if (r != NULL)
11568 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011569 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11570 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11571 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 len = (int)STRLEN(r);
11573 STRCAT(r, s);
11574 /* remove 'foldmarker' and 'commentstring' */
11575 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578 }
11579#endif
11580}
11581
11582/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011583 * "foldtextresult(lnum)" function
11584 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011586f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011587{
11588#ifdef FEAT_FOLDING
11589 linenr_T lnum;
11590 char_u *text;
11591 char_u buf[51];
11592 foldinfo_T foldinfo;
11593 int fold_count;
11594#endif
11595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->v_type = VAR_STRING;
11597 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011598#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011600 /* treat illegal types and illegal string values for {lnum} the same */
11601 if (lnum < 0)
11602 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011603 fold_count = foldedCount(curwin, lnum, &foldinfo);
11604 if (fold_count > 0)
11605 {
11606 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11607 &foldinfo, buf);
11608 if (text == buf)
11609 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011611 }
11612#endif
11613}
11614
11615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 * "foreground()" function
11617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011619f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621#ifdef FEAT_GUI
11622 if (gui.in_use)
11623 gui_mch_set_foreground();
11624#else
11625# ifdef WIN32
11626 win32_set_foreground();
11627# endif
11628#endif
11629}
11630
11631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011632 * "function()" function
11633 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011635f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011636{
11637 char_u *s;
11638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011641 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011642 /* Don't check an autoload name for existence here. */
11643 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011644 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011645 else
11646 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011647 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011648 {
11649 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011650 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011651
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011652 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11653 * also be called from another script. Using trans_function_name()
11654 * would also work, but some plugins depend on the name being
11655 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011656 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011657 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011658 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011659 if (rettv->vval.v_string != NULL)
11660 {
11661 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011662 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011663 }
11664 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011665 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011666 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668 }
11669}
11670
11671/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011672 * "garbagecollect()" function
11673 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011675f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011676{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011677 /* This is postponed until we are back at the toplevel, because we may be
11678 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11679 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011680
11681 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11682 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011683}
11684
11685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686 * "get()" function
11687 */
11688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011689f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690{
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 listitem_T *li;
11692 list_T *l;
11693 dictitem_T *di;
11694 dict_T *d;
11695 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 int error = FALSE;
11702
11703 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11704 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 else if (argvars[0].v_type == VAR_DICT)
11709 {
11710 if ((d = argvars[0].vval.v_dict) != NULL)
11711 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011712 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 if (di != NULL)
11714 tv = &di->di_tv;
11715 }
11716 }
11717 else
11718 EMSG2(_(e_listdictarg), "get()");
11719
11720 if (tv == NULL)
11721 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011722 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 copy_tv(&argvars[2], rettv);
11724 }
11725 else
11726 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727}
11728
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011729static 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 +000011730
11731/*
11732 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011733 * Return a range (from start to end) of lines in rettv from the specified
11734 * buffer.
11735 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011736 */
11737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011738get_buffer_lines(
11739 buf_T *buf,
11740 linenr_T start,
11741 linenr_T end,
11742 int retlist,
11743 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011744{
11745 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011746
Bram Moolenaar959a1432013-12-14 12:17:38 +010011747 rettv->v_type = VAR_STRING;
11748 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011749 if (retlist && rettv_list_alloc(rettv) == FAIL)
11750 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011751
11752 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11753 return;
11754
11755 if (!retlist)
11756 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011757 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11758 p = ml_get_buf(buf, start, FALSE);
11759 else
11760 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011761 rettv->vval.v_string = vim_strsave(p);
11762 }
11763 else
11764 {
11765 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011766 return;
11767
11768 if (start < 1)
11769 start = 1;
11770 if (end > buf->b_ml.ml_line_count)
11771 end = buf->b_ml.ml_line_count;
11772 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011773 if (list_append_string(rettv->vval.v_list,
11774 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011776 }
11777}
11778
11779/*
11780 * "getbufline()" function
11781 */
11782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011783f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011784{
11785 linenr_T lnum;
11786 linenr_T end;
11787 buf_T *buf;
11788
11789 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11790 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011791 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011792 --emsg_off;
11793
Bram Moolenaar661b1822005-07-28 22:36:45 +000011794 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011795 if (argvars[2].v_type == VAR_UNKNOWN)
11796 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011797 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011798 end = get_tv_lnum_buf(&argvars[2], buf);
11799
Bram Moolenaar342337a2005-07-21 21:11:17 +000011800 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011801}
11802
Bram Moolenaar0d660222005-01-07 21:51:51 +000011803/*
11804 * "getbufvar()" function
11805 */
11806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011807f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808{
11809 buf_T *buf;
11810 buf_T *save_curbuf;
11811 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011812 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011813 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11816 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011817 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011818 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011820 rettv->v_type = VAR_STRING;
11821 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011822
11823 if (buf != NULL && varname != NULL)
11824 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011825 /* set curbuf to be our buf, temporarily */
11826 save_curbuf = curbuf;
11827 curbuf = buf;
11828
Bram Moolenaar0d660222005-01-07 21:51:51 +000011829 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011830 {
11831 if (get_option_tv(&varname, rettv, TRUE) == OK)
11832 done = TRUE;
11833 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011834 else if (STRCMP(varname, "changedtick") == 0)
11835 {
11836 rettv->v_type = VAR_NUMBER;
11837 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011838 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011839 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011840 else
11841 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011842 /* Look up the variable. */
11843 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11844 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11845 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011846 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011847 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011849 done = TRUE;
11850 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011852
11853 /* restore previous notion of curbuf */
11854 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011855 }
11856
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11858 /* use the default value */
11859 copy_tv(&argvars[2], rettv);
11860
Bram Moolenaar0d660222005-01-07 21:51:51 +000011861 --emsg_off;
11862}
11863
11864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 * "getchar()" function
11866 */
11867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011868f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
11870 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011873 /* Position the cursor. Needed after a message that ends in a space. */
11874 windgoto(msg_row, msg_col);
11875
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 ++no_mapping;
11877 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011878 for (;;)
11879 {
11880 if (argvars[0].v_type == VAR_UNKNOWN)
11881 /* getchar(): blocking wait. */
11882 n = safe_vgetc();
11883 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11884 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011885 n = vpeekc_any();
11886 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011887 /* illegal argument or getchar(0) and no char avail: return zero */
11888 n = 0;
11889 else
11890 /* getchar(0) and char avail: return char */
11891 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011892
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011893 if (n == K_IGNORE)
11894 continue;
11895 break;
11896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 --no_mapping;
11898 --allow_keys;
11899
Bram Moolenaar219b8702006-11-01 14:32:36 +000011900 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11901 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11902 vimvars[VV_MOUSE_COL].vv_nr = 0;
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 if (IS_SPECIAL(n) || mod_mask != 0)
11906 {
11907 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11908 int i = 0;
11909
11910 /* Turn a special key into three bytes, plus modifier. */
11911 if (mod_mask != 0)
11912 {
11913 temp[i++] = K_SPECIAL;
11914 temp[i++] = KS_MODIFIER;
11915 temp[i++] = mod_mask;
11916 }
11917 if (IS_SPECIAL(n))
11918 {
11919 temp[i++] = K_SPECIAL;
11920 temp[i++] = K_SECOND(n);
11921 temp[i++] = K_THIRD(n);
11922 }
11923#ifdef FEAT_MBYTE
11924 else if (has_mbyte)
11925 i += (*mb_char2bytes)(n, temp + i);
11926#endif
11927 else
11928 temp[i++] = n;
11929 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->v_type = VAR_STRING;
11931 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011932
11933#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011934 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011935 {
11936 int row = mouse_row;
11937 int col = mouse_col;
11938 win_T *win;
11939 linenr_T lnum;
11940# ifdef FEAT_WINDOWS
11941 win_T *wp;
11942# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011943 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011944
11945 if (row >= 0 && col >= 0)
11946 {
11947 /* Find the window at the mouse coordinates and compute the
11948 * text position. */
11949 win = mouse_find_win(&row, &col);
11950 (void)mouse_comp_pos(win, &row, &col, &lnum);
11951# ifdef FEAT_WINDOWS
11952 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011953 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011954# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011955 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011956 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11957 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11958 }
11959 }
11960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 }
11962}
11963
11964/*
11965 * "getcharmod()" function
11966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011968f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971}
11972
11973/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011974 * "getcharsearch()" function
11975 */
11976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011977f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011978{
11979 if (rettv_dict_alloc(rettv) != FAIL)
11980 {
11981 dict_T *dict = rettv->vval.v_dict;
11982
11983 dict_add_nr_str(dict, "char", 0L, last_csearch());
11984 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11985 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11986 }
11987}
11988
11989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 * "getcmdline()" function
11991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011993f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->v_type = VAR_STRING;
11996 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997}
11998
11999/*
12000 * "getcmdpos()" function
12001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012003f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006}
12007
12008/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012009 * "getcmdtype()" function
12010 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012012f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012013{
12014 rettv->v_type = VAR_STRING;
12015 rettv->vval.v_string = alloc(2);
12016 if (rettv->vval.v_string != NULL)
12017 {
12018 rettv->vval.v_string[0] = get_cmdline_type();
12019 rettv->vval.v_string[1] = NUL;
12020 }
12021}
12022
12023/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012024 * "getcmdwintype()" function
12025 */
12026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012027f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012028{
12029 rettv->v_type = VAR_STRING;
12030 rettv->vval.v_string = NULL;
12031#ifdef FEAT_CMDWIN
12032 rettv->vval.v_string = alloc(2);
12033 if (rettv->vval.v_string != NULL)
12034 {
12035 rettv->vval.v_string[0] = cmdwin_type;
12036 rettv->vval.v_string[1] = NUL;
12037 }
12038#endif
12039}
12040
12041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 * "getcwd()" function
12043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012045f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012047 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012048 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012051 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012052
12053 wp = find_tabwin(&argvars[0], &argvars[1]);
12054 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012056 if (wp->w_localdir != NULL)
12057 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12058 else if(globaldir != NULL)
12059 rettv->vval.v_string = vim_strsave(globaldir);
12060 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012061 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012062 cwd = alloc(MAXPATHL);
12063 if (cwd != NULL)
12064 {
12065 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12066 rettv->vval.v_string = vim_strsave(cwd);
12067 vim_free(cwd);
12068 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012069 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012070#ifdef BACKSLASH_IN_FILENAME
12071 if (rettv->vval.v_string != NULL)
12072 slash_adjust(rettv->vval.v_string);
12073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 }
12075}
12076
12077/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012078 * "getfontname()" function
12079 */
12080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012081f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->v_type = VAR_STRING;
12084 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012085#ifdef FEAT_GUI
12086 if (gui.in_use)
12087 {
12088 GuiFont font;
12089 char_u *name = NULL;
12090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012091 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012092 {
12093 /* Get the "Normal" font. Either the name saved by
12094 * hl_set_font_name() or from the font ID. */
12095 font = gui.norm_font;
12096 name = hl_get_font_name();
12097 }
12098 else
12099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012101 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12102 return;
12103 font = gui_mch_get_font(name, FALSE);
12104 if (font == NOFONT)
12105 return; /* Invalid font name, return empty string. */
12106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012109 gui_mch_free_font(font);
12110 }
12111#endif
12112}
12113
12114/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012115 * "getfperm({fname})" function
12116 */
12117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012118f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012119{
12120 char_u *fname;
12121 struct stat st;
12122 char_u *perm = NULL;
12123 char_u flags[] = "rwx";
12124 int i;
12125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012129 if (mch_stat((char *)fname, &st) >= 0)
12130 {
12131 perm = vim_strsave((char_u *)"---------");
12132 if (perm != NULL)
12133 {
12134 for (i = 0; i < 9; i++)
12135 {
12136 if (st.st_mode & (1 << (8 - i)))
12137 perm[i] = flags[i % 3];
12138 }
12139 }
12140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012142}
12143
12144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 * "getfsize({fname})" function
12146 */
12147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012148f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149{
12150 char_u *fname;
12151 struct stat st;
12152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156
12157 if (mch_stat((char *)fname, &st) >= 0)
12158 {
12159 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012164
12165 /* non-perfect check for overflow */
12166 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12167 rettv->vval.v_number = -2;
12168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 }
12170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172}
12173
12174/*
12175 * "getftime({fname})" function
12176 */
12177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012178f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
12180 char_u *fname;
12181 struct stat st;
12182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184
12185 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012192 * "getftype({fname})" function
12193 */
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012196{
12197 char_u *fname;
12198 struct stat st;
12199 char_u *type = NULL;
12200 char *t;
12201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012205 if (mch_lstat((char *)fname, &st) >= 0)
12206 {
12207#ifdef S_ISREG
12208 if (S_ISREG(st.st_mode))
12209 t = "file";
12210 else if (S_ISDIR(st.st_mode))
12211 t = "dir";
12212# ifdef S_ISLNK
12213 else if (S_ISLNK(st.st_mode))
12214 t = "link";
12215# endif
12216# ifdef S_ISBLK
12217 else if (S_ISBLK(st.st_mode))
12218 t = "bdev";
12219# endif
12220# ifdef S_ISCHR
12221 else if (S_ISCHR(st.st_mode))
12222 t = "cdev";
12223# endif
12224# ifdef S_ISFIFO
12225 else if (S_ISFIFO(st.st_mode))
12226 t = "fifo";
12227# endif
12228# ifdef S_ISSOCK
12229 else if (S_ISSOCK(st.st_mode))
12230 t = "fifo";
12231# endif
12232 else
12233 t = "other";
12234#else
12235# ifdef S_IFMT
12236 switch (st.st_mode & S_IFMT)
12237 {
12238 case S_IFREG: t = "file"; break;
12239 case S_IFDIR: t = "dir"; break;
12240# ifdef S_IFLNK
12241 case S_IFLNK: t = "link"; break;
12242# endif
12243# ifdef S_IFBLK
12244 case S_IFBLK: t = "bdev"; break;
12245# endif
12246# ifdef S_IFCHR
12247 case S_IFCHR: t = "cdev"; break;
12248# endif
12249# ifdef S_IFIFO
12250 case S_IFIFO: t = "fifo"; break;
12251# endif
12252# ifdef S_IFSOCK
12253 case S_IFSOCK: t = "socket"; break;
12254# endif
12255 default: t = "other";
12256 }
12257# else
12258 if (mch_isdir(fname))
12259 t = "dir";
12260 else
12261 t = "file";
12262# endif
12263#endif
12264 type = vim_strsave((char_u *)t);
12265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012267}
12268
12269/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012270 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012271 */
12272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012273f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274{
12275 linenr_T lnum;
12276 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012277 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278
12279 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012280 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012281 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012282 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012283 retlist = FALSE;
12284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012286 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012288 retlist = TRUE;
12289 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012290
Bram Moolenaar342337a2005-07-21 21:11:17 +000012291 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012292}
12293
12294/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012295 * "getmatches()" function
12296 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012298f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012299{
12300#ifdef FEAT_SEARCH_EXTRA
12301 dict_T *dict;
12302 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012303 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012304
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012305 if (rettv_list_alloc(rettv) == OK)
12306 {
12307 while (cur != NULL)
12308 {
12309 dict = dict_alloc();
12310 if (dict == NULL)
12311 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012312 if (cur->match.regprog == NULL)
12313 {
12314 /* match added with matchaddpos() */
12315 for (i = 0; i < MAXPOSMATCH; ++i)
12316 {
12317 llpos_T *llpos;
12318 char buf[6];
12319 list_T *l;
12320
12321 llpos = &cur->pos.pos[i];
12322 if (llpos->lnum == 0)
12323 break;
12324 l = list_alloc();
12325 if (l == NULL)
12326 break;
12327 list_append_number(l, (varnumber_T)llpos->lnum);
12328 if (llpos->col > 0)
12329 {
12330 list_append_number(l, (varnumber_T)llpos->col);
12331 list_append_number(l, (varnumber_T)llpos->len);
12332 }
12333 sprintf(buf, "pos%d", i + 1);
12334 dict_add_list(dict, buf, l);
12335 }
12336 }
12337 else
12338 {
12339 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12340 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012341 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012342 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12343 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012344# ifdef FEAT_CONCEAL
12345 if (cur->conceal_char)
12346 {
12347 char_u buf[MB_MAXBYTES + 1];
12348
12349 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12350 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12351 }
12352# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012353 list_append_dict(rettv->vval.v_list, dict);
12354 cur = cur->next;
12355 }
12356 }
12357#endif
12358}
12359
12360/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012361 * "getpid()" function
12362 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012365{
12366 rettv->vval.v_number = mch_get_pid();
12367}
12368
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012369static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012370
12371/*
12372 * "getcurpos()" function
12373 */
12374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012375f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012376{
12377 getpos_both(argvars, rettv, TRUE);
12378}
12379
Bram Moolenaar18081e32008-02-20 19:11:07 +000012380/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012381 * "getpos(string)" function
12382 */
12383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012384f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012385{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012386 getpos_both(argvars, rettv, FALSE);
12387}
12388
12389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012390getpos_both(
12391 typval_T *argvars,
12392 typval_T *rettv,
12393 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012394{
Bram Moolenaara5525202006-03-02 22:52:09 +000012395 pos_T *fp;
12396 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012397 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012398
12399 if (rettv_list_alloc(rettv) == OK)
12400 {
12401 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012402 if (getcurpos)
12403 fp = &curwin->w_cursor;
12404 else
12405 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012406 if (fnum != -1)
12407 list_append_number(l, (varnumber_T)fnum);
12408 else
12409 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012410 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12411 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012412 list_append_number(l, (fp != NULL)
12413 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012414 : (varnumber_T)0);
12415 list_append_number(l,
12416#ifdef FEAT_VIRTUALEDIT
12417 (fp != NULL) ? (varnumber_T)fp->coladd :
12418#endif
12419 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012420 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012421 list_append_number(l, curwin->w_curswant == MAXCOL ?
12422 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012423 }
12424 else
12425 rettv->vval.v_number = FALSE;
12426}
12427
12428/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012429 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012430 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012433{
12434#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012435 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012436#endif
12437
Bram Moolenaar2641f772005-03-25 21:58:17 +000012438#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012439 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012440 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012441 wp = NULL;
12442 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12443 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012444 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012445 if (wp == NULL)
12446 return;
12447 }
12448
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012449 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012450 }
12451#endif
12452}
12453
12454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 * "getreg()" function
12456 */
12457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012458f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459{
12460 char_u *strregname;
12461 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012462 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012463 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012466 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 strregname = get_tv_string_chk(&argvars[0]);
12469 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012470 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012473 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12474 return_list = get_tv_number_chk(&argvars[2], &error);
12475 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012478 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012479
12480 if (error)
12481 return;
12482
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 regname = (strregname == NULL ? '"' : *strregname);
12484 if (regname == 0)
12485 regname = '"';
12486
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012487 if (return_list)
12488 {
12489 rettv->v_type = VAR_LIST;
12490 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12491 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012492 if (rettv->vval.v_list != NULL)
12493 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012494 }
12495 else
12496 {
12497 rettv->v_type = VAR_STRING;
12498 rettv->vval.v_string = get_reg_contents(regname,
12499 arg2 ? GREG_EXPR_SRC : 0);
12500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501}
12502
12503/*
12504 * "getregtype()" function
12505 */
12506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012507f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508{
12509 char_u *strregname;
12510 int regname;
12511 char_u buf[NUMBUFLEN + 2];
12512 long reglen = 0;
12513
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012514 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012515 {
12516 strregname = get_tv_string_chk(&argvars[0]);
12517 if (strregname == NULL) /* type error; errmsg already given */
12518 {
12519 rettv->v_type = VAR_STRING;
12520 rettv->vval.v_string = NULL;
12521 return;
12522 }
12523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 else
12525 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012526 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527
12528 regname = (strregname == NULL ? '"' : *strregname);
12529 if (regname == 0)
12530 regname = '"';
12531
12532 buf[0] = NUL;
12533 buf[1] = NUL;
12534 switch (get_reg_type(regname, &reglen))
12535 {
12536 case MLINE: buf[0] = 'V'; break;
12537 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 case MBLOCK:
12539 buf[0] = Ctrl_V;
12540 sprintf((char *)buf + 1, "%ld", reglen + 1);
12541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 rettv->v_type = VAR_STRING;
12544 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545}
12546
12547/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012548 * "gettabvar()" function
12549 */
12550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012552{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012553 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012554 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012555 dictitem_T *v;
12556 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012558
12559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = NULL;
12561
12562 varname = get_tv_string_chk(&argvars[1]);
12563 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12564 if (tp != NULL && varname != NULL)
12565 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012566 /* Set tp to be our tabpage, temporarily. Also set the window to the
12567 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012568 if (switch_win(&oldcurwin, &oldtabpage,
12569 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012570 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012571 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012572 /* look up the variable */
12573 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12574 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12575 if (v != NULL)
12576 {
12577 copy_tv(&v->di_tv, rettv);
12578 done = TRUE;
12579 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012580 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012581
12582 /* restore previous notion of curwin */
12583 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012584 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012585
12586 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012587 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012588}
12589
12590/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012591 * "gettabwinvar()" function
12592 */
12593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012594f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012595{
12596 getwinvar(argvars, rettv, 1);
12597}
12598
12599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 * "getwinposx()" function
12601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012603f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606#ifdef FEAT_GUI
12607 if (gui.in_use)
12608 {
12609 int x, y;
12610
12611 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 }
12614#endif
12615}
12616
12617/*
12618 * "getwinposy()" function
12619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012621f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624#ifdef FEAT_GUI
12625 if (gui.in_use)
12626 {
12627 int x, y;
12628
12629 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 }
12632#endif
12633}
12634
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012635/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012636 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012637 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012638 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012639find_win_by_nr(
12640 typval_T *vp,
12641 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012642{
12643#ifdef FEAT_WINDOWS
12644 win_T *wp;
12645#endif
12646 int nr;
12647
12648 nr = get_tv_number_chk(vp, NULL);
12649
12650#ifdef FEAT_WINDOWS
12651 if (nr < 0)
12652 return NULL;
12653 if (nr == 0)
12654 return curwin;
12655
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012656 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12657 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012658 if (--nr <= 0)
12659 break;
12660 return wp;
12661#else
12662 if (nr == 0 || nr == 1)
12663 return curwin;
12664 return NULL;
12665#endif
12666}
12667
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012669 * Find window specified by "wvp" in tabpage "tvp".
12670 */
12671 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012672find_tabwin(
12673 typval_T *wvp, /* VAR_UNKNOWN for current window */
12674 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012675{
12676 win_T *wp = NULL;
12677 tabpage_T *tp = NULL;
12678 long n;
12679
12680 if (wvp->v_type != VAR_UNKNOWN)
12681 {
12682 if (tvp->v_type != VAR_UNKNOWN)
12683 {
12684 n = get_tv_number(tvp);
12685 if (n >= 0)
12686 tp = find_tabpage(n);
12687 }
12688 else
12689 tp = curtab;
12690
12691 if (tp != NULL)
12692 wp = find_win_by_nr(wvp, tp);
12693 }
12694 else
12695 wp = curwin;
12696
12697 return wp;
12698}
12699
12700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 * "getwinvar()" function
12702 */
12703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012704f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012706 getwinvar(argvars, rettv, 0);
12707}
12708
12709/*
12710 * getwinvar() and gettabwinvar()
12711 */
12712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012713getwinvar(
12714 typval_T *argvars,
12715 typval_T *rettv,
12716 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012717{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012718 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012720 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012721 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012722 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012723#ifdef FEAT_WINDOWS
12724 win_T *oldcurwin;
12725 tabpage_T *oldtabpage;
12726 int need_switch_win;
12727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012729#ifdef FEAT_WINDOWS
12730 if (off == 1)
12731 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12732 else
12733 tp = curtab;
12734#endif
12735 win = find_win_by_nr(&argvars[off], tp);
12736 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012739 rettv->v_type = VAR_STRING;
12740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
12742 if (win != NULL && varname != NULL)
12743 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012744#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012745 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012746 * otherwise the window is not valid. Only do this when needed,
12747 * autocommands get blocked. */
12748 need_switch_win = !(tp == curtab && win == curwin);
12749 if (!need_switch_win
12750 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12751#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012752 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012753 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012754 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012755 if (get_option_tv(&varname, rettv, 1) == OK)
12756 done = TRUE;
12757 }
12758 else
12759 {
12760 /* Look up the variable. */
12761 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12762 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12763 varname, FALSE);
12764 if (v != NULL)
12765 {
12766 copy_tv(&v->di_tv, rettv);
12767 done = TRUE;
12768 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012771
Bram Moolenaarba117c22015-09-29 16:53:22 +020012772#ifdef FEAT_WINDOWS
12773 if (need_switch_win)
12774 /* restore previous notion of curwin */
12775 restore_win(oldcurwin, oldtabpage, TRUE);
12776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 }
12778
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012779 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12780 /* use the default return value */
12781 copy_tv(&argvars[off + 2], rettv);
12782
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 --emsg_off;
12784}
12785
12786/*
12787 * "glob()" function
12788 */
12789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012792 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012794 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012796 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012797 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012799 if (argvars[1].v_type != VAR_UNKNOWN)
12800 {
12801 if (get_tv_number_chk(&argvars[1], &error))
12802 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012804 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012805 if (get_tv_number_chk(&argvars[2], &error))
12806 {
12807 rettv->v_type = VAR_LIST;
12808 rettv->vval.v_list = NULL;
12809 }
12810 if (argvars[3].v_type != VAR_UNKNOWN
12811 && get_tv_number_chk(&argvars[3], &error))
12812 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012813 }
12814 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012815 if (!error)
12816 {
12817 ExpandInit(&xpc);
12818 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012819 if (p_wic)
12820 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012821 if (rettv->v_type == VAR_STRING)
12822 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012823 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012824 else if (rettv_list_alloc(rettv) != FAIL)
12825 {
12826 int i;
12827
12828 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12829 NULL, options, WILD_ALL_KEEP);
12830 for (i = 0; i < xpc.xp_numfiles; i++)
12831 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12832
12833 ExpandCleanup(&xpc);
12834 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012835 }
12836 else
12837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838}
12839
12840/*
12841 * "globpath()" function
12842 */
12843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012846 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012848 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012849 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012850 garray_T ga;
12851 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012853 /* When the optional second argument is non-zero, don't remove matches
12854 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012856 if (argvars[2].v_type != VAR_UNKNOWN)
12857 {
12858 if (get_tv_number_chk(&argvars[2], &error))
12859 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012860 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012861 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012862 if (get_tv_number_chk(&argvars[3], &error))
12863 {
12864 rettv->v_type = VAR_LIST;
12865 rettv->vval.v_list = NULL;
12866 }
12867 if (argvars[4].v_type != VAR_UNKNOWN
12868 && get_tv_number_chk(&argvars[4], &error))
12869 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012870 }
12871 }
12872 if (file != NULL && !error)
12873 {
12874 ga_init2(&ga, (int)sizeof(char_u *), 10);
12875 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12876 if (rettv->v_type == VAR_STRING)
12877 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12878 else if (rettv_list_alloc(rettv) != FAIL)
12879 for (i = 0; i < ga.ga_len; ++i)
12880 list_append_string(rettv->vval.v_list,
12881 ((char_u **)(ga.ga_data))[i], -1);
12882 ga_clear_strings(&ga);
12883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012885 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012889 * "glob2regpat()" function
12890 */
12891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012893{
12894 char_u *pat = get_tv_string_chk(&argvars[0]);
12895
12896 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012897 rettv->vval.v_string = (pat == NULL)
12898 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012899}
12900
12901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 * "has()" function
12903 */
12904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012905f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
12907 int i;
12908 char_u *name;
12909 int n = FALSE;
12910 static char *(has_list[]) =
12911 {
12912#ifdef AMIGA
12913 "amiga",
12914# ifdef FEAT_ARP
12915 "arp",
12916# endif
12917#endif
12918#ifdef __BEOS__
12919 "beos",
12920#endif
12921#ifdef MSDOS
12922# ifdef DJGPP
12923 "dos32",
12924# else
12925 "dos16",
12926# endif
12927#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012928#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 "mac",
12930#endif
12931#if defined(MACOS_X_UNIX)
12932 "macunix",
12933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934#ifdef __QNX__
12935 "qnx",
12936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937#ifdef UNIX
12938 "unix",
12939#endif
12940#ifdef VMS
12941 "vms",
12942#endif
12943#ifdef WIN16
12944 "win16",
12945#endif
12946#ifdef WIN32
12947 "win32",
12948#endif
12949#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12950 "win32unix",
12951#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012952#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 "win64",
12954#endif
12955#ifdef EBCDIC
12956 "ebcdic",
12957#endif
12958#ifndef CASE_INSENSITIVE_FILENAME
12959 "fname_case",
12960#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012961#ifdef HAVE_ACL
12962 "acl",
12963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964#ifdef FEAT_ARABIC
12965 "arabic",
12966#endif
12967#ifdef FEAT_AUTOCMD
12968 "autocmd",
12969#endif
12970#ifdef FEAT_BEVAL
12971 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012972# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12973 "balloon_multiline",
12974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975#endif
12976#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12977 "builtin_terms",
12978# ifdef ALL_BUILTIN_TCAPS
12979 "all_builtin_terms",
12980# endif
12981#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012982#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12983 || defined(FEAT_GUI_W32) \
12984 || defined(FEAT_GUI_MOTIF))
12985 "browsefilter",
12986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#ifdef FEAT_BYTEOFF
12988 "byte_offset",
12989#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012990#ifdef FEAT_CHANNEL
12991 "channel",
12992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993#ifdef FEAT_CINDENT
12994 "cindent",
12995#endif
12996#ifdef FEAT_CLIENTSERVER
12997 "clientserver",
12998#endif
12999#ifdef FEAT_CLIPBOARD
13000 "clipboard",
13001#endif
13002#ifdef FEAT_CMDL_COMPL
13003 "cmdline_compl",
13004#endif
13005#ifdef FEAT_CMDHIST
13006 "cmdline_hist",
13007#endif
13008#ifdef FEAT_COMMENTS
13009 "comments",
13010#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013011#ifdef FEAT_CONCEAL
13012 "conceal",
13013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014#ifdef FEAT_CRYPT
13015 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013016 "crypt-blowfish",
13017 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018#endif
13019#ifdef FEAT_CSCOPE
13020 "cscope",
13021#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013022#ifdef FEAT_CURSORBIND
13023 "cursorbind",
13024#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013025#ifdef CURSOR_SHAPE
13026 "cursorshape",
13027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028#ifdef DEBUG
13029 "debug",
13030#endif
13031#ifdef FEAT_CON_DIALOG
13032 "dialog_con",
13033#endif
13034#ifdef FEAT_GUI_DIALOG
13035 "dialog_gui",
13036#endif
13037#ifdef FEAT_DIFF
13038 "diff",
13039#endif
13040#ifdef FEAT_DIGRAPHS
13041 "digraphs",
13042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013043#ifdef FEAT_DIRECTX
13044 "directx",
13045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046#ifdef FEAT_DND
13047 "dnd",
13048#endif
13049#ifdef FEAT_EMACS_TAGS
13050 "emacs_tags",
13051#endif
13052 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013053 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054#ifdef FEAT_SEARCH_EXTRA
13055 "extra_search",
13056#endif
13057#ifdef FEAT_FKMAP
13058 "farsi",
13059#endif
13060#ifdef FEAT_SEARCHPATH
13061 "file_in_path",
13062#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013063#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013064 "filterpipe",
13065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066#ifdef FEAT_FIND_ID
13067 "find_in_path",
13068#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013069#ifdef FEAT_FLOAT
13070 "float",
13071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072#ifdef FEAT_FOLDING
13073 "folding",
13074#endif
13075#ifdef FEAT_FOOTER
13076 "footer",
13077#endif
13078#if !defined(USE_SYSTEM) && defined(UNIX)
13079 "fork",
13080#endif
13081#ifdef FEAT_GETTEXT
13082 "gettext",
13083#endif
13084#ifdef FEAT_GUI
13085 "gui",
13086#endif
13087#ifdef FEAT_GUI_ATHENA
13088# ifdef FEAT_GUI_NEXTAW
13089 "gui_neXtaw",
13090# else
13091 "gui_athena",
13092# endif
13093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094#ifdef FEAT_GUI_GTK
13095 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013098#ifdef FEAT_GUI_GNOME
13099 "gui_gnome",
13100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101#ifdef FEAT_GUI_MAC
13102 "gui_mac",
13103#endif
13104#ifdef FEAT_GUI_MOTIF
13105 "gui_motif",
13106#endif
13107#ifdef FEAT_GUI_PHOTON
13108 "gui_photon",
13109#endif
13110#ifdef FEAT_GUI_W16
13111 "gui_win16",
13112#endif
13113#ifdef FEAT_GUI_W32
13114 "gui_win32",
13115#endif
13116#ifdef FEAT_HANGULIN
13117 "hangul_input",
13118#endif
13119#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13120 "iconv",
13121#endif
13122#ifdef FEAT_INS_EXPAND
13123 "insert_expand",
13124#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013125#ifdef FEAT_JOB
13126 "job",
13127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#ifdef FEAT_JUMPLIST
13129 "jumplist",
13130#endif
13131#ifdef FEAT_KEYMAP
13132 "keymap",
13133#endif
13134#ifdef FEAT_LANGMAP
13135 "langmap",
13136#endif
13137#ifdef FEAT_LIBCALL
13138 "libcall",
13139#endif
13140#ifdef FEAT_LINEBREAK
13141 "linebreak",
13142#endif
13143#ifdef FEAT_LISP
13144 "lispindent",
13145#endif
13146#ifdef FEAT_LISTCMDS
13147 "listcmds",
13148#endif
13149#ifdef FEAT_LOCALMAP
13150 "localmap",
13151#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013152#ifdef FEAT_LUA
13153# ifndef DYNAMIC_LUA
13154 "lua",
13155# endif
13156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157#ifdef FEAT_MENU
13158 "menu",
13159#endif
13160#ifdef FEAT_SESSION
13161 "mksession",
13162#endif
13163#ifdef FEAT_MODIFY_FNAME
13164 "modify_fname",
13165#endif
13166#ifdef FEAT_MOUSE
13167 "mouse",
13168#endif
13169#ifdef FEAT_MOUSESHAPE
13170 "mouseshape",
13171#endif
13172#if defined(UNIX) || defined(VMS)
13173# ifdef FEAT_MOUSE_DEC
13174 "mouse_dec",
13175# endif
13176# ifdef FEAT_MOUSE_GPM
13177 "mouse_gpm",
13178# endif
13179# ifdef FEAT_MOUSE_JSB
13180 "mouse_jsbterm",
13181# endif
13182# ifdef FEAT_MOUSE_NET
13183 "mouse_netterm",
13184# endif
13185# ifdef FEAT_MOUSE_PTERM
13186 "mouse_pterm",
13187# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013188# ifdef FEAT_MOUSE_SGR
13189 "mouse_sgr",
13190# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013191# ifdef FEAT_SYSMOUSE
13192 "mouse_sysmouse",
13193# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013194# ifdef FEAT_MOUSE_URXVT
13195 "mouse_urxvt",
13196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197# ifdef FEAT_MOUSE_XTERM
13198 "mouse_xterm",
13199# endif
13200#endif
13201#ifdef FEAT_MBYTE
13202 "multi_byte",
13203#endif
13204#ifdef FEAT_MBYTE_IME
13205 "multi_byte_ime",
13206#endif
13207#ifdef FEAT_MULTI_LANG
13208 "multi_lang",
13209#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013210#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013211#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013212 "mzscheme",
13213#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215#ifdef FEAT_OLE
13216 "ole",
13217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218#ifdef FEAT_PATH_EXTRA
13219 "path_extra",
13220#endif
13221#ifdef FEAT_PERL
13222#ifndef DYNAMIC_PERL
13223 "perl",
13224#endif
13225#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013226#ifdef FEAT_PERSISTENT_UNDO
13227 "persistent_undo",
13228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229#ifdef FEAT_PYTHON
13230#ifndef DYNAMIC_PYTHON
13231 "python",
13232#endif
13233#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013234#ifdef FEAT_PYTHON3
13235#ifndef DYNAMIC_PYTHON3
13236 "python3",
13237#endif
13238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239#ifdef FEAT_POSTSCRIPT
13240 "postscript",
13241#endif
13242#ifdef FEAT_PRINTER
13243 "printer",
13244#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013245#ifdef FEAT_PROFILE
13246 "profile",
13247#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013248#ifdef FEAT_RELTIME
13249 "reltime",
13250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251#ifdef FEAT_QUICKFIX
13252 "quickfix",
13253#endif
13254#ifdef FEAT_RIGHTLEFT
13255 "rightleft",
13256#endif
13257#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13258 "ruby",
13259#endif
13260#ifdef FEAT_SCROLLBIND
13261 "scrollbind",
13262#endif
13263#ifdef FEAT_CMDL_INFO
13264 "showcmd",
13265 "cmdline_info",
13266#endif
13267#ifdef FEAT_SIGNS
13268 "signs",
13269#endif
13270#ifdef FEAT_SMARTINDENT
13271 "smartindent",
13272#endif
13273#ifdef FEAT_SNIFF
13274 "sniff",
13275#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013276#ifdef STARTUPTIME
13277 "startuptime",
13278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279#ifdef FEAT_STL_OPT
13280 "statusline",
13281#endif
13282#ifdef FEAT_SUN_WORKSHOP
13283 "sun_workshop",
13284#endif
13285#ifdef FEAT_NETBEANS_INTG
13286 "netbeans_intg",
13287#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013288#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013289 "spell",
13290#endif
13291#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 "syntax",
13293#endif
13294#if defined(USE_SYSTEM) || !defined(UNIX)
13295 "system",
13296#endif
13297#ifdef FEAT_TAG_BINS
13298 "tag_binary",
13299#endif
13300#ifdef FEAT_TAG_OLDSTATIC
13301 "tag_old_static",
13302#endif
13303#ifdef FEAT_TAG_ANYWHITE
13304 "tag_any_white",
13305#endif
13306#ifdef FEAT_TCL
13307# ifndef DYNAMIC_TCL
13308 "tcl",
13309# endif
13310#endif
13311#ifdef TERMINFO
13312 "terminfo",
13313#endif
13314#ifdef FEAT_TERMRESPONSE
13315 "termresponse",
13316#endif
13317#ifdef FEAT_TEXTOBJ
13318 "textobjects",
13319#endif
13320#ifdef HAVE_TGETENT
13321 "tgetent",
13322#endif
13323#ifdef FEAT_TITLE
13324 "title",
13325#endif
13326#ifdef FEAT_TOOLBAR
13327 "toolbar",
13328#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013329#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13330 "unnamedplus",
13331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332#ifdef FEAT_USR_CMDS
13333 "user-commands", /* was accidentally included in 5.4 */
13334 "user_commands",
13335#endif
13336#ifdef FEAT_VIMINFO
13337 "viminfo",
13338#endif
13339#ifdef FEAT_VERTSPLIT
13340 "vertsplit",
13341#endif
13342#ifdef FEAT_VIRTUALEDIT
13343 "virtualedit",
13344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346#ifdef FEAT_VISUALEXTRA
13347 "visualextra",
13348#endif
13349#ifdef FEAT_VREPLACE
13350 "vreplace",
13351#endif
13352#ifdef FEAT_WILDIGN
13353 "wildignore",
13354#endif
13355#ifdef FEAT_WILDMENU
13356 "wildmenu",
13357#endif
13358#ifdef FEAT_WINDOWS
13359 "windows",
13360#endif
13361#ifdef FEAT_WAK
13362 "winaltkeys",
13363#endif
13364#ifdef FEAT_WRITEBACKUP
13365 "writebackup",
13366#endif
13367#ifdef FEAT_XIM
13368 "xim",
13369#endif
13370#ifdef FEAT_XFONTSET
13371 "xfontset",
13372#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013373#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013374 "xpm",
13375 "xpm_w32", /* for backward compatibility */
13376#else
13377# if defined(HAVE_XPM)
13378 "xpm",
13379# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381#ifdef USE_XSMP
13382 "xsmp",
13383#endif
13384#ifdef USE_XSMP_INTERACT
13385 "xsmp_interact",
13386#endif
13387#ifdef FEAT_XCLIPBOARD
13388 "xterm_clipboard",
13389#endif
13390#ifdef FEAT_XTERM_SAVE
13391 "xterm_save",
13392#endif
13393#if defined(UNIX) && defined(FEAT_X11)
13394 "X11",
13395#endif
13396 NULL
13397 };
13398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 for (i = 0; has_list[i] != NULL; ++i)
13401 if (STRICMP(name, has_list[i]) == 0)
13402 {
13403 n = TRUE;
13404 break;
13405 }
13406
13407 if (n == FALSE)
13408 {
13409 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013410 {
13411 if (name[5] == '-'
13412 && STRLEN(name) > 11
13413 && vim_isdigit(name[6])
13414 && vim_isdigit(name[8])
13415 && vim_isdigit(name[10]))
13416 {
13417 int major = atoi((char *)name + 6);
13418 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013419
13420 /* Expect "patch-9.9.01234". */
13421 n = (major < VIM_VERSION_MAJOR
13422 || (major == VIM_VERSION_MAJOR
13423 && (minor < VIM_VERSION_MINOR
13424 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013425 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013426 }
13427 else
13428 n = has_patch(atoi((char *)name + 5));
13429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 else if (STRICMP(name, "vim_starting") == 0)
13431 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013432#ifdef FEAT_MBYTE
13433 else if (STRICMP(name, "multi_byte_encoding") == 0)
13434 n = has_mbyte;
13435#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013436#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13437 else if (STRICMP(name, "balloon_multiline") == 0)
13438 n = multiline_balloon_available();
13439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440#ifdef DYNAMIC_TCL
13441 else if (STRICMP(name, "tcl") == 0)
13442 n = tcl_enabled(FALSE);
13443#endif
13444#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13445 else if (STRICMP(name, "iconv") == 0)
13446 n = iconv_enabled(FALSE);
13447#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013448#ifdef DYNAMIC_LUA
13449 else if (STRICMP(name, "lua") == 0)
13450 n = lua_enabled(FALSE);
13451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013452#ifdef DYNAMIC_MZSCHEME
13453 else if (STRICMP(name, "mzscheme") == 0)
13454 n = mzscheme_enabled(FALSE);
13455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456#ifdef DYNAMIC_RUBY
13457 else if (STRICMP(name, "ruby") == 0)
13458 n = ruby_enabled(FALSE);
13459#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013460#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461#ifdef DYNAMIC_PYTHON
13462 else if (STRICMP(name, "python") == 0)
13463 n = python_enabled(FALSE);
13464#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013465#endif
13466#ifdef FEAT_PYTHON3
13467#ifdef DYNAMIC_PYTHON3
13468 else if (STRICMP(name, "python3") == 0)
13469 n = python3_enabled(FALSE);
13470#endif
13471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472#ifdef DYNAMIC_PERL
13473 else if (STRICMP(name, "perl") == 0)
13474 n = perl_enabled(FALSE);
13475#endif
13476#ifdef FEAT_GUI
13477 else if (STRICMP(name, "gui_running") == 0)
13478 n = (gui.in_use || gui.starting);
13479# ifdef FEAT_GUI_W32
13480 else if (STRICMP(name, "gui_win32s") == 0)
13481 n = gui_is_win32s();
13482# endif
13483# ifdef FEAT_BROWSE
13484 else if (STRICMP(name, "browse") == 0)
13485 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13486# endif
13487#endif
13488#ifdef FEAT_SYN_HL
13489 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013490 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491#endif
13492#if defined(WIN3264)
13493 else if (STRICMP(name, "win95") == 0)
13494 n = mch_windows95();
13495#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013496#ifdef FEAT_NETBEANS_INTG
13497 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013498 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 }
13501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503}
13504
13505/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013506 * "has_key()" function
13507 */
13508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013509f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013510{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013511 if (argvars[0].v_type != VAR_DICT)
13512 {
13513 EMSG(_(e_dictreq));
13514 return;
13515 }
13516 if (argvars[0].vval.v_dict == NULL)
13517 return;
13518
13519 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013520 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013521}
13522
13523/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013524 * "haslocaldir()" function
13525 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013527f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013528{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013529 win_T *wp = NULL;
13530
13531 wp = find_tabwin(&argvars[0], &argvars[1]);
13532 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013533}
13534
13535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 * "hasmapto()" function
13537 */
13538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013539f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540{
13541 char_u *name;
13542 char_u *mode;
13543 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013544 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013547 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 mode = (char_u *)"nvo";
13549 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013552 if (argvars[2].v_type != VAR_UNKNOWN)
13553 abbr = get_tv_number(&argvars[2]);
13554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555
Bram Moolenaar2c932302006-03-18 21:42:09 +000013556 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560}
13561
13562/*
13563 * "histadd()" function
13564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013566f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567{
13568#ifdef FEAT_CMDHIST
13569 int histype;
13570 char_u *str;
13571 char_u buf[NUMBUFLEN];
13572#endif
13573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 if (check_restricted() || check_secure())
13576 return;
13577#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13579 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 if (histype >= 0)
13581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (*str != NUL)
13584 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013585 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 return;
13589 }
13590 }
13591#endif
13592}
13593
13594/*
13595 * "histdel()" function
13596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013598f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600#ifdef FEAT_CMDHIST
13601 int n;
13602 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13606 if (str == NULL)
13607 n = 0;
13608 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013611 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 else
13616 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 get_tv_string_buf(&argvars[1], buf));
13619 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620#endif
13621}
13622
13623/*
13624 * "histget()" function
13625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013627f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
13629#ifdef FEAT_CMDHIST
13630 int type;
13631 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13635 if (str == NULL)
13636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013638 {
13639 type = get_histtype(str);
13640 if (argvars[1].v_type == VAR_UNKNOWN)
13641 idx = get_history_idx(type);
13642 else
13643 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13644 /* -1 on type error */
13645 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
13653/*
13654 * "histnr()" function
13655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013657f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
13659 int i;
13660
13661#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 char_u *history = get_tv_string_chk(&argvars[0]);
13663
13664 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 if (i >= HIST_CMD && i < HIST_COUNT)
13666 i = get_history_idx(i);
13667 else
13668#endif
13669 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 * "highlightID(name)" function
13675 */
13676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013677f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680}
13681
13682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683 * "highlight_exists()" function
13684 */
13685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013686f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687{
13688 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13689}
13690
13691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 * "hostname()" function
13693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013695f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696{
13697 char_u hostname[256];
13698
13699 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->v_type = VAR_STRING;
13701 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
13705 * iconv() function
13706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013708f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709{
13710#ifdef FEAT_MBYTE
13711 char_u buf1[NUMBUFLEN];
13712 char_u buf2[NUMBUFLEN];
13713 char_u *from, *to, *str;
13714 vimconv_T vimconv;
13715#endif
13716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 rettv->v_type = VAR_STRING;
13718 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719
13720#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 str = get_tv_string(&argvars[0]);
13722 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13723 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 vimconv.vc_type = CONV_NONE;
13725 convert_setup(&vimconv, from, to);
13726
13727 /* If the encodings are equal, no conversion needed. */
13728 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732
13733 convert_setup(&vimconv, NULL, NULL);
13734 vim_free(from);
13735 vim_free(to);
13736#endif
13737}
13738
13739/*
13740 * "indent()" function
13741 */
13742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013743f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744{
13745 linenr_T lnum;
13746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752}
13753
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013754/*
13755 * "index()" function
13756 */
13757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013758f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013759{
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 list_T *l;
13761 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013762 long idx = 0;
13763 int ic = FALSE;
13764
13765 rettv->vval.v_number = -1;
13766 if (argvars[0].v_type != VAR_LIST)
13767 {
13768 EMSG(_(e_listreq));
13769 return;
13770 }
13771 l = argvars[0].vval.v_list;
13772 if (l != NULL)
13773 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013774 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 int error = FALSE;
13778
Bram Moolenaar758711c2005-02-02 23:11:38 +000013779 /* Start at specified item. Use the cached index that list_find()
13780 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013782 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013783 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 ic = get_tv_number_chk(&argvars[3], &error);
13785 if (error)
13786 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013787 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013788
Bram Moolenaar758711c2005-02-02 23:11:38 +000013789 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013790 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013791 {
13792 rettv->vval.v_number = idx;
13793 break;
13794 }
13795 }
13796}
13797
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798static int inputsecret_flag = 0;
13799
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013800static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013801
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013803 * This function is used by f_input() and f_inputdialog() functions. The third
13804 * argument to f_input() specifies the type of completion to use at the
13805 * prompt. The third argument to f_inputdialog() specifies the value to return
13806 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 */
13808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013809get_user_input(
13810 typval_T *argvars,
13811 typval_T *rettv,
13812 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 char_u *p = NULL;
13816 int c;
13817 char_u buf[NUMBUFLEN];
13818 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013820 int xp_type = EXPAND_NOTHING;
13821 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013823 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
13826#ifdef NO_CONSOLE_INPUT
13827 /* While starting up, there is no place to enter text. */
13828 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#endif
13831
13832 cmd_silent = FALSE; /* Want to see the prompt. */
13833 if (prompt != NULL)
13834 {
13835 /* Only the part of the message after the last NL is considered as
13836 * prompt for the command line */
13837 p = vim_strrchr(prompt, '\n');
13838 if (p == NULL)
13839 p = prompt;
13840 else
13841 {
13842 ++p;
13843 c = *p;
13844 *p = NUL;
13845 msg_start();
13846 msg_clr_eos();
13847 msg_puts_attr(prompt, echo_attr);
13848 msg_didout = FALSE;
13849 msg_starthere();
13850 *p = c;
13851 }
13852 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 if (argvars[1].v_type != VAR_UNKNOWN)
13855 {
13856 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13857 if (defstr != NULL)
13858 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013860 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013861 {
13862 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013863 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013864 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013865
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013866 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013867 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013868
Bram Moolenaar4463f292005-09-25 22:20:24 +000013869 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13870 if (xp_name == NULL)
13871 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013872
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013873 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013874
Bram Moolenaar4463f292005-09-25 22:20:24 +000013875 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13876 &xp_arg) == FAIL)
13877 return;
13878 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013879 }
13880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013882 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013883 int save_ex_normal_busy = ex_normal_busy;
13884 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013886 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13887 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013888 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013889 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013890 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013891 && argvars[1].v_type != VAR_UNKNOWN
13892 && argvars[2].v_type != VAR_UNKNOWN)
13893 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13894 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013895
13896 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 /* since the user typed this, no need to wait for return */
13899 need_wait_return = FALSE;
13900 msg_didout = FALSE;
13901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 cmd_silent = cmd_silent_save;
13903}
13904
13905/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013906 * "input()" function
13907 * Also handles inputsecret() when inputsecret is set.
13908 */
13909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013910f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013911{
13912 get_user_input(argvars, rettv, FALSE);
13913}
13914
13915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 * "inputdialog()" function
13917 */
13918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013919f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920{
13921#if defined(FEAT_GUI_TEXTDIALOG)
13922 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13923 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13924 {
13925 char_u *message;
13926 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 message = get_tv_string_chk(&argvars[0]);
13930 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013931 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013932 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 else
13934 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 if (message != NULL && defstr != NULL
13936 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013937 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 else
13940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 if (message != NULL && defstr != NULL
13942 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013943 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->vval.v_string = vim_strsave(
13945 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 }
13951 else
13952#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013953 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954}
13955
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013956/*
13957 * "inputlist()" function
13958 */
13959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013960f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013961{
13962 listitem_T *li;
13963 int selected;
13964 int mouse_used;
13965
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013966#ifdef NO_CONSOLE_INPUT
13967 /* While starting up, there is no place to enter text. */
13968 if (no_console_input())
13969 return;
13970#endif
13971 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13972 {
13973 EMSG2(_(e_listarg), "inputlist()");
13974 return;
13975 }
13976
13977 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013978 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013979 lines_left = Rows; /* avoid more prompt */
13980 msg_scroll = TRUE;
13981 msg_clr_eos();
13982
13983 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13984 {
13985 msg_puts(get_tv_string(&li->li_tv));
13986 msg_putchar('\n');
13987 }
13988
13989 /* Ask for choice. */
13990 selected = prompt_for_number(&mouse_used);
13991 if (mouse_used)
13992 selected -= lines_left;
13993
13994 rettv->vval.v_number = selected;
13995}
13996
13997
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13999
14000/*
14001 * "inputrestore()" function
14002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014004f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005{
14006 if (ga_userinput.ga_len > 0)
14007 {
14008 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14010 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014011 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 }
14013 else if (p_verbose > 1)
14014 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014015 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 }
14018}
14019
14020/*
14021 * "inputsave()" function
14022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014024f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014026 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 if (ga_grow(&ga_userinput, 1) == OK)
14028 {
14029 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14030 + ga_userinput.ga_len);
14031 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014032 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 }
14034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014035 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036}
14037
14038/*
14039 * "inputsecret()" function
14040 */
14041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014042f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043{
14044 ++cmdline_star;
14045 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 --cmdline_star;
14048 --inputsecret_flag;
14049}
14050
14051/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052 * "insert()" function
14053 */
14054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014056{
14057 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 listitem_T *item;
14059 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014061
14062 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014064 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014065 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014066 {
14067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 before = get_tv_number_chk(&argvars[2], &error);
14069 if (error)
14070 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014071
Bram Moolenaar758711c2005-02-02 23:11:38 +000014072 if (before == l->lv_len)
14073 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014074 else
14075 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014076 item = list_find(l, before);
14077 if (item == NULL)
14078 {
14079 EMSGN(_(e_listidx), before);
14080 l = NULL;
14081 }
14082 }
14083 if (l != NULL)
14084 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014085 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014086 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087 }
14088 }
14089}
14090
14091/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014092 * "invert(expr)" function
14093 */
14094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014095f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014096{
14097 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14098}
14099
14100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 * "isdirectory()" function
14102 */
14103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014104f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014106 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107}
14108
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014109/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014110 * "islocked()" function
14111 */
14112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014113f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014114{
14115 lval_T lv;
14116 char_u *end;
14117 dictitem_T *di;
14118
14119 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014120 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14121 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014122 if (end != NULL && lv.ll_name != NULL)
14123 {
14124 if (*end != NUL)
14125 EMSG(_(e_trailing));
14126 else
14127 {
14128 if (lv.ll_tv == NULL)
14129 {
14130 if (check_changedtick(lv.ll_name))
14131 rettv->vval.v_number = 1; /* always locked */
14132 else
14133 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014134 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014135 if (di != NULL)
14136 {
14137 /* Consider a variable locked when:
14138 * 1. the variable itself is locked
14139 * 2. the value of the variable is locked.
14140 * 3. the List or Dict value is locked.
14141 */
14142 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14143 || tv_islocked(&di->di_tv));
14144 }
14145 }
14146 }
14147 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014148 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014149 else if (lv.ll_newkey != NULL)
14150 EMSG2(_(e_dictkey), lv.ll_newkey);
14151 else if (lv.ll_list != NULL)
14152 /* List item. */
14153 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14154 else
14155 /* Dictionary item. */
14156 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14157 }
14158 }
14159
14160 clear_lval(&lv);
14161}
14162
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014163static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014164
14165/*
14166 * Turn a dict into a list:
14167 * "what" == 0: list of keys
14168 * "what" == 1: list of values
14169 * "what" == 2: list of items
14170 */
14171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014172dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014173{
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 list_T *l2;
14175 dictitem_T *di;
14176 hashitem_T *hi;
14177 listitem_T *li;
14178 listitem_T *li2;
14179 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014180 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014181
Bram Moolenaar8c711452005-01-14 21:53:12 +000014182 if (argvars[0].v_type != VAR_DICT)
14183 {
14184 EMSG(_(e_dictreq));
14185 return;
14186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014187 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014188 return;
14189
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014190 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014191 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014193 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014196 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014197 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014198 --todo;
14199 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014201 li = listitem_alloc();
14202 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014203 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014204 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014205
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014206 if (what == 0)
14207 {
14208 /* keys() */
14209 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014210 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014211 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14212 }
14213 else if (what == 1)
14214 {
14215 /* values() */
14216 copy_tv(&di->di_tv, &li->li_tv);
14217 }
14218 else
14219 {
14220 /* items() */
14221 l2 = list_alloc();
14222 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014223 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014224 li->li_tv.vval.v_list = l2;
14225 if (l2 == NULL)
14226 break;
14227 ++l2->lv_refcount;
14228
14229 li2 = listitem_alloc();
14230 if (li2 == NULL)
14231 break;
14232 list_append(l2, li2);
14233 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014234 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014235 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14236
14237 li2 = listitem_alloc();
14238 if (li2 == NULL)
14239 break;
14240 list_append(l2, li2);
14241 copy_tv(&di->di_tv, &li2->li_tv);
14242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014243 }
14244 }
14245}
14246
14247/*
14248 * "items(dict)" function
14249 */
14250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252{
14253 dict_list(argvars, rettv, 2);
14254}
14255
Bram Moolenaar835dc632016-02-07 14:27:38 +010014256#ifdef FEAT_JOB
14257/*
14258 * "job_start()" function
14259 */
14260 static void
14261f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14262{
14263 job_T *job;
14264 char_u *cmd = NULL;
14265#if defined(UNIX)
14266# define USE_ARGV
14267 char **argv = NULL;
14268 int argc = 0;
14269#else
14270 garray_T ga;
14271#endif
14272
14273 rettv->v_type = VAR_JOB;
14274 job = job_alloc();
14275 rettv->vval.v_job = job;
14276 if (job == NULL)
14277 return;
14278
14279 rettv->vval.v_job->jv_status = JOB_FAILED;
14280#ifndef USE_ARGV
14281 ga_init2(&ga, 200);
14282#endif
14283
14284 if (argvars[0].v_type == VAR_STRING)
14285 {
14286 /* Command is a string. */
14287 cmd = argvars[0].vval.v_string;
14288#ifdef USE_ARGV
14289 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14290 return;
14291 argv[argc] = NULL;
14292#endif
14293 }
14294 else if (argvars[0].v_type != VAR_LIST
14295 || argvars[0].vval.v_list == NULL
14296 || argvars[0].vval.v_list->lv_len < 1)
14297 {
14298 EMSG(_(e_invarg));
14299 return;
14300 }
14301 else
14302 {
14303 list_T *l = argvars[0].vval.v_list;
14304 listitem_T *li;
14305 char_u *s;
14306
14307#ifdef USE_ARGV
14308 /* Pass argv[] to mch_call_shell(). */
14309 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14310 if (argv == NULL)
14311 return;
14312#endif
14313 for (li = l->lv_first; li != NULL; li = li->li_next)
14314 {
14315 s = get_tv_string_chk(&li->li_tv);
14316 if (s == NULL)
14317 goto theend;
14318#ifdef USE_ARGV
14319 argv[argc++] = (char *)s;
14320#else
14321 if (li != l->lv_first)
14322 {
14323 s = vim_strsave_shellescape(s, FALSE, TRUE);
14324 if (s == NULL)
14325 goto theend;
14326 }
14327 ga_concat(&ga, s);
14328 vim_free(s);
14329 if (li->li_next != NULL)
14330 ga_append(&ga, ' ');
14331#endif
14332 }
14333#ifdef USE_ARGV
14334 argv[argc] = NULL;
14335#else
14336 cmd = ga.ga_data;
14337#endif
14338 }
14339#ifdef USE_ARGV
14340 mch_start_job(argv, job);
14341#else
14342 mch_start_job(cmd, job);
14343#endif
14344
14345theend:
14346#ifdef USE_ARGV
14347 if (argv != NULL)
14348 vim_free(argv);
14349#else
14350 vim_free(ga.ga_data);
14351#endif
14352}
14353
14354/*
14355 * "job_status()" function
14356 */
14357 static void
14358f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14359{
14360 char *result;
14361
14362 if (argvars[0].v_type != VAR_JOB)
14363 EMSG(_(e_invarg));
14364 else
14365 {
14366 job_T *job = argvars[0].vval.v_job;
14367
14368 if (job->jv_status == JOB_ENDED)
14369 /* No need to check, dead is dead. */
14370 result = "dead";
14371 else if (job->jv_status == JOB_FAILED)
14372 result = "fail";
14373 else
14374 result = mch_job_status(job);
14375 rettv->v_type = VAR_STRING;
14376 rettv->vval.v_string = vim_strsave((char_u *)result);
14377 }
14378}
14379
14380/*
14381 * "job_stop()" function
14382 */
14383 static void
14384f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14385{
14386 if (argvars[0].v_type != VAR_JOB)
14387 EMSG(_(e_invarg));
14388 else
14389 {
14390 char_u *arg;
14391
14392 if (argvars[1].v_type == VAR_UNKNOWN)
14393 arg = (char_u *)"";
14394 else
14395 {
14396 arg = get_tv_string_chk(&argvars[1]);
14397 if (arg == NULL)
14398 {
14399 EMSG(_(e_invarg));
14400 return;
14401 }
14402 }
14403 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14404 rettv->vval.v_number = 0;
14405 else
14406 rettv->vval.v_number = 1;
14407 }
14408}
14409#endif
14410
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014412 * "join()" function
14413 */
14414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416{
14417 garray_T ga;
14418 char_u *sep;
14419
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014420 if (argvars[0].v_type != VAR_LIST)
14421 {
14422 EMSG(_(e_listreq));
14423 return;
14424 }
14425 if (argvars[0].vval.v_list == NULL)
14426 return;
14427 if (argvars[1].v_type == VAR_UNKNOWN)
14428 sep = (char_u *)" ";
14429 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014430 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014431
14432 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433
14434 if (sep != NULL)
14435 {
14436 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014437 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 ga_append(&ga, NUL);
14439 rettv->vval.v_string = (char_u *)ga.ga_data;
14440 }
14441 else
14442 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443}
14444
14445/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014446 * "jsondecode()" function
14447 */
14448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014449f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014450{
14451 js_read_T reader;
14452
14453 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014454 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014455 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014456 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014457 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014458}
14459
14460/*
14461 * "jsonencode()" function
14462 */
14463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014465{
14466 rettv->v_type = VAR_STRING;
14467 rettv->vval.v_string = json_encode(&argvars[0]);
14468}
14469
14470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014471 * "keys()" function
14472 */
14473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014475{
14476 dict_list(argvars, rettv, 0);
14477}
14478
14479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 * "last_buffer_nr()" function.
14481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 int n = 0;
14486 buf_T *buf;
14487
14488 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14489 if (n < buf->b_fnum)
14490 n = buf->b_fnum;
14491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014493}
14494
14495/*
14496 * "len()" function
14497 */
14498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014499f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014500{
14501 switch (argvars[0].v_type)
14502 {
14503 case VAR_STRING:
14504 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_number = (varnumber_T)STRLEN(
14506 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014507 break;
14508 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014510 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014511 case VAR_DICT:
14512 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14513 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014514 case VAR_UNKNOWN:
14515 case VAR_SPECIAL:
14516 case VAR_FLOAT:
14517 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014518 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014519 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014520 break;
14521 }
14522}
14523
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014524static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014525
14526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014527libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014528{
14529#ifdef FEAT_LIBCALL
14530 char_u *string_in;
14531 char_u **string_result;
14532 int nr_result;
14533#endif
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014536 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014538
14539 if (check_restricted() || check_secure())
14540 return;
14541
14542#ifdef FEAT_LIBCALL
14543 /* The first two args must be strings, otherwise its meaningless */
14544 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14545 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014546 string_in = NULL;
14547 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014548 string_in = argvars[2].vval.v_string;
14549 if (type == VAR_NUMBER)
14550 string_result = NULL;
14551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 if (mch_libcall(argvars[0].vval.v_string,
14554 argvars[1].vval.v_string,
14555 string_in,
14556 argvars[2].vval.v_number,
14557 string_result,
14558 &nr_result) == OK
14559 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014561 }
14562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563}
14564
14565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566 * "libcall()" function
14567 */
14568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014569f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570{
14571 libcall_common(argvars, rettv, VAR_STRING);
14572}
14573
14574/*
14575 * "libcallnr()" function
14576 */
14577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014578f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579{
14580 libcall_common(argvars, rettv, VAR_NUMBER);
14581}
14582
14583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 * "line(string)" function
14585 */
14586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014587f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588{
14589 linenr_T lnum = 0;
14590 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014591 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014593 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 if (fp != NULL)
14595 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
14600 * "line2byte(lnum)" function
14601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014603f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
14605#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607#else
14608 linenr_T lnum;
14609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14615 if (rettv->vval.v_number >= 0)
14616 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617#endif
14618}
14619
14620/*
14621 * "lispindent(lnum)" function
14622 */
14623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014624f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625{
14626#ifdef FEAT_LISP
14627 pos_T pos;
14628 linenr_T lnum;
14629
14630 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14633 {
14634 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 curwin->w_cursor = pos;
14637 }
14638 else
14639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641}
14642
14643/*
14644 * "localtime()" function
14645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014647f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650}
14651
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014652static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653
14654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014655get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656{
14657 char_u *keys;
14658 char_u *which;
14659 char_u buf[NUMBUFLEN];
14660 char_u *keys_buf = NULL;
14661 char_u *rhs;
14662 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014663 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014664 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014665 mapblock_T *mp;
14666 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
14668 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669 rettv->v_type = VAR_STRING;
14670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 if (*keys == NUL)
14674 return;
14675
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014676 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014680 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014681 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014682 if (argvars[3].v_type != VAR_UNKNOWN)
14683 get_dict = get_tv_number(&argvars[3]);
14684 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 else
14687 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 if (which == NULL)
14689 return;
14690
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 mode = get_map_mode(&which, 0);
14692
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014693 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014694 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014696
14697 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014699 /* Return a string. */
14700 if (rhs != NULL)
14701 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702
Bram Moolenaarbd743252010-10-20 21:23:33 +020014703 }
14704 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14705 {
14706 /* Return a dictionary. */
14707 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14708 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14709 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaarbd743252010-10-20 21:23:33 +020014711 dict_add_nr_str(dict, "lhs", 0L, lhs);
14712 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14713 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14714 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14715 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14716 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14717 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014718 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014719 dict_add_nr_str(dict, "mode", 0L, mapmode);
14720
14721 vim_free(lhs);
14722 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 }
14724}
14725
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014726#ifdef FEAT_FLOAT
14727/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014728 * "log()" function
14729 */
14730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014731f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014732{
14733 float_T f;
14734
14735 rettv->v_type = VAR_FLOAT;
14736 if (get_float_arg(argvars, &f) == OK)
14737 rettv->vval.v_float = log(f);
14738 else
14739 rettv->vval.v_float = 0.0;
14740}
14741
14742/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014743 * "log10()" function
14744 */
14745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014746f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014747{
14748 float_T f;
14749
14750 rettv->v_type = VAR_FLOAT;
14751 if (get_float_arg(argvars, &f) == OK)
14752 rettv->vval.v_float = log10(f);
14753 else
14754 rettv->vval.v_float = 0.0;
14755}
14756#endif
14757
Bram Moolenaar1dced572012-04-05 16:54:08 +020014758#ifdef FEAT_LUA
14759/*
14760 * "luaeval()" function
14761 */
14762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014763f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014764{
14765 char_u *str;
14766 char_u buf[NUMBUFLEN];
14767
14768 str = get_tv_string_buf(&argvars[0], buf);
14769 do_luaeval(str, argvars + 1, rettv);
14770}
14771#endif
14772
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014774 * "map()" function
14775 */
14776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014777f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014778{
14779 filter_map(argvars, rettv, TRUE);
14780}
14781
14782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 */
14785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014786f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789}
14790
14791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014792 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 */
14794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014795f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014797 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798}
14799
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014800static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801
14802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014805 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014806 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 char_u *pat;
14809 regmatch_T regmatch;
14810 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812 char_u *save_cpo;
14813 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014814 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014815 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014816 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 list_T *l = NULL;
14818 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014819 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821
14822 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14823 save_cpo = p_cpo;
14824 p_cpo = (char_u *)"";
14825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014826 rettv->vval.v_number = -1;
14827 if (type == 3)
14828 {
14829 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014830 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 }
14833 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835 rettv->v_type = VAR_STRING;
14836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014839 if (argvars[0].v_type == VAR_LIST)
14840 {
14841 if ((l = argvars[0].vval.v_list) == NULL)
14842 goto theend;
14843 li = l->lv_first;
14844 }
14845 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014846 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014847 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014848 len = (long)STRLEN(str);
14849 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14852 if (pat == NULL)
14853 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014854
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014857 int error = FALSE;
14858
14859 start = get_tv_number_chk(&argvars[2], &error);
14860 if (error)
14861 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014862 if (l != NULL)
14863 {
14864 li = list_find(l, start);
14865 if (li == NULL)
14866 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014867 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014868 }
14869 else
14870 {
14871 if (start < 0)
14872 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014873 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014874 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014875 /* When "count" argument is there ignore matches before "start",
14876 * otherwise skip part of the string. Differs when pattern is "^"
14877 * or "\<". */
14878 if (argvars[3].v_type != VAR_UNKNOWN)
14879 startcol = start;
14880 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014881 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014882 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014883 len -= start;
14884 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014885 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014887 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014888 nth = get_tv_number_chk(&argvars[3], &error);
14889 if (error)
14890 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 }
14892
14893 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14894 if (regmatch.regprog != NULL)
14895 {
14896 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014897
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014898 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014899 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014900 if (l != NULL)
14901 {
14902 if (li == NULL)
14903 {
14904 match = FALSE;
14905 break;
14906 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014907 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014908 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014909 if (str == NULL)
14910 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014911 }
14912
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014913 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014914
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014915 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014916 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014917 if (l == NULL && !match)
14918 break;
14919
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014920 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014921 if (l != NULL)
14922 {
14923 li = li->li_next;
14924 ++idx;
14925 }
14926 else
14927 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014928#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014929 startcol = (colnr_T)(regmatch.startp[0]
14930 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014931#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014932 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014933#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014934 if (startcol > (colnr_T)len
14935 || str + startcol <= regmatch.startp[0])
14936 {
14937 match = FALSE;
14938 break;
14939 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014941 }
14942
14943 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014947 int i;
14948
14949 /* return list with matched string and submatches */
14950 for (i = 0; i < NSUBEXP; ++i)
14951 {
14952 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014953 {
14954 if (list_append_string(rettv->vval.v_list,
14955 (char_u *)"", 0) == FAIL)
14956 break;
14957 }
14958 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014959 regmatch.startp[i],
14960 (int)(regmatch.endp[i] - regmatch.startp[i]))
14961 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014962 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963 }
14964 }
14965 else if (type == 2)
14966 {
14967 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014968 if (l != NULL)
14969 copy_tv(&li->li_tv, rettv);
14970 else
14971 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014973 }
14974 else if (l != NULL)
14975 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 else
14977 {
14978 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 (varnumber_T)(regmatch.startp[0] - str);
14981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014984 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 }
14986 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014987 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 }
14989
14990theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014991 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 p_cpo = save_cpo;
14993}
14994
14995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 * "match()" function
14997 */
14998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014999f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000{
15001 find_some_match(argvars, rettv, 1);
15002}
15003
15004/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015005 * "matchadd()" function
15006 */
15007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015008f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015009{
15010#ifdef FEAT_SEARCH_EXTRA
15011 char_u buf[NUMBUFLEN];
15012 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15013 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15014 int prio = 10; /* default priority */
15015 int id = -1;
15016 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015017 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015018
15019 rettv->vval.v_number = -1;
15020
15021 if (grp == NULL || pat == NULL)
15022 return;
15023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015024 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015025 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015026 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015027 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015028 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015029 if (argvars[4].v_type != VAR_UNKNOWN)
15030 {
15031 if (argvars[4].v_type != VAR_DICT)
15032 {
15033 EMSG(_(e_dictreq));
15034 return;
15035 }
15036 if (dict_find(argvars[4].vval.v_dict,
15037 (char_u *)"conceal", -1) != NULL)
15038 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15039 (char_u *)"conceal", FALSE);
15040 }
15041 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015042 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015043 if (error == TRUE)
15044 return;
15045 if (id >= 1 && id <= 3)
15046 {
15047 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15048 return;
15049 }
15050
Bram Moolenaar6561d522015-07-21 15:48:27 +020015051 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15052 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015053#endif
15054}
15055
15056/*
15057 * "matchaddpos()" function
15058 */
15059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015061{
15062#ifdef FEAT_SEARCH_EXTRA
15063 char_u buf[NUMBUFLEN];
15064 char_u *group;
15065 int prio = 10;
15066 int id = -1;
15067 int error = FALSE;
15068 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015069 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015070
15071 rettv->vval.v_number = -1;
15072
15073 group = get_tv_string_buf_chk(&argvars[0], buf);
15074 if (group == NULL)
15075 return;
15076
15077 if (argvars[1].v_type != VAR_LIST)
15078 {
15079 EMSG2(_(e_listarg), "matchaddpos()");
15080 return;
15081 }
15082 l = argvars[1].vval.v_list;
15083 if (l == NULL)
15084 return;
15085
15086 if (argvars[2].v_type != VAR_UNKNOWN)
15087 {
15088 prio = get_tv_number_chk(&argvars[2], &error);
15089 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015090 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015091 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015092 if (argvars[4].v_type != VAR_UNKNOWN)
15093 {
15094 if (argvars[4].v_type != VAR_DICT)
15095 {
15096 EMSG(_(e_dictreq));
15097 return;
15098 }
15099 if (dict_find(argvars[4].vval.v_dict,
15100 (char_u *)"conceal", -1) != NULL)
15101 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15102 (char_u *)"conceal", FALSE);
15103 }
15104 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015105 }
15106 if (error == TRUE)
15107 return;
15108
15109 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15110 if (id == 1 || id == 2)
15111 {
15112 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15113 return;
15114 }
15115
Bram Moolenaar6561d522015-07-21 15:48:27 +020015116 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15117 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015118#endif
15119}
15120
15121/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015122 * "matcharg()" function
15123 */
15124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015125f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015126{
15127 if (rettv_list_alloc(rettv) == OK)
15128 {
15129#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015130 int id = get_tv_number(&argvars[0]);
15131 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015132
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015133 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015134 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015135 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15136 {
15137 list_append_string(rettv->vval.v_list,
15138 syn_id2name(m->hlg_id), -1);
15139 list_append_string(rettv->vval.v_list, m->pattern, -1);
15140 }
15141 else
15142 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015143 list_append_string(rettv->vval.v_list, NULL, -1);
15144 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015145 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015146 }
15147#endif
15148 }
15149}
15150
15151/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015152 * "matchdelete()" function
15153 */
15154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015155f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015156{
15157#ifdef FEAT_SEARCH_EXTRA
15158 rettv->vval.v_number = match_delete(curwin,
15159 (int)get_tv_number(&argvars[0]), TRUE);
15160#endif
15161}
15162
15163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 * "matchend()" function
15165 */
15166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015167f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168{
15169 find_some_match(argvars, rettv, 0);
15170}
15171
15172/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015173 * "matchlist()" function
15174 */
15175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015176f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015177{
15178 find_some_match(argvars, rettv, 3);
15179}
15180
15181/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182 * "matchstr()" function
15183 */
15184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015185f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186{
15187 find_some_match(argvars, rettv, 2);
15188}
15189
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015190static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015191
15192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015193max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015194{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015195 long n = 0;
15196 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015198
15199 if (argvars[0].v_type == VAR_LIST)
15200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 list_T *l;
15202 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015203
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015204 l = argvars[0].vval.v_list;
15205 if (l != NULL)
15206 {
15207 li = l->lv_first;
15208 if (li != NULL)
15209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015211 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015212 {
15213 li = li->li_next;
15214 if (li == NULL)
15215 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015217 if (domax ? i > n : i < n)
15218 n = i;
15219 }
15220 }
15221 }
15222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015223 else if (argvars[0].v_type == VAR_DICT)
15224 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015225 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015226 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015228 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015229
15230 d = argvars[0].vval.v_dict;
15231 if (d != NULL)
15232 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015233 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015236 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015238 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015240 if (first)
15241 {
15242 n = i;
15243 first = FALSE;
15244 }
15245 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015246 n = i;
15247 }
15248 }
15249 }
15250 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015251 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015252 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015254}
15255
15256/*
15257 * "max()" function
15258 */
15259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015260f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015261{
15262 max_min(argvars, rettv, TRUE);
15263}
15264
15265/*
15266 * "min()" function
15267 */
15268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015269f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015270{
15271 max_min(argvars, rettv, FALSE);
15272}
15273
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015274static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015275
15276/*
15277 * Create the directory in which "dir" is located, and higher levels when
15278 * needed.
15279 */
15280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015281mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015282{
15283 char_u *p;
15284 char_u *updir;
15285 int r = FAIL;
15286
15287 /* Get end of directory name in "dir".
15288 * We're done when it's "/" or "c:/". */
15289 p = gettail_sep(dir);
15290 if (p <= get_past_head(dir))
15291 return OK;
15292
15293 /* If the directory exists we're done. Otherwise: create it.*/
15294 updir = vim_strnsave(dir, (int)(p - dir));
15295 if (updir == NULL)
15296 return FAIL;
15297 if (mch_isdir(updir))
15298 r = OK;
15299 else if (mkdir_recurse(updir, prot) == OK)
15300 r = vim_mkdir_emsg(updir, prot);
15301 vim_free(updir);
15302 return r;
15303}
15304
15305#ifdef vim_mkdir
15306/*
15307 * "mkdir()" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015311{
15312 char_u *dir;
15313 char_u buf[NUMBUFLEN];
15314 int prot = 0755;
15315
15316 rettv->vval.v_number = FAIL;
15317 if (check_restricted() || check_secure())
15318 return;
15319
15320 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015321 if (*dir == NUL)
15322 rettv->vval.v_number = FAIL;
15323 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015324 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015325 if (*gettail(dir) == NUL)
15326 /* remove trailing slashes */
15327 *gettail_sep(dir) = NUL;
15328
15329 if (argvars[1].v_type != VAR_UNKNOWN)
15330 {
15331 if (argvars[2].v_type != VAR_UNKNOWN)
15332 prot = get_tv_number_chk(&argvars[2], NULL);
15333 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15334 mkdir_recurse(dir, prot);
15335 }
15336 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015337 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015338}
15339#endif
15340
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 * "mode()" function
15343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015345f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015347 char_u buf[3];
15348
15349 buf[1] = NUL;
15350 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 if (VIsual_active)
15353 {
15354 if (VIsual_select)
15355 buf[0] = VIsual_mode + 's' - 'v';
15356 else
15357 buf[0] = VIsual_mode;
15358 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015359 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015360 || State == CONFIRM)
15361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015363 if (State == ASKMORE)
15364 buf[1] = 'm';
15365 else if (State == CONFIRM)
15366 buf[1] = '?';
15367 }
15368 else if (State == EXTERNCMD)
15369 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 else if (State & INSERT)
15371 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015372#ifdef FEAT_VREPLACE
15373 if (State & VREPLACE_FLAG)
15374 {
15375 buf[0] = 'R';
15376 buf[1] = 'v';
15377 }
15378 else
15379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 if (State & REPLACE_FLAG)
15381 buf[0] = 'R';
15382 else
15383 buf[0] = 'i';
15384 }
15385 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015388 if (exmode_active)
15389 buf[1] = 'v';
15390 }
15391 else if (exmode_active)
15392 {
15393 buf[0] = 'c';
15394 buf[1] = 'e';
15395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015399 if (finish_op)
15400 buf[1] = 'o';
15401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015403 /* Clear out the minor mode when the argument is not a non-zero number or
15404 * non-empty string. */
15405 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015406 buf[1] = NUL;
15407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_string = vim_strsave(buf);
15409 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410}
15411
Bram Moolenaar429fa852013-04-15 12:27:36 +020015412#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015413/*
15414 * "mzeval()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015418{
15419 char_u *str;
15420 char_u buf[NUMBUFLEN];
15421
15422 str = get_tv_string_buf(&argvars[0], buf);
15423 do_mzeval(str, rettv);
15424}
Bram Moolenaar75676462013-01-30 14:55:42 +010015425
15426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015427mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015428{
15429 typval_T argvars[3];
15430
15431 argvars[0].v_type = VAR_STRING;
15432 argvars[0].vval.v_string = name;
15433 copy_tv(args, &argvars[1]);
15434 argvars[2].v_type = VAR_UNKNOWN;
15435 f_call(argvars, rettv);
15436 clear_tv(&argvars[1]);
15437}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015438#endif
15439
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441 * "nextnonblank()" function
15442 */
15443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015444f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015445{
15446 linenr_T lnum;
15447
15448 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015450 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015451 {
15452 lnum = 0;
15453 break;
15454 }
15455 if (*skipwhite(ml_get(lnum)) != NUL)
15456 break;
15457 }
15458 rettv->vval.v_number = lnum;
15459}
15460
15461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 * "nr2char()" function
15463 */
15464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015465f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466{
15467 char_u buf[NUMBUFLEN];
15468
15469#ifdef FEAT_MBYTE
15470 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015471 {
15472 int utf8 = 0;
15473
15474 if (argvars[1].v_type != VAR_UNKNOWN)
15475 utf8 = get_tv_number_chk(&argvars[1], NULL);
15476 if (utf8)
15477 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15478 else
15479 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 else
15482#endif
15483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 buf[1] = NUL;
15486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->v_type = VAR_STRING;
15488 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015489}
15490
15491/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015492 * "or(expr, expr)" function
15493 */
15494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015495f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015496{
15497 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15498 | get_tv_number_chk(&argvars[1], NULL);
15499}
15500
15501/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015502 * "pathshorten()" function
15503 */
15504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015505f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015506{
15507 char_u *p;
15508
15509 rettv->v_type = VAR_STRING;
15510 p = get_tv_string_chk(&argvars[0]);
15511 if (p == NULL)
15512 rettv->vval.v_string = NULL;
15513 else
15514 {
15515 p = vim_strsave(p);
15516 rettv->vval.v_string = p;
15517 if (p != NULL)
15518 shorten_dir(p);
15519 }
15520}
15521
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015522#ifdef FEAT_PERL
15523/*
15524 * "perleval()" function
15525 */
15526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015528{
15529 char_u *str;
15530 char_u buf[NUMBUFLEN];
15531
15532 str = get_tv_string_buf(&argvars[0], buf);
15533 do_perleval(str, rettv);
15534}
15535#endif
15536
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015537#ifdef FEAT_FLOAT
15538/*
15539 * "pow()" function
15540 */
15541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015542f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015543{
15544 float_T fx, fy;
15545
15546 rettv->v_type = VAR_FLOAT;
15547 if (get_float_arg(argvars, &fx) == OK
15548 && get_float_arg(&argvars[1], &fy) == OK)
15549 rettv->vval.v_float = pow(fx, fy);
15550 else
15551 rettv->vval.v_float = 0.0;
15552}
15553#endif
15554
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556 * "prevnonblank()" function
15557 */
15558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015559f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015560{
15561 linenr_T lnum;
15562
15563 lnum = get_tv_lnum(argvars);
15564 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15565 lnum = 0;
15566 else
15567 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15568 --lnum;
15569 rettv->vval.v_number = lnum;
15570}
15571
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015572/* This dummy va_list is here because:
15573 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15574 * - locally in the function results in a "used before set" warning
15575 * - using va_start() to initialize it gives "function with fixed args" error */
15576static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015577
Bram Moolenaar8c711452005-01-14 21:53:12 +000015578/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015579 * "printf()" function
15580 */
15581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015582f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015583{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015584 char_u buf[NUMBUFLEN];
15585 int len;
15586 char_u *s;
15587 int saved_did_emsg = did_emsg;
15588 char *fmt;
15589
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015590 rettv->v_type = VAR_STRING;
15591 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015592
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015593 /* Get the required length, allocate the buffer and do it for real. */
15594 did_emsg = FALSE;
15595 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15596 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15597 if (!did_emsg)
15598 {
15599 s = alloc(len + 1);
15600 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015601 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015602 rettv->vval.v_string = s;
15603 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015604 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015605 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015606 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015607}
15608
15609/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015610 * "pumvisible()" function
15611 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015613f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015614{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015615#ifdef FEAT_INS_EXPAND
15616 if (pum_visible())
15617 rettv->vval.v_number = 1;
15618#endif
15619}
15620
Bram Moolenaardb913952012-06-29 12:54:53 +020015621#ifdef FEAT_PYTHON3
15622/*
15623 * "py3eval()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015627{
15628 char_u *str;
15629 char_u buf[NUMBUFLEN];
15630
15631 str = get_tv_string_buf(&argvars[0], buf);
15632 do_py3eval(str, rettv);
15633}
15634#endif
15635
15636#ifdef FEAT_PYTHON
15637/*
15638 * "pyeval()" function
15639 */
15640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015641f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015642{
15643 char_u *str;
15644 char_u buf[NUMBUFLEN];
15645
15646 str = get_tv_string_buf(&argvars[0], buf);
15647 do_pyeval(str, rettv);
15648}
15649#endif
15650
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015651/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015652 * "range()" function
15653 */
15654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015655f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015656{
15657 long start;
15658 long end;
15659 long stride = 1;
15660 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015663 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015664 if (argvars[1].v_type == VAR_UNKNOWN)
15665 {
15666 end = start - 1;
15667 start = 0;
15668 }
15669 else
15670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015672 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015674 }
15675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015676 if (error)
15677 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015678 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015679 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015680 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015681 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015682 else
15683 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015684 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015685 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 if (list_append_number(rettv->vval.v_list,
15687 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015688 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015689 }
15690}
15691
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015692/*
15693 * "readfile()" function
15694 */
15695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015696f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015697{
15698 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015699 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015700 char_u *fname;
15701 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015702 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15703 int io_size = sizeof(buf);
15704 int readlen; /* size of last fread() */
15705 char_u *prev = NULL; /* previously read bytes, if any */
15706 long prevlen = 0; /* length of data in prev */
15707 long prevsize = 0; /* size of prev buffer */
15708 long maxline = MAXLNUM;
15709 long cnt = 0;
15710 char_u *p; /* position in buf */
15711 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015712
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015713 if (argvars[1].v_type != VAR_UNKNOWN)
15714 {
15715 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15716 binary = TRUE;
15717 if (argvars[2].v_type != VAR_UNKNOWN)
15718 maxline = get_tv_number(&argvars[2]);
15719 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015720
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015721 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723
15724 /* Always open the file in binary mode, library functions have a mind of
15725 * their own about CR-LF conversion. */
15726 fname = get_tv_string(&argvars[0]);
15727 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15728 {
15729 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15730 return;
15731 }
15732
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015733 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015735 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015736
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015737 /* This for loop processes what was read, but is also entered at end
15738 * of file so that either:
15739 * - an incomplete line gets written
15740 * - a "binary" file gets an empty line at the end if it ends in a
15741 * newline. */
15742 for (p = buf, start = buf;
15743 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15744 ++p)
15745 {
15746 if (*p == '\n' || readlen <= 0)
15747 {
15748 listitem_T *li;
15749 char_u *s = NULL;
15750 long_u len = p - start;
15751
15752 /* Finished a line. Remove CRs before NL. */
15753 if (readlen > 0 && !binary)
15754 {
15755 while (len > 0 && start[len - 1] == '\r')
15756 --len;
15757 /* removal may cross back to the "prev" string */
15758 if (len == 0)
15759 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15760 --prevlen;
15761 }
15762 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015763 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015764 else
15765 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015766 /* Change "prev" buffer to be the right size. This way
15767 * the bytes are only copied once, and very long lines are
15768 * allocated only once. */
15769 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015771 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015772 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015773 prev = NULL; /* the list will own the string */
15774 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775 }
15776 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015777 if (s == NULL)
15778 {
15779 do_outofmem_msg((long_u) prevlen + len + 1);
15780 failed = TRUE;
15781 break;
15782 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015784 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015785 {
15786 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015787 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015788 break;
15789 }
15790 li->li_tv.v_type = VAR_STRING;
15791 li->li_tv.v_lock = 0;
15792 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015793 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015795 start = p + 1; /* step over newline */
15796 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 break;
15798 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 else if (*p == NUL)
15800 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015801#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015802 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15803 * when finding the BF and check the previous two bytes. */
15804 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015805 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15807 * + 1, these may be in the "prev" string. */
15808 char_u back1 = p >= buf + 1 ? p[-1]
15809 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15810 char_u back2 = p >= buf + 2 ? p[-2]
15811 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15812 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015814 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015815 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015816 char_u *dest = p - 2;
15817
15818 /* Usually a BOM is at the beginning of a file, and so at
15819 * the beginning of a line; then we can just step over it.
15820 */
15821 if (start == dest)
15822 start = p + 1;
15823 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015824 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015825 /* have to shuffle buf to close gap */
15826 int adjust_prevlen = 0;
15827
15828 if (dest < buf)
15829 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015830 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015831 dest = buf;
15832 }
15833 if (readlen > p - buf + 1)
15834 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15835 readlen -= 3 - adjust_prevlen;
15836 prevlen -= adjust_prevlen;
15837 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015838 }
15839 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015840 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015841#endif
15842 } /* for */
15843
15844 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15845 break;
15846 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015847 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015848 /* There's part of a line in buf, store it in "prev". */
15849 if (p - start + prevlen >= prevsize)
15850 {
15851 /* need bigger "prev" buffer */
15852 char_u *newprev;
15853
15854 /* A common use case is ordinary text files and "prev" gets a
15855 * fragment of a line, so the first allocation is made
15856 * small, to avoid repeatedly 'allocing' large and
15857 * 'reallocing' small. */
15858 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015859 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015860 else
15861 {
15862 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015863 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015864 prevsize = grow50pc > growmin ? grow50pc : growmin;
15865 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015866 newprev = prev == NULL ? alloc(prevsize)
15867 : vim_realloc(prev, prevsize);
15868 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015869 {
15870 do_outofmem_msg((long_u)prevsize);
15871 failed = TRUE;
15872 break;
15873 }
15874 prev = newprev;
15875 }
15876 /* Add the line part to end of "prev". */
15877 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015878 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015879 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015880 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015881
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015882 /*
15883 * For a negative line count use only the lines at the end of the file,
15884 * free the rest.
15885 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015887 while (cnt > -maxline)
15888 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015889 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015890 --cnt;
15891 }
15892
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015893 if (failed)
15894 {
15895 list_free(rettv->vval.v_list, TRUE);
15896 /* readfile doc says an empty list is returned on error */
15897 rettv->vval.v_list = list_alloc();
15898 }
15899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015900 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015901 fclose(fd);
15902}
15903
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015904#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015905static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015906
15907/*
15908 * Convert a List to proftime_T.
15909 * Return FAIL when there is something wrong.
15910 */
15911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015912list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015913{
15914 long n1, n2;
15915 int error = FALSE;
15916
15917 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15918 || arg->vval.v_list->lv_len != 2)
15919 return FAIL;
15920 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15921 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15922# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015923 tm->HighPart = n1;
15924 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015925# else
15926 tm->tv_sec = n1;
15927 tm->tv_usec = n2;
15928# endif
15929 return error ? FAIL : OK;
15930}
15931#endif /* FEAT_RELTIME */
15932
15933/*
15934 * "reltime()" function
15935 */
15936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015937f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015938{
15939#ifdef FEAT_RELTIME
15940 proftime_T res;
15941 proftime_T start;
15942
15943 if (argvars[0].v_type == VAR_UNKNOWN)
15944 {
15945 /* No arguments: get current time. */
15946 profile_start(&res);
15947 }
15948 else if (argvars[1].v_type == VAR_UNKNOWN)
15949 {
15950 if (list2proftime(&argvars[0], &res) == FAIL)
15951 return;
15952 profile_end(&res);
15953 }
15954 else
15955 {
15956 /* Two arguments: compute the difference. */
15957 if (list2proftime(&argvars[0], &start) == FAIL
15958 || list2proftime(&argvars[1], &res) == FAIL)
15959 return;
15960 profile_sub(&res, &start);
15961 }
15962
15963 if (rettv_list_alloc(rettv) == OK)
15964 {
15965 long n1, n2;
15966
15967# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015968 n1 = res.HighPart;
15969 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015970# else
15971 n1 = res.tv_sec;
15972 n2 = res.tv_usec;
15973# endif
15974 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15975 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15976 }
15977#endif
15978}
15979
15980/*
15981 * "reltimestr()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015985{
15986#ifdef FEAT_RELTIME
15987 proftime_T tm;
15988#endif
15989
15990 rettv->v_type = VAR_STRING;
15991 rettv->vval.v_string = NULL;
15992#ifdef FEAT_RELTIME
15993 if (list2proftime(&argvars[0], &tm) == OK)
15994 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15995#endif
15996}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015997
Bram Moolenaar0d660222005-01-07 21:51:51 +000015998#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015999static void make_connection(void);
16000static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016001
16002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016003make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016004{
16005 if (X_DISPLAY == NULL
16006# ifdef FEAT_GUI
16007 && !gui.in_use
16008# endif
16009 )
16010 {
16011 x_force_connect = TRUE;
16012 setup_term_clip();
16013 x_force_connect = FALSE;
16014 }
16015}
16016
16017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016018check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016019{
16020 make_connection();
16021 if (X_DISPLAY == NULL)
16022 {
16023 EMSG(_("E240: No connection to Vim server"));
16024 return FAIL;
16025 }
16026 return OK;
16027}
16028#endif
16029
16030#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016031static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032
16033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016034remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035{
16036 char_u *server_name;
16037 char_u *keys;
16038 char_u *r = NULL;
16039 char_u buf[NUMBUFLEN];
16040# ifdef WIN32
16041 HWND w;
16042# else
16043 Window w;
16044# endif
16045
16046 if (check_restricted() || check_secure())
16047 return;
16048
16049# ifdef FEAT_X11
16050 if (check_connection() == FAIL)
16051 return;
16052# endif
16053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016054 server_name = get_tv_string_chk(&argvars[0]);
16055 if (server_name == NULL)
16056 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016057 keys = get_tv_string_buf(&argvars[1], buf);
16058# ifdef WIN32
16059 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16060# else
16061 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16062 < 0)
16063# endif
16064 {
16065 if (r != NULL)
16066 EMSG(r); /* sending worked but evaluation failed */
16067 else
16068 EMSG2(_("E241: Unable to send to %s"), server_name);
16069 return;
16070 }
16071
16072 rettv->vval.v_string = r;
16073
16074 if (argvars[2].v_type != VAR_UNKNOWN)
16075 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016076 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016077 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016078 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016079
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016080 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 v.di_tv.v_type = VAR_STRING;
16082 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 idvar = get_tv_string_chk(&argvars[2]);
16084 if (idvar != NULL)
16085 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 }
16088}
16089#endif
16090
16091/*
16092 * "remote_expr()" function
16093 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016095f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096{
16097 rettv->v_type = VAR_STRING;
16098 rettv->vval.v_string = NULL;
16099#ifdef FEAT_CLIENTSERVER
16100 remote_common(argvars, rettv, TRUE);
16101#endif
16102}
16103
16104/*
16105 * "remote_foreground()" function
16106 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016108f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110#ifdef FEAT_CLIENTSERVER
16111# ifdef WIN32
16112 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016113 {
16114 char_u *server_name = get_tv_string_chk(&argvars[0]);
16115
16116 if (server_name != NULL)
16117 serverForeground(server_name);
16118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119# else
16120 /* Send a foreground() expression to the server. */
16121 argvars[1].v_type = VAR_STRING;
16122 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16123 argvars[2].v_type = VAR_UNKNOWN;
16124 remote_common(argvars, rettv, TRUE);
16125 vim_free(argvars[1].vval.v_string);
16126# endif
16127#endif
16128}
16129
Bram Moolenaar0d660222005-01-07 21:51:51 +000016130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016131f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132{
16133#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016134 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016135 char_u *s = NULL;
16136# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016137 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140
16141 if (check_restricted() || check_secure())
16142 {
16143 rettv->vval.v_number = -1;
16144 return;
16145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 serverid = get_tv_string_chk(&argvars[0]);
16147 if (serverid == NULL)
16148 {
16149 rettv->vval.v_number = -1;
16150 return; /* type error; errmsg already given */
16151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016153 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154 if (n == 0)
16155 rettv->vval.v_number = -1;
16156 else
16157 {
16158 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16159 rettv->vval.v_number = (s != NULL);
16160 }
16161# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 if (check_connection() == FAIL)
16163 return;
16164
16165 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016166 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167# endif
16168
16169 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016171 char_u *retvar;
16172
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 v.di_tv.v_type = VAR_STRING;
16174 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016175 retvar = get_tv_string_chk(&argvars[1]);
16176 if (retvar != NULL)
16177 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179 }
16180#else
16181 rettv->vval.v_number = -1;
16182#endif
16183}
16184
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016186f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187{
16188 char_u *r = NULL;
16189
16190#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016191 char_u *serverid = get_tv_string_chk(&argvars[0]);
16192
16193 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194 {
16195# ifdef WIN32
16196 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016197 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016199 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 if (n != 0)
16201 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16202 if (r == NULL)
16203# else
16204 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016205 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016206# endif
16207 EMSG(_("E277: Unable to read a server reply"));
16208 }
16209#endif
16210 rettv->v_type = VAR_STRING;
16211 rettv->vval.v_string = r;
16212}
16213
16214/*
16215 * "remote_send()" function
16216 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016218f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219{
16220 rettv->v_type = VAR_STRING;
16221 rettv->vval.v_string = NULL;
16222#ifdef FEAT_CLIENTSERVER
16223 remote_common(argvars, rettv, FALSE);
16224#endif
16225}
16226
16227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016228 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016229 */
16230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016231f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016232{
Bram Moolenaar33570922005-01-25 22:26:29 +000016233 list_T *l;
16234 listitem_T *item, *item2;
16235 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016236 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016237 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016238 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 dict_T *d;
16240 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016241 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016242
Bram Moolenaar8c711452005-01-14 21:53:12 +000016243 if (argvars[0].v_type == VAR_DICT)
16244 {
16245 if (argvars[2].v_type != VAR_UNKNOWN)
16246 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016247 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016248 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 key = get_tv_string_chk(&argvars[1]);
16251 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 di = dict_find(d, key, -1);
16254 if (di == NULL)
16255 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016256 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16257 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 {
16259 *rettv = di->di_tv;
16260 init_tv(&di->di_tv);
16261 dictitem_remove(d, di);
16262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016264 }
16265 }
16266 else if (argvars[0].v_type != VAR_LIST)
16267 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016268 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016269 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016271 int error = FALSE;
16272
16273 idx = get_tv_number_chk(&argvars[1], &error);
16274 if (error)
16275 ; /* type error: do nothing, errmsg already given */
16276 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277 EMSGN(_(e_listidx), idx);
16278 else
16279 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016280 if (argvars[2].v_type == VAR_UNKNOWN)
16281 {
16282 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016283 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016284 *rettv = item->li_tv;
16285 vim_free(item);
16286 }
16287 else
16288 {
16289 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016290 end = get_tv_number_chk(&argvars[2], &error);
16291 if (error)
16292 ; /* type error: do nothing */
16293 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016294 EMSGN(_(e_listidx), end);
16295 else
16296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016297 int cnt = 0;
16298
16299 for (li = item; li != NULL; li = li->li_next)
16300 {
16301 ++cnt;
16302 if (li == item2)
16303 break;
16304 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016305 if (li == NULL) /* didn't find "item2" after "item" */
16306 EMSG(_(e_invrange));
16307 else
16308 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016309 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016310 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016311 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016312 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016313 l->lv_first = item;
16314 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016315 item->li_prev = NULL;
16316 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016317 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016318 }
16319 }
16320 }
16321 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016322 }
16323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324}
16325
16326/*
16327 * "rename({from}, {to})" function
16328 */
16329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331{
16332 char_u buf[NUMBUFLEN];
16333
16334 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16338 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339}
16340
16341/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016342 * "repeat()" function
16343 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016345f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016346{
16347 char_u *p;
16348 int n;
16349 int slen;
16350 int len;
16351 char_u *r;
16352 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016353
16354 n = get_tv_number(&argvars[1]);
16355 if (argvars[0].v_type == VAR_LIST)
16356 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016357 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016358 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016359 if (list_extend(rettv->vval.v_list,
16360 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016361 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016362 }
16363 else
16364 {
16365 p = get_tv_string(&argvars[0]);
16366 rettv->v_type = VAR_STRING;
16367 rettv->vval.v_string = NULL;
16368
16369 slen = (int)STRLEN(p);
16370 len = slen * n;
16371 if (len <= 0)
16372 return;
16373
16374 r = alloc(len + 1);
16375 if (r != NULL)
16376 {
16377 for (i = 0; i < n; i++)
16378 mch_memmove(r + i * slen, p, (size_t)slen);
16379 r[len] = NUL;
16380 }
16381
16382 rettv->vval.v_string = r;
16383 }
16384}
16385
16386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 * "resolve()" function
16388 */
16389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391{
16392 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016393#ifdef HAVE_READLINK
16394 char_u *buf = NULL;
16395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016397 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398#ifdef FEAT_SHORTCUT
16399 {
16400 char_u *v = NULL;
16401
16402 v = mch_resolve_shortcut(p);
16403 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016406 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 }
16408#else
16409# ifdef HAVE_READLINK
16410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 char_u *cpy;
16412 int len;
16413 char_u *remain = NULL;
16414 char_u *q;
16415 int is_relative_to_current = FALSE;
16416 int has_trailing_pathsep = FALSE;
16417 int limit = 100;
16418
16419 p = vim_strsave(p);
16420
16421 if (p[0] == '.' && (vim_ispathsep(p[1])
16422 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16423 is_relative_to_current = TRUE;
16424
16425 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016426 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016429 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431
16432 q = getnextcomp(p);
16433 if (*q != NUL)
16434 {
16435 /* Separate the first path component in "p", and keep the
16436 * remainder (beginning with the path separator). */
16437 remain = vim_strsave(q - 1);
16438 q[-1] = NUL;
16439 }
16440
Bram Moolenaard9462e32011-04-11 21:35:11 +020016441 buf = alloc(MAXPATHL + 1);
16442 if (buf == NULL)
16443 goto fail;
16444
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 for (;;)
16446 {
16447 for (;;)
16448 {
16449 len = readlink((char *)p, (char *)buf, MAXPATHL);
16450 if (len <= 0)
16451 break;
16452 buf[len] = NUL;
16453
16454 if (limit-- == 0)
16455 {
16456 vim_free(p);
16457 vim_free(remain);
16458 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 goto fail;
16461 }
16462
16463 /* Ensure that the result will have a trailing path separator
16464 * if the argument has one. */
16465 if (remain == NULL && has_trailing_pathsep)
16466 add_pathsep(buf);
16467
16468 /* Separate the first path component in the link value and
16469 * concatenate the remainders. */
16470 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16471 if (*q != NUL)
16472 {
16473 if (remain == NULL)
16474 remain = vim_strsave(q - 1);
16475 else
16476 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016477 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 if (cpy != NULL)
16479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 vim_free(remain);
16481 remain = cpy;
16482 }
16483 }
16484 q[-1] = NUL;
16485 }
16486
16487 q = gettail(p);
16488 if (q > p && *q == NUL)
16489 {
16490 /* Ignore trailing path separator. */
16491 q[-1] = NUL;
16492 q = gettail(p);
16493 }
16494 if (q > p && !mch_isFullName(buf))
16495 {
16496 /* symlink is relative to directory of argument */
16497 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16498 if (cpy != NULL)
16499 {
16500 STRCPY(cpy, p);
16501 STRCPY(gettail(cpy), buf);
16502 vim_free(p);
16503 p = cpy;
16504 }
16505 }
16506 else
16507 {
16508 vim_free(p);
16509 p = vim_strsave(buf);
16510 }
16511 }
16512
16513 if (remain == NULL)
16514 break;
16515
16516 /* Append the first path component of "remain" to "p". */
16517 q = getnextcomp(remain + 1);
16518 len = q - remain - (*q != NUL);
16519 cpy = vim_strnsave(p, STRLEN(p) + len);
16520 if (cpy != NULL)
16521 {
16522 STRNCAT(cpy, remain, len);
16523 vim_free(p);
16524 p = cpy;
16525 }
16526 /* Shorten "remain". */
16527 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016528 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 else
16530 {
16531 vim_free(remain);
16532 remain = NULL;
16533 }
16534 }
16535
16536 /* If the result is a relative path name, make it explicitly relative to
16537 * the current directory if and only if the argument had this form. */
16538 if (!vim_ispathsep(*p))
16539 {
16540 if (is_relative_to_current
16541 && *p != NUL
16542 && !(p[0] == '.'
16543 && (p[1] == NUL
16544 || vim_ispathsep(p[1])
16545 || (p[1] == '.'
16546 && (p[2] == NUL
16547 || vim_ispathsep(p[2]))))))
16548 {
16549 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016550 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 if (cpy != NULL)
16552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 vim_free(p);
16554 p = cpy;
16555 }
16556 }
16557 else if (!is_relative_to_current)
16558 {
16559 /* Strip leading "./". */
16560 q = p;
16561 while (q[0] == '.' && vim_ispathsep(q[1]))
16562 q += 2;
16563 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016564 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 }
16566 }
16567
16568 /* Ensure that the result will have no trailing path separator
16569 * if the argument had none. But keep "/" or "//". */
16570 if (!has_trailing_pathsep)
16571 {
16572 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016573 if (after_pathsep(p, q))
16574 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 }
16576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 }
16579# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016580 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581# endif
16582#endif
16583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585
16586#ifdef HAVE_READLINK
16587fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016588 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591}
16592
16593/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 */
16596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 list_T *l;
16600 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 if (argvars[0].v_type != VAR_LIST)
16603 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016604 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016605 && !tv_check_lock(l->lv_lock,
16606 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 {
16608 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016609 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016610 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016611 while (li != NULL)
16612 {
16613 ni = li->li_prev;
16614 list_append(l, li);
16615 li = ni;
16616 }
16617 rettv->vval.v_list = l;
16618 rettv->v_type = VAR_LIST;
16619 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016620 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622}
16623
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016624#define SP_NOMOVE 0x01 /* don't move cursor */
16625#define SP_REPEAT 0x02 /* repeat to find outer pair */
16626#define SP_RETCOUNT 0x04 /* return matchcount */
16627#define SP_SETPCMARK 0x08 /* set previous context mark */
16628#define SP_START 0x10 /* accept match at start position */
16629#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16630#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016631#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016632
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016633static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016634
16635/*
16636 * Get flags for a search function.
16637 * Possibly sets "p_ws".
16638 * Returns BACKWARD, FORWARD or zero (for an error).
16639 */
16640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016641get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642{
16643 int dir = FORWARD;
16644 char_u *flags;
16645 char_u nbuf[NUMBUFLEN];
16646 int mask;
16647
16648 if (varp->v_type != VAR_UNKNOWN)
16649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 flags = get_tv_string_buf_chk(varp, nbuf);
16651 if (flags == NULL)
16652 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653 while (*flags != NUL)
16654 {
16655 switch (*flags)
16656 {
16657 case 'b': dir = BACKWARD; break;
16658 case 'w': p_ws = TRUE; break;
16659 case 'W': p_ws = FALSE; break;
16660 default: mask = 0;
16661 if (flagsp != NULL)
16662 switch (*flags)
16663 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016664 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016665 case 'e': mask = SP_END; break;
16666 case 'm': mask = SP_RETCOUNT; break;
16667 case 'n': mask = SP_NOMOVE; break;
16668 case 'p': mask = SP_SUBPAT; break;
16669 case 'r': mask = SP_REPEAT; break;
16670 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016671 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016672 }
16673 if (mask == 0)
16674 {
16675 EMSG2(_(e_invarg2), flags);
16676 dir = 0;
16677 }
16678 else
16679 *flagsp |= mask;
16680 }
16681 if (dir == 0)
16682 break;
16683 ++flags;
16684 }
16685 }
16686 return dir;
16687}
16688
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016690 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016693search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016695 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 char_u *pat;
16697 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016698 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 int save_p_ws = p_ws;
16700 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016701 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016702 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016703 proftime_T tm;
16704#ifdef FEAT_RELTIME
16705 long time_limit = 0;
16706#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016707 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016708 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016711 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016712 if (dir == 0)
16713 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016714 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016715 if (flags & SP_START)
16716 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016717 if (flags & SP_END)
16718 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016719 if (flags & SP_COLUMN)
16720 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016721
Bram Moolenaar76929292008-01-06 19:07:36 +000016722 /* Optional arguments: line number to stop searching and timeout. */
16723 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016724 {
16725 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16726 if (lnum_stop < 0)
16727 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016728#ifdef FEAT_RELTIME
16729 if (argvars[3].v_type != VAR_UNKNOWN)
16730 {
16731 time_limit = get_tv_number_chk(&argvars[3], NULL);
16732 if (time_limit < 0)
16733 goto theend;
16734 }
16735#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016736 }
16737
Bram Moolenaar76929292008-01-06 19:07:36 +000016738#ifdef FEAT_RELTIME
16739 /* Set the time limit, if there is one. */
16740 profile_setlimit(time_limit, &tm);
16741#endif
16742
Bram Moolenaar231334e2005-07-25 20:46:57 +000016743 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016744 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016745 * Check to make sure only those flags are set.
16746 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16747 * flags cannot be set. Check for that condition also.
16748 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016749 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016750 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016752 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016753 goto theend;
16754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016756 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016757 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016758 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016759 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016761 if (flags & SP_SUBPAT)
16762 retval = subpatnum;
16763 else
16764 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016765 if (flags & SP_SETPCMARK)
16766 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016768 if (match_pos != NULL)
16769 {
16770 /* Store the match cursor position */
16771 match_pos->lnum = pos.lnum;
16772 match_pos->col = pos.col + 1;
16773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 /* "/$" will put the cursor after the end of the line, may need to
16775 * correct that here */
16776 check_cursor();
16777 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016778
16779 /* If 'n' flag is used: restore cursor position. */
16780 if (flags & SP_NOMOVE)
16781 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016782 else
16783 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016784theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016786
16787 return retval;
16788}
16789
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016790#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016791
16792/*
16793 * round() is not in C90, use ceil() or floor() instead.
16794 */
16795 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016796vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016797{
16798 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16799}
16800
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016801/*
16802 * "round({float})" function
16803 */
16804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016805f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016806{
16807 float_T f;
16808
16809 rettv->v_type = VAR_FLOAT;
16810 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016811 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016812 else
16813 rettv->vval.v_float = 0.0;
16814}
16815#endif
16816
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016817/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016818 * "screenattr()" function
16819 */
16820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016821f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016822{
16823 int row;
16824 int col;
16825 int c;
16826
16827 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16828 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16829 if (row < 0 || row >= screen_Rows
16830 || col < 0 || col >= screen_Columns)
16831 c = -1;
16832 else
16833 c = ScreenAttrs[LineOffset[row] + col];
16834 rettv->vval.v_number = c;
16835}
16836
16837/*
16838 * "screenchar()" function
16839 */
16840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016841f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016842{
16843 int row;
16844 int col;
16845 int off;
16846 int c;
16847
16848 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16849 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16850 if (row < 0 || row >= screen_Rows
16851 || col < 0 || col >= screen_Columns)
16852 c = -1;
16853 else
16854 {
16855 off = LineOffset[row] + col;
16856#ifdef FEAT_MBYTE
16857 if (enc_utf8 && ScreenLinesUC[off] != 0)
16858 c = ScreenLinesUC[off];
16859 else
16860#endif
16861 c = ScreenLines[off];
16862 }
16863 rettv->vval.v_number = c;
16864}
16865
16866/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016867 * "screencol()" function
16868 *
16869 * First column is 1 to be consistent with virtcol().
16870 */
16871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016872f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016873{
16874 rettv->vval.v_number = screen_screencol() + 1;
16875}
16876
16877/*
16878 * "screenrow()" function
16879 */
16880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016881f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016882{
16883 rettv->vval.v_number = screen_screenrow() + 1;
16884}
16885
16886/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016887 * "search()" function
16888 */
16889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016890f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016891{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016892 int flags = 0;
16893
16894 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895}
16896
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016898 * "searchdecl()" function
16899 */
16900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016901f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016902{
16903 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016904 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016905 int error = FALSE;
16906 char_u *name;
16907
16908 rettv->vval.v_number = 1; /* default: FAIL */
16909
16910 name = get_tv_string_chk(&argvars[0]);
16911 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016912 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016913 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016914 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16915 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16916 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016917 if (!error && name != NULL)
16918 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016919 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016920}
16921
16922/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016923 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016926searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
16928 char_u *spat, *mpat, *epat;
16929 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 int dir;
16932 int flags = 0;
16933 char_u nbuf1[NUMBUFLEN];
16934 char_u nbuf2[NUMBUFLEN];
16935 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016936 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016937 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016938 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016941 spat = get_tv_string_chk(&argvars[0]);
16942 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16943 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16944 if (spat == NULL || mpat == NULL || epat == NULL)
16945 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 /* Handle the optional fourth argument: flags */
16948 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016949 if (dir == 0)
16950 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016951
16952 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016953 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16954 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016955 if ((flags & (SP_END | SP_SUBPAT)) != 0
16956 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016957 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016958 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016959 goto theend;
16960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016962 /* Using 'r' implies 'W', otherwise it doesn't work. */
16963 if (flags & SP_REPEAT)
16964 p_ws = FALSE;
16965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016966 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016967 if (argvars[3].v_type == VAR_UNKNOWN
16968 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 skip = (char_u *)"";
16970 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016972 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016973 if (argvars[5].v_type != VAR_UNKNOWN)
16974 {
16975 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16976 if (lnum_stop < 0)
16977 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016978#ifdef FEAT_RELTIME
16979 if (argvars[6].v_type != VAR_UNKNOWN)
16980 {
16981 time_limit = get_tv_number_chk(&argvars[6], NULL);
16982 if (time_limit < 0)
16983 goto theend;
16984 }
16985#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016986 }
16987 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 if (skip == NULL)
16989 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016991 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016992 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016993
16994theend:
16995 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016996
16997 return retval;
16998}
16999
17000/*
17001 * "searchpair()" function
17002 */
17003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017004f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017005{
17006 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17007}
17008
17009/*
17010 * "searchpairpos()" function
17011 */
17012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017013f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017014{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017015 pos_T match_pos;
17016 int lnum = 0;
17017 int col = 0;
17018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017019 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017020 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017021
17022 if (searchpair_cmn(argvars, &match_pos) > 0)
17023 {
17024 lnum = match_pos.lnum;
17025 col = match_pos.col;
17026 }
17027
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017028 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17029 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017030}
17031
17032/*
17033 * Search for a start/middle/end thing.
17034 * Used by searchpair(), see its documentation for the details.
17035 * Returns 0 or -1 for no match,
17036 */
17037 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017038do_searchpair(
17039 char_u *spat, /* start pattern */
17040 char_u *mpat, /* middle pattern */
17041 char_u *epat, /* end pattern */
17042 int dir, /* BACKWARD or FORWARD */
17043 char_u *skip, /* skip expression */
17044 int flags, /* SP_SETPCMARK and other SP_ values */
17045 pos_T *match_pos,
17046 linenr_T lnum_stop, /* stop at this line if not zero */
17047 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017048{
17049 char_u *save_cpo;
17050 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17051 long retval = 0;
17052 pos_T pos;
17053 pos_T firstpos;
17054 pos_T foundpos;
17055 pos_T save_cursor;
17056 pos_T save_pos;
17057 int n;
17058 int r;
17059 int nest = 1;
17060 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017061 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017062 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017063
17064 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17065 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017066 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017067
Bram Moolenaar76929292008-01-06 19:07:36 +000017068#ifdef FEAT_RELTIME
17069 /* Set the time limit, if there is one. */
17070 profile_setlimit(time_limit, &tm);
17071#endif
17072
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017073 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17074 * start/middle/end (pat3, for the top pair). */
17075 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17076 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17077 if (pat2 == NULL || pat3 == NULL)
17078 goto theend;
17079 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17080 if (*mpat == NUL)
17081 STRCPY(pat3, pat2);
17082 else
17083 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17084 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017085 if (flags & SP_START)
17086 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017087
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 save_cursor = curwin->w_cursor;
17089 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017090 clearpos(&firstpos);
17091 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 pat = pat3;
17093 for (;;)
17094 {
17095 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017096 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17098 /* didn't find it or found the first match again: FAIL */
17099 break;
17100
17101 if (firstpos.lnum == 0)
17102 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017103 if (equalpos(pos, foundpos))
17104 {
17105 /* Found the same position again. Can happen with a pattern that
17106 * has "\zs" at the end and searching backwards. Advance one
17107 * character and try again. */
17108 if (dir == BACKWARD)
17109 decl(&pos);
17110 else
17111 incl(&pos);
17112 }
17113 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017115 /* clear the start flag to avoid getting stuck here */
17116 options &= ~SEARCH_START;
17117
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 /* If the skip pattern matches, ignore this match. */
17119 if (*skip != NUL)
17120 {
17121 save_pos = curwin->w_cursor;
17122 curwin->w_cursor = pos;
17123 r = eval_to_bool(skip, &err, NULL, FALSE);
17124 curwin->w_cursor = save_pos;
17125 if (err)
17126 {
17127 /* Evaluating {skip} caused an error, break here. */
17128 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017129 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 break;
17131 }
17132 if (r)
17133 continue;
17134 }
17135
17136 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17137 {
17138 /* Found end when searching backwards or start when searching
17139 * forward: nested pair. */
17140 ++nest;
17141 pat = pat2; /* nested, don't search for middle */
17142 }
17143 else
17144 {
17145 /* Found end when searching forward or start when searching
17146 * backward: end of (nested) pair; or found middle in outer pair. */
17147 if (--nest == 1)
17148 pat = pat3; /* outer level, search for middle */
17149 }
17150
17151 if (nest == 0)
17152 {
17153 /* Found the match: return matchcount or line number. */
17154 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017155 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017157 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017158 if (flags & SP_SETPCMARK)
17159 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 curwin->w_cursor = pos;
17161 if (!(flags & SP_REPEAT))
17162 break;
17163 nest = 1; /* search for next unmatched */
17164 }
17165 }
17166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017167 if (match_pos != NULL)
17168 {
17169 /* Store the match cursor position */
17170 match_pos->lnum = curwin->w_cursor.lnum;
17171 match_pos->col = curwin->w_cursor.col + 1;
17172 }
17173
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017175 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 curwin->w_cursor = save_cursor;
17177
17178theend:
17179 vim_free(pat2);
17180 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017181 if (p_cpo == empty_option)
17182 p_cpo = save_cpo;
17183 else
17184 /* Darn, evaluating the {skip} expression changed the value. */
17185 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017186
17187 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188}
17189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017190/*
17191 * "searchpos()" function
17192 */
17193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017195{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196 pos_T match_pos;
17197 int lnum = 0;
17198 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017199 int n;
17200 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017203 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017204
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017205 n = search_cmn(argvars, &match_pos, &flags);
17206 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017207 {
17208 lnum = match_pos.lnum;
17209 col = match_pos.col;
17210 }
17211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017212 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17213 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017214 if (flags & SP_SUBPAT)
17215 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017216}
17217
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017219f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221#ifdef FEAT_CLIENTSERVER
17222 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017223 char_u *server = get_tv_string_chk(&argvars[0]);
17224 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017227 if (server == NULL || reply == NULL)
17228 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229 if (check_restricted() || check_secure())
17230 return;
17231# ifdef FEAT_X11
17232 if (check_connection() == FAIL)
17233 return;
17234# endif
17235
17236 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017238 EMSG(_("E258: Unable to send to client"));
17239 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 rettv->vval.v_number = 0;
17242#else
17243 rettv->vval.v_number = -1;
17244#endif
17245}
17246
Bram Moolenaar0d660222005-01-07 21:51:51 +000017247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017248f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249{
17250 char_u *r = NULL;
17251
17252#ifdef FEAT_CLIENTSERVER
17253# ifdef WIN32
17254 r = serverGetVimNames();
17255# else
17256 make_connection();
17257 if (X_DISPLAY != NULL)
17258 r = serverGetVimNames(X_DISPLAY);
17259# endif
17260#endif
17261 rettv->v_type = VAR_STRING;
17262 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263}
17264
17265/*
17266 * "setbufvar()" function
17267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270{
17271 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 char_u nbuf[NUMBUFLEN];
17276
17277 if (check_restricted() || check_secure())
17278 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17280 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017281 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 varp = &argvars[2];
17283
17284 if (buf != NULL && varname != NULL && varp != NULL)
17285 {
17286 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
17289 if (*varname == '&')
17290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 long numval;
17292 char_u *strval;
17293 int error = FALSE;
17294
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 numval = get_tv_number_chk(varp, &error);
17297 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 if (!error && strval != NULL)
17299 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
17301 else
17302 {
17303 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17304 if (bufvarname != NULL)
17305 {
17306 STRCPY(bufvarname, "b:");
17307 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017308 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 vim_free(bufvarname);
17310 }
17311 }
17312
17313 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316}
17317
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017319f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017320{
17321 dict_T *d;
17322 dictitem_T *di;
17323 char_u *csearch;
17324
17325 if (argvars[0].v_type != VAR_DICT)
17326 {
17327 EMSG(_(e_dictreq));
17328 return;
17329 }
17330
17331 if ((d = argvars[0].vval.v_dict) != NULL)
17332 {
17333 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17334 if (csearch != NULL)
17335 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017336#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017337 if (enc_utf8)
17338 {
17339 int pcc[MAX_MCO];
17340 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017341
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017342 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17343 }
17344 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017345#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017346 set_last_csearch(PTR2CHAR(csearch),
17347 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017348 }
17349
17350 di = dict_find(d, (char_u *)"forward", -1);
17351 if (di != NULL)
17352 set_csearch_direction(get_tv_number(&di->di_tv)
17353 ? FORWARD : BACKWARD);
17354
17355 di = dict_find(d, (char_u *)"until", -1);
17356 if (di != NULL)
17357 set_csearch_until(!!get_tv_number(&di->di_tv));
17358 }
17359}
17360
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361/*
17362 * "setcmdpos()" function
17363 */
17364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017365f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 int pos = (int)get_tv_number(&argvars[0]) - 1;
17368
17369 if (pos >= 0)
17370 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371}
17372
17373/*
17374 * "setline()" function
17375 */
17376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017377f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
17379 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017380 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017381 list_T *l = NULL;
17382 listitem_T *li = NULL;
17383 long added = 0;
17384 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017386 lnum = get_tv_lnum(&argvars[0]);
17387 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017389 l = argvars[1].vval.v_list;
17390 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017392 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017393 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017394
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017395 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017396 for (;;)
17397 {
17398 if (l != NULL)
17399 {
17400 /* list argument, get next string */
17401 if (li == NULL)
17402 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017403 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017404 li = li->li_next;
17405 }
17406
17407 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017408 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017409 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017410
17411 /* When coming here from Insert mode, sync undo, so that this can be
17412 * undone separately from what was previously inserted. */
17413 if (u_sync_once == 2)
17414 {
17415 u_sync_once = 1; /* notify that u_sync() was called */
17416 u_sync(TRUE);
17417 }
17418
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017419 if (lnum <= curbuf->b_ml.ml_line_count)
17420 {
17421 /* existing line, replace it */
17422 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17423 {
17424 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017425 if (lnum == curwin->w_cursor.lnum)
17426 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017427 rettv->vval.v_number = 0; /* OK */
17428 }
17429 }
17430 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17431 {
17432 /* lnum is one past the last line, append the line */
17433 ++added;
17434 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17435 rettv->vval.v_number = 0; /* OK */
17436 }
17437
17438 if (l == NULL) /* only one string argument */
17439 break;
17440 ++lnum;
17441 }
17442
17443 if (added > 0)
17444 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445}
17446
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017447static 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 +000017448
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017450 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017451 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017453set_qf_ll_list(
17454 win_T *wp UNUSED,
17455 typval_T *list_arg UNUSED,
17456 typval_T *action_arg UNUSED,
17457 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017458{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017459#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017460 char_u *act;
17461 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017462#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017463
Bram Moolenaar2641f772005-03-25 21:58:17 +000017464 rettv->vval.v_number = -1;
17465
17466#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017467 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017468 EMSG(_(e_listreq));
17469 else
17470 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017471 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017472
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017473 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017474 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017475 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017476 if (act == NULL)
17477 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017478 if (*act == 'a' || *act == 'r')
17479 action = *act;
17480 }
17481
Bram Moolenaar81484f42012-12-05 15:16:47 +010017482 if (l != NULL && set_errorlist(wp, l, action,
17483 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017484 rettv->vval.v_number = 0;
17485 }
17486#endif
17487}
17488
17489/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017490 * "setloclist()" function
17491 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017493f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017494{
17495 win_T *win;
17496
17497 rettv->vval.v_number = -1;
17498
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017499 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017500 if (win != NULL)
17501 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17502}
17503
17504/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017505 * "setmatches()" function
17506 */
17507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017508f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017509{
17510#ifdef FEAT_SEARCH_EXTRA
17511 list_T *l;
17512 listitem_T *li;
17513 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017514 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017515
17516 rettv->vval.v_number = -1;
17517 if (argvars[0].v_type != VAR_LIST)
17518 {
17519 EMSG(_(e_listreq));
17520 return;
17521 }
17522 if ((l = argvars[0].vval.v_list) != NULL)
17523 {
17524
17525 /* To some extent make sure that we are dealing with a list from
17526 * "getmatches()". */
17527 li = l->lv_first;
17528 while (li != NULL)
17529 {
17530 if (li->li_tv.v_type != VAR_DICT
17531 || (d = li->li_tv.vval.v_dict) == NULL)
17532 {
17533 EMSG(_(e_invarg));
17534 return;
17535 }
17536 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017537 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17538 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017539 && dict_find(d, (char_u *)"priority", -1) != NULL
17540 && dict_find(d, (char_u *)"id", -1) != NULL))
17541 {
17542 EMSG(_(e_invarg));
17543 return;
17544 }
17545 li = li->li_next;
17546 }
17547
17548 clear_matches(curwin);
17549 li = l->lv_first;
17550 while (li != NULL)
17551 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017552 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017553 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017554 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017555 char_u *group;
17556 int priority;
17557 int id;
17558 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017559
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017560 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017561 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17562 {
17563 if (s == NULL)
17564 {
17565 s = list_alloc();
17566 if (s == NULL)
17567 return;
17568 }
17569
17570 /* match from matchaddpos() */
17571 for (i = 1; i < 9; i++)
17572 {
17573 sprintf((char *)buf, (char *)"pos%d", i);
17574 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17575 {
17576 if (di->di_tv.v_type != VAR_LIST)
17577 return;
17578
17579 list_append_tv(s, &di->di_tv);
17580 s->lv_refcount++;
17581 }
17582 else
17583 break;
17584 }
17585 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017586
17587 group = get_dict_string(d, (char_u *)"group", FALSE);
17588 priority = (int)get_dict_number(d, (char_u *)"priority");
17589 id = (int)get_dict_number(d, (char_u *)"id");
17590 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17591 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17592 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017593 if (i == 0)
17594 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017595 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017596 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017597 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017598 }
17599 else
17600 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017601 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017602 list_unref(s);
17603 s = NULL;
17604 }
17605
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017606 li = li->li_next;
17607 }
17608 rettv->vval.v_number = 0;
17609 }
17610#endif
17611}
17612
17613/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017614 * "setpos()" function
17615 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017617f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017618{
17619 pos_T pos;
17620 int fnum;
17621 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017622 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017623
Bram Moolenaar08250432008-02-13 11:42:46 +000017624 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017625 name = get_tv_string_chk(argvars);
17626 if (name != NULL)
17627 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017628 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017629 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017630 if (--pos.col < 0)
17631 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017632 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017633 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017634 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017635 if (fnum == curbuf->b_fnum)
17636 {
17637 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017638 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017639 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017640 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017641 curwin->w_set_curswant = FALSE;
17642 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017643 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017644 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017645 }
17646 else
17647 EMSG(_(e_invarg));
17648 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017649 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17650 {
17651 /* set mark */
17652 if (setmark_pos(name[1], &pos, fnum) == OK)
17653 rettv->vval.v_number = 0;
17654 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017655 else
17656 EMSG(_(e_invarg));
17657 }
17658 }
17659}
17660
17661/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017662 * "setqflist()" function
17663 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017665f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017666{
17667 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17668}
17669
17670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 * "setreg()" function
17672 */
17673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017674f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675{
17676 int regname;
17677 char_u *strregname;
17678 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 int append;
17681 char_u yank_type;
17682 long block_len;
17683
17684 block_len = -1;
17685 yank_type = MAUTO;
17686 append = FALSE;
17687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017691 if (strregname == NULL)
17692 return; /* type error; errmsg already given */
17693 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 if (regname == 0 || regname == '@')
17695 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699 stropt = get_tv_string_chk(&argvars[2]);
17700 if (stropt == NULL)
17701 return; /* type error */
17702 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 switch (*stropt)
17704 {
17705 case 'a': case 'A': /* append */
17706 append = TRUE;
17707 break;
17708 case 'v': case 'c': /* character-wise selection */
17709 yank_type = MCHAR;
17710 break;
17711 case 'V': case 'l': /* line-wise selection */
17712 yank_type = MLINE;
17713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 case 'b': case Ctrl_V: /* block-wise selection */
17715 yank_type = MBLOCK;
17716 if (VIM_ISDIGIT(stropt[1]))
17717 {
17718 ++stropt;
17719 block_len = getdigits(&stropt) - 1;
17720 --stropt;
17721 }
17722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 }
17724 }
17725
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017726 if (argvars[1].v_type == VAR_LIST)
17727 {
17728 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017729 char_u **allocval;
17730 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017731 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017732 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017733 int len = argvars[1].vval.v_list->lv_len;
17734 listitem_T *li;
17735
Bram Moolenaar7d647822014-04-05 21:28:56 +020017736 /* First half: use for pointers to result lines; second half: use for
17737 * pointers to allocated copies. */
17738 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017739 if (lstval == NULL)
17740 return;
17741 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017742 allocval = lstval + len + 2;
17743 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017744
17745 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17746 li = li->li_next)
17747 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017748 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017749 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017750 goto free_lstval;
17751 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017752 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017753 /* Need to make a copy, next get_tv_string_buf_chk() will
17754 * overwrite the string. */
17755 strval = vim_strsave(buf);
17756 if (strval == NULL)
17757 goto free_lstval;
17758 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017759 }
17760 *curval++ = strval;
17761 }
17762 *curval++ = NULL;
17763
17764 write_reg_contents_lst(regname, lstval, -1,
17765 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017766free_lstval:
17767 while (curallocval > allocval)
17768 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017769 vim_free(lstval);
17770 }
17771 else
17772 {
17773 strval = get_tv_string_chk(&argvars[1]);
17774 if (strval == NULL)
17775 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017776 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780}
17781
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017782/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017783 * "settabvar()" function
17784 */
17785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017786f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017787{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017788#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017789 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017790 tabpage_T *tp;
17791#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017792 char_u *varname, *tabvarname;
17793 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017794
17795 rettv->vval.v_number = 0;
17796
17797 if (check_restricted() || check_secure())
17798 return;
17799
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017800#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017801 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017802#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017803 varname = get_tv_string_chk(&argvars[1]);
17804 varp = &argvars[2];
17805
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017806 if (varname != NULL && varp != NULL
17807#ifdef FEAT_WINDOWS
17808 && tp != NULL
17809#endif
17810 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017811 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017812#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017813 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017814 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017815#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017816
17817 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17818 if (tabvarname != NULL)
17819 {
17820 STRCPY(tabvarname, "t:");
17821 STRCPY(tabvarname + 2, varname);
17822 set_var(tabvarname, varp, TRUE);
17823 vim_free(tabvarname);
17824 }
17825
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017826#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017827 /* Restore current tabpage */
17828 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017829 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017830#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017831 }
17832}
17833
17834/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017835 * "settabwinvar()" function
17836 */
17837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017838f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017839{
17840 setwinvar(argvars, rettv, 1);
17841}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842
17843/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017844 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017847f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017849 setwinvar(argvars, rettv, 0);
17850}
17851
17852/*
17853 * "setwinvar()" and "settabwinvar()" functions
17854 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017855
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017857setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017858{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 win_T *win;
17860#ifdef FEAT_WINDOWS
17861 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017862 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017863 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864#endif
17865 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017866 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017868 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869
17870 if (check_restricted() || check_secure())
17871 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017872
17873#ifdef FEAT_WINDOWS
17874 if (off == 1)
17875 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17876 else
17877 tp = curtab;
17878#endif
17879 win = find_win_by_nr(&argvars[off], tp);
17880 varname = get_tv_string_chk(&argvars[off + 1]);
17881 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882
17883 if (win != NULL && varname != NULL && varp != NULL)
17884 {
17885#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017886 need_switch_win = !(tp == curtab && win == curwin);
17887 if (!need_switch_win
17888 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017891 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017893 long numval;
17894 char_u *strval;
17895 int error = FALSE;
17896
17897 ++varname;
17898 numval = get_tv_number_chk(varp, &error);
17899 strval = get_tv_string_buf_chk(varp, nbuf);
17900 if (!error && strval != NULL)
17901 set_option_value(varname, numval, strval, OPT_LOCAL);
17902 }
17903 else
17904 {
17905 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17906 if (winvarname != NULL)
17907 {
17908 STRCPY(winvarname, "w:");
17909 STRCPY(winvarname + 2, varname);
17910 set_var(winvarname, varp, TRUE);
17911 vim_free(winvarname);
17912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 }
17914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017916 if (need_switch_win)
17917 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918#endif
17919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920}
17921
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017922#ifdef FEAT_CRYPT
17923/*
17924 * "sha256({string})" function
17925 */
17926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017927f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017928{
17929 char_u *p;
17930
17931 p = get_tv_string(&argvars[0]);
17932 rettv->vval.v_string = vim_strsave(
17933 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17934 rettv->v_type = VAR_STRING;
17935}
17936#endif /* FEAT_CRYPT */
17937
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017939 * "shellescape({string})" function
17940 */
17941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017942f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017943{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017944 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017945 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017946 rettv->v_type = VAR_STRING;
17947}
17948
17949/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017950 * shiftwidth() function
17951 */
17952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017953f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017954{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017955 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017956}
17957
17958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017959 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 */
17961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017962f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965
Bram Moolenaar0d660222005-01-07 21:51:51 +000017966 p = get_tv_string(&argvars[0]);
17967 rettv->vval.v_string = vim_strsave(p);
17968 simplify_filename(rettv->vval.v_string); /* simplify in place */
17969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970}
17971
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017972#ifdef FEAT_FLOAT
17973/*
17974 * "sin()" function
17975 */
17976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017977f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017978{
17979 float_T f;
17980
17981 rettv->v_type = VAR_FLOAT;
17982 if (get_float_arg(argvars, &f) == OK)
17983 rettv->vval.v_float = sin(f);
17984 else
17985 rettv->vval.v_float = 0.0;
17986}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017987
17988/*
17989 * "sinh()" function
17990 */
17991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017992f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017993{
17994 float_T f;
17995
17996 rettv->v_type = VAR_FLOAT;
17997 if (get_float_arg(argvars, &f) == OK)
17998 rettv->vval.v_float = sinh(f);
17999 else
18000 rettv->vval.v_float = 0.0;
18001}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018002#endif
18003
Bram Moolenaar0d660222005-01-07 21:51:51 +000018004static int
18005#ifdef __BORLANDC__
18006 _RTLENTRYF
18007#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018008 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009static int
18010#ifdef __BORLANDC__
18011 _RTLENTRYF
18012#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018013 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018015/* struct used in the array that's given to qsort() */
18016typedef struct
18017{
18018 listitem_T *item;
18019 int idx;
18020} sortItem_T;
18021
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018023static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018024static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018025#ifdef FEAT_FLOAT
18026static int item_compare_float;
18027#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018029static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018030static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018031static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018032static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033#define ITEM_COMPARE_FAIL 999
18034
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018036 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038 static int
18039#ifdef __BORLANDC__
18040_RTLENTRYF
18041#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018042item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018044 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018045 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018047 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048 int res;
18049 char_u numbuf1[NUMBUFLEN];
18050 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018052 si1 = (sortItem_T *)s1;
18053 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018054 tv1 = &si1->item->li_tv;
18055 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018056
18057 if (item_compare_numbers)
18058 {
18059 long v1 = get_tv_number(tv1);
18060 long v2 = get_tv_number(tv2);
18061
18062 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18063 }
18064
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018065#ifdef FEAT_FLOAT
18066 if (item_compare_float)
18067 {
18068 float_T v1 = get_tv_float(tv1);
18069 float_T v2 = get_tv_float(tv2);
18070
18071 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18072 }
18073#endif
18074
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018075 /* tv2string() puts quotes around a string and allocates memory. Don't do
18076 * that for string variables. Use a single quote when comparing with a
18077 * non-string to do what the docs promise. */
18078 if (tv1->v_type == VAR_STRING)
18079 {
18080 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18081 p1 = (char_u *)"'";
18082 else
18083 p1 = tv1->vval.v_string;
18084 }
18085 else
18086 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18087 if (tv2->v_type == VAR_STRING)
18088 {
18089 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18090 p2 = (char_u *)"'";
18091 else
18092 p2 = tv2->vval.v_string;
18093 }
18094 else
18095 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018096 if (p1 == NULL)
18097 p1 = (char_u *)"";
18098 if (p2 == NULL)
18099 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018100 if (!item_compare_numeric)
18101 {
18102 if (item_compare_ic)
18103 res = STRICMP(p1, p2);
18104 else
18105 res = STRCMP(p1, p2);
18106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018108 {
18109 double n1, n2;
18110 n1 = strtod((char *)p1, (char **)&p1);
18111 n2 = strtod((char *)p2, (char **)&p2);
18112 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18113 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018114
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018115 /* When the result would be zero, compare the item indexes. Makes the
18116 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018117 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018118 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018119
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 vim_free(tofree1);
18121 vim_free(tofree2);
18122 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123}
18124
18125 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126#ifdef __BORLANDC__
18127_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018129item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018130{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018131 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018132 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018134 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018137 /* shortcut after failure in previous call; compare all items equal */
18138 if (item_compare_func_err)
18139 return 0;
18140
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018141 si1 = (sortItem_T *)s1;
18142 si2 = (sortItem_T *)s2;
18143
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018144 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018145 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018146 copy_tv(&si1->item->li_tv, &argv[0]);
18147 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148
18149 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018150 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018151 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18152 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018153 clear_tv(&argv[0]);
18154 clear_tv(&argv[1]);
18155
18156 if (res == FAIL)
18157 res = ITEM_COMPARE_FAIL;
18158 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018159 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18160 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018161 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018163
18164 /* When the result would be zero, compare the pointers themselves. Makes
18165 * the sort stable. */
18166 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018167 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018168
Bram Moolenaar0d660222005-01-07 21:51:51 +000018169 return res;
18170}
18171
18172/*
18173 * "sort({list})" function
18174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018176do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177{
Bram Moolenaar33570922005-01-25 22:26:29 +000018178 list_T *l;
18179 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018180 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 long len;
18182 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183
Bram Moolenaar0d660222005-01-07 21:51:51 +000018184 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018185 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 else
18187 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018188 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018189 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018190 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18191 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 return;
18193 rettv->vval.v_list = l;
18194 rettv->v_type = VAR_LIST;
18195 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196
Bram Moolenaar0d660222005-01-07 21:51:51 +000018197 len = list_len(l);
18198 if (len <= 1)
18199 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018202 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018203 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018204#ifdef FEAT_FLOAT
18205 item_compare_float = FALSE;
18206#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018207 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018208 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 if (argvars[1].v_type != VAR_UNKNOWN)
18210 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018211 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018212 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018213 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214 else
18215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018216 int error = FALSE;
18217
18218 i = get_tv_number_chk(&argvars[1], &error);
18219 if (error)
18220 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 if (i == 1)
18222 item_compare_ic = TRUE;
18223 else
18224 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018225 if (item_compare_func != NULL)
18226 {
18227 if (STRCMP(item_compare_func, "n") == 0)
18228 {
18229 item_compare_func = NULL;
18230 item_compare_numeric = TRUE;
18231 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018232 else if (STRCMP(item_compare_func, "N") == 0)
18233 {
18234 item_compare_func = NULL;
18235 item_compare_numbers = TRUE;
18236 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018237#ifdef FEAT_FLOAT
18238 else if (STRCMP(item_compare_func, "f") == 0)
18239 {
18240 item_compare_func = NULL;
18241 item_compare_float = TRUE;
18242 }
18243#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018244 else if (STRCMP(item_compare_func, "i") == 0)
18245 {
18246 item_compare_func = NULL;
18247 item_compare_ic = TRUE;
18248 }
18249 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018250 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018251
18252 if (argvars[2].v_type != VAR_UNKNOWN)
18253 {
18254 /* optional third argument: {dict} */
18255 if (argvars[2].v_type != VAR_DICT)
18256 {
18257 EMSG(_(e_dictreq));
18258 return;
18259 }
18260 item_compare_selfdict = argvars[2].vval.v_dict;
18261 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018265 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018266 if (ptrs == NULL)
18267 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268
Bram Moolenaar327aa022014-03-25 18:24:23 +010018269 i = 0;
18270 if (sort)
18271 {
18272 /* sort(): ptrs will be the list to sort */
18273 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 {
18275 ptrs[i].item = li;
18276 ptrs[i].idx = i;
18277 ++i;
18278 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018279
18280 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018281 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018282 /* test the compare function */
18283 if (item_compare_func != NULL
18284 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018286 EMSG(_("E702: Sort compare function failed"));
18287 else
18288 {
18289 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018290 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018291 item_compare_func == NULL ? item_compare : item_compare2);
18292
18293 if (!item_compare_func_err)
18294 {
18295 /* Clear the List and append the items in sorted order. */
18296 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18297 l->lv_len = 0;
18298 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018299 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018300 }
18301 }
18302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018305 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018306
18307 /* f_uniq(): ptrs will be a stack of items to remove */
18308 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018309 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018310 item_compare_func_ptr = item_compare_func
18311 ? item_compare2 : item_compare;
18312
18313 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18314 li = li->li_next)
18315 {
18316 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18317 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018318 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018319 if (item_compare_func_err)
18320 {
18321 EMSG(_("E882: Uniq compare function failed"));
18322 break;
18323 }
18324 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018326 if (!item_compare_func_err)
18327 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018328 while (--i >= 0)
18329 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018330 li = ptrs[i].item->li_next;
18331 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018332 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018333 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018334 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018335 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018336 list_fix_watch(l, li);
18337 listitem_free(li);
18338 l->lv_len--;
18339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018341 }
18342
18343 vim_free(ptrs);
18344 }
18345}
18346
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018347/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018348 * "sort({list})" function
18349 */
18350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018351f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018352{
18353 do_sort_uniq(argvars, rettv, TRUE);
18354}
18355
18356/*
18357 * "uniq({list})" function
18358 */
18359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018360f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018361{
18362 do_sort_uniq(argvars, rettv, FALSE);
18363}
18364
18365/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018366 * "soundfold({word})" function
18367 */
18368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018369f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018370{
18371 char_u *s;
18372
18373 rettv->v_type = VAR_STRING;
18374 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018375#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018376 rettv->vval.v_string = eval_soundfold(s);
18377#else
18378 rettv->vval.v_string = vim_strsave(s);
18379#endif
18380}
18381
18382/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018383 * "spellbadword()" function
18384 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018386f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018387{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018388 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018389 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018390 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018391
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018392 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018393 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018394
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018395#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018396 if (argvars[0].v_type == VAR_UNKNOWN)
18397 {
18398 /* Find the start and length of the badly spelled word. */
18399 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18400 if (len != 0)
18401 word = ml_get_cursor();
18402 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018403 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018404 {
18405 char_u *str = get_tv_string_chk(&argvars[0]);
18406 int capcol = -1;
18407
18408 if (str != NULL)
18409 {
18410 /* Check the argument for spelling. */
18411 while (*str != NUL)
18412 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018413 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018414 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018415 {
18416 word = str;
18417 break;
18418 }
18419 str += len;
18420 }
18421 }
18422 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018423#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018425 list_append_string(rettv->vval.v_list, word, len);
18426 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018427 attr == HLF_SPB ? "bad" :
18428 attr == HLF_SPR ? "rare" :
18429 attr == HLF_SPL ? "local" :
18430 attr == HLF_SPC ? "caps" :
18431 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018432}
18433
18434/*
18435 * "spellsuggest()" function
18436 */
18437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018438f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018439{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018440#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018441 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018442 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018443 int maxcount;
18444 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018445 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018446 listitem_T *li;
18447 int need_capital = FALSE;
18448#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018450 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018451 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018452
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018453#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018454 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018455 {
18456 str = get_tv_string(&argvars[0]);
18457 if (argvars[1].v_type != VAR_UNKNOWN)
18458 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018459 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018460 if (maxcount <= 0)
18461 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018462 if (argvars[2].v_type != VAR_UNKNOWN)
18463 {
18464 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18465 if (typeerr)
18466 return;
18467 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018468 }
18469 else
18470 maxcount = 25;
18471
Bram Moolenaar4770d092006-01-12 23:22:24 +000018472 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018473
18474 for (i = 0; i < ga.ga_len; ++i)
18475 {
18476 str = ((char_u **)ga.ga_data)[i];
18477
18478 li = listitem_alloc();
18479 if (li == NULL)
18480 vim_free(str);
18481 else
18482 {
18483 li->li_tv.v_type = VAR_STRING;
18484 li->li_tv.v_lock = 0;
18485 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018486 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018487 }
18488 }
18489 ga_clear(&ga);
18490 }
18491#endif
18492}
18493
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496{
18497 char_u *str;
18498 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018499 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018500 regmatch_T regmatch;
18501 char_u patbuf[NUMBUFLEN];
18502 char_u *save_cpo;
18503 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018505 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018507
18508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18509 save_cpo = p_cpo;
18510 p_cpo = (char_u *)"";
18511
18512 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018513 if (argvars[1].v_type != VAR_UNKNOWN)
18514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018515 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18516 if (pat == NULL)
18517 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018520 }
18521 if (pat == NULL || *pat == NUL)
18522 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018524 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 if (typeerr)
18527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18530 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018532 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018533 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018534 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018535 if (*str == NUL)
18536 match = FALSE; /* empty item at the end */
18537 else
18538 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539 if (match)
18540 end = regmatch.startp[0];
18541 else
18542 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018543 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18544 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018546 if (list_append_string(rettv->vval.v_list, str,
18547 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018548 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018549 }
18550 if (!match)
18551 break;
18552 /* Advance to just after the match. */
18553 if (regmatch.endp[0] > str)
18554 col = 0;
18555 else
18556 {
18557 /* Don't get stuck at the same match. */
18558#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018559 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018560#else
18561 col = 1;
18562#endif
18563 }
18564 str = regmatch.endp[0];
18565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaar473de612013-06-08 18:19:48 +020018567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571}
18572
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018573#ifdef FEAT_FLOAT
18574/*
18575 * "sqrt()" function
18576 */
18577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018578f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018579{
18580 float_T f;
18581
18582 rettv->v_type = VAR_FLOAT;
18583 if (get_float_arg(argvars, &f) == OK)
18584 rettv->vval.v_float = sqrt(f);
18585 else
18586 rettv->vval.v_float = 0.0;
18587}
18588
18589/*
18590 * "str2float()" function
18591 */
18592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018593f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018594{
18595 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18596
18597 if (*p == '+')
18598 p = skipwhite(p + 1);
18599 (void)string2float(p, &rettv->vval.v_float);
18600 rettv->v_type = VAR_FLOAT;
18601}
18602#endif
18603
Bram Moolenaar2c932302006-03-18 21:42:09 +000018604/*
18605 * "str2nr()" function
18606 */
18607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018608f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018609{
18610 int base = 10;
18611 char_u *p;
18612 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018613 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018614
18615 if (argvars[1].v_type != VAR_UNKNOWN)
18616 {
18617 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018618 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018619 {
18620 EMSG(_(e_invarg));
18621 return;
18622 }
18623 }
18624
18625 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018626 if (*p == '+')
18627 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018628 switch (base)
18629 {
18630 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18631 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18632 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18633 default: what = 0;
18634 }
18635 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018636 rettv->vval.v_number = n;
18637}
18638
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639#ifdef HAVE_STRFTIME
18640/*
18641 * "strftime({format}[, {time}])" function
18642 */
18643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018644f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645{
18646 char_u result_buf[256];
18647 struct tm *curtime;
18648 time_t seconds;
18649 char_u *p;
18650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018654 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 seconds = time(NULL);
18656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 curtime = localtime(&seconds);
18659 /* MSVC returns NULL for an invalid value of seconds. */
18660 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 else
18663 {
18664# ifdef FEAT_MBYTE
18665 vimconv_T conv;
18666 char_u *enc;
18667
18668 conv.vc_type = CONV_NONE;
18669 enc = enc_locale();
18670 convert_setup(&conv, p_enc, enc);
18671 if (conv.vc_type != CONV_NONE)
18672 p = string_convert(&conv, p, NULL);
18673# endif
18674 if (p != NULL)
18675 (void)strftime((char *)result_buf, sizeof(result_buf),
18676 (char *)p, curtime);
18677 else
18678 result_buf[0] = NUL;
18679
18680# ifdef FEAT_MBYTE
18681 if (conv.vc_type != CONV_NONE)
18682 vim_free(p);
18683 convert_setup(&conv, enc, p_enc);
18684 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 else
18687# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689
18690# ifdef FEAT_MBYTE
18691 /* Release conversion descriptors */
18692 convert_setup(&conv, NULL, NULL);
18693 vim_free(enc);
18694# endif
18695 }
18696}
18697#endif
18698
18699/*
18700 * "stridx()" function
18701 */
18702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018703f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704{
18705 char_u buf[NUMBUFLEN];
18706 char_u *needle;
18707 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018708 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018712 needle = get_tv_string_chk(&argvars[1]);
18713 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018714 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018715 if (needle == NULL || haystack == NULL)
18716 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 if (argvars[2].v_type != VAR_UNKNOWN)
18719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018720 int error = FALSE;
18721
18722 start_idx = get_tv_number_chk(&argvars[2], &error);
18723 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018725 if (start_idx >= 0)
18726 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018727 }
18728
18729 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18730 if (pos != NULL)
18731 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732}
18733
18734/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018735 * "string()" function
18736 */
18737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018738f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018739{
18740 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018741 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018744 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018745 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018746 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748}
18749
18750/*
18751 * "strlen()" function
18752 */
18753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018754f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018756 rettv->vval.v_number = (varnumber_T)(STRLEN(
18757 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758}
18759
18760/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018761 * "strchars()" function
18762 */
18763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018764f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018765{
18766 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018767 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018768#ifdef FEAT_MBYTE
18769 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018770 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018771#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018772
18773 if (argvars[1].v_type != VAR_UNKNOWN)
18774 skipcc = get_tv_number_chk(&argvars[1], NULL);
18775 if (skipcc < 0 || skipcc > 1)
18776 EMSG(_(e_invarg));
18777 else
18778 {
18779#ifdef FEAT_MBYTE
18780 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18781 while (*s != NUL)
18782 {
18783 func_mb_ptr2char_adv(&s);
18784 ++len;
18785 }
18786 rettv->vval.v_number = len;
18787#else
18788 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18789#endif
18790 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018791}
18792
18793/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018794 * "strdisplaywidth()" function
18795 */
18796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018797f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018798{
18799 char_u *s = get_tv_string(&argvars[0]);
18800 int col = 0;
18801
18802 if (argvars[1].v_type != VAR_UNKNOWN)
18803 col = get_tv_number(&argvars[1]);
18804
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018805 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018806}
18807
18808/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018809 * "strwidth()" function
18810 */
18811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018812f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018813{
18814 char_u *s = get_tv_string(&argvars[0]);
18815
18816 rettv->vval.v_number = (varnumber_T)(
18817#ifdef FEAT_MBYTE
18818 mb_string2cells(s, -1)
18819#else
18820 STRLEN(s)
18821#endif
18822 );
18823}
18824
18825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 * "strpart()" function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830{
18831 char_u *p;
18832 int n;
18833 int len;
18834 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018835 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 slen = (int)STRLEN(p);
18839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018840 n = get_tv_number_chk(&argvars[1], &error);
18841 if (error)
18842 len = 0;
18843 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018844 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 else
18846 len = slen - n; /* default len: all bytes that are available. */
18847
18848 /*
18849 * Only return the overlap between the specified part and the actual
18850 * string.
18851 */
18852 if (n < 0)
18853 {
18854 len += n;
18855 n = 0;
18856 }
18857 else if (n > slen)
18858 n = slen;
18859 if (len < 0)
18860 len = 0;
18861 else if (n + len > slen)
18862 len = slen - n;
18863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 rettv->v_type = VAR_STRING;
18865 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867
18868/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018869 * "strridx()" function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018873{
18874 char_u buf[NUMBUFLEN];
18875 char_u *needle;
18876 char_u *haystack;
18877 char_u *rest;
18878 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018879 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018881 needle = get_tv_string_chk(&argvars[1]);
18882 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018883
18884 rettv->vval.v_number = -1;
18885 if (needle == NULL || haystack == NULL)
18886 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018887
18888 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018889 if (argvars[2].v_type != VAR_UNKNOWN)
18890 {
18891 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018892 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018893 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018894 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018895 }
18896 else
18897 end_idx = haystack_len;
18898
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018900 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018901 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018902 lastmatch = haystack + end_idx;
18903 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018904 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018905 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018906 for (rest = haystack; *rest != '\0'; ++rest)
18907 {
18908 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018909 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910 break;
18911 lastmatch = rest;
18912 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018913 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914
18915 if (lastmatch == NULL)
18916 rettv->vval.v_number = -1;
18917 else
18918 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18919}
18920
18921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 * "strtrans()" function
18923 */
18924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018925f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927 rettv->v_type = VAR_STRING;
18928 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929}
18930
18931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 * "submatch()" function
18933 */
18934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018935f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936{
Bram Moolenaar41571762014-04-02 19:00:58 +020018937 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018938 int no;
18939 int retList = 0;
18940
18941 no = (int)get_tv_number_chk(&argvars[0], &error);
18942 if (error)
18943 return;
18944 error = FALSE;
18945 if (argvars[1].v_type != VAR_UNKNOWN)
18946 retList = get_tv_number_chk(&argvars[1], &error);
18947 if (error)
18948 return;
18949
18950 if (retList == 0)
18951 {
18952 rettv->v_type = VAR_STRING;
18953 rettv->vval.v_string = reg_submatch(no);
18954 }
18955 else
18956 {
18957 rettv->v_type = VAR_LIST;
18958 rettv->vval.v_list = reg_submatch_list(no);
18959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960}
18961
18962/*
18963 * "substitute()" function
18964 */
18965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018966f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967{
18968 char_u patbuf[NUMBUFLEN];
18969 char_u subbuf[NUMBUFLEN];
18970 char_u flagsbuf[NUMBUFLEN];
18971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972 char_u *str = get_tv_string_chk(&argvars[0]);
18973 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18974 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18975 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18976
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018978 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18979 rettv->vval.v_string = NULL;
18980 else
18981 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982}
18983
18984/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018985 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018988f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989{
18990 int id = 0;
18991#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018992 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 long col;
18994 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018995 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018997 lnum = get_tv_lnum(argvars); /* -1 on type error */
18998 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18999 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019001 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019002 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019003 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004#endif
19005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007}
19008
19009/*
19010 * "synIDattr(id, what [, mode])" function
19011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019013f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014{
19015 char_u *p = NULL;
19016#ifdef FEAT_SYN_HL
19017 int id;
19018 char_u *what;
19019 char_u *mode;
19020 char_u modebuf[NUMBUFLEN];
19021 int modec;
19022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019023 id = get_tv_number(&argvars[0]);
19024 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019029 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 modec = 0; /* replace invalid with current */
19031 }
19032 else
19033 {
19034#ifdef FEAT_GUI
19035 if (gui.in_use)
19036 modec = 'g';
19037 else
19038#endif
19039 if (t_colors > 1)
19040 modec = 'c';
19041 else
19042 modec = 't';
19043 }
19044
19045
19046 switch (TOLOWER_ASC(what[0]))
19047 {
19048 case 'b':
19049 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19050 p = highlight_color(id, what, modec);
19051 else /* bold */
19052 p = highlight_has_attr(id, HL_BOLD, modec);
19053 break;
19054
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019055 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 p = highlight_color(id, what, modec);
19057 break;
19058
19059 case 'i':
19060 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19061 p = highlight_has_attr(id, HL_INVERSE, modec);
19062 else /* italic */
19063 p = highlight_has_attr(id, HL_ITALIC, modec);
19064 break;
19065
19066 case 'n': /* name */
19067 p = get_highlight_name(NULL, id - 1);
19068 break;
19069
19070 case 'r': /* reverse */
19071 p = highlight_has_attr(id, HL_INVERSE, modec);
19072 break;
19073
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019074 case 's':
19075 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19076 p = highlight_color(id, what, modec);
19077 else /* standout */
19078 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 break;
19080
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019081 case 'u':
19082 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19083 /* underline */
19084 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19085 else
19086 /* undercurl */
19087 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 break;
19089 }
19090
19091 if (p != NULL)
19092 p = vim_strsave(p);
19093#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 rettv->v_type = VAR_STRING;
19095 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097
19098/*
19099 * "synIDtrans(id)" function
19100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019102f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103{
19104 int id;
19105
19106#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108
19109 if (id > 0)
19110 id = syn_get_final_id(id);
19111 else
19112#endif
19113 id = 0;
19114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116}
19117
19118/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019119 * "synconcealed(lnum, col)" function
19120 */
19121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019122f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019123{
19124#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19125 long lnum;
19126 long col;
19127 int syntax_flags = 0;
19128 int cchar;
19129 int matchid = 0;
19130 char_u str[NUMBUFLEN];
19131#endif
19132
19133 rettv->v_type = VAR_LIST;
19134 rettv->vval.v_list = NULL;
19135
19136#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19137 lnum = get_tv_lnum(argvars); /* -1 on type error */
19138 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19139
19140 vim_memset(str, NUL, sizeof(str));
19141
19142 if (rettv_list_alloc(rettv) != FAIL)
19143 {
19144 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19145 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19146 && curwin->w_p_cole > 0)
19147 {
19148 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19149 syntax_flags = get_syntax_info(&matchid);
19150
19151 /* get the conceal character */
19152 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19153 {
19154 cchar = syn_get_sub_char();
19155 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19156 cchar = lcs_conceal;
19157 if (cchar != NUL)
19158 {
19159# ifdef FEAT_MBYTE
19160 if (has_mbyte)
19161 (*mb_char2bytes)(cchar, str);
19162 else
19163# endif
19164 str[0] = cchar;
19165 }
19166 }
19167 }
19168
19169 list_append_number(rettv->vval.v_list,
19170 (syntax_flags & HL_CONCEAL) != 0);
19171 /* -1 to auto-determine strlen */
19172 list_append_string(rettv->vval.v_list, str, -1);
19173 list_append_number(rettv->vval.v_list, matchid);
19174 }
19175#endif
19176}
19177
19178/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019179 * "synstack(lnum, col)" function
19180 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019182f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019183{
19184#ifdef FEAT_SYN_HL
19185 long lnum;
19186 long col;
19187 int i;
19188 int id;
19189#endif
19190
19191 rettv->v_type = VAR_LIST;
19192 rettv->vval.v_list = NULL;
19193
19194#ifdef FEAT_SYN_HL
19195 lnum = get_tv_lnum(argvars); /* -1 on type error */
19196 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19197
19198 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019199 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019200 && rettv_list_alloc(rettv) != FAIL)
19201 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019202 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019203 for (i = 0; ; ++i)
19204 {
19205 id = syn_get_stack_item(i);
19206 if (id < 0)
19207 break;
19208 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19209 break;
19210 }
19211 }
19212#endif
19213}
19214
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019216get_cmd_output_as_rettv(
19217 typval_T *argvars,
19218 typval_T *rettv,
19219 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019221 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019223 char_u *infile = NULL;
19224 char_u buf[NUMBUFLEN];
19225 int err = FALSE;
19226 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019227 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019228 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019230 rettv->v_type = VAR_STRING;
19231 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019232 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019233 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019235 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019236 {
19237 /*
19238 * Write the string to a temp file, to be used for input of the shell
19239 * command.
19240 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019241 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019242 {
19243 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019244 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019245 }
19246
19247 fd = mch_fopen((char *)infile, WRITEBIN);
19248 if (fd == NULL)
19249 {
19250 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019251 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019252 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019253 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019254 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019255 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19256 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019257 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019258 else
19259 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019260 size_t len;
19261
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019262 p = get_tv_string_buf_chk(&argvars[1], buf);
19263 if (p == NULL)
19264 {
19265 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019266 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019267 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019268 len = STRLEN(p);
19269 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019270 err = TRUE;
19271 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019272 if (fclose(fd) != 0)
19273 err = TRUE;
19274 if (err)
19275 {
19276 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019277 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019278 }
19279 }
19280
Bram Moolenaar52a72462014-08-29 15:53:52 +020019281 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19282 * echoes typeahead, that messes up the display. */
19283 if (!msg_silent)
19284 flags += SHELL_COOKED;
19285
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019286 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019288 int len;
19289 listitem_T *li;
19290 char_u *s = NULL;
19291 char_u *start;
19292 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294
Bram Moolenaar52a72462014-08-29 15:53:52 +020019295 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019296 if (res == NULL)
19297 goto errret;
19298
19299 list = list_alloc();
19300 if (list == NULL)
19301 goto errret;
19302
19303 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019306 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019307 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019308 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019309
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019310 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019311 if (s == NULL)
19312 goto errret;
19313
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019314 for (p = s; start < end; ++p, ++start)
19315 *p = *start == NUL ? NL : *start;
19316 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317
19318 li = listitem_alloc();
19319 if (li == NULL)
19320 {
19321 vim_free(s);
19322 goto errret;
19323 }
19324 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019325 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019326 li->li_tv.vval.v_string = s;
19327 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019329
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019330 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019331 rettv->v_type = VAR_LIST;
19332 rettv->vval.v_list = list;
19333 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019335 else
19336 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019337 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338#ifdef USE_CR
19339 /* translate <CR> into <NL> */
19340 if (res != NULL)
19341 {
19342 char_u *s;
19343
19344 for (s = res; *s; ++s)
19345 {
19346 if (*s == CAR)
19347 *s = NL;
19348 }
19349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350#else
19351# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019352 /* translate <CR><NL> into <NL> */
19353 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019355 char_u *s, *d;
19356
19357 d = res;
19358 for (s = res; *s; ++s)
19359 {
19360 if (s[0] == CAR && s[1] == NL)
19361 ++s;
19362 *d++ = *s;
19363 }
19364 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366# endif
19367#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368 rettv->vval.v_string = res;
19369 res = NULL;
19370 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019371
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019372errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019373 if (infile != NULL)
19374 {
19375 mch_remove(infile);
19376 vim_free(infile);
19377 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019378 if (res != NULL)
19379 vim_free(res);
19380 if (list != NULL)
19381 list_free(list, TRUE);
19382}
19383
19384/*
19385 * "system()" function
19386 */
19387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019388f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019389{
19390 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19391}
19392
19393/*
19394 * "systemlist()" function
19395 */
19396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019397f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019398{
19399 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
19402/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019403 * "tabpagebuflist()" function
19404 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019406f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019407{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019408#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019409 tabpage_T *tp;
19410 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019411
19412 if (argvars[0].v_type == VAR_UNKNOWN)
19413 wp = firstwin;
19414 else
19415 {
19416 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19417 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019418 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019419 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019420 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019421 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019422 for (; wp != NULL; wp = wp->w_next)
19423 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019424 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019425 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019426 }
19427#endif
19428}
19429
19430
19431/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019432 * "tabpagenr()" function
19433 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019435f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019436{
19437 int nr = 1;
19438#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019439 char_u *arg;
19440
19441 if (argvars[0].v_type != VAR_UNKNOWN)
19442 {
19443 arg = get_tv_string_chk(&argvars[0]);
19444 nr = 0;
19445 if (arg != NULL)
19446 {
19447 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019448 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019449 else
19450 EMSG2(_(e_invexpr2), arg);
19451 }
19452 }
19453 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019454 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019455#endif
19456 rettv->vval.v_number = nr;
19457}
19458
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019459
19460#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019461static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019462
19463/*
19464 * Common code for tabpagewinnr() and winnr().
19465 */
19466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019467get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019468{
19469 win_T *twin;
19470 int nr = 1;
19471 win_T *wp;
19472 char_u *arg;
19473
19474 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19475 if (argvar->v_type != VAR_UNKNOWN)
19476 {
19477 arg = get_tv_string_chk(argvar);
19478 if (arg == NULL)
19479 nr = 0; /* type error; errmsg already given */
19480 else if (STRCMP(arg, "$") == 0)
19481 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19482 else if (STRCMP(arg, "#") == 0)
19483 {
19484 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19485 if (twin == NULL)
19486 nr = 0;
19487 }
19488 else
19489 {
19490 EMSG2(_(e_invexpr2), arg);
19491 nr = 0;
19492 }
19493 }
19494
19495 if (nr > 0)
19496 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19497 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019498 {
19499 if (wp == NULL)
19500 {
19501 /* didn't find it in this tabpage */
19502 nr = 0;
19503 break;
19504 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019505 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019506 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019507 return nr;
19508}
19509#endif
19510
19511/*
19512 * "tabpagewinnr()" function
19513 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019515f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019516{
19517 int nr = 1;
19518#ifdef FEAT_WINDOWS
19519 tabpage_T *tp;
19520
19521 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19522 if (tp == NULL)
19523 nr = 0;
19524 else
19525 nr = get_winnr(tp, &argvars[1]);
19526#endif
19527 rettv->vval.v_number = nr;
19528}
19529
19530
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019531/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019532 * "tagfiles()" function
19533 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019535f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019536{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019537 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019538 tagname_T tn;
19539 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019542 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019543 fname = alloc(MAXPATHL);
19544 if (fname == NULL)
19545 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019547 for (first = TRUE; ; first = FALSE)
19548 if (get_tagfname(&tn, first, fname) == FAIL
19549 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019550 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019551 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019552 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019553}
19554
19555/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019556 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019557 */
19558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019559f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019560{
19561 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019562
19563 tag_pattern = get_tv_string(&argvars[0]);
19564
19565 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 if (*tag_pattern == NUL)
19567 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019568
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019569 if (rettv_list_alloc(rettv) == OK)
19570 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019571}
19572
19573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 * "tempname()" function
19575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019577f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578{
19579 static int x = 'A';
19580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019582 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583
19584 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19585 * names. Skip 'I' and 'O', they are used for shell redirection. */
19586 do
19587 {
19588 if (x == 'Z')
19589 x = '0';
19590 else if (x == '9')
19591 x = 'A';
19592 else
19593 {
19594#ifdef EBCDIC
19595 if (x == 'I')
19596 x = 'J';
19597 else if (x == 'R')
19598 x = 'S';
19599 else
19600#endif
19601 ++x;
19602 }
19603 } while (x == 'I' || x == 'O');
19604}
19605
19606/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019607 * "test(list)" function: Just checking the walls...
19608 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019610f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019611{
19612 /* Used for unit testing. Change the code below to your liking. */
19613#if 0
19614 listitem_T *li;
19615 list_T *l;
19616 char_u *bad, *good;
19617
19618 if (argvars[0].v_type != VAR_LIST)
19619 return;
19620 l = argvars[0].vval.v_list;
19621 if (l == NULL)
19622 return;
19623 li = l->lv_first;
19624 if (li == NULL)
19625 return;
19626 bad = get_tv_string(&li->li_tv);
19627 li = li->li_next;
19628 if (li == NULL)
19629 return;
19630 good = get_tv_string(&li->li_tv);
19631 rettv->vval.v_number = test_edit_score(bad, good);
19632#endif
19633}
19634
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019635#ifdef FEAT_FLOAT
19636/*
19637 * "tan()" function
19638 */
19639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019641{
19642 float_T f;
19643
19644 rettv->v_type = VAR_FLOAT;
19645 if (get_float_arg(argvars, &f) == OK)
19646 rettv->vval.v_float = tan(f);
19647 else
19648 rettv->vval.v_float = 0.0;
19649}
19650
19651/*
19652 * "tanh()" function
19653 */
19654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019655f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019656{
19657 float_T f;
19658
19659 rettv->v_type = VAR_FLOAT;
19660 if (get_float_arg(argvars, &f) == OK)
19661 rettv->vval.v_float = tanh(f);
19662 else
19663 rettv->vval.v_float = 0.0;
19664}
19665#endif
19666
Bram Moolenaard52d9742005-08-21 22:20:28 +000019667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 * "tolower(string)" function
19669 */
19670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019671f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672{
19673 char_u *p;
19674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675 p = vim_strsave(get_tv_string(&argvars[0]));
19676 rettv->v_type = VAR_STRING;
19677 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
19679 if (p != NULL)
19680 while (*p != NUL)
19681 {
19682#ifdef FEAT_MBYTE
19683 int l;
19684
19685 if (enc_utf8)
19686 {
19687 int c, lc;
19688
19689 c = utf_ptr2char(p);
19690 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019691 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 /* TODO: reallocate string when byte count changes. */
19693 if (utf_char2len(lc) == l)
19694 utf_char2bytes(lc, p);
19695 p += l;
19696 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019697 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 p += l; /* skip multi-byte character */
19699 else
19700#endif
19701 {
19702 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19703 ++p;
19704 }
19705 }
19706}
19707
19708/*
19709 * "toupper(string)" function
19710 */
19711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019712f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019715 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716}
19717
19718/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019719 * "tr(string, fromstr, tostr)" function
19720 */
19721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019722f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019723{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019724 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019725 char_u *fromstr;
19726 char_u *tostr;
19727 char_u *p;
19728#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019729 int inlen;
19730 int fromlen;
19731 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019732 int idx;
19733 char_u *cpstr;
19734 int cplen;
19735 int first = TRUE;
19736#endif
19737 char_u buf[NUMBUFLEN];
19738 char_u buf2[NUMBUFLEN];
19739 garray_T ga;
19740
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019741 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019742 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19743 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019744
19745 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 rettv->v_type = VAR_STRING;
19747 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019748 if (fromstr == NULL || tostr == NULL)
19749 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019750 ga_init2(&ga, (int)sizeof(char), 80);
19751
19752#ifdef FEAT_MBYTE
19753 if (!has_mbyte)
19754#endif
19755 /* not multi-byte: fromstr and tostr must be the same length */
19756 if (STRLEN(fromstr) != STRLEN(tostr))
19757 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019758#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019759error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019760#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019761 EMSG2(_(e_invarg2), fromstr);
19762 ga_clear(&ga);
19763 return;
19764 }
19765
19766 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019767 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019768 {
19769#ifdef FEAT_MBYTE
19770 if (has_mbyte)
19771 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019772 inlen = (*mb_ptr2len)(in_str);
19773 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019774 cplen = inlen;
19775 idx = 0;
19776 for (p = fromstr; *p != NUL; p += fromlen)
19777 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019778 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019779 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019780 {
19781 for (p = tostr; *p != NUL; p += tolen)
19782 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019783 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019784 if (idx-- == 0)
19785 {
19786 cplen = tolen;
19787 cpstr = p;
19788 break;
19789 }
19790 }
19791 if (*p == NUL) /* tostr is shorter than fromstr */
19792 goto error;
19793 break;
19794 }
19795 ++idx;
19796 }
19797
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019798 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019799 {
19800 /* Check that fromstr and tostr have the same number of
19801 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019802 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019803 first = FALSE;
19804 for (p = tostr; *p != NUL; p += tolen)
19805 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019806 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 --idx;
19808 }
19809 if (idx != 0)
19810 goto error;
19811 }
19812
Bram Moolenaarcde88542015-08-11 19:14:00 +020019813 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019814 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019815 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019816
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019817 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019818 }
19819 else
19820#endif
19821 {
19822 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019823 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019824 if (p != NULL)
19825 ga_append(&ga, tostr[p - fromstr]);
19826 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019827 ga_append(&ga, *in_str);
19828 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019829 }
19830 }
19831
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019832 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019833 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019834 ga_append(&ga, NUL);
19835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019837}
19838
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019839#ifdef FEAT_FLOAT
19840/*
19841 * "trunc({float})" function
19842 */
19843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019844f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019845{
19846 float_T f;
19847
19848 rettv->v_type = VAR_FLOAT;
19849 if (get_float_arg(argvars, &f) == OK)
19850 /* trunc() is not in C90, use floor() or ceil() instead. */
19851 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19852 else
19853 rettv->vval.v_float = 0.0;
19854}
19855#endif
19856
Bram Moolenaar8299df92004-07-10 09:47:34 +000019857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 * "type(expr)" function
19859 */
19860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019861f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019863 int n;
19864
19865 switch (argvars[0].v_type)
19866 {
19867 case VAR_NUMBER: n = 0; break;
19868 case VAR_STRING: n = 1; break;
19869 case VAR_FUNC: n = 2; break;
19870 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019871 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019872 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019873 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019874 if (argvars[0].vval.v_number == VVAL_FALSE
19875 || argvars[0].vval.v_number == VVAL_TRUE)
19876 n = 6;
19877 else
19878 n = 7;
19879 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019880 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019881 case VAR_UNKNOWN:
19882 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19883 n = -1;
19884 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019885 }
19886 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887}
19888
19889/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019890 * "undofile(name)" function
19891 */
19892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019893f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019894{
19895 rettv->v_type = VAR_STRING;
19896#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019897 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019898 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019899
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019900 if (*fname == NUL)
19901 {
19902 /* If there is no file name there will be no undo file. */
19903 rettv->vval.v_string = NULL;
19904 }
19905 else
19906 {
19907 char_u *ffname = FullName_save(fname, FALSE);
19908
19909 if (ffname != NULL)
19910 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19911 vim_free(ffname);
19912 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019913 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019914#else
19915 rettv->vval.v_string = NULL;
19916#endif
19917}
19918
19919/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019920 * "undotree()" function
19921 */
19922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019923f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019924{
19925 if (rettv_dict_alloc(rettv) == OK)
19926 {
19927 dict_T *dict = rettv->vval.v_dict;
19928 list_T *list;
19929
Bram Moolenaar730cde92010-06-27 05:18:54 +020019930 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019931 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019932 dict_add_nr_str(dict, "save_last",
19933 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019934 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19935 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019936 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019937
19938 list = list_alloc();
19939 if (list != NULL)
19940 {
19941 u_eval_tree(curbuf->b_u_oldhead, list);
19942 dict_add_list(dict, "entries", list);
19943 }
19944 }
19945}
19946
19947/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019948 * "values(dict)" function
19949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019952{
19953 dict_list(argvars, rettv, 1);
19954}
19955
19956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 * "virtcol(string)" function
19958 */
19959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961{
19962 colnr_T vcol = 0;
19963 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019964 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019966 fp = var2fpos(&argvars[0], FALSE, &fnum);
19967 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19968 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 {
19970 getvvcol(curwin, fp, NULL, NULL, &vcol);
19971 ++vcol;
19972 }
19973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977/*
19978 * "visualmode()" function
19979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019981f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 char_u str[2];
19984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 str[0] = curbuf->b_visual_mode_eval;
19987 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019988 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989
19990 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019991 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993}
19994
19995/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019996 * "wildmenumode()" function
19997 */
19998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019999f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020000{
20001#ifdef FEAT_WILDMENU
20002 if (wild_menu_showing)
20003 rettv->vval.v_number = 1;
20004#endif
20005}
20006
20007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 * "winbufnr(nr)" function
20009 */
20010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020011f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012{
20013 win_T *wp;
20014
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020015 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020}
20021
20022/*
20023 * "wincol()" function
20024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020026f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027{
20028 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030}
20031
20032/*
20033 * "winheight(nr)" function
20034 */
20035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020036f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037{
20038 win_T *wp;
20039
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020040 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045}
20046
20047/*
20048 * "winline()" function
20049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020051f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052{
20053 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055}
20056
20057/*
20058 * "winnr()" function
20059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020061f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062{
20063 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020064
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020066 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
20072 * "winrestcmd()" function
20073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077#ifdef FEAT_WINDOWS
20078 win_T *wp;
20079 int winnr = 1;
20080 garray_T ga;
20081 char_u buf[50];
20082
20083 ga_init2(&ga, (int)sizeof(char), 70);
20084 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20085 {
20086 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20087 ga_concat(&ga, buf);
20088# ifdef FEAT_VERTSPLIT
20089 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20090 ga_concat(&ga, buf);
20091# endif
20092 ++winnr;
20093 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020094 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101}
20102
20103/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020104 * "winrestview()" function
20105 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020107f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020108{
20109 dict_T *dict;
20110
20111 if (argvars[0].v_type != VAR_DICT
20112 || (dict = argvars[0].vval.v_dict) == NULL)
20113 EMSG(_(e_invarg));
20114 else
20115 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020116 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20117 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20118 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20119 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020120#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020121 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20122 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020123#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020124 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20125 {
20126 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20127 curwin->w_set_curswant = FALSE;
20128 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020129
Bram Moolenaar82c25852014-05-28 16:47:16 +020020130 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20131 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020132#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020133 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20134 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020135#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020136 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20137 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20138 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20139 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020140
20141 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020142 win_new_height(curwin, curwin->w_height);
20143# ifdef FEAT_VERTSPLIT
20144 win_new_width(curwin, W_WIDTH(curwin));
20145# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020146 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020147
Bram Moolenaarb851a962014-10-31 15:45:52 +010020148 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020149 curwin->w_topline = 1;
20150 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20151 curwin->w_topline = curbuf->b_ml.ml_line_count;
20152#ifdef FEAT_DIFF
20153 check_topfill(curwin, TRUE);
20154#endif
20155 }
20156}
20157
20158/*
20159 * "winsaveview()" function
20160 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020162f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020163{
20164 dict_T *dict;
20165
Bram Moolenaara800b422010-06-27 01:15:55 +020020166 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020167 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020168 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020169
20170 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20171 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20172#ifdef FEAT_VIRTUALEDIT
20173 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20174#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020175 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020176 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20177
20178 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20179#ifdef FEAT_DIFF
20180 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20181#endif
20182 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20183 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20184}
20185
20186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 * "winwidth(nr)" function
20188 */
20189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020190f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191{
20192 win_T *wp;
20193
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020194 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 else
20198#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202#endif
20203}
20204
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020206 * "wordcount()" function
20207 */
20208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020209f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020210{
20211 if (rettv_dict_alloc(rettv) == FAIL)
20212 return;
20213 cursor_pos_info(rettv->vval.v_dict);
20214}
20215
20216/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020217 * Write list of strings to file
20218 */
20219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020220write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020221{
20222 listitem_T *li;
20223 int c;
20224 int ret = OK;
20225 char_u *s;
20226
20227 for (li = list->lv_first; li != NULL; li = li->li_next)
20228 {
20229 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20230 {
20231 if (*s == '\n')
20232 c = putc(NUL, fd);
20233 else
20234 c = putc(*s, fd);
20235 if (c == EOF)
20236 {
20237 ret = FAIL;
20238 break;
20239 }
20240 }
20241 if (!binary || li->li_next != NULL)
20242 if (putc('\n', fd) == EOF)
20243 {
20244 ret = FAIL;
20245 break;
20246 }
20247 if (ret == FAIL)
20248 {
20249 EMSG(_(e_write));
20250 break;
20251 }
20252 }
20253 return ret;
20254}
20255
20256/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020257 * "writefile()" function
20258 */
20259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020260f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020261{
20262 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020263 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020264 char_u *fname;
20265 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020266 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020267
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020268 if (check_restricted() || check_secure())
20269 return;
20270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020271 if (argvars[0].v_type != VAR_LIST)
20272 {
20273 EMSG2(_(e_listarg), "writefile()");
20274 return;
20275 }
20276 if (argvars[0].vval.v_list == NULL)
20277 return;
20278
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020279 if (argvars[2].v_type != VAR_UNKNOWN)
20280 {
20281 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20282 binary = TRUE;
20283 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20284 append = TRUE;
20285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020286
20287 /* Always open the file in binary mode, library functions have a mind of
20288 * their own about CR-LF conversion. */
20289 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020290 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20291 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020292 {
20293 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20294 ret = -1;
20295 }
20296 else
20297 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020298 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20299 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020300 fclose(fd);
20301 }
20302
20303 rettv->vval.v_number = ret;
20304}
20305
20306/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020307 * "xor(expr, expr)" function
20308 */
20309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020310f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020311{
20312 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20313 ^ get_tv_number_chk(&argvars[1], NULL);
20314}
20315
20316
20317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020319 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 */
20321 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322var2fpos(
20323 typval_T *varp,
20324 int dollar_lnum, /* TRUE when $ is last line */
20325 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020327 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020329 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330
Bram Moolenaara5525202006-03-02 22:52:09 +000020331 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020332 if (varp->v_type == VAR_LIST)
20333 {
20334 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020335 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020336 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020337 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020338
20339 l = varp->vval.v_list;
20340 if (l == NULL)
20341 return NULL;
20342
20343 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020344 pos.lnum = list_find_nr(l, 0L, &error);
20345 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020346 return NULL; /* invalid line number */
20347
20348 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020349 pos.col = list_find_nr(l, 1L, &error);
20350 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020351 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020352 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020353
20354 /* We accept "$" for the column number: last column. */
20355 li = list_find(l, 1L);
20356 if (li != NULL && li->li_tv.v_type == VAR_STRING
20357 && li->li_tv.vval.v_string != NULL
20358 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20359 pos.col = len + 1;
20360
Bram Moolenaara5525202006-03-02 22:52:09 +000020361 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020362 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020363 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020364 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020365
Bram Moolenaara5525202006-03-02 22:52:09 +000020366#ifdef FEAT_VIRTUALEDIT
20367 /* Get the virtual offset. Defaults to zero. */
20368 pos.coladd = list_find_nr(l, 2L, &error);
20369 if (error)
20370 pos.coladd = 0;
20371#endif
20372
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020373 return &pos;
20374 }
20375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020376 name = get_tv_string_chk(varp);
20377 if (name == NULL)
20378 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020379 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020381 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20382 {
20383 if (VIsual_active)
20384 return &VIsual;
20385 return &curwin->w_cursor;
20386 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020387 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020389 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20391 return NULL;
20392 return pp;
20393 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020394
20395#ifdef FEAT_VIRTUALEDIT
20396 pos.coladd = 0;
20397#endif
20398
Bram Moolenaar477933c2007-07-17 14:32:23 +000020399 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020400 {
20401 pos.col = 0;
20402 if (name[1] == '0') /* "w0": first visible line */
20403 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020404 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020405 pos.lnum = curwin->w_topline;
20406 return &pos;
20407 }
20408 else if (name[1] == '$') /* "w$": last visible line */
20409 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020410 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020411 pos.lnum = curwin->w_botline - 1;
20412 return &pos;
20413 }
20414 }
20415 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020417 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
20419 pos.lnum = curbuf->b_ml.ml_line_count;
20420 pos.col = 0;
20421 }
20422 else
20423 {
20424 pos.lnum = curwin->w_cursor.lnum;
20425 pos.col = (colnr_T)STRLEN(ml_get_curline());
20426 }
20427 return &pos;
20428 }
20429 return NULL;
20430}
20431
20432/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020433 * Convert list in "arg" into a position and optional file number.
20434 * When "fnump" is NULL there is no file number, only 3 items.
20435 * Note that the column is passed on as-is, the caller may want to decrement
20436 * it to use 1 for the first column.
20437 * Return FAIL when conversion is not possible, doesn't check the position for
20438 * validity.
20439 */
20440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020441list2fpos(
20442 typval_T *arg,
20443 pos_T *posp,
20444 int *fnump,
20445 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020446{
20447 list_T *l = arg->vval.v_list;
20448 long i = 0;
20449 long n;
20450
Bram Moolenaar493c1782014-05-28 14:34:46 +020020451 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20452 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020453 if (arg->v_type != VAR_LIST
20454 || l == NULL
20455 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020456 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020457 return FAIL;
20458
20459 if (fnump != NULL)
20460 {
20461 n = list_find_nr(l, i++, NULL); /* fnum */
20462 if (n < 0)
20463 return FAIL;
20464 if (n == 0)
20465 n = curbuf->b_fnum; /* current buffer */
20466 *fnump = n;
20467 }
20468
20469 n = list_find_nr(l, i++, NULL); /* lnum */
20470 if (n < 0)
20471 return FAIL;
20472 posp->lnum = n;
20473
20474 n = list_find_nr(l, i++, NULL); /* col */
20475 if (n < 0)
20476 return FAIL;
20477 posp->col = n;
20478
20479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020480 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020481 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020482 posp->coladd = 0;
20483 else
20484 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020485#endif
20486
Bram Moolenaar493c1782014-05-28 14:34:46 +020020487 if (curswantp != NULL)
20488 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20489
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020490 return OK;
20491}
20492
20493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 * Get the length of an environment variable name.
20495 * Advance "arg" to the first character after the name.
20496 * Return 0 for error.
20497 */
20498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020499get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500{
20501 char_u *p;
20502 int len;
20503
20504 for (p = *arg; vim_isIDc(*p); ++p)
20505 ;
20506 if (p == *arg) /* no name found */
20507 return 0;
20508
20509 len = (int)(p - *arg);
20510 *arg = p;
20511 return len;
20512}
20513
20514/*
20515 * Get the length of the name of a function or internal variable.
20516 * "arg" is advanced to the first non-white character after the name.
20517 * Return 0 if something is wrong.
20518 */
20519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020520get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521{
20522 char_u *p;
20523 int len;
20524
20525 /* Find the end of the name. */
20526 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020527 {
20528 if (*p == ':')
20529 {
20530 /* "s:" is start of "s:var", but "n:" is not and can be used in
20531 * slice "[n:]". Also "xx:" is not a namespace. */
20532 len = (int)(p - *arg);
20533 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20534 || len > 1)
20535 break;
20536 }
20537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 if (p == *arg) /* no name found */
20539 return 0;
20540
20541 len = (int)(p - *arg);
20542 *arg = skipwhite(p);
20543
20544 return len;
20545}
20546
20547/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020548 * Get the length of the name of a variable or function.
20549 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020551 * Return -1 if curly braces expansion failed.
20552 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 * If the name contains 'magic' {}'s, expand them and return the
20554 * expanded name in an allocated string via 'alias' - caller must free.
20555 */
20556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020557get_name_len(
20558 char_u **arg,
20559 char_u **alias,
20560 int evaluate,
20561 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562{
20563 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 char_u *p;
20565 char_u *expr_start;
20566 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567
20568 *alias = NULL; /* default to no alias */
20569
20570 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20571 && (*arg)[2] == (int)KE_SNR)
20572 {
20573 /* hard coded <SNR>, already translated */
20574 *arg += 3;
20575 return get_id_len(arg) + 3;
20576 }
20577 len = eval_fname_script(*arg);
20578 if (len > 0)
20579 {
20580 /* literal "<SID>", "s:" or "<SNR>" */
20581 *arg += len;
20582 }
20583
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020585 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020587 p = find_name_end(*arg, &expr_start, &expr_end,
20588 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 if (expr_start != NULL)
20590 {
20591 char_u *temp_string;
20592
20593 if (!evaluate)
20594 {
20595 len += (int)(p - *arg);
20596 *arg = skipwhite(p);
20597 return len;
20598 }
20599
20600 /*
20601 * Include any <SID> etc in the expanded string:
20602 * Thus the -len here.
20603 */
20604 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20605 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020606 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 *alias = temp_string;
20608 *arg = skipwhite(p);
20609 return (int)STRLEN(temp_string);
20610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611
20612 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020613 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 EMSG2(_(e_invexpr2), *arg);
20615
20616 return len;
20617}
20618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020619/*
20620 * Find the end of a variable or function name, taking care of magic braces.
20621 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20622 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020623 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 * Return a pointer to just after the name. Equal to "arg" if there is no
20625 * valid name.
20626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628find_name_end(
20629 char_u *arg,
20630 char_u **expr_start,
20631 char_u **expr_end,
20632 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 int mb_nest = 0;
20635 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020637 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020641 *expr_start = NULL;
20642 *expr_end = NULL;
20643 }
20644
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020645 /* Quick check for valid starting character. */
20646 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20647 return arg;
20648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020649 for (p = arg; *p != NUL
20650 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020651 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020652 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020653 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020654 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020655 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020656 if (*p == '\'')
20657 {
20658 /* skip over 'string' to avoid counting [ and ] inside it. */
20659 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20660 ;
20661 if (*p == NUL)
20662 break;
20663 }
20664 else if (*p == '"')
20665 {
20666 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20667 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20668 if (*p == '\\' && p[1] != NUL)
20669 ++p;
20670 if (*p == NUL)
20671 break;
20672 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020673 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20674 {
20675 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020676 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020677 len = (int)(p - arg);
20678 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020679 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020680 break;
20681 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020683 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020685 if (*p == '[')
20686 ++br_nest;
20687 else if (*p == ']')
20688 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693 if (*p == '{')
20694 {
20695 mb_nest++;
20696 if (expr_start != NULL && *expr_start == NULL)
20697 *expr_start = p;
20698 }
20699 else if (*p == '}')
20700 {
20701 mb_nest--;
20702 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20703 *expr_end = p;
20704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 }
20707
20708 return p;
20709}
20710
20711/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020712 * Expands out the 'magic' {}'s in a variable/function name.
20713 * Note that this can call itself recursively, to deal with
20714 * constructs like foo{bar}{baz}{bam}
20715 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20716 * "in_start" ^
20717 * "expr_start" ^
20718 * "expr_end" ^
20719 * "in_end" ^
20720 *
20721 * Returns a new allocated string, which the caller must free.
20722 * Returns NULL for failure.
20723 */
20724 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020725make_expanded_name(
20726 char_u *in_start,
20727 char_u *expr_start,
20728 char_u *expr_end,
20729 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020730{
20731 char_u c1;
20732 char_u *retval = NULL;
20733 char_u *temp_result;
20734 char_u *nextcmd = NULL;
20735
20736 if (expr_end == NULL || in_end == NULL)
20737 return NULL;
20738 *expr_start = NUL;
20739 *expr_end = NUL;
20740 c1 = *in_end;
20741 *in_end = NUL;
20742
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020743 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020744 if (temp_result != NULL && nextcmd == NULL)
20745 {
20746 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20747 + (in_end - expr_end) + 1));
20748 if (retval != NULL)
20749 {
20750 STRCPY(retval, in_start);
20751 STRCAT(retval, temp_result);
20752 STRCAT(retval, expr_end + 1);
20753 }
20754 }
20755 vim_free(temp_result);
20756
20757 *in_end = c1; /* put char back for error messages */
20758 *expr_start = '{';
20759 *expr_end = '}';
20760
20761 if (retval != NULL)
20762 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020763 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020764 if (expr_start != NULL)
20765 {
20766 /* Further expansion! */
20767 temp_result = make_expanded_name(retval, expr_start,
20768 expr_end, temp_result);
20769 vim_free(retval);
20770 retval = temp_result;
20771 }
20772 }
20773
20774 return retval;
20775}
20776
20777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020779 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 */
20781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020784 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20785}
20786
20787/*
20788 * Return TRUE if character "c" can be used as the first character in a
20789 * variable or function name (excluding '{' and '}').
20790 */
20791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020793{
20794 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795}
20796
20797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 * Set number v: variable to "val".
20799 */
20800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020801set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020803 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804}
20805
20806/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020807 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 */
20809 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020810get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020812 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813}
20814
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020815/*
20816 * Get string v: variable value. Uses a static buffer, can only be used once.
20817 */
20818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020819get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020820{
20821 return get_tv_string(&vimvars[idx].vv_tv);
20822}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020823
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020825 * Get List v: variable value. Caller must take care of reference count when
20826 * needed.
20827 */
20828 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020829get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020830{
20831 return vimvars[idx].vv_list;
20832}
20833
20834/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020835 * Set v:char to character "c".
20836 */
20837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020838set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020839{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020840 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020841
20842#ifdef FEAT_MBYTE
20843 if (has_mbyte)
20844 buf[(*mb_char2bytes)(c, buf)] = NUL;
20845 else
20846#endif
20847 {
20848 buf[0] = c;
20849 buf[1] = NUL;
20850 }
20851 set_vim_var_string(VV_CHAR, buf, -1);
20852}
20853
20854/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020855 * Set v:count to "count" and v:count1 to "count1".
20856 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 */
20858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020859set_vcount(
20860 long count,
20861 long count1,
20862 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020864 if (set_prevcount)
20865 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020866 vimvars[VV_COUNT].vv_nr = count;
20867 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868}
20869
20870/*
20871 * Set string v: variable to a copy of "val".
20872 */
20873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020874set_vim_var_string(
20875 int idx,
20876 char_u *val,
20877 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878{
Bram Moolenaara542c682016-01-31 16:28:04 +010020879 clear_tv(&vimvars[idx].vv_di.di_tv);
20880 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020882 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020884 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020886 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887}
20888
20889/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020890 * Set List v: variable to "val".
20891 */
20892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020893set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020894{
Bram Moolenaara542c682016-01-31 16:28:04 +010020895 clear_tv(&vimvars[idx].vv_di.di_tv);
20896 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020897 vimvars[idx].vv_list = val;
20898 if (val != NULL)
20899 ++val->lv_refcount;
20900}
20901
20902/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020903 * Set Dictionary v: variable to "val".
20904 */
20905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020906set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020907{
20908 int todo;
20909 hashitem_T *hi;
20910
Bram Moolenaara542c682016-01-31 16:28:04 +010020911 clear_tv(&vimvars[idx].vv_di.di_tv);
20912 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020913 vimvars[idx].vv_dict = val;
20914 if (val != NULL)
20915 {
20916 ++val->dv_refcount;
20917
20918 /* Set readonly */
20919 todo = (int)val->dv_hashtab.ht_used;
20920 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20921 {
20922 if (HASHITEM_EMPTY(hi))
20923 continue;
20924 --todo;
20925 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20926 }
20927 }
20928}
20929
20930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 * Set v:register if needed.
20932 */
20933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020934set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935{
20936 char_u regname;
20937
20938 if (c == 0 || c == ' ')
20939 regname = '"';
20940 else
20941 regname = c;
20942 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020943 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 set_vim_var_string(VV_REG, &regname, 1);
20945}
20946
20947/*
20948 * Get or set v:exception. If "oldval" == NULL, return the current value.
20949 * Otherwise, restore the value to "oldval" and return NULL.
20950 * Must always be called in pairs to save and restore v:exception! Does not
20951 * take care of memory allocations.
20952 */
20953 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020954v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955{
20956 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020957 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
Bram Moolenaare9a41262005-01-15 22:18:47 +000020959 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 return NULL;
20961}
20962
20963/*
20964 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20965 * Otherwise, restore the value to "oldval" and return NULL.
20966 * Must always be called in pairs to save and restore v:throwpoint! Does not
20967 * take care of memory allocations.
20968 */
20969 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020970v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971{
20972 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020973 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974
Bram Moolenaare9a41262005-01-15 22:18:47 +000020975 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 return NULL;
20977}
20978
20979#if defined(FEAT_AUTOCMD) || defined(PROTO)
20980/*
20981 * Set v:cmdarg.
20982 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20983 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20984 * Must always be called in pairs!
20985 */
20986 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020987set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988{
20989 char_u *oldval;
20990 char_u *newval;
20991 unsigned len;
20992
Bram Moolenaare9a41262005-01-15 22:18:47 +000020993 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020994 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020996 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020997 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020998 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 }
21000
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021001 if (eap->force_bin == FORCE_BIN)
21002 len = 6;
21003 else if (eap->force_bin == FORCE_NOBIN)
21004 len = 8;
21005 else
21006 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021007
21008 if (eap->read_edit)
21009 len += 7;
21010
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021011 if (eap->force_ff != 0)
21012 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21013# ifdef FEAT_MBYTE
21014 if (eap->force_enc != 0)
21015 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021016 if (eap->bad_char != 0)
21017 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021018# endif
21019
21020 newval = alloc(len + 1);
21021 if (newval == NULL)
21022 return NULL;
21023
21024 if (eap->force_bin == FORCE_BIN)
21025 sprintf((char *)newval, " ++bin");
21026 else if (eap->force_bin == FORCE_NOBIN)
21027 sprintf((char *)newval, " ++nobin");
21028 else
21029 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021030
21031 if (eap->read_edit)
21032 STRCAT(newval, " ++edit");
21033
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021034 if (eap->force_ff != 0)
21035 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21036 eap->cmd + eap->force_ff);
21037# ifdef FEAT_MBYTE
21038 if (eap->force_enc != 0)
21039 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21040 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021041 if (eap->bad_char == BAD_KEEP)
21042 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21043 else if (eap->bad_char == BAD_DROP)
21044 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21045 else if (eap->bad_char != 0)
21046 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021047# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021048 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021049 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050}
21051#endif
21052
21053/*
21054 * Get the value of internal variable "name".
21055 * Return OK or FAIL.
21056 */
21057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058get_var_tv(
21059 char_u *name,
21060 int len, /* length of "name" */
21061 typval_T *rettv, /* NULL when only checking existence */
21062 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21063 int verbose, /* may give error message */
21064 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065{
21066 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021067 typval_T *tv = NULL;
21068 typval_T atv;
21069 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071
21072 /* truncate the name, so that we can use strcmp() */
21073 cc = name[len];
21074 name[len] = NUL;
21075
21076 /*
21077 * Check for "b:changedtick".
21078 */
21079 if (STRCMP(name, "b:changedtick") == 0)
21080 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021081 atv.v_type = VAR_NUMBER;
21082 atv.vval.v_number = curbuf->b_changedtick;
21083 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 }
21085
21086 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * Check for user-defined variables.
21088 */
21089 else
21090 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021091 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021094 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021095 if (dip != NULL)
21096 *dip = v;
21097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 }
21099
Bram Moolenaare9a41262005-01-15 22:18:47 +000021100 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021102 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021103 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 ret = FAIL;
21105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021106 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021107 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108
21109 name[len] = cc;
21110
21111 return ret;
21112}
21113
21114/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21116 * Also handle function call with Funcref variable: func(expr)
21117 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21118 */
21119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021120handle_subscript(
21121 char_u **arg,
21122 typval_T *rettv,
21123 int evaluate, /* do more than finding the end */
21124 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021125{
21126 int ret = OK;
21127 dict_T *selfdict = NULL;
21128 char_u *s;
21129 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021130 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021131
21132 while (ret == OK
21133 && (**arg == '['
21134 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021135 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021136 && !vim_iswhite(*(*arg - 1)))
21137 {
21138 if (**arg == '(')
21139 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021140 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021141 if (evaluate)
21142 {
21143 functv = *rettv;
21144 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021145
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021146 /* Invoke the function. Recursive! */
21147 s = functv.vval.v_string;
21148 }
21149 else
21150 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021151 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021152 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21153 &len, evaluate, selfdict);
21154
21155 /* Clear the funcref afterwards, so that deleting it while
21156 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021157 if (evaluate)
21158 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159
21160 /* Stop the expression evaluation when immediately aborting on
21161 * error, or when an interrupt occurred or an exception was thrown
21162 * but not caught. */
21163 if (aborting())
21164 {
21165 if (ret == OK)
21166 clear_tv(rettv);
21167 ret = FAIL;
21168 }
21169 dict_unref(selfdict);
21170 selfdict = NULL;
21171 }
21172 else /* **arg == '[' || **arg == '.' */
21173 {
21174 dict_unref(selfdict);
21175 if (rettv->v_type == VAR_DICT)
21176 {
21177 selfdict = rettv->vval.v_dict;
21178 if (selfdict != NULL)
21179 ++selfdict->dv_refcount;
21180 }
21181 else
21182 selfdict = NULL;
21183 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21184 {
21185 clear_tv(rettv);
21186 ret = FAIL;
21187 }
21188 }
21189 }
21190 dict_unref(selfdict);
21191 return ret;
21192}
21193
21194/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021195 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021196 * value).
21197 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021198 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021199alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021200{
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021202}
21203
21204/*
21205 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 * The string "s" must have been allocated, it is consumed.
21207 * Return NULL for out of memory, the variable otherwise.
21208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021209 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211{
Bram Moolenaar33570922005-01-25 22:26:29 +000021212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 rettv = alloc_tv();
21215 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 rettv->v_type = VAR_STRING;
21218 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 }
21220 else
21221 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021222 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223}
21224
21225/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021226 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230{
21231 if (varp != NULL)
21232 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233 switch (varp->v_type)
21234 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021235 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021236 func_unref(varp->vval.v_string);
21237 /*FALLTHROUGH*/
21238 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 vim_free(varp->vval.v_string);
21240 break;
21241 case VAR_LIST:
21242 list_unref(varp->vval.v_list);
21243 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244 case VAR_DICT:
21245 dict_unref(varp->vval.v_dict);
21246 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021247 case VAR_JOB:
21248#ifdef FEAT_JOB
21249 job_unref(varp->vval.v_job);
21250 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021251#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021252 case VAR_NUMBER:
21253 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021254 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021255 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021256 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 vim_free(varp);
21259 }
21260}
21261
21262/*
21263 * Free the memory for a variable value and set the value to NULL or 0.
21264 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021266clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267{
21268 if (varp != NULL)
21269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021270 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021272 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021273 func_unref(varp->vval.v_string);
21274 /*FALLTHROUGH*/
21275 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021276 vim_free(varp->vval.v_string);
21277 varp->vval.v_string = NULL;
21278 break;
21279 case VAR_LIST:
21280 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021281 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021282 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021283 case VAR_DICT:
21284 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021285 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021286 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021287 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021288 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 varp->vval.v_number = 0;
21290 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021291 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021292#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021293 varp->vval.v_float = 0.0;
21294 break;
21295#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021296 case VAR_JOB:
21297#ifdef FEAT_JOB
21298 job_unref(varp->vval.v_job);
21299 varp->vval.v_job = NULL;
21300#endif
21301 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021302 case VAR_UNKNOWN:
21303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021305 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 }
21307}
21308
21309/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 * Set the value of a variable to NULL without freeing items.
21311 */
21312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021314{
21315 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021316 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021317}
21318
21319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 * Get the number value of a variable.
21321 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021322 * For incompatible types, return 0.
21323 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21324 * caller of incompatible types: it sets *denote to TRUE if "denote"
21325 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 */
21327 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021330 int error = FALSE;
21331
21332 return get_tv_number_chk(varp, &error); /* return 0L on error */
21333}
21334
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021335 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021336get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021337{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021338 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021340 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021342 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021343 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021344 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021345#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021346 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021347 break;
21348#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021349 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021350 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021351 break;
21352 case VAR_STRING:
21353 if (varp->vval.v_string != NULL)
21354 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021355 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021356 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021357 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021358 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021359 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021360 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021361 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021362 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021363 case VAR_SPECIAL:
21364 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21365 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021366 case VAR_JOB:
21367#ifdef FEAT_JOB
21368 EMSG(_("E910: Using a Job as a Number"));
21369 break;
21370#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021371 case VAR_UNKNOWN:
21372 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021375 if (denote == NULL) /* useful for values that must be unsigned */
21376 n = -1;
21377 else
21378 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021379 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380}
21381
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021382#ifdef FEAT_FLOAT
21383 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021384get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021385{
21386 switch (varp->v_type)
21387 {
21388 case VAR_NUMBER:
21389 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021390 case VAR_FLOAT:
21391 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021392 case VAR_FUNC:
21393 EMSG(_("E891: Using a Funcref as a Float"));
21394 break;
21395 case VAR_STRING:
21396 EMSG(_("E892: Using a String as a Float"));
21397 break;
21398 case VAR_LIST:
21399 EMSG(_("E893: Using a List as a Float"));
21400 break;
21401 case VAR_DICT:
21402 EMSG(_("E894: Using a Dictionary as a Float"));
21403 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021404 case VAR_SPECIAL:
21405 EMSG(_("E907: Using a special value as a Float"));
21406 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021407 case VAR_JOB:
21408# ifdef FEAT_JOB
21409 EMSG(_("E911: Using a Job as a Float"));
21410 break;
21411# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021412 case VAR_UNKNOWN:
21413 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021414 break;
21415 }
21416 return 0;
21417}
21418#endif
21419
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021421 * Get the lnum from the first argument.
21422 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021423 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 */
21425 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021426get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427{
Bram Moolenaar33570922005-01-25 22:26:29 +000021428 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 linenr_T lnum;
21430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021431 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 if (lnum == 0) /* no valid number, try using line() */
21433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 rettv.v_type = VAR_NUMBER;
21435 f_line(argvars, &rettv);
21436 lnum = rettv.vval.v_number;
21437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 }
21439 return lnum;
21440}
21441
21442/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021443 * Get the lnum from the first argument.
21444 * Also accepts "$", then "buf" is used.
21445 * Returns 0 on error.
21446 */
21447 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021448get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021449{
21450 if (argvars[0].v_type == VAR_STRING
21451 && argvars[0].vval.v_string != NULL
21452 && argvars[0].vval.v_string[0] == '$'
21453 && buf != NULL)
21454 return buf->b_ml.ml_line_count;
21455 return get_tv_number_chk(&argvars[0], NULL);
21456}
21457
21458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 * Get the string value of a variable.
21460 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021461 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21462 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 * If the String variable has never been set, return an empty string.
21464 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021465 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21466 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 */
21468 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021469get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
21471 static char_u mybuf[NUMBUFLEN];
21472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021473 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474}
21475
21476 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021477get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021479 char_u *res = get_tv_string_buf_chk(varp, buf);
21480
21481 return res != NULL ? res : (char_u *)"";
21482}
21483
Bram Moolenaar7d647822014-04-05 21:28:56 +020021484/*
21485 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21486 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021488get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021489{
21490 static char_u mybuf[NUMBUFLEN];
21491
21492 return get_tv_string_buf_chk(varp, mybuf);
21493}
21494
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021495 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021496get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021497{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500 case VAR_NUMBER:
21501 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21502 return buf;
21503 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505 break;
21506 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021508 break;
21509 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021510 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021511 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021512 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021513#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021514 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021515 break;
21516#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021517 case VAR_STRING:
21518 if (varp->vval.v_string != NULL)
21519 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021520 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021521 case VAR_SPECIAL:
21522 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21523 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021524 case VAR_JOB:
21525#ifdef FEAT_JOB
21526 {
21527 job_T *job = varp->vval.v_job;
21528 char *status = job->jv_status == JOB_FAILED ? "fail"
21529 : job->jv_status == JOB_ENDED ? "dead"
21530 : "run";
21531# ifdef UNIX
21532 vim_snprintf((char *)buf, NUMBUFLEN,
21533 "process %ld %s", (long)job->jv_pid, status);
21534# else
21535 /* TODO */
21536 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21537# endif
21538 return buf;
21539 }
21540#endif
21541 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021542 case VAR_UNKNOWN:
21543 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021546 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547}
21548
21549/*
21550 * Find variable "name" in the list of variables.
21551 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021553 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021557find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561
Bram Moolenaara7043832005-01-21 11:56:39 +000021562 ht = find_var_ht(name, &varname);
21563 if (htp != NULL)
21564 *htp = ht;
21565 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021567 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568}
21569
21570/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021571 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021572 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021575find_var_in_ht(
21576 hashtab_T *ht,
21577 int htname,
21578 char_u *varname,
21579 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021580{
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 hashitem_T *hi;
21582
21583 if (*varname == NUL)
21584 {
21585 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021586 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021588 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 case 'g': return &globvars_var;
21590 case 'v': return &vimvars_var;
21591 case 'b': return &curbuf->b_bufvar;
21592 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021593#ifdef FEAT_WINDOWS
21594 case 't': return &curtab->tp_winvar;
21595#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021596 case 'l': return current_funccal == NULL
21597 ? NULL : &current_funccal->l_vars_var;
21598 case 'a': return current_funccal == NULL
21599 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021600 }
21601 return NULL;
21602 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021603
21604 hi = hash_find(ht, varname);
21605 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021606 {
21607 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021608 * worked find the variable again. Don't auto-load a script if it was
21609 * loaded already, otherwise it would be loaded every time when
21610 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021611 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021612 {
21613 /* Note: script_autoload() may make "hi" invalid. It must either
21614 * be obtained again or not used. */
21615 if (!script_autoload(varname, FALSE) || aborting())
21616 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021617 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021618 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 if (HASHITEM_EMPTY(hi))
21620 return NULL;
21621 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021623}
21624
21625/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021627 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021628 * Set "varname" to the start of name without ':'.
21629 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021633 hashitem_T *hi;
21634
Bram Moolenaar73627d02015-08-11 15:46:09 +020021635 if (name[0] == NUL)
21636 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 if (name[1] != ':')
21638 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021639 /* The name must not start with a colon or #. */
21640 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 return NULL;
21642 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021643
21644 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021645 hi = hash_find(&compat_hashtab, name);
21646 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021647 return &compat_hashtab;
21648
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021650 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021651 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 }
21653 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021654 if (*name == 'g') /* global variable */
21655 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021656 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21657 */
21658 if (vim_strchr(name + 2, ':') != NULL
21659 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021662 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021664 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021665#ifdef FEAT_WINDOWS
21666 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021667 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 if (*name == 'v') /* v: variable */
21670 return &vimvarht;
21671 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021672 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021674 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 if (*name == 's' /* script variable */
21676 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21677 return &SCRIPT_VARS(current_SID);
21678 return NULL;
21679}
21680
21681/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021682 * Get function call environment based on bactrace debug level
21683 */
21684 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021685get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021686{
21687 int i;
21688 funccall_T *funccal;
21689 funccall_T *temp_funccal;
21690
21691 funccal = current_funccal;
21692 if (debug_backtrace_level > 0)
21693 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021694 for (i = 0; i < debug_backtrace_level; i++)
21695 {
21696 temp_funccal = funccal->caller;
21697 if (temp_funccal)
21698 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021699 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021700 /* backtrace level overflow. reset to max */
21701 debug_backtrace_level = i;
21702 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021703 }
21704 return funccal;
21705}
21706
21707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021709 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 * Returns NULL when it doesn't exist.
21711 */
21712 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021713get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714{
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021717 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 if (v == NULL)
21719 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721}
21722
21723/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 * sourcing this script and when executing functions defined in the script.
21726 */
21727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021728new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729{
Bram Moolenaara7043832005-01-21 11:56:39 +000021730 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 hashtab_T *ht;
21732 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021733
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21735 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 /* Re-allocating ga_data means that an ht_array pointing to
21737 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021739 for (i = 1; i <= ga_scripts.ga_len; ++i)
21740 {
21741 ht = &SCRIPT_VARS(i);
21742 if (ht->ht_mask == HT_INIT_SIZE - 1)
21743 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021744 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021745 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021746 }
21747
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 while (ga_scripts.ga_len < id)
21749 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021750 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021751 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021752 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 }
21755 }
21756}
21757
21758/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21760 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 */
21762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021763init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764{
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021766 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021767 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021768 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021769 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 dict_var->di_tv.vval.v_dict = dict;
21771 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021772 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21774 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775}
21776
21777/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021778 * Unreference a dictionary initialized by init_var_dict().
21779 */
21780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021781unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021782{
21783 /* Now the dict needs to be freed if no one else is using it, go back to
21784 * normal reference counting. */
21785 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21786 dict_unref(dict);
21787}
21788
21789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 * Frees all allocated variables and the value they contain.
21792 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 */
21794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021795vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021796{
21797 vars_clear_ext(ht, TRUE);
21798}
21799
21800/*
21801 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21802 */
21803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021804vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805{
Bram Moolenaara7043832005-01-21 11:56:39 +000021806 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 hashitem_T *hi;
21808 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021811 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021812 for (hi = ht->ht_array; todo > 0; ++hi)
21813 {
21814 if (!HASHITEM_EMPTY(hi))
21815 {
21816 --todo;
21817
Bram Moolenaar33570922005-01-25 22:26:29 +000021818 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021819 * ht_array might change then. hash_clear() takes care of it
21820 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 v = HI2DI(hi);
21822 if (free_val)
21823 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021824 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021826 }
21827 }
21828 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021829 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830}
21831
Bram Moolenaara7043832005-01-21 11:56:39 +000021832/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021833 * Delete a variable from hashtab "ht" at item "hi".
21834 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021837delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838{
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021840
21841 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 clear_tv(&di->di_tv);
21843 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844}
21845
21846/*
21847 * List the value of one internal variable.
21848 */
21849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021850list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021852 char_u *tofree;
21853 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021854 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021855
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021856 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021858 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021859 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860}
21861
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021863list_one_var_a(
21864 char_u *prefix,
21865 char_u *name,
21866 int type,
21867 char_u *string,
21868 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869{
Bram Moolenaar31859182007-08-14 20:41:13 +000021870 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21871 msg_start();
21872 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 if (name != NULL) /* "a:" vars don't have a name stored */
21874 msg_puts(name);
21875 msg_putchar(' ');
21876 msg_advance(22);
21877 if (type == VAR_NUMBER)
21878 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021879 else if (type == VAR_FUNC)
21880 msg_putchar('*');
21881 else if (type == VAR_LIST)
21882 {
21883 msg_putchar('[');
21884 if (*string == '[')
21885 ++string;
21886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021887 else if (type == VAR_DICT)
21888 {
21889 msg_putchar('{');
21890 if (*string == '{')
21891 ++string;
21892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 else
21894 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021895
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897
21898 if (type == VAR_FUNC)
21899 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021900 if (*first)
21901 {
21902 msg_clr_eos();
21903 *first = FALSE;
21904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905}
21906
21907/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021908 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 * If the variable already exists, the value is updated.
21910 * Otherwise the variable is created.
21911 */
21912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913set_var(
21914 char_u *name,
21915 typval_T *tv,
21916 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917{
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021920 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021922 ht = find_var_ht(name, &varname);
21923 if (ht == NULL || *varname == NUL)
21924 {
21925 EMSG2(_(e_illvar), name);
21926 return;
21927 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021928 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021929
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021930 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21931 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021932
Bram Moolenaar33570922005-01-25 22:26:29 +000021933 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021935 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021936 if (var_check_ro(v->di_flags, name, FALSE)
21937 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021938 return;
21939 if (v->di_tv.v_type != tv->v_type
21940 && !((v->di_tv.v_type == VAR_STRING
21941 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021942 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021943 || tv->v_type == VAR_NUMBER))
21944#ifdef FEAT_FLOAT
21945 && !((v->di_tv.v_type == VAR_NUMBER
21946 || v->di_tv.v_type == VAR_FLOAT)
21947 && (tv->v_type == VAR_NUMBER
21948 || tv->v_type == VAR_FLOAT))
21949#endif
21950 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021951 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021952 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 return;
21954 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021955
21956 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021957 * Handle setting internal v: variables separately where needed to
21958 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 */
21960 if (ht == &vimvarht)
21961 {
21962 if (v->di_tv.v_type == VAR_STRING)
21963 {
21964 vim_free(v->di_tv.vval.v_string);
21965 if (copy || tv->v_type != VAR_STRING)
21966 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21967 else
21968 {
21969 /* Take over the string to avoid an extra alloc/free. */
21970 v->di_tv.vval.v_string = tv->vval.v_string;
21971 tv->vval.v_string = NULL;
21972 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021973 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021975 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021977 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021978 if (STRCMP(varname, "searchforward") == 0)
21979 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021980#ifdef FEAT_SEARCH_EXTRA
21981 else if (STRCMP(varname, "hlsearch") == 0)
21982 {
21983 no_hlsearch = !v->di_tv.vval.v_number;
21984 redraw_all_later(SOME_VALID);
21985 }
21986#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021987 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021988 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021989 else if (v->di_tv.v_type != tv->v_type)
21990 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 }
21992
21993 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 }
21995 else /* add a new variable */
21996 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021997 /* Can't add "v:" variable. */
21998 if (ht == &vimvarht)
21999 {
22000 EMSG2(_(e_illvar), name);
22001 return;
22002 }
22003
Bram Moolenaar92124a32005-06-17 22:03:40 +000022004 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022005 if (!valid_varname(varname))
22006 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022007
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022008 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22009 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022010 if (v == NULL)
22011 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022015 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 return;
22017 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022018 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022020
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022021 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022023 else
22024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022026 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022027 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029}
22030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022032 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 * Also give an error message.
22034 */
22035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022036var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022037{
22038 if (flags & DI_FLAGS_RO)
22039 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022040 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 return TRUE;
22042 }
22043 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22044 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022045 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 return TRUE;
22047 }
22048 return FALSE;
22049}
22050
22051/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022052 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22053 * Also give an error message.
22054 */
22055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022056var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022057{
22058 if (flags & DI_FLAGS_FIX)
22059 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022060 EMSG2(_("E795: Cannot delete variable %s"),
22061 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022062 return TRUE;
22063 }
22064 return FALSE;
22065}
22066
22067/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022068 * Check if a funcref is assigned to a valid variable name.
22069 * Return TRUE and give an error if not.
22070 */
22071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022072var_check_func_name(
22073 char_u *name, /* points to start of variable name */
22074 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022075{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022076 /* Allow for w: b: s: and t:. */
22077 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022078 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22079 ? name[2] : name[0]))
22080 {
22081 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22082 name);
22083 return TRUE;
22084 }
22085 /* Don't allow hiding a function. When "v" is not NULL we might be
22086 * assigning another function to the same var, the type is checked
22087 * below. */
22088 if (new_var && function_exists(name))
22089 {
22090 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22091 name);
22092 return TRUE;
22093 }
22094 return FALSE;
22095}
22096
22097/*
22098 * Check if a variable name is valid.
22099 * Return FALSE and give an error if not.
22100 */
22101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022103{
22104 char_u *p;
22105
22106 for (p = varname; *p != NUL; ++p)
22107 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22108 && *p != AUTOLOAD_CHAR)
22109 {
22110 EMSG2(_(e_illvar), varname);
22111 return FALSE;
22112 }
22113 return TRUE;
22114}
22115
22116/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022117 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022118 * Also give an error message, using "name" or _("name") when use_gettext is
22119 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022120 */
22121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022122tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022123{
22124 if (lock & VAR_LOCKED)
22125 {
22126 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022127 name == NULL ? (char_u *)_("Unknown")
22128 : use_gettext ? (char_u *)_(name)
22129 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022130 return TRUE;
22131 }
22132 if (lock & VAR_FIXED)
22133 {
22134 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022135 name == NULL ? (char_u *)_("Unknown")
22136 : use_gettext ? (char_u *)_(name)
22137 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022138 return TRUE;
22139 }
22140 return FALSE;
22141}
22142
22143/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022144 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022146 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022147 * It is OK for "from" and "to" to point to the same item. This is used to
22148 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022149 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022151copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022153 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022154 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155 switch (from->v_type)
22156 {
22157 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022158 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022159 to->vval.v_number = from->vval.v_number;
22160 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022161 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022162#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022163 to->vval.v_float = from->vval.v_float;
22164 break;
22165#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022166 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022167#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022168 to->vval.v_job = from->vval.v_job;
22169 ++to->vval.v_job->jv_refcount;
22170 break;
22171#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022172 case VAR_STRING:
22173 case VAR_FUNC:
22174 if (from->vval.v_string == NULL)
22175 to->vval.v_string = NULL;
22176 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022178 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022179 if (from->v_type == VAR_FUNC)
22180 func_ref(to->vval.v_string);
22181 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 break;
22183 case VAR_LIST:
22184 if (from->vval.v_list == NULL)
22185 to->vval.v_list = NULL;
22186 else
22187 {
22188 to->vval.v_list = from->vval.v_list;
22189 ++to->vval.v_list->lv_refcount;
22190 }
22191 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022192 case VAR_DICT:
22193 if (from->vval.v_dict == NULL)
22194 to->vval.v_dict = NULL;
22195 else
22196 {
22197 to->vval.v_dict = from->vval.v_dict;
22198 ++to->vval.v_dict->dv_refcount;
22199 }
22200 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022201 case VAR_UNKNOWN:
22202 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022203 break;
22204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205}
22206
22207/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022208 * Make a copy of an item.
22209 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022210 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22211 * reference to an already copied list/dict can be used.
22212 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022213 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215item_copy(
22216 typval_T *from,
22217 typval_T *to,
22218 int deep,
22219 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022220{
22221 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022222 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022223
Bram Moolenaar33570922005-01-25 22:26:29 +000022224 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022225 {
22226 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022227 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022228 }
22229 ++recurse;
22230
22231 switch (from->v_type)
22232 {
22233 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022234 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022235 case VAR_STRING:
22236 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022237 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022238 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 copy_tv(from, to);
22240 break;
22241 case VAR_LIST:
22242 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022243 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022244 if (from->vval.v_list == NULL)
22245 to->vval.v_list = NULL;
22246 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22247 {
22248 /* use the copy made earlier */
22249 to->vval.v_list = from->vval.v_list->lv_copylist;
22250 ++to->vval.v_list->lv_refcount;
22251 }
22252 else
22253 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22254 if (to->vval.v_list == NULL)
22255 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022256 break;
22257 case VAR_DICT:
22258 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022259 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022260 if (from->vval.v_dict == NULL)
22261 to->vval.v_dict = NULL;
22262 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22263 {
22264 /* use the copy made earlier */
22265 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22266 ++to->vval.v_dict->dv_refcount;
22267 }
22268 else
22269 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22270 if (to->vval.v_dict == NULL)
22271 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022272 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022273 case VAR_UNKNOWN:
22274 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022275 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022276 }
22277 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022278 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022279}
22280
22281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 * ":echo expr1 ..." print each argument separated with a space, add a
22283 * newline at the end.
22284 * ":echon expr1 ..." print each argument plain.
22285 */
22286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022287ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288{
22289 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022291 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 char_u *p;
22293 int needclr = TRUE;
22294 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022295 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296
22297 if (eap->skip)
22298 ++emsg_skip;
22299 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22300 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022301 /* If eval1() causes an error message the text from the command may
22302 * still need to be cleared. E.g., "echo 22,44". */
22303 need_clr_eos = needclr;
22304
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022306 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 {
22308 /*
22309 * Report the invalid expression unless the expression evaluation
22310 * has been cancelled due to an aborting error, an interrupt, or an
22311 * exception.
22312 */
22313 if (!aborting())
22314 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022315 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 break;
22317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022318 need_clr_eos = FALSE;
22319
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 if (!eap->skip)
22321 {
22322 if (atstart)
22323 {
22324 atstart = FALSE;
22325 /* Call msg_start() after eval1(), evaluating the expression
22326 * may cause a message to appear. */
22327 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022328 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022329 /* Mark the saved text as finishing the line, so that what
22330 * follows is displayed on a new line when scrolling back
22331 * at the more prompt. */
22332 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 }
22336 else if (eap->cmdidx == CMD_echo)
22337 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022338 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022339 if (p != NULL)
22340 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022342 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022344 if (*p != TAB && needclr)
22345 {
22346 /* remove any text still there from the command */
22347 msg_clr_eos();
22348 needclr = FALSE;
22349 }
22350 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 }
22352 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022353 {
22354#ifdef FEAT_MBYTE
22355 if (has_mbyte)
22356 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022357 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022358
22359 (void)msg_outtrans_len_attr(p, i, echo_attr);
22360 p += i - 1;
22361 }
22362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022364 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022367 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022369 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 arg = skipwhite(arg);
22371 }
22372 eap->nextcmd = check_nextcmd(arg);
22373
22374 if (eap->skip)
22375 --emsg_skip;
22376 else
22377 {
22378 /* remove text that may still be there from the command */
22379 if (needclr)
22380 msg_clr_eos();
22381 if (eap->cmdidx == CMD_echo)
22382 msg_end();
22383 }
22384}
22385
22386/*
22387 * ":echohl {name}".
22388 */
22389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022390ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391{
22392 int id;
22393
22394 id = syn_name2id(eap->arg);
22395 if (id == 0)
22396 echo_attr = 0;
22397 else
22398 echo_attr = syn_id2attr(id);
22399}
22400
22401/*
22402 * ":execute expr1 ..." execute the result of an expression.
22403 * ":echomsg expr1 ..." Print a message
22404 * ":echoerr expr1 ..." Print an error
22405 * Each gets spaces around each argument and a newline at the end for
22406 * echo commands
22407 */
22408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022409ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410{
22411 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 int ret = OK;
22414 char_u *p;
22415 garray_T ga;
22416 int len;
22417 int save_did_emsg;
22418
22419 ga_init2(&ga, 1, 80);
22420
22421 if (eap->skip)
22422 ++emsg_skip;
22423 while (*arg != NUL && *arg != '|' && *arg != '\n')
22424 {
22425 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022426 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 {
22428 /*
22429 * Report the invalid expression unless the expression evaluation
22430 * has been cancelled due to an aborting error, an interrupt, or an
22431 * exception.
22432 */
22433 if (!aborting())
22434 EMSG2(_(e_invexpr2), p);
22435 ret = FAIL;
22436 break;
22437 }
22438
22439 if (!eap->skip)
22440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022441 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 len = (int)STRLEN(p);
22443 if (ga_grow(&ga, len + 2) == FAIL)
22444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022445 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 ret = FAIL;
22447 break;
22448 }
22449 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 ga.ga_len += len;
22453 }
22454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 arg = skipwhite(arg);
22457 }
22458
22459 if (ret != FAIL && ga.ga_data != NULL)
22460 {
22461 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022464 out_flush();
22465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 else if (eap->cmdidx == CMD_echoerr)
22467 {
22468 /* We don't want to abort following commands, restore did_emsg. */
22469 save_did_emsg = did_emsg;
22470 EMSG((char_u *)ga.ga_data);
22471 if (!force_abort)
22472 did_emsg = save_did_emsg;
22473 }
22474 else if (eap->cmdidx == CMD_execute)
22475 do_cmdline((char_u *)ga.ga_data,
22476 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22477 }
22478
22479 ga_clear(&ga);
22480
22481 if (eap->skip)
22482 --emsg_skip;
22483
22484 eap->nextcmd = check_nextcmd(arg);
22485}
22486
22487/*
22488 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22489 * "arg" points to the "&" or '+' when called, to "option" when returning.
22490 * Returns NULL when no option name found. Otherwise pointer to the char
22491 * after the option name.
22492 */
22493 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022494find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495{
22496 char_u *p = *arg;
22497
22498 ++p;
22499 if (*p == 'g' && p[1] == ':')
22500 {
22501 *opt_flags = OPT_GLOBAL;
22502 p += 2;
22503 }
22504 else if (*p == 'l' && p[1] == ':')
22505 {
22506 *opt_flags = OPT_LOCAL;
22507 p += 2;
22508 }
22509 else
22510 *opt_flags = 0;
22511
22512 if (!ASCII_ISALPHA(*p))
22513 return NULL;
22514 *arg = p;
22515
22516 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22517 p += 4; /* termcap option */
22518 else
22519 while (ASCII_ISALPHA(*p))
22520 ++p;
22521 return p;
22522}
22523
22524/*
22525 * ":function"
22526 */
22527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022528ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529{
22530 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022531 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 int j;
22533 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022535 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 char_u *name = NULL;
22537 char_u *p;
22538 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022539 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 garray_T newargs;
22541 garray_T newlines;
22542 int varargs = FALSE;
22543 int mustend = FALSE;
22544 int flags = 0;
22545 ufunc_T *fp;
22546 int indent;
22547 int nesting;
22548 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 dictitem_T *v;
22550 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022551 static int func_nr = 0; /* number for nameless function */
22552 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022553 hashtab_T *ht;
22554 int todo;
22555 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022556 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557
22558 /*
22559 * ":function" without argument: list functions.
22560 */
22561 if (ends_excmd(*eap->arg))
22562 {
22563 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022564 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022565 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022566 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022567 {
22568 if (!HASHITEM_EMPTY(hi))
22569 {
22570 --todo;
22571 fp = HI2UF(hi);
22572 if (!isdigit(*fp->uf_name))
22573 list_func_head(fp, FALSE);
22574 }
22575 }
22576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 eap->nextcmd = check_nextcmd(eap->arg);
22578 return;
22579 }
22580
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022582 * ":function /pat": list functions matching pattern.
22583 */
22584 if (*eap->arg == '/')
22585 {
22586 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22587 if (!eap->skip)
22588 {
22589 regmatch_T regmatch;
22590
22591 c = *p;
22592 *p = NUL;
22593 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22594 *p = c;
22595 if (regmatch.regprog != NULL)
22596 {
22597 regmatch.rm_ic = p_ic;
22598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022599 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022600 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22601 {
22602 if (!HASHITEM_EMPTY(hi))
22603 {
22604 --todo;
22605 fp = HI2UF(hi);
22606 if (!isdigit(*fp->uf_name)
22607 && vim_regexec(&regmatch, fp->uf_name, 0))
22608 list_func_head(fp, FALSE);
22609 }
22610 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022611 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022612 }
22613 }
22614 if (*p == '/')
22615 ++p;
22616 eap->nextcmd = check_nextcmd(p);
22617 return;
22618 }
22619
22620 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022621 * Get the function name. There are these situations:
22622 * func normal function name
22623 * "name" == func, "fudi.fd_dict" == NULL
22624 * dict.func new dictionary entry
22625 * "name" == NULL, "fudi.fd_dict" set,
22626 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22627 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022628 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22630 * dict.func existing dict entry that's not a Funcref
22631 * "name" == NULL, "fudi.fd_dict" set,
22632 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022633 * s:func script-local function name
22634 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022637 name = trans_function_name(&p, eap->skip, 0, &fudi);
22638 paren = (vim_strchr(p, '(') != NULL);
22639 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 {
22641 /*
22642 * Return on an invalid expression in braces, unless the expression
22643 * evaluation has been cancelled due to an aborting error, an
22644 * interrupt, or an exception.
22645 */
22646 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022648 if (!eap->skip && fudi.fd_newkey != NULL)
22649 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022650 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 else
22654 eap->skip = TRUE;
22655 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022656
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 /* An error in a function call during evaluation of an expression in magic
22658 * braces should not cause the function not to be defined. */
22659 saved_did_emsg = did_emsg;
22660 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661
22662 /*
22663 * ":function func" with only function name: list function.
22664 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 {
22667 if (!ends_excmd(*skipwhite(p)))
22668 {
22669 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022670 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 }
22672 eap->nextcmd = check_nextcmd(p);
22673 if (eap->nextcmd != NULL)
22674 *p = NUL;
22675 if (!eap->skip && !got_int)
22676 {
22677 fp = find_func(name);
22678 if (fp != NULL)
22679 {
22680 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022681 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022683 if (FUNCLINE(fp, j) == NULL)
22684 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 msg_putchar('\n');
22686 msg_outnum((long)(j + 1));
22687 if (j < 9)
22688 msg_putchar(' ');
22689 if (j < 99)
22690 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022691 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 out_flush(); /* show a line at a time */
22693 ui_breakcheck();
22694 }
22695 if (!got_int)
22696 {
22697 msg_putchar('\n');
22698 msg_puts((char_u *)" endfunction");
22699 }
22700 }
22701 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022702 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022704 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 }
22706
22707 /*
22708 * ":function name(arg1, arg2)" Define function.
22709 */
22710 p = skipwhite(p);
22711 if (*p != '(')
22712 {
22713 if (!eap->skip)
22714 {
22715 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022716 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 }
22718 /* attempt to continue by skipping some text */
22719 if (vim_strchr(p, '(') != NULL)
22720 p = vim_strchr(p, '(');
22721 }
22722 p = skipwhite(p + 1);
22723
22724 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22725 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22726
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022727 if (!eap->skip)
22728 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022729 /* Check the name of the function. Unless it's a dictionary function
22730 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022731 if (name != NULL)
22732 arg = name;
22733 else
22734 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022735 if (arg != NULL && (fudi.fd_di == NULL
22736 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022737 {
22738 if (*arg == K_SPECIAL)
22739 j = 3;
22740 else
22741 j = 0;
22742 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22743 : eval_isnamec(arg[j])))
22744 ++j;
22745 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022746 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022747 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022748 /* Disallow using the g: dict. */
22749 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22750 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022751 }
22752
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 /*
22754 * Isolate the arguments: "arg1, arg2, ...)"
22755 */
22756 while (*p != ')')
22757 {
22758 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22759 {
22760 varargs = TRUE;
22761 p += 3;
22762 mustend = TRUE;
22763 }
22764 else
22765 {
22766 arg = p;
22767 while (ASCII_ISALNUM(*p) || *p == '_')
22768 ++p;
22769 if (arg == p || isdigit(*arg)
22770 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22771 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22772 {
22773 if (!eap->skip)
22774 EMSG2(_("E125: Illegal argument: %s"), arg);
22775 break;
22776 }
22777 if (ga_grow(&newargs, 1) == FAIL)
22778 goto erret;
22779 c = *p;
22780 *p = NUL;
22781 arg = vim_strsave(arg);
22782 if (arg == NULL)
22783 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022784
22785 /* Check for duplicate argument name. */
22786 for (i = 0; i < newargs.ga_len; ++i)
22787 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22788 {
22789 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022790 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022791 goto erret;
22792 }
22793
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22795 *p = c;
22796 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 if (*p == ',')
22798 ++p;
22799 else
22800 mustend = TRUE;
22801 }
22802 p = skipwhite(p);
22803 if (mustend && *p != ')')
22804 {
22805 if (!eap->skip)
22806 EMSG2(_(e_invarg2), eap->arg);
22807 break;
22808 }
22809 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022810 if (*p != ')')
22811 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 ++p; /* skip the ')' */
22813
Bram Moolenaare9a41262005-01-15 22:18:47 +000022814 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 for (;;)
22816 {
22817 p = skipwhite(p);
22818 if (STRNCMP(p, "range", 5) == 0)
22819 {
22820 flags |= FC_RANGE;
22821 p += 5;
22822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022823 else if (STRNCMP(p, "dict", 4) == 0)
22824 {
22825 flags |= FC_DICT;
22826 p += 4;
22827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 else if (STRNCMP(p, "abort", 5) == 0)
22829 {
22830 flags |= FC_ABORT;
22831 p += 5;
22832 }
22833 else
22834 break;
22835 }
22836
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022837 /* When there is a line break use what follows for the function body.
22838 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22839 if (*p == '\n')
22840 line_arg = p + 1;
22841 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 EMSG(_(e_trailing));
22843
22844 /*
22845 * Read the body of the function, until ":endfunction" is found.
22846 */
22847 if (KeyTyped)
22848 {
22849 /* Check if the function already exists, don't let the user type the
22850 * whole function before telling him it doesn't work! For a script we
22851 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022852 if (!eap->skip && !eap->forceit)
22853 {
22854 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22855 EMSG(_(e_funcdict));
22856 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022857 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022860 if (!eap->skip && did_emsg)
22861 goto erret;
22862
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 msg_putchar('\n'); /* don't overwrite the function name */
22864 cmdline_row = msg_row;
22865 }
22866
22867 indent = 2;
22868 nesting = 0;
22869 for (;;)
22870 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022871 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022872 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022873 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022874 saved_wait_return = FALSE;
22875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022877 sourcing_lnum_off = sourcing_lnum;
22878
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022879 if (line_arg != NULL)
22880 {
22881 /* Use eap->arg, split up in parts by line breaks. */
22882 theline = line_arg;
22883 p = vim_strchr(theline, '\n');
22884 if (p == NULL)
22885 line_arg += STRLEN(line_arg);
22886 else
22887 {
22888 *p = NUL;
22889 line_arg = p + 1;
22890 }
22891 }
22892 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 theline = getcmdline(':', 0L, indent);
22894 else
22895 theline = eap->getline(':', eap->cookie, indent);
22896 if (KeyTyped)
22897 lines_left = Rows - 1;
22898 if (theline == NULL)
22899 {
22900 EMSG(_("E126: Missing :endfunction"));
22901 goto erret;
22902 }
22903
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022904 /* Detect line continuation: sourcing_lnum increased more than one. */
22905 if (sourcing_lnum > sourcing_lnum_off + 1)
22906 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22907 else
22908 sourcing_lnum_off = 0;
22909
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 if (skip_until != NULL)
22911 {
22912 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22913 * don't check for ":endfunc". */
22914 if (STRCMP(theline, skip_until) == 0)
22915 {
22916 vim_free(skip_until);
22917 skip_until = NULL;
22918 }
22919 }
22920 else
22921 {
22922 /* skip ':' and blanks*/
22923 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22924 ;
22925
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022926 /* Check for "endfunction". */
22927 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022929 if (line_arg == NULL)
22930 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 break;
22932 }
22933
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022934 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 * at "end". */
22936 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22937 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022938 else if (STRNCMP(p, "if", 2) == 0
22939 || STRNCMP(p, "wh", 2) == 0
22940 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 || STRNCMP(p, "try", 3) == 0)
22942 indent += 2;
22943
22944 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022945 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022947 if (*p == '!')
22948 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022950 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22951 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022953 ++nesting;
22954 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 }
22956 }
22957
22958 /* Check for ":append" or ":insert". */
22959 p = skip_range(p, NULL);
22960 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22961 || (p[0] == 'i'
22962 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22963 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22964 skip_until = vim_strsave((char_u *)".");
22965
22966 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22967 arg = skipwhite(skiptowhite(p));
22968 if (arg[0] == '<' && arg[1] =='<'
22969 && ((p[0] == 'p' && p[1] == 'y'
22970 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22971 || (p[0] == 'p' && p[1] == 'e'
22972 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22973 || (p[0] == 't' && p[1] == 'c'
22974 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022975 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22976 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22978 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022979 || (p[0] == 'm' && p[1] == 'z'
22980 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 ))
22982 {
22983 /* ":python <<" continues until a dot, like ":append" */
22984 p = skipwhite(arg + 2);
22985 if (*p == NUL)
22986 skip_until = vim_strsave((char_u *)".");
22987 else
22988 skip_until = vim_strsave(p);
22989 }
22990 }
22991
22992 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022993 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022994 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022995 if (line_arg == NULL)
22996 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022998 }
22999
23000 /* Copy the line to newly allocated memory. get_one_sourceline()
23001 * allocates 250 bytes per line, this saves 80% on average. The cost
23002 * is an extra alloc/free. */
23003 p = vim_strsave(theline);
23004 if (p != NULL)
23005 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023006 if (line_arg == NULL)
23007 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023008 theline = p;
23009 }
23010
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023011 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23012
23013 /* Add NULL lines for continuation lines, so that the line count is
23014 * equal to the index in the growarray. */
23015 while (sourcing_lnum_off-- > 0)
23016 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023017
23018 /* Check for end of eap->arg. */
23019 if (line_arg != NULL && *line_arg == NUL)
23020 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 }
23022
23023 /* Don't define the function when skipping commands or when an error was
23024 * detected. */
23025 if (eap->skip || did_emsg)
23026 goto erret;
23027
23028 /*
23029 * If there are no errors, add the function
23030 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023031 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023032 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023033 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023034 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023035 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023036 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023037 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023038 goto erret;
23039 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023040
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023041 fp = find_func(name);
23042 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023044 if (!eap->forceit)
23045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023046 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023047 goto erret;
23048 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023049 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023050 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023051 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023052 name);
23053 goto erret;
23054 }
23055 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023056 ga_clear_strings(&(fp->uf_args));
23057 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023058 vim_free(name);
23059 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 }
23062 else
23063 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 char numbuf[20];
23065
23066 fp = NULL;
23067 if (fudi.fd_newkey == NULL && !eap->forceit)
23068 {
23069 EMSG(_(e_funcdict));
23070 goto erret;
23071 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023072 if (fudi.fd_di == NULL)
23073 {
23074 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023075 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023076 goto erret;
23077 }
23078 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023079 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023080 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023081
23082 /* Give the function a sequential number. Can only be used with a
23083 * Funcref! */
23084 vim_free(name);
23085 sprintf(numbuf, "%d", ++func_nr);
23086 name = vim_strsave((char_u *)numbuf);
23087 if (name == NULL)
23088 goto erret;
23089 }
23090
23091 if (fp == NULL)
23092 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023093 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023094 {
23095 int slen, plen;
23096 char_u *scriptname;
23097
23098 /* Check that the autoload name matches the script name. */
23099 j = FAIL;
23100 if (sourcing_name != NULL)
23101 {
23102 scriptname = autoload_name(name);
23103 if (scriptname != NULL)
23104 {
23105 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023106 plen = (int)STRLEN(p);
23107 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023108 if (slen > plen && fnamecmp(p,
23109 sourcing_name + slen - plen) == 0)
23110 j = OK;
23111 vim_free(scriptname);
23112 }
23113 }
23114 if (j == FAIL)
23115 {
23116 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23117 goto erret;
23118 }
23119 }
23120
23121 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 if (fp == NULL)
23123 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023124
23125 if (fudi.fd_dict != NULL)
23126 {
23127 if (fudi.fd_di == NULL)
23128 {
23129 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023130 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023131 if (fudi.fd_di == NULL)
23132 {
23133 vim_free(fp);
23134 goto erret;
23135 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023136 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23137 {
23138 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023139 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023140 goto erret;
23141 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023142 }
23143 else
23144 /* overwrite existing dict entry */
23145 clear_tv(&fudi.fd_di->di_tv);
23146 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023147 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023148 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023149 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023150
23151 /* behave like "dict" was used */
23152 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023153 }
23154
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023156 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023157 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23158 {
23159 vim_free(fp);
23160 goto erret;
23161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023163 fp->uf_args = newargs;
23164 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023165#ifdef FEAT_PROFILE
23166 fp->uf_tml_count = NULL;
23167 fp->uf_tml_total = NULL;
23168 fp->uf_tml_self = NULL;
23169 fp->uf_profiling = FALSE;
23170 if (prof_def_func())
23171 func_do_profile(fp);
23172#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023173 fp->uf_varargs = varargs;
23174 fp->uf_flags = flags;
23175 fp->uf_calls = 0;
23176 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023177 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178
23179erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 ga_clear_strings(&newargs);
23181 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023182ret_free:
23183 vim_free(skip_until);
23184 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023187 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188}
23189
23190/*
23191 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023192 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023194 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023195 * TFN_INT: internal function name OK
23196 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023197 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 * Advances "pp" to just after the function name (if no error).
23199 */
23200 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023201trans_function_name(
23202 char_u **pp,
23203 int skip, /* only find the end, don't evaluate */
23204 int flags,
23205 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023207 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 char_u *start;
23209 char_u *end;
23210 int lead;
23211 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023214
23215 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023216 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023218
23219 /* Check for hard coded <SNR>: already translated function ID (from a user
23220 * command). */
23221 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23222 && (*pp)[2] == (int)KE_SNR)
23223 {
23224 *pp += 3;
23225 len = get_id_len(pp) + 3;
23226 return vim_strnsave(start, len);
23227 }
23228
23229 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23230 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023232 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023234
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023235 /* Note that TFN_ flags use the same values as GLV_ flags. */
23236 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023237 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023238 if (end == start)
23239 {
23240 if (!skip)
23241 EMSG(_("E129: Function name required"));
23242 goto theend;
23243 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023244 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023245 {
23246 /*
23247 * Report an invalid expression in braces, unless the expression
23248 * evaluation has been cancelled due to an aborting error, an
23249 * interrupt, or an exception.
23250 */
23251 if (!aborting())
23252 {
23253 if (end != NULL)
23254 EMSG2(_(e_invarg2), start);
23255 }
23256 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023257 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023258 goto theend;
23259 }
23260
23261 if (lv.ll_tv != NULL)
23262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023263 if (fdp != NULL)
23264 {
23265 fdp->fd_dict = lv.ll_dict;
23266 fdp->fd_newkey = lv.ll_newkey;
23267 lv.ll_newkey = NULL;
23268 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023270 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23271 {
23272 name = vim_strsave(lv.ll_tv->vval.v_string);
23273 *pp = end;
23274 }
23275 else
23276 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023277 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23278 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023279 EMSG(_(e_funcref));
23280 else
23281 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023282 name = NULL;
23283 }
23284 goto theend;
23285 }
23286
23287 if (lv.ll_name == NULL)
23288 {
23289 /* Error found, but continue after the function name. */
23290 *pp = end;
23291 goto theend;
23292 }
23293
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023294 /* Check if the name is a Funcref. If so, use the value. */
23295 if (lv.ll_exp_name != NULL)
23296 {
23297 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023298 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023299 if (name == lv.ll_exp_name)
23300 name = NULL;
23301 }
23302 else
23303 {
23304 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023305 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023306 if (name == *pp)
23307 name = NULL;
23308 }
23309 if (name != NULL)
23310 {
23311 name = vim_strsave(name);
23312 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023313 if (STRNCMP(name, "<SNR>", 5) == 0)
23314 {
23315 /* Change "<SNR>" to the byte sequence. */
23316 name[0] = K_SPECIAL;
23317 name[1] = KS_EXTRA;
23318 name[2] = (int)KE_SNR;
23319 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23320 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023321 goto theend;
23322 }
23323
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023324 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023325 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023326 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023327 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23328 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23329 {
23330 /* When there was "s:" already or the name expanded to get a
23331 * leading "s:" then remove it. */
23332 lv.ll_name += 2;
23333 len -= 2;
23334 lead = 2;
23335 }
23336 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023337 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023338 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023339 /* skip over "s:" and "g:" */
23340 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023341 lv.ll_name += 2;
23342 len = (int)(end - lv.ll_name);
23343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023344
23345 /*
23346 * Copy the function name to allocated memory.
23347 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23348 * Accept <SNR>123_name() outside a script.
23349 */
23350 if (skip)
23351 lead = 0; /* do nothing */
23352 else if (lead > 0)
23353 {
23354 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023355 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23356 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023357 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023358 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023359 if (current_SID <= 0)
23360 {
23361 EMSG(_(e_usingsid));
23362 goto theend;
23363 }
23364 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23365 lead += (int)STRLEN(sid_buf);
23366 }
23367 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023368 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023369 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023370 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023371 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023372 goto theend;
23373 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023374 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023375 {
23376 char_u *cp = vim_strchr(lv.ll_name, ':');
23377
23378 if (cp != NULL && cp < end)
23379 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023380 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023381 goto theend;
23382 }
23383 }
23384
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023385 name = alloc((unsigned)(len + lead + 1));
23386 if (name != NULL)
23387 {
23388 if (lead > 0)
23389 {
23390 name[0] = K_SPECIAL;
23391 name[1] = KS_EXTRA;
23392 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023393 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023394 STRCPY(name + 3, sid_buf);
23395 }
23396 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023397 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023398 }
23399 *pp = end;
23400
23401theend:
23402 clear_lval(&lv);
23403 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404}
23405
23406/*
23407 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23408 * Return 2 if "p" starts with "s:".
23409 * Return 0 otherwise.
23410 */
23411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023412eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023414 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23415 * the standard library function. */
23416 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23417 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 return 5;
23419 if (p[0] == 's' && p[1] == ':')
23420 return 2;
23421 return 0;
23422}
23423
23424/*
23425 * Return TRUE if "p" starts with "<SID>" or "s:".
23426 * Only works if eval_fname_script() returned non-zero for "p"!
23427 */
23428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023429eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430{
23431 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23432}
23433
23434/*
23435 * List the head of the function: "name(arg1, arg2)".
23436 */
23437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023438list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439{
23440 int j;
23441
23442 msg_start();
23443 if (indent)
23444 MSG_PUTS(" ");
23445 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023446 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 {
23448 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023449 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 }
23451 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023454 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 {
23456 if (j)
23457 MSG_PUTS(", ");
23458 msg_puts(FUNCARG(fp, j));
23459 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023460 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 {
23462 if (j)
23463 MSG_PUTS(", ");
23464 MSG_PUTS("...");
23465 }
23466 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023467 if (fp->uf_flags & FC_ABORT)
23468 MSG_PUTS(" abort");
23469 if (fp->uf_flags & FC_RANGE)
23470 MSG_PUTS(" range");
23471 if (fp->uf_flags & FC_DICT)
23472 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023473 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023474 if (p_verbose > 0)
23475 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476}
23477
23478/*
23479 * Find a function by name, return pointer to it in ufuncs.
23480 * Return NULL for unknown function.
23481 */
23482 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023483find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023485 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023487 hi = hash_find(&func_hashtab, name);
23488 if (!HASHITEM_EMPTY(hi))
23489 return HI2UF(hi);
23490 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491}
23492
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023493#if defined(EXITFREE) || defined(PROTO)
23494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023495free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023496{
23497 hashitem_T *hi;
23498
23499 /* Need to start all over every time, because func_free() may change the
23500 * hash table. */
23501 while (func_hashtab.ht_used > 0)
23502 for (hi = func_hashtab.ht_array; ; ++hi)
23503 if (!HASHITEM_EMPTY(hi))
23504 {
23505 func_free(HI2UF(hi));
23506 break;
23507 }
23508}
23509#endif
23510
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023512translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023513{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023514 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023515 return find_internal_func(name) >= 0;
23516 return find_func(name) != NULL;
23517}
23518
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023519/*
23520 * Return TRUE if a function "name" exists.
23521 */
23522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023523function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023524{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023525 char_u *nm = name;
23526 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023527 int n = FALSE;
23528
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023529 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23530 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023531 nm = skipwhite(nm);
23532
23533 /* Only accept "funcname", "funcname ", "funcname (..." and
23534 * "funcname(...", not "funcname!...". */
23535 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023536 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023537 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023538 return n;
23539}
23540
Bram Moolenaara1544c02013-05-30 12:35:52 +020023541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023542get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023543{
23544 char_u *nm = name;
23545 char_u *p;
23546
23547 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23548
23549 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023550 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023551 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023552
Bram Moolenaara1544c02013-05-30 12:35:52 +020023553 vim_free(p);
23554 return NULL;
23555}
23556
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023557/*
23558 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023559 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23560 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023561 */
23562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023563builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023564{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023565 char_u *p;
23566
23567 if (!ASCII_ISLOWER(name[0]))
23568 return FALSE;
23569 p = vim_strchr(name, AUTOLOAD_CHAR);
23570 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023571}
23572
Bram Moolenaar05159a02005-02-26 23:04:13 +000023573#if defined(FEAT_PROFILE) || defined(PROTO)
23574/*
23575 * Start profiling function "fp".
23576 */
23577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023578func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023579{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023580 int len = fp->uf_lines.ga_len;
23581
23582 if (len == 0)
23583 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023584 fp->uf_tm_count = 0;
23585 profile_zero(&fp->uf_tm_self);
23586 profile_zero(&fp->uf_tm_total);
23587 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023588 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023589 if (fp->uf_tml_total == NULL)
23590 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023591 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023592 if (fp->uf_tml_self == NULL)
23593 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023594 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023595 fp->uf_tml_idx = -1;
23596 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23597 || fp->uf_tml_self == NULL)
23598 return; /* out of memory */
23599
23600 fp->uf_profiling = TRUE;
23601}
23602
23603/*
23604 * Dump the profiling results for all functions in file "fd".
23605 */
23606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023607func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023608{
23609 hashitem_T *hi;
23610 int todo;
23611 ufunc_T *fp;
23612 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023613 ufunc_T **sorttab;
23614 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023616 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023617 if (todo == 0)
23618 return; /* nothing to dump */
23619
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023620 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023621
Bram Moolenaar05159a02005-02-26 23:04:13 +000023622 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23623 {
23624 if (!HASHITEM_EMPTY(hi))
23625 {
23626 --todo;
23627 fp = HI2UF(hi);
23628 if (fp->uf_profiling)
23629 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023630 if (sorttab != NULL)
23631 sorttab[st_len++] = fp;
23632
Bram Moolenaar05159a02005-02-26 23:04:13 +000023633 if (fp->uf_name[0] == K_SPECIAL)
23634 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23635 else
23636 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23637 if (fp->uf_tm_count == 1)
23638 fprintf(fd, "Called 1 time\n");
23639 else
23640 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23641 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23642 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23643 fprintf(fd, "\n");
23644 fprintf(fd, "count total (s) self (s)\n");
23645
23646 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23647 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023648 if (FUNCLINE(fp, i) == NULL)
23649 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023650 prof_func_line(fd, fp->uf_tml_count[i],
23651 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023652 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23653 }
23654 fprintf(fd, "\n");
23655 }
23656 }
23657 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023658
23659 if (sorttab != NULL && st_len > 0)
23660 {
23661 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23662 prof_total_cmp);
23663 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23664 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23665 prof_self_cmp);
23666 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23667 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023668
23669 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670}
Bram Moolenaar73830342005-02-28 22:48:19 +000023671
23672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023673prof_sort_list(
23674 FILE *fd,
23675 ufunc_T **sorttab,
23676 int st_len,
23677 char *title,
23678 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023679{
23680 int i;
23681 ufunc_T *fp;
23682
23683 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23684 fprintf(fd, "count total (s) self (s) function\n");
23685 for (i = 0; i < 20 && i < st_len; ++i)
23686 {
23687 fp = sorttab[i];
23688 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23689 prefer_self);
23690 if (fp->uf_name[0] == K_SPECIAL)
23691 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23692 else
23693 fprintf(fd, " %s()\n", fp->uf_name);
23694 }
23695 fprintf(fd, "\n");
23696}
23697
23698/*
23699 * Print the count and times for one function or function line.
23700 */
23701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023702prof_func_line(
23703 FILE *fd,
23704 int count,
23705 proftime_T *total,
23706 proftime_T *self,
23707 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023708{
23709 if (count > 0)
23710 {
23711 fprintf(fd, "%5d ", count);
23712 if (prefer_self && profile_equal(total, self))
23713 fprintf(fd, " ");
23714 else
23715 fprintf(fd, "%s ", profile_msg(total));
23716 if (!prefer_self && profile_equal(total, self))
23717 fprintf(fd, " ");
23718 else
23719 fprintf(fd, "%s ", profile_msg(self));
23720 }
23721 else
23722 fprintf(fd, " ");
23723}
23724
23725/*
23726 * Compare function for total time sorting.
23727 */
23728 static int
23729#ifdef __BORLANDC__
23730_RTLENTRYF
23731#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023732prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023733{
23734 ufunc_T *p1, *p2;
23735
23736 p1 = *(ufunc_T **)s1;
23737 p2 = *(ufunc_T **)s2;
23738 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23739}
23740
23741/*
23742 * Compare function for self time sorting.
23743 */
23744 static int
23745#ifdef __BORLANDC__
23746_RTLENTRYF
23747#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023748prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023749{
23750 ufunc_T *p1, *p2;
23751
23752 p1 = *(ufunc_T **)s1;
23753 p2 = *(ufunc_T **)s2;
23754 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23755}
23756
Bram Moolenaar05159a02005-02-26 23:04:13 +000023757#endif
23758
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023759/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023760 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023761 * Return TRUE if a package was loaded.
23762 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023764script_autoload(
23765 char_u *name,
23766 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023767{
23768 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023769 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023770 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023771 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023772
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023773 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023774 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023775 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023776 return FALSE;
23777
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023778 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023779
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023780 /* Find the name in the list of previously loaded package names. Skip
23781 * "autoload/", it's always the same. */
23782 for (i = 0; i < ga_loaded.ga_len; ++i)
23783 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23784 break;
23785 if (!reload && i < ga_loaded.ga_len)
23786 ret = FALSE; /* was loaded already */
23787 else
23788 {
23789 /* Remember the name if it wasn't loaded already. */
23790 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23791 {
23792 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23793 tofree = NULL;
23794 }
23795
23796 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023797 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023798 ret = TRUE;
23799 }
23800
23801 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023802 return ret;
23803}
23804
23805/*
23806 * Return the autoload script name for a function or variable name.
23807 * Returns NULL when out of memory.
23808 */
23809 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023810autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023811{
23812 char_u *p;
23813 char_u *scriptname;
23814
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023815 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023816 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23817 if (scriptname == NULL)
23818 return FALSE;
23819 STRCPY(scriptname, "autoload/");
23820 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023821 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023822 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023823 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023824 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023825 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023826}
23827
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23829
23830/*
23831 * Function given to ExpandGeneric() to obtain the list of user defined
23832 * function names.
23833 */
23834 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023835get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023837 static long_u done;
23838 static hashitem_T *hi;
23839 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840
23841 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023843 done = 0;
23844 hi = func_hashtab.ht_array;
23845 }
23846 if (done < func_hashtab.ht_used)
23847 {
23848 if (done++ > 0)
23849 ++hi;
23850 while (HASHITEM_EMPTY(hi))
23851 ++hi;
23852 fp = HI2UF(hi);
23853
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023854 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023855 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023857 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23858 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859
23860 cat_func_name(IObuff, fp);
23861 if (xp->xp_context != EXPAND_USER_FUNC)
23862 {
23863 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023864 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 STRCAT(IObuff, ")");
23866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 return IObuff;
23868 }
23869 return NULL;
23870}
23871
23872#endif /* FEAT_CMDL_COMPL */
23873
23874/*
23875 * Copy the function name of "fp" to buffer "buf".
23876 * "buf" must be able to hold the function name plus three bytes.
23877 * Takes care of script-local function names.
23878 */
23879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023880cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 {
23884 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
23887 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889}
23890
23891/*
23892 * ":delfunction {name}"
23893 */
23894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023895ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023897 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 char_u *p;
23899 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901
23902 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023903 name = trans_function_name(&p, eap->skip, 0, &fudi);
23904 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906 {
23907 if (fudi.fd_dict != NULL && !eap->skip)
23908 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911 if (!ends_excmd(*skipwhite(p)))
23912 {
23913 vim_free(name);
23914 EMSG(_(e_trailing));
23915 return;
23916 }
23917 eap->nextcmd = check_nextcmd(p);
23918 if (eap->nextcmd != NULL)
23919 *p = NUL;
23920
23921 if (!eap->skip)
23922 fp = find_func(name);
23923 vim_free(name);
23924
23925 if (!eap->skip)
23926 {
23927 if (fp == NULL)
23928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023929 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 return;
23931 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023932 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 {
23934 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23935 return;
23936 }
23937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023938 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940 /* Delete the dict item that refers to the function, it will
23941 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023942 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023944 else
23945 func_free(fp);
23946 }
23947}
23948
23949/*
23950 * Free a function and remove it from the list of functions.
23951 */
23952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023953func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023955 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023956
23957 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023958 ga_clear_strings(&(fp->uf_args));
23959 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023960#ifdef FEAT_PROFILE
23961 vim_free(fp->uf_tml_count);
23962 vim_free(fp->uf_tml_total);
23963 vim_free(fp->uf_tml_self);
23964#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023966 /* remove the function from the function hashtable */
23967 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23968 if (HASHITEM_EMPTY(hi))
23969 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023971 hash_remove(&func_hashtab, hi);
23972
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 vim_free(fp);
23974}
23975
23976/*
23977 * Unreference a Function: decrement the reference count and free it when it
23978 * becomes zero. Only for numbered functions.
23979 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023981func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023982{
23983 ufunc_T *fp;
23984
23985 if (name != NULL && isdigit(*name))
23986 {
23987 fp = find_func(name);
23988 if (fp == NULL)
23989 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023991 {
23992 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 * when "uf_calls" becomes zero. */
23994 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 func_free(fp);
23996 }
23997 }
23998}
23999
24000/*
24001 * Count a reference to a Function.
24002 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024004func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024005{
24006 ufunc_T *fp;
24007
24008 if (name != NULL && isdigit(*name))
24009 {
24010 fp = find_func(name);
24011 if (fp == NULL)
24012 EMSG2(_(e_intern2), "func_ref()");
24013 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024014 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 }
24016}
24017
24018/*
24019 * Call a user function.
24020 */
24021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024022call_user_func(
24023 ufunc_T *fp, /* pointer to function */
24024 int argcount, /* nr of args */
24025 typval_T *argvars, /* arguments */
24026 typval_T *rettv, /* return value */
24027 linenr_T firstline, /* first line of range */
24028 linenr_T lastline, /* last line of range */
24029 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030{
Bram Moolenaar33570922005-01-25 22:26:29 +000024031 char_u *save_sourcing_name;
24032 linenr_T save_sourcing_lnum;
24033 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024034 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024035 int save_did_emsg;
24036 static int depth = 0;
24037 dictitem_T *v;
24038 int fixvar_idx = 0; /* index in fixvar[] */
24039 int i;
24040 int ai;
24041 char_u numbuf[NUMBUFLEN];
24042 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024043 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024044#ifdef FEAT_PROFILE
24045 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024046 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048
24049 /* If depth of calling is getting too high, don't execute the function */
24050 if (depth >= p_mfd)
24051 {
24052 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024053 rettv->v_type = VAR_NUMBER;
24054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 return;
24056 }
24057 ++depth;
24058
24059 line_breakcheck(); /* check for CTRL-C hit */
24060
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024061 fc = (funccall_T *)alloc(sizeof(funccall_T));
24062 fc->caller = current_funccal;
24063 current_funccal = fc;
24064 fc->func = fp;
24065 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024066 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024067 fc->linenr = 0;
24068 fc->returned = FALSE;
24069 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024071 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24072 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073
Bram Moolenaar33570922005-01-25 22:26:29 +000024074 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024075 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024076 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24077 * each argument variable and saves a lot of time.
24078 */
24079 /*
24080 * Init l: variables.
24081 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024082 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024083 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024084 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024085 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24086 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024087 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024088 name = v->di_key;
24089 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024090 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024093 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024094 v->di_tv.vval.v_dict = selfdict;
24095 ++selfdict->dv_refcount;
24096 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024097
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 /*
24099 * Init a: variables.
24100 * Set a:0 to "argcount".
24101 * Set a:000 to a list with room for the "..." arguments.
24102 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024103 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024104 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024106 /* Use "name" to avoid a warning from some compiler that checks the
24107 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024108 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024109 name = v->di_key;
24110 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024111 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024112 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024113 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024114 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024115 v->di_tv.vval.v_list = &fc->l_varlist;
24116 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24117 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24118 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024119
24120 /*
24121 * Set a:firstline to "firstline" and a:lastline to "lastline".
24122 * Set a:name to named arguments.
24123 * Set a:N to the "..." arguments.
24124 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024125 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024126 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024127 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024128 (varnumber_T)lastline);
24129 for (i = 0; i < argcount; ++i)
24130 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024131 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 if (ai < 0)
24133 /* named argument a:name */
24134 name = FUNCARG(fp, i);
24135 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024136 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024137 /* "..." argument a:1, a:2, etc. */
24138 sprintf((char *)numbuf, "%d", ai + 1);
24139 name = numbuf;
24140 }
24141 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24142 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024143 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024144 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24145 }
24146 else
24147 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024148 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24149 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024150 if (v == NULL)
24151 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024152 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024153 }
24154 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024155 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024156
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024157 /* Note: the values are copied directly to avoid alloc/free.
24158 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024159 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024160 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024161
24162 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24163 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024164 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24165 fc->l_listitems[ai].li_tv = argvars[i];
24166 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024167 }
24168 }
24169
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 /* Don't redraw while executing the function. */
24171 ++RedrawingDisabled;
24172 save_sourcing_name = sourcing_name;
24173 save_sourcing_lnum = sourcing_lnum;
24174 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024175 /* need space for function name + ("function " + 3) or "[number]" */
24176 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24177 + STRLEN(fp->uf_name) + 20;
24178 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 if (sourcing_name != NULL)
24180 {
24181 if (save_sourcing_name != NULL
24182 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024183 sprintf((char *)sourcing_name, "%s[%d]..",
24184 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 else
24186 STRCPY(sourcing_name, "function ");
24187 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24188
24189 if (p_verbose >= 12)
24190 {
24191 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024192 verbose_enter_scroll();
24193
Bram Moolenaar555b2802005-05-19 21:08:39 +000024194 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 if (p_verbose >= 14)
24196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024198 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024199 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024200 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201
24202 msg_puts((char_u *)"(");
24203 for (i = 0; i < argcount; ++i)
24204 {
24205 if (i > 0)
24206 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024207 if (argvars[i].v_type == VAR_NUMBER)
24208 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 else
24210 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024211 /* Do not want errors such as E724 here. */
24212 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024213 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024214 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024215 if (s != NULL)
24216 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024217 if (vim_strsize(s) > MSG_BUF_CLEN)
24218 {
24219 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24220 s = buf;
24221 }
24222 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024223 vim_free(tofree);
24224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 }
24226 }
24227 msg_puts((char_u *)")");
24228 }
24229 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024230
24231 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 --no_wait_return;
24233 }
24234 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024235#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024236 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024237 {
24238 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24239 func_do_profile(fp);
24240 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024241 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024242 {
24243 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024244 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024245 profile_zero(&fp->uf_tm_children);
24246 }
24247 script_prof_save(&wait_start);
24248 }
24249#endif
24250
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024252 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 save_did_emsg = did_emsg;
24254 did_emsg = FALSE;
24255
24256 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024257 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24259
24260 --RedrawingDisabled;
24261
24262 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024263 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 clear_tv(rettv);
24266 rettv->v_type = VAR_NUMBER;
24267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 }
24269
Bram Moolenaar05159a02005-02-26 23:04:13 +000024270#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024271 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024272 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024273 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024274 profile_end(&call_start);
24275 profile_sub_wait(&wait_start, &call_start);
24276 profile_add(&fp->uf_tm_total, &call_start);
24277 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024278 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024279 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024280 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24281 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024282 }
24283 }
24284#endif
24285
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 /* when being verbose, mention the return value */
24287 if (p_verbose >= 12)
24288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024290 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024293 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024294 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024295 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024296 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024299 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024300 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024301 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024302 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024303
Bram Moolenaar555b2802005-05-19 21:08:39 +000024304 /* The value may be very long. Skip the middle part, so that we
24305 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024306 * truncate it at the end. Don't want errors such as E724 here. */
24307 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024308 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024309 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024310 if (s != NULL)
24311 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024312 if (vim_strsize(s) > MSG_BUF_CLEN)
24313 {
24314 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24315 s = buf;
24316 }
24317 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024318 vim_free(tofree);
24319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320 }
24321 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024322
24323 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 --no_wait_return;
24325 }
24326
24327 vim_free(sourcing_name);
24328 sourcing_name = save_sourcing_name;
24329 sourcing_lnum = save_sourcing_lnum;
24330 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024331#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024332 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024333 script_prof_restore(&wait_start);
24334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335
24336 if (p_verbose >= 12 && sourcing_name != NULL)
24337 {
24338 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024339 verbose_enter_scroll();
24340
Bram Moolenaar555b2802005-05-19 21:08:39 +000024341 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024343
24344 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 --no_wait_return;
24346 }
24347
24348 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024349 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024352 /* If the a:000 list and the l: and a: dicts are not referenced we can
24353 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024354 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24355 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24356 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24357 {
24358 free_funccal(fc, FALSE);
24359 }
24360 else
24361 {
24362 hashitem_T *hi;
24363 listitem_T *li;
24364 int todo;
24365
24366 /* "fc" is still in use. This can happen when returning "a:000" or
24367 * assigning "l:" to a global variable.
24368 * Link "fc" in the list for garbage collection later. */
24369 fc->caller = previous_funccal;
24370 previous_funccal = fc;
24371
24372 /* Make a copy of the a: variables, since we didn't do that above. */
24373 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24374 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24375 {
24376 if (!HASHITEM_EMPTY(hi))
24377 {
24378 --todo;
24379 v = HI2DI(hi);
24380 copy_tv(&v->di_tv, &v->di_tv);
24381 }
24382 }
24383
24384 /* Make a copy of the a:000 items, since we didn't do that above. */
24385 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24386 copy_tv(&li->li_tv, &li->li_tv);
24387 }
24388}
24389
24390/*
24391 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024392 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024393 */
24394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024395can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024396{
24397 return (fc->l_varlist.lv_copyID != copyID
24398 && fc->l_vars.dv_copyID != copyID
24399 && fc->l_avars.dv_copyID != copyID);
24400}
24401
24402/*
24403 * Free "fc" and what it contains.
24404 */
24405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024406free_funccal(
24407 funccall_T *fc,
24408 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024409{
24410 listitem_T *li;
24411
24412 /* The a: variables typevals may not have been allocated, only free the
24413 * allocated variables. */
24414 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24415
24416 /* free all l: variables */
24417 vars_clear(&fc->l_vars.dv_hashtab);
24418
24419 /* Free the a:000 variables if they were allocated. */
24420 if (free_val)
24421 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24422 clear_tv(&li->li_tv);
24423
24424 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425}
24426
24427/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024428 * Add a number variable "name" to dict "dp" with value "nr".
24429 */
24430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024431add_nr_var(
24432 dict_T *dp,
24433 dictitem_T *v,
24434 char *name,
24435 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024436{
24437 STRCPY(v->di_key, name);
24438 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24439 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24440 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024441 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024442 v->di_tv.vval.v_number = nr;
24443}
24444
24445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 * ":return [expr]"
24447 */
24448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024449ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450{
24451 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453 int returning = FALSE;
24454
24455 if (current_funccal == NULL)
24456 {
24457 EMSG(_("E133: :return not inside a function"));
24458 return;
24459 }
24460
24461 if (eap->skip)
24462 ++emsg_skip;
24463
24464 eap->nextcmd = NULL;
24465 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024466 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467 {
24468 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024469 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 }
24473 /* It's safer to return also on error. */
24474 else if (!eap->skip)
24475 {
24476 /*
24477 * Return unless the expression evaluation has been cancelled due to an
24478 * aborting error, an interrupt, or an exception.
24479 */
24480 if (!aborting())
24481 returning = do_return(eap, FALSE, TRUE, NULL);
24482 }
24483
24484 /* When skipping or the return gets pending, advance to the next command
24485 * in this line (!returning). Otherwise, ignore the rest of the line.
24486 * Following lines will be ignored by get_func_line(). */
24487 if (returning)
24488 eap->nextcmd = NULL;
24489 else if (eap->nextcmd == NULL) /* no argument */
24490 eap->nextcmd = check_nextcmd(arg);
24491
24492 if (eap->skip)
24493 --emsg_skip;
24494}
24495
24496/*
24497 * Return from a function. Possibly makes the return pending. Also called
24498 * for a pending return at the ":endtry" or after returning from an extra
24499 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024500 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024501 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 * FALSE when the return gets pending.
24503 */
24504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024505do_return(
24506 exarg_T *eap,
24507 int reanimate,
24508 int is_cmd,
24509 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510{
24511 int idx;
24512 struct condstack *cstack = eap->cstack;
24513
24514 if (reanimate)
24515 /* Undo the return. */
24516 current_funccal->returned = FALSE;
24517
24518 /*
24519 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24520 * not in its finally clause (which then is to be executed next) is found.
24521 * In this case, make the ":return" pending for execution at the ":endtry".
24522 * Otherwise, return normally.
24523 */
24524 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24525 if (idx >= 0)
24526 {
24527 cstack->cs_pending[idx] = CSTP_RETURN;
24528
24529 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024530 /* A pending return again gets pending. "rettv" points to an
24531 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024533 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 else
24535 {
24536 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024537 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024539 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024541 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 {
24543 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024544 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024545 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 else
24547 EMSG(_(e_outofmem));
24548 }
24549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024550 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551
24552 if (reanimate)
24553 {
24554 /* The pending return value could be overwritten by a ":return"
24555 * without argument in a finally clause; reset the default
24556 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024557 current_funccal->rettv->v_type = VAR_NUMBER;
24558 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 }
24560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024561 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 }
24563 else
24564 {
24565 current_funccal->returned = TRUE;
24566
24567 /* If the return is carried out now, store the return value. For
24568 * a return immediately after reanimation, the value is already
24569 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024570 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024573 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024575 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 }
24577 }
24578
24579 return idx < 0;
24580}
24581
24582/*
24583 * Free the variable with a pending return value.
24584 */
24585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024586discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587{
Bram Moolenaar33570922005-01-25 22:26:29 +000024588 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589}
24590
24591/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024592 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593 * is an allocated string. Used by report_pending() for verbose messages.
24594 */
24595 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024596get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024598 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024599 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024602 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024603 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024604 if (s == NULL)
24605 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024606
24607 STRCPY(IObuff, ":return ");
24608 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24609 if (STRLEN(s) + 8 >= IOSIZE)
24610 STRCPY(IObuff + IOSIZE - 4, "...");
24611 vim_free(tofree);
24612 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613}
24614
24615/*
24616 * Get next function line.
24617 * Called by do_cmdline() to get the next line.
24618 * Returns allocated string, or NULL for end of function.
24619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024621get_func_line(
24622 int c UNUSED,
24623 void *cookie,
24624 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625{
Bram Moolenaar33570922005-01-25 22:26:29 +000024626 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024627 ufunc_T *fp = fcp->func;
24628 char_u *retval;
24629 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630
24631 /* If breakpoints have been added/deleted need to check for it. */
24632 if (fcp->dbg_tick != debug_tick)
24633 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024634 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 sourcing_lnum);
24636 fcp->dbg_tick = debug_tick;
24637 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024638#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024639 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024640 func_line_end(cookie);
24641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024644 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24645 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 retval = NULL;
24647 else
24648 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024649 /* Skip NULL lines (continuation lines). */
24650 while (fcp->linenr < gap->ga_len
24651 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24652 ++fcp->linenr;
24653 if (fcp->linenr >= gap->ga_len)
24654 retval = NULL;
24655 else
24656 {
24657 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24658 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024659#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024660 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024661 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024662#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 }
24665
24666 /* Did we encounter a breakpoint? */
24667 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24668 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024669 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024671 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 sourcing_lnum);
24673 fcp->dbg_tick = debug_tick;
24674 }
24675
24676 return retval;
24677}
24678
Bram Moolenaar05159a02005-02-26 23:04:13 +000024679#if defined(FEAT_PROFILE) || defined(PROTO)
24680/*
24681 * Called when starting to read a function line.
24682 * "sourcing_lnum" must be correct!
24683 * When skipping lines it may not actually be executed, but we won't find out
24684 * until later and we need to store the time now.
24685 */
24686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024687func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024688{
24689 funccall_T *fcp = (funccall_T *)cookie;
24690 ufunc_T *fp = fcp->func;
24691
24692 if (fp->uf_profiling && sourcing_lnum >= 1
24693 && sourcing_lnum <= fp->uf_lines.ga_len)
24694 {
24695 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024696 /* Skip continuation lines. */
24697 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24698 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024699 fp->uf_tml_execed = FALSE;
24700 profile_start(&fp->uf_tml_start);
24701 profile_zero(&fp->uf_tml_children);
24702 profile_get_wait(&fp->uf_tml_wait);
24703 }
24704}
24705
24706/*
24707 * Called when actually executing a function line.
24708 */
24709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024710func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024711{
24712 funccall_T *fcp = (funccall_T *)cookie;
24713 ufunc_T *fp = fcp->func;
24714
24715 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24716 fp->uf_tml_execed = TRUE;
24717}
24718
24719/*
24720 * Called when done with a function line.
24721 */
24722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024723func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024724{
24725 funccall_T *fcp = (funccall_T *)cookie;
24726 ufunc_T *fp = fcp->func;
24727
24728 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24729 {
24730 if (fp->uf_tml_execed)
24731 {
24732 ++fp->uf_tml_count[fp->uf_tml_idx];
24733 profile_end(&fp->uf_tml_start);
24734 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024735 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024736 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24737 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024738 }
24739 fp->uf_tml_idx = -1;
24740 }
24741}
24742#endif
24743
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744/*
24745 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024746 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 */
24748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024749func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750{
Bram Moolenaar33570922005-01-25 22:26:29 +000024751 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752
24753 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24754 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 || fcp->returned);
24757}
24758
24759/*
24760 * return TRUE if cookie indicates a function which "abort"s on errors.
24761 */
24762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024763func_has_abort(
24764 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024766 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767}
24768
24769#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24770typedef enum
24771{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024772 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24773 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24774 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775} var_flavour_T;
24776
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024777static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778
24779 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781{
24782 char_u *p = varname;
24783
24784 if (ASCII_ISUPPER(*p))
24785 {
24786 while (*(++p))
24787 if (ASCII_ISLOWER(*p))
24788 return VAR_FLAVOUR_SESSION;
24789 return VAR_FLAVOUR_VIMINFO;
24790 }
24791 else
24792 return VAR_FLAVOUR_DEFAULT;
24793}
24794#endif
24795
24796#if defined(FEAT_VIMINFO) || defined(PROTO)
24797/*
24798 * Restore global vars that start with a capital from the viminfo file
24799 */
24800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024801read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802{
24803 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024804 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024805 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024806 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
24808 if (!writing && (find_viminfo_parameter('!') != NULL))
24809 {
24810 tab = vim_strchr(virp->vir_line + 1, '\t');
24811 if (tab != NULL)
24812 {
24813 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024814 switch (*tab)
24815 {
24816 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024817#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024818 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024819#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024820 case 'D': type = VAR_DICT; break;
24821 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024822 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824
24825 tab = vim_strchr(tab, '\t');
24826 if (tab != NULL)
24827 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024828 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024829 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024830 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024832#ifdef FEAT_FLOAT
24833 else if (type == VAR_FLOAT)
24834 (void)string2float(tab + 1, &tv.vval.v_float);
24835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024837 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024838 if (type == VAR_DICT || type == VAR_LIST)
24839 {
24840 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24841
24842 if (etv == NULL)
24843 /* Failed to parse back the dict or list, use it as a
24844 * string. */
24845 tv.v_type = VAR_STRING;
24846 else
24847 {
24848 vim_free(tv.vval.v_string);
24849 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024850 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024851 }
24852 }
24853
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024854 /* when in a function use global variables */
24855 save_funccal = current_funccal;
24856 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024857 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024858 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024859
24860 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024861 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024862 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24863 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 }
24865 }
24866 }
24867
24868 return viminfo_readline(virp);
24869}
24870
24871/*
24872 * Write global vars that start with a capital to the viminfo file
24873 */
24874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024875write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876{
Bram Moolenaar33570922005-01-25 22:26:29 +000024877 hashitem_T *hi;
24878 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024879 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024880 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024881 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024882 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024883 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884
24885 if (find_viminfo_parameter('!') == NULL)
24886 return;
24887
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024888 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024889
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024890 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024891 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024893 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024895 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024896 this_var = HI2DI(hi);
24897 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024898 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024900 {
24901 case VAR_STRING: s = "STR"; break;
24902 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024903 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024904 case VAR_DICT: s = "DIC"; break;
24905 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024906 case VAR_SPECIAL: s = "XPL"; break;
24907
24908 case VAR_UNKNOWN:
24909 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010024910 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010024911 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024912 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024913 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024914 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024915 if (p != NULL)
24916 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024917 vim_free(tofree);
24918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 }
24920 }
24921}
24922#endif
24923
24924#if defined(FEAT_SESSION) || defined(PROTO)
24925 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024926store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927{
Bram Moolenaar33570922005-01-25 22:26:29 +000024928 hashitem_T *hi;
24929 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024930 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 char_u *p, *t;
24932
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024933 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024936 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024938 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024939 this_var = HI2DI(hi);
24940 if ((this_var->di_tv.v_type == VAR_NUMBER
24941 || this_var->di_tv.v_type == VAR_STRING)
24942 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024943 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024944 /* Escape special characters with a backslash. Turn a LF and
24945 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024946 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024947 (char_u *)"\\\"\n\r");
24948 if (p == NULL) /* out of memory */
24949 break;
24950 for (t = p; *t != NUL; ++t)
24951 if (*t == '\n')
24952 *t = 'n';
24953 else if (*t == '\r')
24954 *t = 'r';
24955 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024956 this_var->di_key,
24957 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24958 : ' ',
24959 p,
24960 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24961 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024962 || put_eol(fd) == FAIL)
24963 {
24964 vim_free(p);
24965 return FAIL;
24966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 vim_free(p);
24968 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024969#ifdef FEAT_FLOAT
24970 else if (this_var->di_tv.v_type == VAR_FLOAT
24971 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24972 {
24973 float_T f = this_var->di_tv.vval.v_float;
24974 int sign = ' ';
24975
24976 if (f < 0)
24977 {
24978 f = -f;
24979 sign = '-';
24980 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024981 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024982 this_var->di_key, sign, f) < 0)
24983 || put_eol(fd) == FAIL)
24984 return FAIL;
24985 }
24986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 }
24988 }
24989 return OK;
24990}
24991#endif
24992
Bram Moolenaar661b1822005-07-28 22:36:45 +000024993/*
24994 * Display script name where an item was last set.
24995 * Should only be invoked when 'verbose' is non-zero.
24996 */
24997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024998last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024999{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025000 char_u *p;
25001
Bram Moolenaar661b1822005-07-28 22:36:45 +000025002 if (scriptID != 0)
25003 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025004 p = home_replace_save(NULL, get_scriptname(scriptID));
25005 if (p != NULL)
25006 {
25007 verbose_enter();
25008 MSG_PUTS(_("\n\tLast set from "));
25009 MSG_PUTS(p);
25010 vim_free(p);
25011 verbose_leave();
25012 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025013 }
25014}
25015
Bram Moolenaard812df62008-11-09 12:46:09 +000025016/*
25017 * List v:oldfiles in a nice way.
25018 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025020ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025021{
25022 list_T *l = vimvars[VV_OLDFILES].vv_list;
25023 listitem_T *li;
25024 int nr = 0;
25025
25026 if (l == NULL)
25027 msg((char_u *)_("No old files"));
25028 else
25029 {
25030 msg_start();
25031 msg_scroll = TRUE;
25032 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25033 {
25034 msg_outnum((long)++nr);
25035 MSG_PUTS(": ");
25036 msg_outtrans(get_tv_string(&li->li_tv));
25037 msg_putchar('\n');
25038 out_flush(); /* output one line at a time */
25039 ui_breakcheck();
25040 }
25041 /* Assume "got_int" was set to truncate the listing. */
25042 got_int = FALSE;
25043
25044#ifdef FEAT_BROWSE_CMD
25045 if (cmdmod.browse)
25046 {
25047 quit_more = FALSE;
25048 nr = prompt_for_number(FALSE);
25049 msg_starthere();
25050 if (nr > 0)
25051 {
25052 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25053 (long)nr);
25054
25055 if (p != NULL)
25056 {
25057 p = expand_env_save(p);
25058 eap->arg = p;
25059 eap->cmdidx = CMD_edit;
25060 cmdmod.browse = FALSE;
25061 do_exedit(eap, NULL);
25062 vim_free(p);
25063 }
25064 }
25065 }
25066#endif
25067 }
25068}
25069
Bram Moolenaar53744302015-07-17 17:38:22 +020025070/* reset v:option_new, v:option_old and v:option_type */
25071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025072reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025073{
25074 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25075 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25076 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25077}
25078
25079
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080#endif /* FEAT_EVAL */
25081
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025083#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084
25085#ifdef WIN3264
25086/*
25087 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25088 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025089static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25090static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25091static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092
25093/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025094 * Get the short path (8.3) for the filename in "fnamep".
25095 * Only works for a valid file name.
25096 * When the path gets longer "fnamep" is changed and the allocated buffer
25097 * is put in "bufp".
25098 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25099 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 */
25101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025102get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025104 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 char_u *newbuf;
25106
25107 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 l = GetShortPathName(*fnamep, *fnamep, len);
25109 if (l > len - 1)
25110 {
25111 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025112 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 newbuf = vim_strnsave(*fnamep, l);
25114 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116
25117 vim_free(*bufp);
25118 *fnamep = *bufp = newbuf;
25119
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025120 /* Really should always succeed, as the buffer is big enough. */
25121 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 }
25123
25124 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025125 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126}
25127
25128/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025129 * Get the short path (8.3) for the filename in "fname". The converted
25130 * path is returned in "bufp".
25131 *
25132 * Some of the directories specified in "fname" may not exist. This function
25133 * will shorten the existing directories at the beginning of the path and then
25134 * append the remaining non-existing path.
25135 *
25136 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025137 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025138 * bufp - Pointer to an allocated buffer for the filename.
25139 * fnamelen - Length of the filename pointed to by fname
25140 *
25141 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 */
25143 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025144shortpath_for_invalid_fname(
25145 char_u **fname,
25146 char_u **bufp,
25147 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025149 char_u *short_fname, *save_fname, *pbuf_unused;
25150 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025152 int old_len, len;
25153 int new_len, sfx_len;
25154 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155
25156 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025157 old_len = *fnamelen;
25158 save_fname = vim_strnsave(*fname, old_len);
25159 pbuf_unused = NULL;
25160 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025162 endp = save_fname + old_len - 1; /* Find the end of the copy */
25163 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025165 /*
25166 * Try shortening the supplied path till it succeeds by removing one
25167 * directory at a time from the tail of the path.
25168 */
25169 len = 0;
25170 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025172 /* go back one path-separator */
25173 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25174 --endp;
25175 if (endp <= save_fname)
25176 break; /* processed the complete path */
25177
25178 /*
25179 * Replace the path separator with a NUL and try to shorten the
25180 * resulting path.
25181 */
25182 ch = *endp;
25183 *endp = 0;
25184 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025185 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025186 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25187 {
25188 retval = FAIL;
25189 goto theend;
25190 }
25191 *endp = ch; /* preserve the string */
25192
25193 if (len > 0)
25194 break; /* successfully shortened the path */
25195
25196 /* failed to shorten the path. Skip the path separator */
25197 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 }
25199
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025200 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025202 /*
25203 * Succeeded in shortening the path. Now concatenate the shortened
25204 * path with the remaining path at the tail.
25205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025207 /* Compute the length of the new path. */
25208 sfx_len = (int)(save_endp - endp) + 1;
25209 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025211 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025213 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025215 /* There is not enough space in the currently allocated string,
25216 * copy it to a buffer big enough. */
25217 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 {
25220 retval = FAIL;
25221 goto theend;
25222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 }
25224 else
25225 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025226 /* Transfer short_fname to the main buffer (it's big enough),
25227 * unless get_short_pathname() did its work in-place. */
25228 *fname = *bufp = save_fname;
25229 if (short_fname != save_fname)
25230 vim_strncpy(save_fname, short_fname, len);
25231 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025233
25234 /* concat the not-shortened part of the path */
25235 vim_strncpy(*fname + len, endp, sfx_len);
25236 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025238
25239theend:
25240 vim_free(pbuf_unused);
25241 vim_free(save_fname);
25242
25243 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244}
25245
25246/*
25247 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025248 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 */
25250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025251shortpath_for_partial(
25252 char_u **fnamep,
25253 char_u **bufp,
25254 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255{
25256 int sepcount, len, tflen;
25257 char_u *p;
25258 char_u *pbuf, *tfname;
25259 int hasTilde;
25260
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025261 /* Count up the path separators from the RHS.. so we know which part
25262 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025264 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 if (vim_ispathsep(*p))
25266 ++sepcount;
25267
25268 /* Need full path first (use expand_env() to remove a "~/") */
25269 hasTilde = (**fnamep == '~');
25270 if (hasTilde)
25271 pbuf = tfname = expand_env_save(*fnamep);
25272 else
25273 pbuf = tfname = FullName_save(*fnamep, FALSE);
25274
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025275 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279
25280 if (len == 0)
25281 {
25282 /* Don't have a valid filename, so shorten the rest of the
25283 * path if we can. This CAN give us invalid 8.3 filenames, but
25284 * there's not a lot of point in guessing what it might be.
25285 */
25286 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025287 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25288 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 }
25290
25291 /* Count the paths backward to find the beginning of the desired string. */
25292 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025293 {
25294#ifdef FEAT_MBYTE
25295 if (has_mbyte)
25296 p -= mb_head_off(tfname, p);
25297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 if (vim_ispathsep(*p))
25299 {
25300 if (sepcount == 0 || (hasTilde && sepcount == 1))
25301 break;
25302 else
25303 sepcount --;
25304 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 if (hasTilde)
25307 {
25308 --p;
25309 if (p >= tfname)
25310 *p = '~';
25311 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313 }
25314 else
25315 ++p;
25316
25317 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25318 vim_free(*bufp);
25319 *fnamelen = (int)STRLEN(p);
25320 *bufp = pbuf;
25321 *fnamep = p;
25322
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324}
25325#endif /* WIN3264 */
25326
25327/*
25328 * Adjust a filename, according to a string of modifiers.
25329 * *fnamep must be NUL terminated when called. When returning, the length is
25330 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025331 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 * When there is an error, *fnamep is set to NULL.
25333 */
25334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025335modify_fname(
25336 char_u *src, /* string with modifiers */
25337 int *usedlen, /* characters after src that are used */
25338 char_u **fnamep, /* file name so far */
25339 char_u **bufp, /* buffer for allocated file name or NULL */
25340 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341{
25342 int valid = 0;
25343 char_u *tail;
25344 char_u *s, *p, *pbuf;
25345 char_u dirname[MAXPATHL];
25346 int c;
25347 int has_fullname = 0;
25348#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025349 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 int has_shortname = 0;
25351#endif
25352
25353repeat:
25354 /* ":p" - full path/file_name */
25355 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25356 {
25357 has_fullname = 1;
25358
25359 valid |= VALID_PATH;
25360 *usedlen += 2;
25361
25362 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25363 if ((*fnamep)[0] == '~'
25364#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25365 && ((*fnamep)[1] == '/'
25366# ifdef BACKSLASH_IN_FILENAME
25367 || (*fnamep)[1] == '\\'
25368# endif
25369 || (*fnamep)[1] == NUL)
25370
25371#endif
25372 )
25373 {
25374 *fnamep = expand_env_save(*fnamep);
25375 vim_free(*bufp); /* free any allocated file name */
25376 *bufp = *fnamep;
25377 if (*fnamep == NULL)
25378 return -1;
25379 }
25380
25381 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025382 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 {
25384 if (vim_ispathsep(*p)
25385 && p[1] == '.'
25386 && (p[2] == NUL
25387 || vim_ispathsep(p[2])
25388 || (p[2] == '.'
25389 && (p[3] == NUL || vim_ispathsep(p[3])))))
25390 break;
25391 }
25392
25393 /* FullName_save() is slow, don't use it when not needed. */
25394 if (*p != NUL || !vim_isAbsName(*fnamep))
25395 {
25396 *fnamep = FullName_save(*fnamep, *p != NUL);
25397 vim_free(*bufp); /* free any allocated file name */
25398 *bufp = *fnamep;
25399 if (*fnamep == NULL)
25400 return -1;
25401 }
25402
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025403#ifdef WIN3264
25404# if _WIN32_WINNT >= 0x0500
25405 if (vim_strchr(*fnamep, '~') != NULL)
25406 {
25407 /* Expand 8.3 filename to full path. Needed to make sure the same
25408 * file does not have two different names.
25409 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25410 p = alloc(_MAX_PATH + 1);
25411 if (p != NULL)
25412 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025413 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025414 {
25415 vim_free(*bufp);
25416 *bufp = *fnamep = p;
25417 }
25418 else
25419 vim_free(p);
25420 }
25421 }
25422# endif
25423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 /* Append a path separator to a directory. */
25425 if (mch_isdir(*fnamep))
25426 {
25427 /* Make room for one or two extra characters. */
25428 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25429 vim_free(*bufp); /* free any allocated file name */
25430 *bufp = *fnamep;
25431 if (*fnamep == NULL)
25432 return -1;
25433 add_pathsep(*fnamep);
25434 }
25435 }
25436
25437 /* ":." - path relative to the current directory */
25438 /* ":~" - path relative to the home directory */
25439 /* ":8" - shortname path - postponed till after */
25440 while (src[*usedlen] == ':'
25441 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25442 {
25443 *usedlen += 2;
25444 if (c == '8')
25445 {
25446#ifdef WIN3264
25447 has_shortname = 1; /* Postpone this. */
25448#endif
25449 continue;
25450 }
25451 pbuf = NULL;
25452 /* Need full path first (use expand_env() to remove a "~/") */
25453 if (!has_fullname)
25454 {
25455 if (c == '.' && **fnamep == '~')
25456 p = pbuf = expand_env_save(*fnamep);
25457 else
25458 p = pbuf = FullName_save(*fnamep, FALSE);
25459 }
25460 else
25461 p = *fnamep;
25462
25463 has_fullname = 0;
25464
25465 if (p != NULL)
25466 {
25467 if (c == '.')
25468 {
25469 mch_dirname(dirname, MAXPATHL);
25470 s = shorten_fname(p, dirname);
25471 if (s != NULL)
25472 {
25473 *fnamep = s;
25474 if (pbuf != NULL)
25475 {
25476 vim_free(*bufp); /* free any allocated file name */
25477 *bufp = pbuf;
25478 pbuf = NULL;
25479 }
25480 }
25481 }
25482 else
25483 {
25484 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25485 /* Only replace it when it starts with '~' */
25486 if (*dirname == '~')
25487 {
25488 s = vim_strsave(dirname);
25489 if (s != NULL)
25490 {
25491 *fnamep = s;
25492 vim_free(*bufp);
25493 *bufp = s;
25494 }
25495 }
25496 }
25497 vim_free(pbuf);
25498 }
25499 }
25500
25501 tail = gettail(*fnamep);
25502 *fnamelen = (int)STRLEN(*fnamep);
25503
25504 /* ":h" - head, remove "/file_name", can be repeated */
25505 /* Don't remove the first "/" or "c:\" */
25506 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25507 {
25508 valid |= VALID_HEAD;
25509 *usedlen += 2;
25510 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025511 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025512 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 *fnamelen = (int)(tail - *fnamep);
25514#ifdef VMS
25515 if (*fnamelen > 0)
25516 *fnamelen += 1; /* the path separator is part of the path */
25517#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025518 if (*fnamelen == 0)
25519 {
25520 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25521 p = vim_strsave((char_u *)".");
25522 if (p == NULL)
25523 return -1;
25524 vim_free(*bufp);
25525 *bufp = *fnamep = tail = p;
25526 *fnamelen = 1;
25527 }
25528 else
25529 {
25530 while (tail > s && !after_pathsep(s, tail))
25531 mb_ptr_back(*fnamep, tail);
25532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 }
25534
25535 /* ":8" - shortname */
25536 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25537 {
25538 *usedlen += 2;
25539#ifdef WIN3264
25540 has_shortname = 1;
25541#endif
25542 }
25543
25544#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025545 /*
25546 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 */
25548 if (has_shortname)
25549 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025550 /* Copy the string if it is shortened by :h and when it wasn't copied
25551 * yet, because we are going to change it in place. Avoids changing
25552 * the buffer name for "%:8". */
25553 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 {
25555 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025556 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 return -1;
25558 vim_free(*bufp);
25559 *bufp = *fnamep = p;
25560 }
25561
25562 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025563 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 if (!has_fullname && !vim_isAbsName(*fnamep))
25565 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025566 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567 return -1;
25568 }
25569 else
25570 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025571 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572
Bram Moolenaardc935552011-08-17 15:23:23 +020025573 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025575 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 return -1;
25577
25578 if (l == 0)
25579 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025580 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025582 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583 return -1;
25584 }
25585 *fnamelen = l;
25586 }
25587 }
25588#endif /* WIN3264 */
25589
25590 /* ":t" - tail, just the basename */
25591 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25592 {
25593 *usedlen += 2;
25594 *fnamelen -= (int)(tail - *fnamep);
25595 *fnamep = tail;
25596 }
25597
25598 /* ":e" - extension, can be repeated */
25599 /* ":r" - root, without extension, can be repeated */
25600 while (src[*usedlen] == ':'
25601 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25602 {
25603 /* find a '.' in the tail:
25604 * - for second :e: before the current fname
25605 * - otherwise: The last '.'
25606 */
25607 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25608 s = *fnamep - 2;
25609 else
25610 s = *fnamep + *fnamelen - 1;
25611 for ( ; s > tail; --s)
25612 if (s[0] == '.')
25613 break;
25614 if (src[*usedlen + 1] == 'e') /* :e */
25615 {
25616 if (s > tail)
25617 {
25618 *fnamelen += (int)(*fnamep - (s + 1));
25619 *fnamep = s + 1;
25620#ifdef VMS
25621 /* cut version from the extension */
25622 s = *fnamep + *fnamelen - 1;
25623 for ( ; s > *fnamep; --s)
25624 if (s[0] == ';')
25625 break;
25626 if (s > *fnamep)
25627 *fnamelen = s - *fnamep;
25628#endif
25629 }
25630 else if (*fnamep <= tail)
25631 *fnamelen = 0;
25632 }
25633 else /* :r */
25634 {
25635 if (s > tail) /* remove one extension */
25636 *fnamelen = (int)(s - *fnamep);
25637 }
25638 *usedlen += 2;
25639 }
25640
25641 /* ":s?pat?foo?" - substitute */
25642 /* ":gs?pat?foo?" - global substitute */
25643 if (src[*usedlen] == ':'
25644 && (src[*usedlen + 1] == 's'
25645 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25646 {
25647 char_u *str;
25648 char_u *pat;
25649 char_u *sub;
25650 int sep;
25651 char_u *flags;
25652 int didit = FALSE;
25653
25654 flags = (char_u *)"";
25655 s = src + *usedlen + 2;
25656 if (src[*usedlen + 1] == 'g')
25657 {
25658 flags = (char_u *)"g";
25659 ++s;
25660 }
25661
25662 sep = *s++;
25663 if (sep)
25664 {
25665 /* find end of pattern */
25666 p = vim_strchr(s, sep);
25667 if (p != NULL)
25668 {
25669 pat = vim_strnsave(s, (int)(p - s));
25670 if (pat != NULL)
25671 {
25672 s = p + 1;
25673 /* find end of substitution */
25674 p = vim_strchr(s, sep);
25675 if (p != NULL)
25676 {
25677 sub = vim_strnsave(s, (int)(p - s));
25678 str = vim_strnsave(*fnamep, *fnamelen);
25679 if (sub != NULL && str != NULL)
25680 {
25681 *usedlen = (int)(p + 1 - src);
25682 s = do_string_sub(str, pat, sub, flags);
25683 if (s != NULL)
25684 {
25685 *fnamep = s;
25686 *fnamelen = (int)STRLEN(s);
25687 vim_free(*bufp);
25688 *bufp = s;
25689 didit = TRUE;
25690 }
25691 }
25692 vim_free(sub);
25693 vim_free(str);
25694 }
25695 vim_free(pat);
25696 }
25697 }
25698 /* after using ":s", repeat all the modifiers */
25699 if (didit)
25700 goto repeat;
25701 }
25702 }
25703
Bram Moolenaar26df0922014-02-23 23:39:13 +010025704 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25705 {
25706 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25707 if (p == NULL)
25708 return -1;
25709 vim_free(*bufp);
25710 *bufp = *fnamep = p;
25711 *fnamelen = (int)STRLEN(p);
25712 *usedlen += 2;
25713 }
25714
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 return valid;
25716}
25717
25718/*
25719 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25720 * "flags" can be "g" to do a global substitute.
25721 * Returns an allocated string, NULL for error.
25722 */
25723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025724do_string_sub(
25725 char_u *str,
25726 char_u *pat,
25727 char_u *sub,
25728 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729{
25730 int sublen;
25731 regmatch_T regmatch;
25732 int i;
25733 int do_all;
25734 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025735 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736 garray_T ga;
25737 char_u *ret;
25738 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025739 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
25741 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25742 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025743 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744
25745 ga_init2(&ga, 1, 200);
25746
25747 do_all = (flags[0] == 'g');
25748
25749 regmatch.rm_ic = p_ic;
25750 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25751 if (regmatch.regprog != NULL)
25752 {
25753 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025754 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25756 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025757 /* Skip empty match except for first match. */
25758 if (regmatch.startp[0] == regmatch.endp[0])
25759 {
25760 if (zero_width == regmatch.startp[0])
25761 {
25762 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025763 i = MB_PTR2LEN(tail);
25764 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25765 (size_t)i);
25766 ga.ga_len += i;
25767 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025768 continue;
25769 }
25770 zero_width = regmatch.startp[0];
25771 }
25772
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 /*
25774 * Get some space for a temporary buffer to do the substitution
25775 * into. It will contain:
25776 * - The text up to where the match is.
25777 * - The substituted text.
25778 * - The text after the match.
25779 */
25780 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025781 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25783 {
25784 ga_clear(&ga);
25785 break;
25786 }
25787
25788 /* copy the text up to where the match is */
25789 i = (int)(regmatch.startp[0] - tail);
25790 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25791 /* add the substituted text */
25792 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25793 + ga.ga_len + i, TRUE, TRUE, FALSE);
25794 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025795 tail = regmatch.endp[0];
25796 if (*tail == NUL)
25797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798 if (!do_all)
25799 break;
25800 }
25801
25802 if (ga.ga_data != NULL)
25803 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25804
Bram Moolenaar473de612013-06-08 18:19:48 +020025805 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 }
25807
25808 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25809 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025810 if (p_cpo == empty_option)
25811 p_cpo = save_cpo;
25812 else
25813 /* Darn, evaluating {sub} expression changed the value. */
25814 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815
25816 return ret;
25817}
25818
25819#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */