blob: a350757dac8ea642c9172632a18fbafed2dc9d08 [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 Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
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 Moolenaar33570922005-01-25 22:26:29 +0000368};
369
370/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_type vv_di.di_tv.v_type
372#define vv_nr vv_di.di_tv.vval.v_number
373#define vv_float vv_di.di_tv.vval.v_float
374#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000375#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000376#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200378static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000429static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000431static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000435static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100436static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000438static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200439static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
447static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000449#ifdef FEAT_FLOAT
450static int string2float __ARGS((char_u *text, float_T *value));
451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100454static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static int get_func_tv __ARGS((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 Moolenaarf506c5b2010-06-22 06:28:58 +0200456static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000458static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200462static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100465static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200469static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200472static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100485static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100487static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
490static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000492static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000495static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000497#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000498static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000499static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200506static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
511static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200521static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000580static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000594static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100599static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000601static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200614static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000615static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
616#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200617#ifdef FEAT_LUA
618static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000624static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000625static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000632#ifdef vim_mkdir
633static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100636#ifdef FEAT_MZSCHEME
637static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100641static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
644static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000647static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200649#ifdef FEAT_PYTHON3
650static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
651#endif
652#ifdef FEAT_PYTHON
653static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
654#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000656static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000657static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
670static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200672static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100674static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000677static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000679static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000686static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000687static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000688static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000689static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200691static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000692static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100694#ifdef FEAT_CRYPT
695static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
696#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000697static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200698static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200702static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000705static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000706static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
710static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000713static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200714static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715#ifdef HAVE_STRFTIME
716static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
717#endif
718static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200724static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200725static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000731static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200732static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200734static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000735static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000736static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000738static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000739static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000741static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200742#ifdef FEAT_FLOAT
743static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
745#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#ifdef FEAT_FLOAT
750static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
751#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200753static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200754static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100755static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100759static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000766static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100770static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771
Bram Moolenaar493c1782014-05-28 14:34:46 +0200772static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000773static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static int get_env_len __ARGS((char_u **arg));
775static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000776static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000777static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
778#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
779#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
780 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000781static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000783static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static typval_T *alloc_tv __ARGS((void));
787static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static void init_tv __ARGS((typval_T *varp));
789static long get_tv_number __ARGS((typval_T *varp));
790static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000791static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *get_tv_string __ARGS((typval_T *varp));
793static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000794static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100795static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
796static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
798static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
799static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000800static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
801static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
803static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000804static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200805static int var_check_func_name __ARGS((char_u *name, int new_var));
806static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000807static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000808static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
810static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
811static int eval_fname_script __ARGS((char_u *p));
812static int eval_fname_sid __ARGS((char_u *p));
813static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000814static ufunc_T *find_func __ARGS((char_u *name));
815static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200816static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#ifdef FEAT_PROFILE
818static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000819static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
820static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_total_cmp __ARGS((const void *s1, const void *s2));
826static int
827# ifdef __BORLANDC__
828 _RTLENTRYF
829# endif
830 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200832static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000833static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000834static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000837static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
838static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000840static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
841static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000842static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000843static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000844static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200845static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200846static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200848
849#ifdef EBCDIC
850static int compare_func_name __ARGS((const void *s1, const void *s2));
851static void sortFunctions __ARGS(());
852#endif
853
Bram Moolenaar33570922005-01-25 22:26:29 +0000854/*
855 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000856 */
857 void
858eval_init()
859{
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 int i;
861 struct vimvar *p;
862
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200863 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
864 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200865 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000866 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000867 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868
869 for (i = 0; i < VV_LEN; ++i)
870 {
871 p = &vimvars[i];
872 STRCPY(p->vv_di.di_key, p->vv_name);
873 if (p->vv_flags & VV_RO)
874 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
875 else if (p->vv_flags & VV_RO_SBX)
876 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
877 else
878 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000879
880 /* add to v: scope dict, unless the value is not always available */
881 if (p->vv_type != VAR_UNKNOWN)
882 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000884 /* add to compat scope dict */
885 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000887 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100888 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200889 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890
891#ifdef EBCDIC
892 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100893 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200894 */
895 sortFunctions();
896#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000897}
898
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000899#if defined(EXITFREE) || defined(PROTO)
900 void
901eval_clear()
902{
903 int i;
904 struct vimvar *p;
905
906 for (i = 0; i < VV_LEN; ++i)
907 {
908 p = &vimvars[i];
909 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000911 vim_free(p->vv_str);
912 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000913 }
914 else if (p->vv_di.di_tv.v_type == VAR_LIST)
915 {
916 list_unref(p->vv_list);
917 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000919 }
920 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000921 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000922 hash_clear(&compat_hashtab);
923
Bram Moolenaard9fba312005-06-26 22:34:35 +0000924 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100925# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200926 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000928
929 /* global variables */
930 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000931
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000932 /* autoloaded script names */
933 ga_clear_strings(&ga_loaded);
934
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 /* Script-local variables. First clear all the variables and in a second
936 * loop free the scriptvar_T, because a variable in one script might hold
937 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200938 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200939 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200942 ga_clear(&ga_scripts);
943
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000944 /* unreferenced lists and dicts */
945 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946
947 /* functions */
948 free_all_functions();
949 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000950}
951#endif
952
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 * Return the name of the executed function.
955 */
956 char_u *
957func_name(cookie)
958 void *cookie;
959{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the address holding the next breakpoint line for a funccall cookie.
965 */
966 linenr_T *
967func_breakpoint(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/*
974 * Return the address holding the debug tick for a funccall cookie.
975 */
976 int *
977func_dbg_tick(cookie)
978 void *cookie;
979{
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982
983/*
984 * Return the nesting level for a funccall cookie.
985 */
986 int
987func_level(cookie)
988 void *cookie;
989{
Bram Moolenaar33570922005-01-25 22:26:29 +0000990 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000994funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000996/* pointer to list of previously used funccal, still around because some
997 * item in it is still being used. */
998funccall_T *previous_funccal = NULL;
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Return TRUE when a function was ended by a ":return" command.
1002 */
1003 int
1004current_func_returned()
1005{
1006 return current_funccal->returned;
1007}
1008
1009
1010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 * Set an internal variable to a string value. Creates the variable if it does
1012 * not already exist.
1013 */
1014 void
1015set_internal_string_var(name, value)
1016 char_u *name;
1017 char_u *value;
1018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001019 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 val = vim_strsave(value);
1023 if (val != NULL)
1024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001025 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001026 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001029 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 }
1032}
1033
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001035static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static char_u *redir_endp = NULL;
1037static char_u *redir_varname = NULL;
1038
1039/*
1040 * Start recording command output to a variable
1041 * Returns OK if successfully completed the setup. FAIL otherwise.
1042 */
1043 int
1044var_redir_start(name, append)
1045 char_u *name;
1046 int append; /* append to an existing variable */
1047{
1048 int save_emsg;
1049 int err;
1050 typval_T tv;
1051
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001052 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001053 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 {
1055 EMSG(_(e_invarg));
1056 return FAIL;
1057 }
1058
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001059 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 redir_varname = vim_strsave(name);
1061 if (redir_varname == NULL)
1062 return FAIL;
1063
1064 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1065 if (redir_lval == NULL)
1066 {
1067 var_redir_stop();
1068 return FAIL;
1069 }
1070
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001071 /* The output is stored in growarray "redir_ga" until redirection ends. */
1072 ga_init2(&redir_ga, (int)sizeof(char), 500);
1073
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001075 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001076 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1078 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001079 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (redir_endp != NULL && *redir_endp != NUL)
1081 /* Trailing characters are present after the variable name */
1082 EMSG(_(e_trailing));
1083 else
1084 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
1087 return FAIL;
1088 }
1089
1090 /* check if we can write to the variable: set it to or append an empty
1091 * string */
1092 save_emsg = did_emsg;
1093 did_emsg = FALSE;
1094 tv.v_type = VAR_STRING;
1095 tv.vval.v_string = (char_u *)"";
1096 if (append)
1097 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1098 else
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001100 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001102 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 if (err)
1104 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 var_redir_stop();
1107 return FAIL;
1108 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109
1110 return OK;
1111}
1112
1113/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 * Append "value[value_len]" to the variable set by var_redir_start().
1115 * The actual appending is postponed until redirection ends, because the value
1116 * appended may in fact be the string we write to, changing it may cause freed
1117 * memory to be used:
1118 * :redir => foo
1119 * :let foo
1120 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 */
1122 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
1129 if (redir_lval == NULL)
1130 return;
1131
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001133 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 {
1139 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001140 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141 }
1142 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144}
1145
1146/*
1147 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 */
1150 void
1151var_redir_stop()
1152{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153 typval_T tv;
1154
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 if (redir_lval != NULL)
1156 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* If there was no error: assign the text to the variable. */
1158 if (redir_endp != NULL)
1159 {
1160 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1161 tv.v_type = VAR_STRING;
1162 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 /* Call get_lval() again, if it's inside a Dict or List it may
1164 * have changed. */
1165 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001166 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001167 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1168 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1169 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001170 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 /* free the collected output */
1173 vim_free(redir_ga.ga_data);
1174 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 vim_free(redir_lval);
1177 redir_lval = NULL;
1178 }
1179 vim_free(redir_varname);
1180 redir_varname = NULL;
1181}
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183# if defined(FEAT_MBYTE) || defined(PROTO)
1184 int
1185eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1186 char_u *enc_from;
1187 char_u *enc_to;
1188 char_u *fname_from;
1189 char_u *fname_to;
1190{
1191 int err = FALSE;
1192
1193 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1194 set_vim_var_string(VV_CC_TO, enc_to, -1);
1195 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1196 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1197 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1198 err = TRUE;
1199 set_vim_var_string(VV_CC_FROM, NULL, -1);
1200 set_vim_var_string(VV_CC_TO, NULL, -1);
1201 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1202 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1203
1204 if (err)
1205 return FAIL;
1206 return OK;
1207}
1208# endif
1209
1210# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1211 int
1212eval_printexpr(fname, args)
1213 char_u *fname;
1214 char_u *args;
1215{
1216 int err = FALSE;
1217
1218 set_vim_var_string(VV_FNAME_IN, fname, -1);
1219 set_vim_var_string(VV_CMDARG, args, -1);
1220 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1221 err = TRUE;
1222 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1223 set_vim_var_string(VV_CMDARG, NULL, -1);
1224
1225 if (err)
1226 {
1227 mch_remove(fname);
1228 return FAIL;
1229 }
1230 return OK;
1231}
1232# endif
1233
1234# if defined(FEAT_DIFF) || defined(PROTO)
1235 void
1236eval_diff(origfile, newfile, outfile)
1237 char_u *origfile;
1238 char_u *newfile;
1239 char_u *outfile;
1240{
1241 int err = FALSE;
1242
1243 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1244 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1245 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1246 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1247 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1248 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1249 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1250}
1251
1252 void
1253eval_patch(origfile, difffile, outfile)
1254 char_u *origfile;
1255 char_u *difffile;
1256 char_u *outfile;
1257{
1258 int err;
1259
1260 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1261 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1262 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1263 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1264 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1265 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1266 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1267}
1268# endif
1269
1270/*
1271 * Top level evaluation function, returning a boolean.
1272 * Sets "error" to TRUE if there was an error.
1273 * Return TRUE or FALSE.
1274 */
1275 int
1276eval_to_bool(arg, error, nextcmd, skip)
1277 char_u *arg;
1278 int *error;
1279 char_u **nextcmd;
1280 int skip; /* only parse, don't execute */
1281{
Bram Moolenaar33570922005-01-25 22:26:29 +00001282 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 int retval = FALSE;
1284
1285 if (skip)
1286 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 else
1290 {
1291 *error = FALSE;
1292 if (!skip)
1293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001294 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297 }
1298 if (skip)
1299 --emsg_skip;
1300
1301 return retval;
1302}
1303
1304/*
1305 * Top level evaluation function, returning a string. If "skip" is TRUE,
1306 * only parsing to "nextcmd" is done, without reporting errors. Return
1307 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1308 */
1309 char_u *
1310eval_to_string_skip(arg, nextcmd, skip)
1311 char_u *arg;
1312 char_u **nextcmd;
1313 int skip; /* only parse, don't execute */
1314{
Bram Moolenaar33570922005-01-25 22:26:29 +00001315 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 char_u *retval;
1317
1318 if (skip)
1319 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 retval = NULL;
1322 else
1323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 retval = vim_strsave(get_tv_string(&tv));
1325 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327 if (skip)
1328 --emsg_skip;
1329
1330 return retval;
1331}
1332
1333/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334 * Skip over an expression at "*pp".
1335 * Return FAIL for an error, OK otherwise.
1336 */
1337 int
1338skip_expr(pp)
1339 char_u **pp;
1340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001342
1343 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001345}
1346
1347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 * When "convert" is TRUE convert a List into a sequence of lines and convert
1350 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 * Return pointer to allocated memory, or NULL for failure.
1352 */
1353 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *arg;
1356 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001362#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 retval = NULL;
1368 else
1369 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001370 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 {
1372 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001373 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001374 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 if (tv.vval.v_list->lv_len > 0)
1377 ga_append(&ga, NL);
1378 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 ga_append(&ga, NUL);
1380 retval = (char_u *)ga.ga_data;
1381 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001382#ifdef FEAT_FLOAT
1383 else if (convert && tv.v_type == VAR_FLOAT)
1384 {
1385 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1386 retval = vim_strsave(numbuf);
1387 }
1388#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 else
1390 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393
1394 return retval;
1395}
1396
1397/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 * Call eval_to_string() without using current local variables and using
1399 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
1401 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 char_u *arg;
1404 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001405 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 char_u *retval;
1408 void *save_funccalp;
1409
1410 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001411 if (use_sandbox)
1412 ++sandbox;
1413 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001414 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 if (use_sandbox)
1416 --sandbox;
1417 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 restore_funccal(save_funccalp);
1419 return retval;
1420}
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/*
1423 * Top level evaluation function, returning a number.
1424 * Evaluates "expr" silently.
1425 * Returns -1 for an error.
1426 */
1427 int
1428eval_to_number(expr)
1429 char_u *expr;
1430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001433 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
1435 ++emsg_off;
1436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 retval = -1;
1439 else
1440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001441 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444 --emsg_off;
1445
1446 return retval;
1447}
1448
Bram Moolenaara40058a2005-07-11 22:42:07 +00001449/*
1450 * Prepare v: variable "idx" to be used.
1451 * Save the current typeval in "save_tv".
1452 * When not used yet add the variable to the v: hashtable.
1453 */
1454 static void
1455prepare_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 *save_tv = vimvars[idx].vv_tv;
1460 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1461 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1462}
1463
1464/*
1465 * Restore v: variable "idx" to typeval "save_tv".
1466 * When no longer defined, remove the variable from the v: hashtable.
1467 */
1468 static void
1469restore_vimvar(idx, save_tv)
1470 int idx;
1471 typval_T *save_tv;
1472{
1473 hashitem_T *hi;
1474
Bram Moolenaara40058a2005-07-11 22:42:07 +00001475 vimvars[idx].vv_tv = *save_tv;
1476 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1477 {
1478 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1479 if (HASHITEM_EMPTY(hi))
1480 EMSG2(_(e_intern2), "restore_vimvar()");
1481 else
1482 hash_remove(&vimvarht, hi);
1483 }
1484}
1485
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001486#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001487/*
1488 * Evaluate an expression to a list with suggestions.
1489 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001490 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001491 */
1492 list_T *
1493eval_spell_expr(badword, expr)
1494 char_u *badword;
1495 char_u *expr;
1496{
1497 typval_T save_val;
1498 typval_T rettv;
1499 list_T *list = NULL;
1500 char_u *p = skipwhite(expr);
1501
1502 /* Set "v:val" to the bad word. */
1503 prepare_vimvar(VV_VAL, &save_val);
1504 vimvars[VV_VAL].vv_type = VAR_STRING;
1505 vimvars[VV_VAL].vv_str = badword;
1506 if (p_verbose == 0)
1507 ++emsg_off;
1508
1509 if (eval1(&p, &rettv, TRUE) == OK)
1510 {
1511 if (rettv.v_type != VAR_LIST)
1512 clear_tv(&rettv);
1513 else
1514 list = rettv.vval.v_list;
1515 }
1516
1517 if (p_verbose == 0)
1518 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001519 restore_vimvar(VV_VAL, &save_val);
1520
1521 return list;
1522}
1523
1524/*
1525 * "list" is supposed to contain two items: a word and a number. Return the
1526 * word in "pp" and the number as the return value.
1527 * Return -1 if anything isn't right.
1528 * Used to get the good word and score from the eval_spell_expr() result.
1529 */
1530 int
1531get_spellword(list, pp)
1532 list_T *list;
1533 char_u **pp;
1534{
1535 listitem_T *li;
1536
1537 li = list->lv_first;
1538 if (li == NULL)
1539 return -1;
1540 *pp = get_tv_string(&li->li_tv);
1541
1542 li = li->li_next;
1543 if (li == NULL)
1544 return -1;
1545 return get_tv_number(&li->li_tv);
1546}
1547#endif
1548
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001550 * Top level evaluation function.
1551 * Returns an allocated typval_T with the result.
1552 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001553 */
1554 typval_T *
1555eval_expr(arg, nextcmd)
1556 char_u *arg;
1557 char_u **nextcmd;
1558{
1559 typval_T *tv;
1560
1561 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001562 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563 {
1564 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001565 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001566 }
1567
1568 return tv;
1569}
1570
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574 * Uses argv[argc] for the function arguments. Only Number and String
1575 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001578 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 char_u *func;
1581 int argc;
1582 char_u **argv;
1583 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001584 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 long n;
1589 int len;
1590 int i;
1591 int doesrange;
1592 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001595 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 for (i = 0; i < argc; i++)
1600 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 /* Pass a NULL or empty argument as an empty string */
1602 if (argv[i] == NULL || *argv[i] == NUL)
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_STRING;
1605 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001606 continue;
1607 }
1608
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001609 if (str_arg_only)
1610 len = 0;
1611 else
1612 /* Recognize a number argument, the others must be strings. */
1613 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (len != 0 && len == (int)STRLEN(argv[i]))
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_NUMBER;
1617 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 else
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 }
1625
1626 if (safe)
1627 {
1628 save_funccalp = save_funccal();
1629 ++sandbox;
1630 }
1631
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1633 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (safe)
1637 {
1638 --sandbox;
1639 restore_funccal(save_funccalp);
1640 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 vim_free(argvars);
1642
1643 if (ret == FAIL)
1644 clear_tv(rettv);
1645
1646 return ret;
1647}
1648
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001649/*
1650 * Call vimL function "func" and return the result as a number.
1651 * Returns -1 when calling the function fails.
1652 * Uses argv[argc] for the function arguments.
1653 */
1654 long
1655call_func_retnr(func, argc, argv, safe)
1656 char_u *func;
1657 int argc;
1658 char_u **argv;
1659 int safe; /* use the sandbox */
1660{
1661 typval_T rettv;
1662 long retval;
1663
1664 /* All arguments are passed as strings, no conversion to number. */
1665 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1666 return -1;
1667
1668 retval = get_tv_number_chk(&rettv, NULL);
1669 clear_tv(&rettv);
1670 return retval;
1671}
1672
1673#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1674 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1675
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001678 * Call vimL function "func" and return the result as a string.
1679 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
1681 */
1682 void *
1683call_func_retstr(func, argc, argv, safe)
1684 char_u *func;
1685 int argc;
1686 char_u **argv;
1687 int safe; /* use the sandbox */
1688{
1689 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001690 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001692 /* All arguments are passed as strings, no conversion to number. */
1693 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 return NULL;
1695
1696 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 return retval;
1699}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001700# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001702/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001703 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 */
1707 void *
1708call_func_retlist(func, argc, argv, safe)
1709 char_u *func;
1710 int argc;
1711 char_u **argv;
1712 int safe; /* use the sandbox */
1713{
1714 typval_T rettv;
1715
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001716 /* All arguments are passed as strings, no conversion to number. */
1717 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 return NULL;
1719
1720 if (rettv.v_type != VAR_LIST)
1721 {
1722 clear_tv(&rettv);
1723 return NULL;
1724 }
1725
1726 return rettv.vval.v_list;
1727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
1729
1730/*
1731 * Save the current function call pointer, and set it to NULL.
1732 * Used when executing autocommands and for ":source".
1733 */
1734 void *
1735save_funccal()
1736{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001737 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 current_funccal = NULL;
1740 return (void *)fc;
1741}
1742
1743 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001744restore_funccal(vfc)
1745 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747 funccall_T *fc = (funccall_T *)vfc;
1748
1749 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752#if defined(FEAT_PROFILE) || defined(PROTO)
1753/*
1754 * Prepare profiling for entering a child or something else that is not
1755 * counted for the script/function itself.
1756 * Should always be called in pair with prof_child_exit().
1757 */
1758 void
1759prof_child_enter(tm)
1760 proftime_T *tm; /* place to store waittime */
1761{
1762 funccall_T *fc = current_funccal;
1763
1764 if (fc != NULL && fc->func->uf_profiling)
1765 profile_start(&fc->prof_child);
1766 script_prof_save(tm);
1767}
1768
1769/*
1770 * Take care of time spent in a child.
1771 * Should always be called after prof_child_enter().
1772 */
1773 void
1774prof_child_exit(tm)
1775 proftime_T *tm; /* where waittime was stored */
1776{
1777 funccall_T *fc = current_funccal;
1778
1779 if (fc != NULL && fc->func->uf_profiling)
1780 {
1781 profile_end(&fc->prof_child);
1782 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1783 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1784 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1785 }
1786 script_prof_restore(tm);
1787}
1788#endif
1789
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_FOLDING
1792/*
1793 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1794 * it in "*cp". Doesn't give error messages.
1795 */
1796 int
1797eval_foldexpr(arg, cp)
1798 char_u *arg;
1799 int *cp;
1800{
Bram Moolenaar33570922005-01-25 22:26:29 +00001801 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 int retval;
1803 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001804 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1805 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001808 if (use_sandbox)
1809 ++sandbox;
1810 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 retval = 0;
1814 else
1815 {
1816 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 if (tv.v_type == VAR_NUMBER)
1818 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001819 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 retval = 0;
1821 else
1822 {
1823 /* If the result is a string, check if there is a non-digit before
1824 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (!VIM_ISDIGIT(*s) && *s != '-')
1827 *cp = *s++;
1828 retval = atol((char *)s);
1829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001833 if (use_sandbox)
1834 --sandbox;
1835 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837 return retval;
1838}
1839#endif
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 * ":let" list all variable values
1843 * ":let var1 var2" list variable values
1844 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 * ":let var += expr" assignment command.
1846 * ":let var -= expr" assignment command.
1847 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 */
1850 void
1851ex_let(eap)
1852 exarg_T *eap;
1853{
1854 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001856 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 int var_count = 0;
1859 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001861 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaardb552d602006-03-23 22:59:57 +00001864 argend = skip_var_list(arg, &var_count, &semicolon);
1865 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001867 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1868 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001869 expr = skipwhite(argend);
1870 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1871 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001873 /*
1874 * ":let" without "=": list variables
1875 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 if (*arg == '[')
1877 EMSG(_(e_invarg));
1878 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_glob_vars(&first);
1885 list_buf_vars(&first);
1886 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001887#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_script_vars(&first);
1891 list_func_vars(&first);
1892 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 eap->nextcmd = check_nextcmd(arg);
1895 }
1896 else
1897 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 op[0] = '=';
1899 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001900 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1903 op[0] = *expr; /* +=, -= or .= */
1904 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001906 else
1907 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (eap->skip)
1910 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (eap->skip)
1913 {
1914 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 --emsg_skip;
1917 }
1918 else if (i != FAIL)
1919 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 }
1925}
1926
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927/*
1928 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1929 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 * When "nextchars" is not NULL it points to a string with characters that
1931 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1932 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 * Returns OK or FAIL;
1934 */
1935 static int
1936ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1937 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 int copy; /* copy values from "tv", don't move */
1940 int semicolon; /* from skip_var_list() */
1941 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943{
1944 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 listitem_T *item;
1948 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949
1950 if (*arg != '[')
1951 {
1952 /*
1953 * ":let var = expr" or ":for var in list"
1954 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 return OK;
1958 }
1959
1960 /*
1961 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1962 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001963 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 {
1965 EMSG(_(e_listreq));
1966 return FAIL;
1967 }
1968
1969 i = list_len(l);
1970 if (semicolon == 0 && var_count < i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975 if (var_count - semicolon > i)
1976 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001977 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 return FAIL;
1979 }
1980
1981 item = l->lv_first;
1982 while (*arg != ']')
1983 {
1984 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 item = item->li_next;
1987 if (arg == NULL)
1988 return FAIL;
1989
1990 arg = skipwhite(arg);
1991 if (*arg == ';')
1992 {
1993 /* Put the rest of the list (may be empty) in the var after ';'.
1994 * Create a new list for this. */
1995 l = list_alloc();
1996 if (l == NULL)
1997 return FAIL;
1998 while (item != NULL)
1999 {
2000 list_append_tv(l, &item->li_tv);
2001 item = item->li_next;
2002 }
2003
2004 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002005 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 ltv.vval.v_list = l;
2007 l->lv_refcount = 1;
2008
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2010 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 clear_tv(&ltv);
2012 if (arg == NULL)
2013 return FAIL;
2014 break;
2015 }
2016 else if (*arg != ',' && *arg != ']')
2017 {
2018 EMSG2(_(e_intern2), "ex_let_vars()");
2019 return FAIL;
2020 }
2021 }
2022
2023 return OK;
2024}
2025
2026/*
2027 * Skip over assignable variable "var" or list of variables "[var, var]".
2028 * Used for ":let varvar = expr" and ":for varvar in expr".
2029 * For "[var, var]" increment "*var_count" for each variable.
2030 * for "[var, var; var]" set "semicolon".
2031 * Return NULL for an error.
2032 */
2033 static char_u *
2034skip_var_list(arg, var_count, semicolon)
2035 char_u *arg;
2036 int *var_count;
2037 int *semicolon;
2038{
2039 char_u *p, *s;
2040
2041 if (*arg == '[')
2042 {
2043 /* "[var, var]": find the matching ']'. */
2044 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002045 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 {
2047 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2048 s = skip_var_one(p);
2049 if (s == p)
2050 {
2051 EMSG2(_(e_invarg2), p);
2052 return NULL;
2053 }
2054 ++*var_count;
2055
2056 p = skipwhite(s);
2057 if (*p == ']')
2058 break;
2059 else if (*p == ';')
2060 {
2061 if (*semicolon == 1)
2062 {
2063 EMSG(_("Double ; in list of variables"));
2064 return NULL;
2065 }
2066 *semicolon = 1;
2067 }
2068 else if (*p != ',')
2069 {
2070 EMSG2(_(e_invarg2), p);
2071 return NULL;
2072 }
2073 }
2074 return p + 1;
2075 }
2076 else
2077 return skip_var_one(arg);
2078}
2079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002081 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002082 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 static char_u *
2085skip_var_one(arg)
2086 char_u *arg;
2087{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002088 if (*arg == '@' && arg[1] != NUL)
2089 return arg + 2;
2090 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2091 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092}
2093
Bram Moolenaara7043832005-01-21 11:56:39 +00002094/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 * List variables for hashtab "ht" with prefix "prefix".
2096 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002104{
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 hashitem_T *hi;
2106 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 int todo;
2108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002109 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002110 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2111 {
2112 if (!HASHITEM_EMPTY(hi))
2113 {
2114 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 di = HI2DI(hi);
2116 if (empty || di->di_tv.v_type != VAR_STRING
2117 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
2120 }
2121}
2122
2123/*
2124 * List global variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_glob_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131}
2132
2133/*
2134 * List buffer variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_buf_vars(first)
2138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002140 char_u numbuf[NUMBUFLEN];
2141
Bram Moolenaar429fa852013-04-15 12:27:36 +02002142 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144
2145 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2147 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002148}
2149
2150/*
2151 * List window variables.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_win_vars(first)
2155 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002156{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002157 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159}
2160
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002161#ifdef FEAT_WINDOWS
2162/*
2163 * List tab page variables.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_tab_vars(first)
2167 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002168{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002169 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171}
2172#endif
2173
Bram Moolenaara7043832005-01-21 11:56:39 +00002174/*
2175 * List Vim variables.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_vim_vars(first)
2179 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182}
2183
2184/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185 * List script-local variables, if there is a script.
2186 */
2187 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_script_vars(first)
2189 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190{
2191 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2193 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194}
2195
2196/*
2197 * List function variables, if there is a function.
2198 */
2199 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200list_func_vars(first)
2201 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202{
2203 if (current_funccal != NULL)
2204 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206}
2207
2208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 * List variables in "arg".
2210 */
2211 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 exarg_T *eap;
2214 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216{
2217 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 char_u *name_start;
2221 char_u *arg_subsc;
2222 char_u *tofree;
2223 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224
2225 while (!ends_excmd(*arg) && !got_int)
2226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002229 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2231 {
2232 emsg_severe = TRUE;
2233 EMSG(_(e_trailing));
2234 break;
2235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* get_name_len() takes care of expanding curly braces */
2240 name_start = name = arg;
2241 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2242 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* This is mainly to keep test 49 working: when expanding
2245 * curly braces fails overrule the exception error message. */
2246 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 emsg_severe = TRUE;
2249 EMSG2(_(e_invarg2), arg);
2250 break;
2251 }
2252 error = TRUE;
2253 }
2254 else
2255 {
2256 if (tofree != NULL)
2257 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002258 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 else
2261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* handle d.key, l[idx], f(expr) */
2263 arg_subsc = arg;
2264 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002265 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 case 'g': list_glob_vars(first); break;
2273 case 'b': list_buf_vars(first); break;
2274 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002275#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 'v': list_vim_vars(first); break;
2279 case 's': list_script_vars(first); break;
2280 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 default:
2282 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
2285 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 {
2287 char_u numbuf[NUMBUFLEN];
2288 char_u *tf;
2289 int c;
2290 char_u *s;
2291
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002292 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 c = *arg;
2294 *arg = NUL;
2295 list_one_var_a((char_u *)"",
2296 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002297 tv.v_type,
2298 s == NULL ? (char_u *)"" : s,
2299 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 *arg = c;
2301 vim_free(tf);
2302 }
2303 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307
2308 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310
2311 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 }
2313
2314 return arg;
2315}
2316
2317/*
2318 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2319 * Returns a pointer to the char just after the var name.
2320 * Returns NULL if there is an error.
2321 */
2322 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329{
2330 int c1;
2331 char_u *name;
2332 char_u *p;
2333 char_u *arg_end = NULL;
2334 int len;
2335 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337
2338 /*
2339 * ":let $VAR = expr": Set environment variable.
2340 */
2341 if (*arg == '$')
2342 {
2343 /* Find the end of the name. */
2344 ++arg;
2345 name = arg;
2346 len = get_env_len(&arg);
2347 if (len == 0)
2348 EMSG2(_(e_invarg2), name - 1);
2349 else
2350 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 if (op != NULL && (*op == '+' || *op == '-'))
2352 EMSG2(_(e_letwrong), op);
2353 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002354 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002356 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 {
2358 c1 = name[len];
2359 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 p = get_tv_string_chk(tv);
2361 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 {
2363 int mustfree = FALSE;
2364 char_u *s = vim_getenv(name, &mustfree);
2365
2366 if (s != NULL)
2367 {
2368 p = tofree = concat_str(s, p);
2369 if (mustfree)
2370 vim_free(s);
2371 }
2372 }
2373 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 if (STRICMP(name, "HOME") == 0)
2377 init_homedir();
2378 else if (didset_vim && STRICMP(name, "VIM") == 0)
2379 didset_vim = FALSE;
2380 else if (didset_vimruntime
2381 && STRICMP(name, "VIMRUNTIME") == 0)
2382 didset_vimruntime = FALSE;
2383 arg_end = arg;
2384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 }
2388 }
2389 }
2390
2391 /*
2392 * ":let &option = expr": Set option value.
2393 * ":let &l:option = expr": Set local option value.
2394 * ":let &g:option = expr": Set global option value.
2395 */
2396 else if (*arg == '&')
2397 {
2398 /* Find the end of the name. */
2399 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002400 if (p == NULL || (endchars != NULL
2401 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 EMSG(_(e_letunexp));
2403 else
2404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 long n;
2406 int opt_type;
2407 long numval;
2408 char_u *stringval = NULL;
2409 char_u *s;
2410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 c1 = *p;
2412 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413
2414 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 s = get_tv_string_chk(tv); /* != NULL if number or string */
2416 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 {
2418 opt_type = get_option_value(arg, &numval,
2419 &stringval, opt_flags);
2420 if ((opt_type == 1 && *op == '.')
2421 || (opt_type == 0 && *op != '.'))
2422 EMSG2(_(e_letwrong), op);
2423 else
2424 {
2425 if (opt_type == 1) /* number */
2426 {
2427 if (*op == '+')
2428 n = numval + n;
2429 else
2430 n = numval - n;
2431 }
2432 else if (opt_type == 0 && stringval != NULL) /* string */
2433 {
2434 s = concat_str(stringval, s);
2435 vim_free(stringval);
2436 stringval = s;
2437 }
2438 }
2439 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (s != NULL)
2441 {
2442 set_option_value(arg, n, s, opt_flags);
2443 arg_end = p;
2444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
2448 }
2449
2450 /*
2451 * ":let @r = expr": Set register contents.
2452 */
2453 else if (*arg == '@')
2454 {
2455 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (op != NULL && (*op == '+' || *op == '-'))
2457 EMSG2(_(e_letwrong), op);
2458 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 EMSG(_(e_letunexp));
2461 else
2462 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 char_u *s;
2465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 p = get_tv_string_chk(tv);
2467 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002469 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 if (s != NULL)
2471 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002472 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 vim_free(s);
2474 }
2475 }
2476 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 arg_end = arg + 1;
2480 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002481 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483 }
2484
2485 /*
2486 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002489 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002493 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2497 EMSG(_(e_letunexp));
2498 else
2499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 arg_end = p;
2502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
2506
2507 else
2508 EMSG2(_(e_invarg2), arg);
2509
2510 return arg_end;
2511}
2512
2513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2515 */
2516 static int
2517check_changedtick(arg)
2518 char_u *arg;
2519{
2520 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2521 {
2522 EMSG2(_(e_readonlyvar), arg);
2523 return TRUE;
2524 }
2525 return FALSE;
2526}
2527
2528/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 * Get an lval: variable, Dict item or List item that can be assigned a value
2530 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2531 * "name.key", "name.key[expr]" etc.
2532 * Indexing only works if "name" is an existing List or Dictionary.
2533 * "name" points to the start of the name.
2534 * If "rettv" is not NULL it points to the value to be assigned.
2535 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2536 * wrong; must end in space or cmd separator.
2537 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002538 * flags:
2539 * GLV_QUIET: do not give error messages
2540 * GLV_NO_AUTOLOAD: do not use script autoloading
2541 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002543 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 */
2546 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002547get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 typval_T *rettv;
2550 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 int unlet;
2552 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 char_u *expr_start, *expr_end;
2558 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 dictitem_T *v;
2560 typval_T var1;
2561 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571
2572 if (skip)
2573 {
2574 /* When skipping just find the end of the name. */
2575 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002576 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 }
2578
2579 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002580 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (expr_start != NULL)
2582 {
2583 /* Don't expand the name when we already know there is an error. */
2584 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2585 && *p != '[' && *p != '.')
2586 {
2587 EMSG(_(e_trailing));
2588 return NULL;
2589 }
2590
2591 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2592 if (lp->ll_exp_name == NULL)
2593 {
2594 /* Report an invalid expression in braces, unless the
2595 * expression evaluation has been cancelled due to an
2596 * aborting error, an interrupt, or an exception. */
2597 if (!aborting() && !quiet)
2598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002599 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 EMSG2(_(e_invarg2), name);
2601 return NULL;
2602 }
2603 }
2604 lp->ll_name = lp->ll_exp_name;
2605 }
2606 else
2607 lp->ll_name = name;
2608
2609 /* Without [idx] or .key we are done. */
2610 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2611 return p;
2612
2613 cc = *p;
2614 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002615 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (v == NULL && !quiet)
2617 EMSG2(_(e_undefvar), lp->ll_name);
2618 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 if (v == NULL)
2620 return NULL;
2621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 /*
2623 * Loop until no more [idx] or .key is following.
2624 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002625 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2629 && !(lp->ll_tv->v_type == VAR_DICT
2630 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (!quiet)
2633 EMSG(_("E689: Can only index a List or Dictionary"));
2634 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_("E708: [:] must come last"));
2640 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 len = -1;
2644 if (*p == '.')
2645 {
2646 key = p + 1;
2647 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2648 ;
2649 if (len == 0)
2650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!quiet)
2652 EMSG(_(e_emptykey));
2653 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 }
2655 p = key + len;
2656 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 else
2658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (*p == ':')
2662 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 else
2664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 empty1 = FALSE;
2666 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002668 if (get_tv_string_chk(&var1) == NULL)
2669 {
2670 /* not a number or string */
2671 clear_tv(&var1);
2672 return NULL;
2673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
2675
2676 /* Optionally get the second index [ :expr]. */
2677 if (*p == ':')
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 if (!empty1)
2684 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (rettv != NULL && (rettv->v_type != VAR_LIST
2688 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (!quiet)
2691 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 if (!empty1)
2693 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696 p = skipwhite(p + 1);
2697 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 else
2700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002708 if (get_tv_string_chk(&var2) == NULL)
2709 {
2710 /* not a number or string */
2711 if (!empty1)
2712 clear_tv(&var1);
2713 clear_tv(&var2);
2714 return NULL;
2715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
2725 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
2732
2733 /* Skip to past ']'. */
2734 ++p;
2735 }
2736
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 {
2739 if (len == -1)
2740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (*key == NUL)
2744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
2746 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002751 lp->ll_list = NULL;
2752 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002753 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 /* When assigning to a scope dictionary check that a function and
2756 * variable name is valid (only variable name unless it is l: or
2757 * g: dictionary). Disallow overwriting a builtin function. */
2758 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002759 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002760 int prevval;
2761 int wrong;
2762
2763 if (len != -1)
2764 {
2765 prevval = key[len];
2766 key[len] = NUL;
2767 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002768 else
2769 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2771 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 || !valid_varname(key);
2774 if (len != -1)
2775 key[len] = prevval;
2776 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 return NULL;
2778 }
2779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 /* Can't add "v:" variable. */
2783 if (lp->ll_dict == &vimvardict)
2784 {
2785 EMSG2(_(e_illvar), name);
2786 return NULL;
2787 }
2788
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002789 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (len == -1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 }
2798 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (len == -1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 p = NULL;
2806 break;
2807 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 /* existing variable, need to check if it can be changed */
2809 else if (var_check_ro(lp->ll_di->di_flags, name))
2810 return NULL;
2811
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 else
2817 {
2818 /*
2819 * Get the number and item for the only or first index of the List.
2820 */
2821 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 else
2824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002825 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 clear_tv(&var1);
2827 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_list = lp->ll_tv->vval.v_list;
2830 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2831 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002833 if (lp->ll_n1 < 0)
2834 {
2835 lp->ll_n1 = 0;
2836 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2837 }
2838 }
2839 if (lp->ll_li == NULL)
2840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 if (!quiet)
2844 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847
2848 /*
2849 * May need to find the item or absolute index for the second
2850 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 * When no index given: "lp->ll_empty2" is TRUE.
2852 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002856 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002862 {
2863 if (!quiet)
2864 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 }
2869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2871 if (lp->ll_n1 < 0)
2872 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2873 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 {
2875 if (!quiet)
2876 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002879 }
2880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 return p;
2886}
2887
2888/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 */
2891 static void
2892clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894{
2895 vim_free(lp->ll_exp_name);
2896 vim_free(lp->ll_newkey);
2897}
2898
2899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 */
2904 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *ri;
2914 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915
2916 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *endp;
2921 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002927 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002928 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 {
2930 if (tv_op(&tv, rettv, op) == OK)
2931 set_var(lp->ll_name, &tv, FALSE);
2932 clear_tv(&tv);
2933 }
2934 }
2935 else
2936 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 else if (tv_check_lock(lp->ll_newkey == NULL
2941 ? lp->ll_tv->v_lock
2942 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2943 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 else if (lp->ll_range)
2945 {
2946 /*
2947 * Assign the List values to the list items.
2948 */
2949 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 if (op != NULL && *op != '=')
2952 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2953 else
2954 {
2955 clear_tv(&lp->ll_li->li_tv);
2956 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 ri = ri->li_next;
2959 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2960 break;
2961 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002964 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 ri = NULL;
2967 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002968 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 lp->ll_li = lp->ll_li->li_next;
2971 ++lp->ll_n1;
2972 }
2973 if (ri != NULL)
2974 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 else if (lp->ll_empty2
2976 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 : lp->ll_n1 != lp->ll_n2)
2978 EMSG(_("E711: List value has not enough items"));
2979 }
2980 else
2981 {
2982 /*
2983 * Assign to a List or Dictionary item.
2984 */
2985 if (lp->ll_newkey != NULL)
2986 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 if (op != NULL && *op != '=')
2988 {
2989 EMSG2(_(e_letwrong), op);
2990 return;
2991 }
2992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002994 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 if (di == NULL)
2996 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002997 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2998 {
2999 vim_free(di);
3000 return;
3001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 else if (op != NULL && *op != '=')
3005 {
3006 tv_op(lp->ll_tv, rettv, op);
3007 return;
3008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /*
3013 * Assign the value to the variable or list item.
3014 */
3015 if (copy)
3016 copy_tv(rettv, lp->ll_tv);
3017 else
3018 {
3019 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003020 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 }
3023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024}
3025
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3028 * Returns OK or FAIL.
3029 */
3030 static int
3031tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 typval_T *tv1;
3033 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 char_u *op;
3035{
3036 long n;
3037 char_u numbuf[NUMBUFLEN];
3038 char_u *s;
3039
3040 /* Can't do anything with a Funcref or a Dict on the right. */
3041 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3042 {
3043 switch (tv1->v_type)
3044 {
3045 case VAR_DICT:
3046 case VAR_FUNC:
3047 break;
3048
3049 case VAR_LIST:
3050 if (*op != '+' || tv2->v_type != VAR_LIST)
3051 break;
3052 /* List += List */
3053 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3054 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3055 return OK;
3056
3057 case VAR_NUMBER:
3058 case VAR_STRING:
3059 if (tv2->v_type == VAR_LIST)
3060 break;
3061 if (*op == '+' || *op == '-')
3062 {
3063 /* nr += nr or nr -= nr*/
3064 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#ifdef FEAT_FLOAT
3066 if (tv2->v_type == VAR_FLOAT)
3067 {
3068 float_T f = n;
3069
3070 if (*op == '+')
3071 f += tv2->vval.v_float;
3072 else
3073 f -= tv2->vval.v_float;
3074 clear_tv(tv1);
3075 tv1->v_type = VAR_FLOAT;
3076 tv1->vval.v_float = f;
3077 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079#endif
3080 {
3081 if (*op == '+')
3082 n += get_tv_number(tv2);
3083 else
3084 n -= get_tv_number(tv2);
3085 clear_tv(tv1);
3086 tv1->v_type = VAR_NUMBER;
3087 tv1->vval.v_number = n;
3088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 }
3090 else
3091 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 if (tv2->v_type == VAR_FLOAT)
3093 break;
3094
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 /* str .= str */
3096 s = get_tv_string(tv1);
3097 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3098 clear_tv(tv1);
3099 tv1->v_type = VAR_STRING;
3100 tv1->vval.v_string = s;
3101 }
3102 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103
3104#ifdef FEAT_FLOAT
3105 case VAR_FLOAT:
3106 {
3107 float_T f;
3108
3109 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3110 && tv2->v_type != VAR_NUMBER
3111 && tv2->v_type != VAR_STRING))
3112 break;
3113 if (tv2->v_type == VAR_FLOAT)
3114 f = tv2->vval.v_float;
3115 else
3116 f = get_tv_number(tv2);
3117 if (*op == '+')
3118 tv1->vval.v_float += f;
3119 else
3120 tv1->vval.v_float -= f;
3121 }
3122 return OK;
3123#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 }
3125 }
3126
3127 EMSG2(_(e_letwrong), op);
3128 return FAIL;
3129}
3130
3131/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 * Add a watcher to a list.
3133 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003134 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003136 list_T *l;
3137 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138{
3139 lw->lw_next = l->lv_watch;
3140 l->lv_watch = lw;
3141}
3142
3143/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003144 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 * No warning when it isn't found...
3146 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003147 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 list_T *l;
3150 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151{
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153
3154 lwp = &l->lv_watch;
3155 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3156 {
3157 if (lw == lwrem)
3158 {
3159 *lwp = lw->lw_next;
3160 break;
3161 }
3162 lwp = &lw->lw_next;
3163 }
3164}
3165
3166/*
3167 * Just before removing an item from a list: advance watchers to the next
3168 * item.
3169 */
3170 static void
3171list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 list_T *l;
3173 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174{
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176
3177 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3178 if (lw->lw_item == item)
3179 lw->lw_item = item->li_next;
3180}
3181
3182/*
3183 * Evaluate the expression used in a ":for var in expr" command.
3184 * "arg" points to "var".
3185 * Set "*errp" to TRUE for an error, FALSE otherwise;
3186 * Return a pointer that holds the info. Null when there is an error.
3187 */
3188 void *
3189eval_for_line(arg, errp, nextcmdp, skip)
3190 char_u *arg;
3191 int *errp;
3192 char_u **nextcmdp;
3193 int skip;
3194{
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 typval_T tv;
3198 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199
3200 *errp = TRUE; /* default: there is an error */
3201
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 if (fi == NULL)
3204 return NULL;
3205
3206 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3207 if (expr == NULL)
3208 return fi;
3209
3210 expr = skipwhite(expr);
3211 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3212 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003213 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 return fi;
3215 }
3216
3217 if (skip)
3218 ++emsg_skip;
3219 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3220 {
3221 *errp = FALSE;
3222 if (!skip)
3223 {
3224 l = tv.vval.v_list;
3225 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003226 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 clear_tv(&tv);
3229 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 else
3231 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003232 /* No need to increment the refcount, it's already set for the
3233 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 fi->fi_list = l;
3235 list_add_watch(l, &fi->fi_lw);
3236 fi->fi_lw.lw_item = l->lv_first;
3237 }
3238 }
3239 }
3240 if (skip)
3241 --emsg_skip;
3242
3243 return fi;
3244}
3245
3246/*
3247 * Use the first item in a ":for" list. Advance to the next.
3248 * Assign the values to the variable (list). "arg" points to the first one.
3249 * Return TRUE when a valid item was found, FALSE when at end of list or
3250 * something wrong.
3251 */
3252 int
3253next_for_item(fi_void, arg)
3254 void *fi_void;
3255 char_u *arg;
3256{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003257 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
3261 item = fi->fi_lw.lw_item;
3262 if (item == NULL)
3263 result = FALSE;
3264 else
3265 {
3266 fi->fi_lw.lw_item = item->li_next;
3267 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3268 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3269 }
3270 return result;
3271}
3272
3273/*
3274 * Free the structure used to store info used by ":for".
3275 */
3276 void
3277free_for_info(fi_void)
3278 void *fi_void;
3279{
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003282 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003283 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 list_unref(fi->fi_list);
3286 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 vim_free(fi);
3288}
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3291
3292 void
3293set_context_for_expression(xp, arg, cmdidx)
3294 expand_T *xp;
3295 char_u *arg;
3296 cmdidx_T cmdidx;
3297{
3298 int got_eq = FALSE;
3299 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 if (cmdidx == CMD_let)
3303 {
3304 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003305 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 {
3307 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003308 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 {
3310 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003311 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 if (vim_iswhite(*p))
3313 break;
3314 }
3315 return;
3316 }
3317 }
3318 else
3319 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3320 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 while ((xp->xp_pattern = vim_strpbrk(arg,
3322 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3323 {
3324 c = *xp->xp_pattern;
3325 if (c == '&')
3326 {
3327 c = xp->xp_pattern[1];
3328 if (c == '&')
3329 {
3330 ++xp->xp_pattern;
3331 xp->xp_context = cmdidx != CMD_let || got_eq
3332 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3333 }
3334 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003337 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3338 xp->xp_pattern += 2;
3339
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342 else if (c == '$')
3343 {
3344 /* environment variable */
3345 xp->xp_context = EXPAND_ENV_VARS;
3346 }
3347 else if (c == '=')
3348 {
3349 got_eq = TRUE;
3350 xp->xp_context = EXPAND_EXPRESSION;
3351 }
3352 else if (c == '<'
3353 && xp->xp_context == EXPAND_FUNCTIONS
3354 && vim_strchr(xp->xp_pattern, '(') == NULL)
3355 {
3356 /* Function name can start with "<SNR>" */
3357 break;
3358 }
3359 else if (cmdidx != CMD_let || got_eq)
3360 {
3361 if (c == '"') /* string */
3362 {
3363 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3364 if (c == '\\' && xp->xp_pattern[1] != NUL)
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_NOTHING;
3367 }
3368 else if (c == '\'') /* literal string */
3369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3372 /* skip */ ;
3373 xp->xp_context = EXPAND_NOTHING;
3374 }
3375 else if (c == '|')
3376 {
3377 if (xp->xp_pattern[1] == '|')
3378 {
3379 ++xp->xp_pattern;
3380 xp->xp_context = EXPAND_EXPRESSION;
3381 }
3382 else
3383 xp->xp_context = EXPAND_COMMANDS;
3384 }
3385 else
3386 xp->xp_context = EXPAND_EXPRESSION;
3387 }
3388 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003389 /* Doesn't look like something valid, expand as an expression
3390 * anyway. */
3391 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 arg = xp->xp_pattern;
3393 if (*arg != NUL)
3394 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3395 /* skip */ ;
3396 }
3397 xp->xp_pattern = arg;
3398}
3399
3400#endif /* FEAT_CMDL_COMPL */
3401
3402/*
3403 * ":1,25call func(arg1, arg2)" function call.
3404 */
3405 void
3406ex_call(eap)
3407 exarg_T *eap;
3408{
3409 char_u *arg = eap->arg;
3410 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003412 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 linenr_T lnum;
3416 int doesrange;
3417 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003418 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003420 if (eap->skip)
3421 {
3422 /* trans_function_name() doesn't work well when skipping, use eval0()
3423 * instead to skip to any following command, e.g. for:
3424 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003425 ++emsg_skip;
3426 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3427 clear_tv(&rettv);
3428 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003429 return;
3430 }
3431
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003432 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003433 if (fudi.fd_newkey != NULL)
3434 {
3435 /* Still need to give an error message for missing key. */
3436 EMSG2(_(e_dictkey), fudi.fd_newkey);
3437 vim_free(fudi.fd_newkey);
3438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 if (tofree == NULL)
3440 return;
3441
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003442 /* Increase refcount on dictionary, it could get deleted when evaluating
3443 * the arguments. */
3444 if (fudi.fd_dict != NULL)
3445 ++fudi.fd_dict->dv_refcount;
3446
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003447 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003448 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003449 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar532c7802005-01-27 14:44:31 +00003451 /* Skip white space to allow ":call func ()". Not good, but required for
3452 * backward compatibility. */
3453 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003454 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 if (*startarg != '(')
3457 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003458 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 goto end;
3460 }
3461
3462 /*
3463 * When skipping, evaluate the function once, to find the end of the
3464 * arguments.
3465 * When the function takes a range, this is discovered after the first
3466 * call, and the loop is broken.
3467 */
3468 if (eap->skip)
3469 {
3470 ++emsg_skip;
3471 lnum = eap->line2; /* do it once, also with an invalid range */
3472 }
3473 else
3474 lnum = eap->line1;
3475 for ( ; lnum <= eap->line2; ++lnum)
3476 {
3477 if (!eap->skip && eap->addr_count > 0)
3478 {
3479 curwin->w_cursor.lnum = lnum;
3480 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003481#ifdef FEAT_VIRTUALEDIT
3482 curwin->w_cursor.coladd = 0;
3483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003487 eap->line1, eap->line2, &doesrange,
3488 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
3490 failed = TRUE;
3491 break;
3492 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003493
3494 /* Handle a function returning a Funcref, Dictionary or List. */
3495 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3496 {
3497 failed = TRUE;
3498 break;
3499 }
3500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (doesrange || eap->skip)
3503 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003506 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (aborting())
3510 break;
3511 }
3512 if (eap->skip)
3513 --emsg_skip;
3514
3515 if (!failed)
3516 {
3517 /* Check for trailing illegal characters and a following command. */
3518 if (!ends_excmd(*arg))
3519 {
3520 emsg_severe = TRUE;
3521 EMSG(_(e_trailing));
3522 }
3523 else
3524 eap->nextcmd = check_nextcmd(arg);
3525 }
3526
3527end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003529 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530}
3531
3532/*
3533 * ":unlet[!] var1 ... " command.
3534 */
3535 void
3536ex_unlet(eap)
3537 exarg_T *eap;
3538{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003539 ex_unletlock(eap, eap->arg, 0);
3540}
3541
3542/*
3543 * ":lockvar" and ":unlockvar" commands
3544 */
3545 void
3546ex_lockvar(eap)
3547 exarg_T *eap;
3548{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 int deep = 2;
3551
3552 if (eap->forceit)
3553 deep = -1;
3554 else if (vim_isdigit(*arg))
3555 {
3556 deep = getdigits(&arg);
3557 arg = skipwhite(arg);
3558 }
3559
3560 ex_unletlock(eap, arg, deep);
3561}
3562
3563/*
3564 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3565 */
3566 static void
3567ex_unletlock(eap, argstart, deep)
3568 exarg_T *eap;
3569 char_u *argstart;
3570 int deep;
3571{
3572 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 do
3578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003580 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003581 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (lv.ll_name == NULL)
3583 error = TRUE; /* error but continue parsing */
3584 if (name_end == NULL || (!vim_iswhite(*name_end)
3585 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 if (name_end != NULL)
3588 {
3589 emsg_severe = TRUE;
3590 EMSG(_(e_trailing));
3591 }
3592 if (!(eap->skip || error))
3593 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 break;
3595 }
3596
3597 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 {
3599 if (eap->cmdidx == CMD_unlet)
3600 {
3601 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3602 error = TRUE;
3603 }
3604 else
3605 {
3606 if (do_lock_var(&lv, name_end, deep,
3607 eap->cmdidx == CMD_lockvar) == FAIL)
3608 error = TRUE;
3609 }
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 if (!eap->skip)
3613 clear_lval(&lv);
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 arg = skipwhite(name_end);
3616 } while (!ends_excmd(*arg));
3617
3618 eap->nextcmd = check_nextcmd(arg);
3619}
3620
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003623 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 int forceit;
3626{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 int ret = OK;
3628 int cc;
3629
3630 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 cc = *name_end;
3633 *name_end = NUL;
3634
3635 /* Normal name or expanded name. */
3636 if (check_changedtick(lp->ll_name))
3637 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3643 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 else if (lp->ll_range)
3645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647
3648 /* Delete a range of List items. */
3649 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3650 {
3651 li = lp->ll_li->li_next;
3652 listitem_remove(lp->ll_list, lp->ll_li);
3653 lp->ll_li = li;
3654 ++lp->ll_n1;
3655 }
3656 }
3657 else
3658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 /* unlet a List item. */
3661 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003664 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 }
3666
3667 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668}
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670/*
3671 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
3674 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hashtab_T *ht;
3680 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003682 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 ht = find_var_ht(name, &varname);
3685 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 hi = hash_find(ht, varname);
3688 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003689 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003690 di = HI2DI(hi);
3691 if (var_check_fixed(di->di_flags, name)
3692 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 return FAIL;
3694 delete_var(ht, hi);
3695 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003698 if (forceit)
3699 return OK;
3700 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 return FAIL;
3702}
3703
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003704/*
3705 * Lock or unlock variable indicated by "lp".
3706 * "deep" is the levels to go (-1 for unlimited);
3707 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3708 */
3709 static int
3710do_lock_var(lp, name_end, deep, lock)
3711 lval_T *lp;
3712 char_u *name_end;
3713 int deep;
3714 int lock;
3715{
3716 int ret = OK;
3717 int cc;
3718 dictitem_T *di;
3719
3720 if (deep == 0) /* nothing to do */
3721 return OK;
3722
3723 if (lp->ll_tv == NULL)
3724 {
3725 cc = *name_end;
3726 *name_end = NUL;
3727
3728 /* Normal name or expanded name. */
3729 if (check_changedtick(lp->ll_name))
3730 ret = FAIL;
3731 else
3732 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003733 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 if (di == NULL)
3735 ret = FAIL;
3736 else
3737 {
3738 if (lock)
3739 di->di_flags |= DI_FLAGS_LOCK;
3740 else
3741 di->di_flags &= ~DI_FLAGS_LOCK;
3742 item_lock(&di->di_tv, deep, lock);
3743 }
3744 }
3745 *name_end = cc;
3746 }
3747 else if (lp->ll_range)
3748 {
3749 listitem_T *li = lp->ll_li;
3750
3751 /* (un)lock a range of List items. */
3752 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3753 {
3754 item_lock(&li->li_tv, deep, lock);
3755 li = li->li_next;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else if (lp->ll_list != NULL)
3760 /* (un)lock a List item. */
3761 item_lock(&lp->ll_li->li_tv, deep, lock);
3762 else
3763 /* un(lock) a Dictionary item. */
3764 item_lock(&lp->ll_di->di_tv, deep, lock);
3765
3766 return ret;
3767}
3768
3769/*
3770 * Lock or unlock an item. "deep" is nr of levels to go.
3771 */
3772 static void
3773item_lock(tv, deep, lock)
3774 typval_T *tv;
3775 int deep;
3776 int lock;
3777{
3778 static int recurse = 0;
3779 list_T *l;
3780 listitem_T *li;
3781 dict_T *d;
3782 hashitem_T *hi;
3783 int todo;
3784
3785 if (recurse >= DICT_MAXNEST)
3786 {
3787 EMSG(_("E743: variable nested too deep for (un)lock"));
3788 return;
3789 }
3790 if (deep == 0)
3791 return;
3792 ++recurse;
3793
3794 /* lock/unlock the item itself */
3795 if (lock)
3796 tv->v_lock |= VAR_LOCKED;
3797 else
3798 tv->v_lock &= ~VAR_LOCKED;
3799
3800 switch (tv->v_type)
3801 {
3802 case VAR_LIST:
3803 if ((l = tv->vval.v_list) != NULL)
3804 {
3805 if (lock)
3806 l->lv_lock |= VAR_LOCKED;
3807 else
3808 l->lv_lock &= ~VAR_LOCKED;
3809 if (deep < 0 || deep > 1)
3810 /* recursive: lock/unlock the items the List contains */
3811 for (li = l->lv_first; li != NULL; li = li->li_next)
3812 item_lock(&li->li_tv, deep - 1, lock);
3813 }
3814 break;
3815 case VAR_DICT:
3816 if ((d = tv->vval.v_dict) != NULL)
3817 {
3818 if (lock)
3819 d->dv_lock |= VAR_LOCKED;
3820 else
3821 d->dv_lock &= ~VAR_LOCKED;
3822 if (deep < 0 || deep > 1)
3823 {
3824 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003825 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3827 {
3828 if (!HASHITEM_EMPTY(hi))
3829 {
3830 --todo;
3831 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3832 }
3833 }
3834 }
3835 }
3836 }
3837 --recurse;
3838}
3839
Bram Moolenaara40058a2005-07-11 22:42:07 +00003840/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003841 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3842 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003843 */
3844 static int
3845tv_islocked(tv)
3846 typval_T *tv;
3847{
3848 return (tv->v_lock & VAR_LOCKED)
3849 || (tv->v_type == VAR_LIST
3850 && tv->vval.v_list != NULL
3851 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3852 || (tv->v_type == VAR_DICT
3853 && tv->vval.v_dict != NULL
3854 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3855}
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3858/*
3859 * Delete all "menutrans_" variables.
3860 */
3861 void
3862del_menutrans_vars()
3863{
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003868 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003870 {
3871 if (!HASHITEM_EMPTY(hi))
3872 {
3873 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003874 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3875 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003876 }
3877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003878 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879}
3880#endif
3881
3882#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3883
3884/*
3885 * Local string buffer for the next two functions to store a variable name
3886 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3887 * get_user_var_name().
3888 */
3889
3890static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3891
3892static char_u *varnamebuf = NULL;
3893static int varnamebuflen = 0;
3894
3895/*
3896 * Function to concatenate a prefix and a variable name.
3897 */
3898 static char_u *
3899cat_prefix_varname(prefix, name)
3900 int prefix;
3901 char_u *name;
3902{
3903 int len;
3904
3905 len = (int)STRLEN(name) + 3;
3906 if (len > varnamebuflen)
3907 {
3908 vim_free(varnamebuf);
3909 len += 10; /* some additional space */
3910 varnamebuf = alloc(len);
3911 if (varnamebuf == NULL)
3912 {
3913 varnamebuflen = 0;
3914 return NULL;
3915 }
3916 varnamebuflen = len;
3917 }
3918 *varnamebuf = prefix;
3919 varnamebuf[1] = ':';
3920 STRCPY(varnamebuf + 2, name);
3921 return varnamebuf;
3922}
3923
3924/*
3925 * Function given to ExpandGeneric() to obtain the list of user defined
3926 * (global/buffer/window/built-in) variable names.
3927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 char_u *
3929get_user_var_name(xp, idx)
3930 expand_T *xp;
3931 int idx;
3932{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003933 static long_u gdone;
3934 static long_u bdone;
3935 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003936#ifdef FEAT_WINDOWS
3937 static long_u tdone;
3938#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003939 static int vidx;
3940 static hashitem_T *hi;
3941 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003944 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003946#ifdef FEAT_WINDOWS
3947 tdone = 0;
3948#endif
3949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* Global variables */
3952 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3961 return cat_prefix_varname('g', hi->hi_key);
3962 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003964
3965 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003966 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003971 else
3972 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003973 while (HASHITEM_EMPTY(hi))
3974 ++hi;
3975 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003979 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return (char_u *)"b:changedtick";
3981 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003982
3983 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003987 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 else
3990 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003996#ifdef FEAT_WINDOWS
3997 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003998 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003999 if (tdone < ht->ht_used)
4000 {
4001 if (tdone++ == 0)
4002 hi = ht->ht_array;
4003 else
4004 ++hi;
4005 while (HASHITEM_EMPTY(hi))
4006 ++hi;
4007 return cat_prefix_varname('t', hi->hi_key);
4008 }
4009#endif
4010
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 /* v: variables */
4012 if (vidx < VV_LEN)
4013 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 vim_free(varnamebuf);
4016 varnamebuf = NULL;
4017 varnamebuflen = 0;
4018 return NULL;
4019}
4020
4021#endif /* FEAT_CMDL_COMPL */
4022
4023/*
4024 * types for expressions.
4025 */
4026typedef enum
4027{
4028 TYPE_UNKNOWN = 0
4029 , TYPE_EQUAL /* == */
4030 , TYPE_NEQUAL /* != */
4031 , TYPE_GREATER /* > */
4032 , TYPE_GEQUAL /* >= */
4033 , TYPE_SMALLER /* < */
4034 , TYPE_SEQUAL /* <= */
4035 , TYPE_MATCH /* =~ */
4036 , TYPE_NOMATCH /* !~ */
4037} exptype_T;
4038
4039/*
4040 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4043 */
4044
4045/*
4046 * Handle zero level expression.
4047 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004049 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 * Return OK or FAIL.
4051 */
4052 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u **nextcmd;
4057 int evaluate;
4058{
4059 int ret;
4060 char_u *p;
4061
4062 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (ret == FAIL || !ends_excmd(*p))
4065 {
4066 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 /*
4069 * Report the invalid expression unless the expression evaluation has
4070 * been cancelled due to an aborting error, an interrupt, or an
4071 * exception.
4072 */
4073 if (!aborting())
4074 EMSG2(_(e_invexpr2), arg);
4075 ret = FAIL;
4076 }
4077 if (nextcmd != NULL)
4078 *nextcmd = check_nextcmd(p);
4079
4080 return ret;
4081}
4082
4083/*
4084 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004085 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 *
4087 * "arg" must point to the first non-white of the expression.
4088 * "arg" is advanced to the next non-white after the recognized expression.
4089 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004090 * Note: "rettv.v_lock" is not set.
4091 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * Return OK or FAIL.
4093 */
4094 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 int evaluate;
4099{
4100 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004101 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 /*
4104 * Get the first variable.
4105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108
4109 if ((*arg)[0] == '?')
4110 {
4111 result = FALSE;
4112 if (evaluate)
4113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 int error = FALSE;
4115
4116 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004119 if (error)
4120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122
4123 /*
4124 * Get the second variable.
4125 */
4126 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return FAIL;
4129
4130 /*
4131 * Check for the ":".
4132 */
4133 if ((*arg)[0] != ':')
4134 {
4135 EMSG(_("E109: Missing ':' after '?'"));
4136 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 return FAIL;
4139 }
4140
4141 /*
4142 * Get the third variable.
4143 */
4144 *arg = skipwhite(*arg + 1);
4145 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4146 {
4147 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return FAIL;
4150 }
4151 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154
4155 return OK;
4156}
4157
4158/*
4159 * Handle first level expression:
4160 * expr2 || expr2 || expr2 logical OR
4161 *
4162 * "arg" must point to the first non-white of the expression.
4163 * "arg" is advanced to the next non-white after the recognized expression.
4164 *
4165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 int evaluate;
4172{
Bram Moolenaar33570922005-01-25 22:26:29 +00004173 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 long result;
4175 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /*
4179 * Get the first variable.
4180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183
4184 /*
4185 * Repeat until there is no following "||".
4186 */
4187 first = TRUE;
4188 result = FALSE;
4189 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4190 {
4191 if (evaluate && first)
4192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (error)
4197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 first = FALSE;
4199 }
4200
4201 /*
4202 * Get the second variable.
4203 */
4204 *arg = skipwhite(*arg + 2);
4205 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4206 return FAIL;
4207
4208 /*
4209 * Compute the result.
4210 */
4211 if (evaluate && !result)
4212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004213 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 if (error)
4217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 if (evaluate)
4220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 rettv->v_type = VAR_NUMBER;
4222 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 }
4225
4226 return OK;
4227}
4228
4229/*
4230 * Handle second level expression:
4231 * expr3 && expr3 && expr3 logical AND
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 int evaluate;
4243{
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 long result;
4246 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 /*
4256 * Repeat until there is no following "&&".
4257 */
4258 first = TRUE;
4259 result = TRUE;
4260 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4261 {
4262 if (evaluate && first)
4263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (error)
4268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 first = FALSE;
4270 }
4271
4272 /*
4273 * Get the second variable.
4274 */
4275 *arg = skipwhite(*arg + 2);
4276 if (eval4(arg, &var2, evaluate && result) == FAIL)
4277 return FAIL;
4278
4279 /*
4280 * Compute the result.
4281 */
4282 if (evaluate && result)
4283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (error)
4288 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 if (evaluate)
4291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 rettv->v_type = VAR_NUMBER;
4293 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 }
4296
4297 return OK;
4298}
4299
4300/*
4301 * Handle third level expression:
4302 * var1 == var2
4303 * var1 =~ var2
4304 * var1 != var2
4305 * var1 !~ var2
4306 * var1 > var2
4307 * var1 >= var2
4308 * var1 < var2
4309 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004310 * var1 is var2
4311 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 *
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4315 *
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int evaluate;
4323{
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 char_u *p;
4326 int i;
4327 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004328 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int len = 2;
4330 long n1, n2;
4331 char_u *s1, *s2;
4332 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4333 regmatch_T regmatch;
4334 int ic;
4335 char_u *save_cpo;
4336
4337 /*
4338 * Get the first variable.
4339 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return FAIL;
4342
4343 p = *arg;
4344 switch (p[0])
4345 {
4346 case '=': if (p[1] == '=')
4347 type = TYPE_EQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_MATCH;
4350 break;
4351 case '!': if (p[1] == '=')
4352 type = TYPE_NEQUAL;
4353 else if (p[1] == '~')
4354 type = TYPE_NOMATCH;
4355 break;
4356 case '>': if (p[1] != '=')
4357 {
4358 type = TYPE_GREATER;
4359 len = 1;
4360 }
4361 else
4362 type = TYPE_GEQUAL;
4363 break;
4364 case '<': if (p[1] != '=')
4365 {
4366 type = TYPE_SMALLER;
4367 len = 1;
4368 }
4369 else
4370 type = TYPE_SEQUAL;
4371 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 case 'i': if (p[1] == 's')
4373 {
4374 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4375 len = 5;
4376 if (!vim_isIDc(p[len]))
4377 {
4378 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4379 type_is = TRUE;
4380 }
4381 }
4382 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384
4385 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
4388 if (type != TYPE_UNKNOWN)
4389 {
4390 /* extra question mark appended: ignore case */
4391 if (p[len] == '?')
4392 {
4393 ic = TRUE;
4394 ++len;
4395 }
4396 /* extra '#' appended: match case */
4397 else if (p[len] == '#')
4398 {
4399 ic = FALSE;
4400 ++len;
4401 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004402 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else
4404 ic = p_ic;
4405
4406 /*
4407 * Get the second variable.
4408 */
4409 *arg = skipwhite(p + len);
4410 if (eval5(arg, &var2, evaluate) == FAIL)
4411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 return FAIL;
4414 }
4415
4416 if (evaluate)
4417 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 if (type_is && rettv->v_type != var2.v_type)
4419 {
4420 /* For "is" a different type always means FALSE, for "notis"
4421 * it means TRUE. */
4422 n1 = (type == TYPE_NEQUAL);
4423 }
4424 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4425 {
4426 if (type_is)
4427 {
4428 n1 = (rettv->v_type == var2.v_type
4429 && rettv->vval.v_list == var2.vval.v_list);
4430 if (type == TYPE_NEQUAL)
4431 n1 = !n1;
4432 }
4433 else if (rettv->v_type != var2.v_type
4434 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4435 {
4436 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004437 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004438 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004439 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 clear_tv(rettv);
4441 clear_tv(&var2);
4442 return FAIL;
4443 }
4444 else
4445 {
4446 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004447 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4448 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 if (type == TYPE_NEQUAL)
4450 n1 = !n1;
4451 }
4452 }
4453
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004454 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4455 {
4456 if (type_is)
4457 {
4458 n1 = (rettv->v_type == var2.v_type
4459 && rettv->vval.v_dict == var2.vval.v_dict);
4460 if (type == TYPE_NEQUAL)
4461 n1 = !n1;
4462 }
4463 else if (rettv->v_type != var2.v_type
4464 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4465 {
4466 if (rettv->v_type != var2.v_type)
4467 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4468 else
4469 EMSG(_("E736: Invalid operation for Dictionary"));
4470 clear_tv(rettv);
4471 clear_tv(&var2);
4472 return FAIL;
4473 }
4474 else
4475 {
4476 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004477 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4478 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004479 if (type == TYPE_NEQUAL)
4480 n1 = !n1;
4481 }
4482 }
4483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004484 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4485 {
4486 if (rettv->v_type != var2.v_type
4487 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4488 {
4489 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004490 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004491 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 clear_tv(rettv);
4494 clear_tv(&var2);
4495 return FAIL;
4496 }
4497 else
4498 {
4499 /* Compare two Funcrefs for being equal or unequal. */
4500 if (rettv->vval.v_string == NULL
4501 || var2.vval.v_string == NULL)
4502 n1 = FALSE;
4503 else
4504 n1 = STRCMP(rettv->vval.v_string,
4505 var2.vval.v_string) == 0;
4506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 }
4510
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004511#ifdef FEAT_FLOAT
4512 /*
4513 * If one of the two variables is a float, compare as a float.
4514 * When using "=~" or "!~", always compare as string.
4515 */
4516 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4517 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4518 {
4519 float_T f1, f2;
4520
4521 if (rettv->v_type == VAR_FLOAT)
4522 f1 = rettv->vval.v_float;
4523 else
4524 f1 = get_tv_number(rettv);
4525 if (var2.v_type == VAR_FLOAT)
4526 f2 = var2.vval.v_float;
4527 else
4528 f2 = get_tv_number(&var2);
4529 n1 = FALSE;
4530 switch (type)
4531 {
4532 case TYPE_EQUAL: n1 = (f1 == f2); break;
4533 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4534 case TYPE_GREATER: n1 = (f1 > f2); break;
4535 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4536 case TYPE_SMALLER: n1 = (f1 < f2); break;
4537 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4538 case TYPE_UNKNOWN:
4539 case TYPE_MATCH:
4540 case TYPE_NOMATCH: break; /* avoid gcc warning */
4541 }
4542 }
4543#endif
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 /*
4546 * If one of the two variables is a number, compare as a number.
4547 * When using "=~" or "!~", always compare as string.
4548 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 n1 = get_tv_number(rettv);
4553 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 switch (type)
4555 {
4556 case TYPE_EQUAL: n1 = (n1 == n2); break;
4557 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4558 case TYPE_GREATER: n1 = (n1 > n2); break;
4559 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4560 case TYPE_SMALLER: n1 = (n1 < n2); break;
4561 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4562 case TYPE_UNKNOWN:
4563 case TYPE_MATCH:
4564 case TYPE_NOMATCH: break; /* avoid gcc warning */
4565 }
4566 }
4567 else
4568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 s1 = get_tv_string_buf(rettv, buf1);
4570 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4572 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4573 else
4574 i = 0;
4575 n1 = FALSE;
4576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (i == 0); break;
4579 case TYPE_NEQUAL: n1 = (i != 0); break;
4580 case TYPE_GREATER: n1 = (i > 0); break;
4581 case TYPE_GEQUAL: n1 = (i >= 0); break;
4582 case TYPE_SMALLER: n1 = (i < 0); break;
4583 case TYPE_SEQUAL: n1 = (i <= 0); break;
4584
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH:
4587 /* avoid 'l' flag in 'cpoptions' */
4588 save_cpo = p_cpo;
4589 p_cpo = (char_u *)"";
4590 regmatch.regprog = vim_regcomp(s2,
4591 RE_MAGIC + RE_STRING);
4592 regmatch.rm_ic = ic;
4593 if (regmatch.regprog != NULL)
4594 {
4595 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004596 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (type == TYPE_NOMATCH)
4598 n1 = !n1;
4599 }
4600 p_cpo = save_cpo;
4601 break;
4602
4603 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4604 }
4605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 rettv->v_type = VAR_NUMBER;
4609 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 }
4612
4613 return OK;
4614}
4615
4616/*
4617 * Handle fourth level expression:
4618 * + number addition
4619 * - number subtraction
4620 * . string concatenation
4621 *
4622 * "arg" must point to the first non-white of the expression.
4623 * "arg" is advanced to the next non-white after the recognized expression.
4624 *
4625 * Return OK or FAIL.
4626 */
4627 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 int evaluate;
4632{
Bram Moolenaar33570922005-01-25 22:26:29 +00004633 typval_T var2;
4634 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 int op;
4636 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004637#ifdef FEAT_FLOAT
4638 float_T f1 = 0, f2 = 0;
4639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 char_u *s1, *s2;
4641 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4642 char_u *p;
4643
4644 /*
4645 * Get the first variable.
4646 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004647 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 return FAIL;
4649
4650 /*
4651 * Repeat computing, until no '+', '-' or '.' is following.
4652 */
4653 for (;;)
4654 {
4655 op = **arg;
4656 if (op != '+' && op != '-' && op != '.')
4657 break;
4658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659 if ((op != '+' || rettv->v_type != VAR_LIST)
4660#ifdef FEAT_FLOAT
4661 && (op == '.' || rettv->v_type != VAR_FLOAT)
4662#endif
4663 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 {
4665 /* For "list + ...", an illegal use of the first operand as
4666 * a number cannot be determined before evaluating the 2nd
4667 * operand: if this is also a list, all is ok.
4668 * For "something . ...", "something - ..." or "non-list + ...",
4669 * we know that the first operand needs to be a string or number
4670 * without evaluating the 2nd operand. So check before to avoid
4671 * side effects after an error. */
4672 if (evaluate && get_tv_string_chk(rettv) == NULL)
4673 {
4674 clear_tv(rettv);
4675 return FAIL;
4676 }
4677 }
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 /*
4680 * Get the second variable.
4681 */
4682 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004683 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 return FAIL;
4687 }
4688
4689 if (evaluate)
4690 {
4691 /*
4692 * Compute the result.
4693 */
4694 if (op == '.')
4695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4697 s2 = get_tv_string_buf_chk(&var2, buf2);
4698 if (s2 == NULL) /* type error ? */
4699 {
4700 clear_tv(rettv);
4701 clear_tv(&var2);
4702 return FAIL;
4703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004704 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004705 clear_tv(rettv);
4706 rettv->v_type = VAR_STRING;
4707 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004709 else if (op == '+' && rettv->v_type == VAR_LIST
4710 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004711 {
4712 /* concatenate Lists */
4713 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4714 &var3) == FAIL)
4715 {
4716 clear_tv(rettv);
4717 clear_tv(&var2);
4718 return FAIL;
4719 }
4720 clear_tv(rettv);
4721 *rettv = var3;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
4724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 int error = FALSE;
4726
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727#ifdef FEAT_FLOAT
4728 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730 f1 = rettv->vval.v_float;
4731 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733 else
4734#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004735 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004736 n1 = get_tv_number_chk(rettv, &error);
4737 if (error)
4738 {
4739 /* This can only happen for "list + non-list". For
4740 * "non-list + ..." or "something - ...", we returned
4741 * before evaluating the 2nd operand. */
4742 clear_tv(rettv);
4743 return FAIL;
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 f1 = n1;
4748#endif
4749 }
4750#ifdef FEAT_FLOAT
4751 if (var2.v_type == VAR_FLOAT)
4752 {
4753 f2 = var2.vval.v_float;
4754 n2 = 0;
4755 }
4756 else
4757#endif
4758 {
4759 n2 = get_tv_number_chk(&var2, &error);
4760 if (error)
4761 {
4762 clear_tv(rettv);
4763 clear_tv(&var2);
4764 return FAIL;
4765 }
4766#ifdef FEAT_FLOAT
4767 if (rettv->v_type == VAR_FLOAT)
4768 f2 = n2;
4769#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772
4773#ifdef FEAT_FLOAT
4774 /* If there is a float on either side the result is a float. */
4775 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4776 {
4777 if (op == '+')
4778 f1 = f1 + f2;
4779 else
4780 f1 = f1 - f2;
4781 rettv->v_type = VAR_FLOAT;
4782 rettv->vval.v_float = f1;
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#endif
4786 {
4787 if (op == '+')
4788 n1 = n1 + n2;
4789 else
4790 n1 = n1 - n2;
4791 rettv->v_type = VAR_NUMBER;
4792 rettv->vval.v_number = n1;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798 return OK;
4799}
4800
4801/*
4802 * Handle fifth level expression:
4803 * * number multiplication
4804 * / number division
4805 * % number modulo
4806 *
4807 * "arg" must point to the first non-white of the expression.
4808 * "arg" is advanced to the next non-white after the recognized expression.
4809 *
4810 * Return OK or FAIL.
4811 */
4812 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004813eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
Bram Moolenaar33570922005-01-25 22:26:29 +00004819 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 int op;
4821 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822#ifdef FEAT_FLOAT
4823 int use_float = FALSE;
4824 float_T f1 = 0, f2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828 /*
4829 * Get the first variable.
4830 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004831 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return FAIL;
4833
4834 /*
4835 * Repeat computing, until no '*', '/' or '%' is following.
4836 */
4837 for (;;)
4838 {
4839 op = **arg;
4840 if (op != '*' && op != '/' && op != '%')
4841 break;
4842
4843 if (evaluate)
4844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 if (rettv->v_type == VAR_FLOAT)
4847 {
4848 f1 = rettv->vval.v_float;
4849 use_float = TRUE;
4850 n1 = 0;
4851 }
4852 else
4853#endif
4854 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 if (error)
4857 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else
4860 n1 = 0;
4861
4862 /*
4863 * Get the second variable.
4864 */
4865 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004866 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return FAIL;
4868
4869 if (evaluate)
4870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871#ifdef FEAT_FLOAT
4872 if (var2.v_type == VAR_FLOAT)
4873 {
4874 if (!use_float)
4875 {
4876 f1 = n1;
4877 use_float = TRUE;
4878 }
4879 f2 = var2.vval.v_float;
4880 n2 = 0;
4881 }
4882 else
4883#endif
4884 {
4885 n2 = get_tv_number_chk(&var2, &error);
4886 clear_tv(&var2);
4887 if (error)
4888 return FAIL;
4889#ifdef FEAT_FLOAT
4890 if (use_float)
4891 f2 = n2;
4892#endif
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 /*
4896 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899#ifdef FEAT_FLOAT
4900 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 if (op == '*')
4903 f1 = f1 * f2;
4904 else if (op == '/')
4905 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004906# ifdef VMS
4907 /* VMS crashes on divide by zero, work around it */
4908 if (f2 == 0.0)
4909 {
4910 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004911 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004912 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004913 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004914 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004915 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004916 }
4917 else
4918 f1 = f1 / f2;
4919# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 /* We rely on the floating point library to handle divide
4921 * by zero to result in "inf" and not a crash. */
4922 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004923# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004927 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 return FAIL;
4929 }
4930 rettv->v_type = VAR_FLOAT;
4931 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936 if (op == '*')
4937 n1 = n1 * n2;
4938 else if (op == '/')
4939 {
4940 if (n2 == 0) /* give an error message? */
4941 {
4942 if (n1 == 0)
4943 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4944 else if (n1 < 0)
4945 n1 = -0x7fffffffL;
4946 else
4947 n1 = 0x7fffffffL;
4948 }
4949 else
4950 n1 = n1 / n2;
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 {
4954 if (n2 == 0) /* give an error message? */
4955 n1 = 0;
4956 else
4957 n1 = n1 % n2;
4958 }
4959 rettv->v_type = VAR_NUMBER;
4960 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 }
4964
4965 return OK;
4966}
4967
4968/*
4969 * Handle sixth level expression:
4970 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004971 * "string" string constant
4972 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 * &option-name option value
4974 * @r register contents
4975 * identifier variable value
4976 * function() function call
4977 * $VAR environment variable
4978 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004979 * [expr, expr] List
4980 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 *
4982 * Also handle:
4983 * ! in front logical NOT
4984 * - in front unary minus
4985 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 * trailing [] subscript in String or List
4987 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 *
4989 * "arg" must point to the first non-white of the expression.
4990 * "arg" is advanced to the next non-white after the recognized expression.
4991 *
4992 * Return OK or FAIL.
4993 */
4994 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004995eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004999 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 long n;
5002 int len;
5003 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 char_u *start_leader, *end_leader;
5005 int ret = OK;
5006 char_u *alias;
5007
5008 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 /*
5015 * Skip '!' and '-' characters. They are handled later.
5016 */
5017 start_leader = *arg;
5018 while (**arg == '!' || **arg == '-' || **arg == '+')
5019 *arg = skipwhite(*arg + 1);
5020 end_leader = *arg;
5021
5022 switch (**arg)
5023 {
5024 /*
5025 * Number constant.
5026 */
5027 case '0':
5028 case '1':
5029 case '2':
5030 case '3':
5031 case '4':
5032 case '5':
5033 case '6':
5034 case '7':
5035 case '8':
5036 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 {
5038#ifdef FEAT_FLOAT
5039 char_u *p = skipdigits(*arg + 1);
5040 int get_float = FALSE;
5041
5042 /* We accept a float when the format matches
5043 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005044 * strict to avoid backwards compatibility problems.
5045 * Don't look for a float after the "." operator, so that
5046 * ":let vers = 1.2.3" doesn't fail. */
5047 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 get_float = TRUE;
5050 p = skipdigits(p + 2);
5051 if (*p == 'e' || *p == 'E')
5052 {
5053 ++p;
5054 if (*p == '-' || *p == '+')
5055 ++p;
5056 if (!vim_isdigit(*p))
5057 get_float = FALSE;
5058 else
5059 p = skipdigits(p + 1);
5060 }
5061 if (ASCII_ISALPHA(*p) || *p == '.')
5062 get_float = FALSE;
5063 }
5064 if (get_float)
5065 {
5066 float_T f;
5067
5068 *arg += string2float(*arg, &f);
5069 if (evaluate)
5070 {
5071 rettv->v_type = VAR_FLOAT;
5072 rettv->vval.v_float = f;
5073 }
5074 }
5075 else
5076#endif
5077 {
5078 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5079 *arg += len;
5080 if (evaluate)
5081 {
5082 rettv->v_type = VAR_NUMBER;
5083 rettv->vval.v_number = n;
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * String constant: "string".
5091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005092 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 break;
5094
5095 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 break;
5100
5101 /*
5102 * List: [expr, expr]
5103 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 break;
5106
5107 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 * Dictionary: {key: val, key: val}
5109 */
5110 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5111 break;
5112
5113 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005116 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 break;
5118
5119 /*
5120 * Environment variable: $VAR.
5121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 break;
5124
5125 /*
5126 * Register contents: @r.
5127 */
5128 case '@': ++*arg;
5129 if (evaluate)
5130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005131 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005132 rettv->vval.v_string = get_reg_contents(**arg,
5133 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 if (**arg != NUL)
5136 ++*arg;
5137 break;
5138
5139 /*
5140 * nested expression: (expression).
5141 */
5142 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (**arg == ')')
5145 ++*arg;
5146 else if (ret == OK)
5147 {
5148 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 ret = FAIL;
5151 }
5152 break;
5153
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157
5158 if (ret == NOTDONE)
5159 {
5160 /*
5161 * Must be a variable or function name.
5162 * Can also be a curly-braces kind of name: {expr}.
5163 */
5164 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 if (alias != NULL)
5167 s = alias;
5168
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005169 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 ret = FAIL;
5171 else
5172 {
5173 if (**arg == '(') /* recursive! */
5174 {
5175 /* If "s" is the name of a variable of type VAR_FUNC
5176 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005177 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178
5179 /* Invoke the function. */
5180 ret = get_func_tv(s, len, rettv, arg,
5181 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005182 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005183
5184 /* If evaluate is FALSE rettv->v_type was not set in
5185 * get_func_tv, but it's needed in handle_subscript() to parse
5186 * what follows. So set it here. */
5187 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5188 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005189 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005190 rettv->v_type = VAR_FUNC;
5191 }
5192
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 /* Stop the expression evaluation when immediately
5194 * aborting on error, or when an interrupt occurred or
5195 * an exception was thrown but not caught. */
5196 if (aborting())
5197 {
5198 if (ret == OK)
5199 clear_tv(rettv);
5200 ret = FAIL;
5201 }
5202 }
5203 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005204 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005205 else
5206 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005208 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 }
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 *arg = skipwhite(*arg);
5212
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5214 * expr(expr). */
5215 if (ret == OK)
5216 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 /*
5219 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5220 */
5221 if (ret == OK && evaluate && end_leader > start_leader)
5222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005224 int val = 0;
5225#ifdef FEAT_FLOAT
5226 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228 if (rettv->v_type == VAR_FLOAT)
5229 f = rettv->vval.v_float;
5230 else
5231#endif
5232 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 clear_tv(rettv);
5236 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005238 else
5239 {
5240 while (end_leader > start_leader)
5241 {
5242 --end_leader;
5243 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005244 {
5245#ifdef FEAT_FLOAT
5246 if (rettv->v_type == VAR_FLOAT)
5247 f = !f;
5248 else
5249#endif
5250 val = !val;
5251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253 {
5254#ifdef FEAT_FLOAT
5255 if (rettv->v_type == VAR_FLOAT)
5256 f = -f;
5257 else
5258#endif
5259 val = -val;
5260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005261 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005262#ifdef FEAT_FLOAT
5263 if (rettv->v_type == VAR_FLOAT)
5264 {
5265 clear_tv(rettv);
5266 rettv->vval.v_float = f;
5267 }
5268 else
5269#endif
5270 {
5271 clear_tv(rettv);
5272 rettv->v_type = VAR_NUMBER;
5273 rettv->vval.v_number = val;
5274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277
5278 return ret;
5279}
5280
5281/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005282 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5283 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5285 */
5286 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292{
5293 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 long len = -1;
5297 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005301 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005303 if (verbose)
5304 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 return FAIL;
5306 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005307#ifdef FEAT_FLOAT
5308 else if (rettv->v_type == VAR_FLOAT)
5309 {
5310 if (verbose)
5311 EMSG(_(e_float_as_string));
5312 return FAIL;
5313 }
5314#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315
Bram Moolenaar8c711452005-01-14 21:53:12 +00005316 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 /*
5319 * dict.name
5320 */
5321 key = *arg + 1;
5322 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5323 ;
5324 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 }
5328 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005330 /*
5331 * something[idx]
5332 *
5333 * Get the (first) variable from inside the [].
5334 */
5335 *arg = skipwhite(*arg + 1);
5336 if (**arg == ':')
5337 empty1 = TRUE;
5338 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5339 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5341 {
5342 /* not a number or string */
5343 clear_tv(&var1);
5344 return FAIL;
5345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346
5347 /*
5348 * Get the second variable from inside the [:].
5349 */
5350 if (**arg == ':')
5351 {
5352 range = TRUE;
5353 *arg = skipwhite(*arg + 1);
5354 if (**arg == ']')
5355 empty2 = TRUE;
5356 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 if (!empty1)
5359 clear_tv(&var1);
5360 return FAIL;
5361 }
5362 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5363 {
5364 /* not a number or string */
5365 if (!empty1)
5366 clear_tv(&var1);
5367 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 return FAIL;
5369 }
5370 }
5371
5372 /* Check for the ']'. */
5373 if (**arg != ']')
5374 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005375 if (verbose)
5376 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 clear_tv(&var1);
5378 if (range)
5379 clear_tv(&var2);
5380 return FAIL;
5381 }
5382 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384
5385 if (evaluate)
5386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 n1 = 0;
5388 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 n1 = get_tv_number(&var1);
5391 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 if (range)
5394 {
5395 if (empty2)
5396 n2 = -1;
5397 else
5398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 n2 = get_tv_number(&var2);
5400 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 }
5402 }
5403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
5406 case VAR_NUMBER:
5407 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 len = (long)STRLEN(s);
5410 if (range)
5411 {
5412 /* The resulting variable is a substring. If the indexes
5413 * are out of range the result is empty. */
5414 if (n1 < 0)
5415 {
5416 n1 = len + n1;
5417 if (n1 < 0)
5418 n1 = 0;
5419 }
5420 if (n2 < 0)
5421 n2 = len + n2;
5422 else if (n2 >= len)
5423 n2 = len;
5424 if (n1 >= len || n2 < 0 || n1 > n2)
5425 s = NULL;
5426 else
5427 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5428 }
5429 else
5430 {
5431 /* The resulting variable is a string of a single
5432 * character. If the index is too big or negative the
5433 * result is empty. */
5434 if (n1 >= len || n1 < 0)
5435 s = NULL;
5436 else
5437 s = vim_strnsave(s + n1, 1);
5438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 clear_tv(rettv);
5440 rettv->v_type = VAR_STRING;
5441 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 break;
5443
5444 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 if (n1 < 0)
5447 n1 = len + n1;
5448 if (!empty1 && (n1 < 0 || n1 >= len))
5449 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005450 /* For a range we allow invalid values and return an empty
5451 * list. A list index out of range is an error. */
5452 if (!range)
5453 {
5454 if (verbose)
5455 EMSGN(_(e_listidx), n1);
5456 return FAIL;
5457 }
5458 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 if (range)
5461 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 list_T *l;
5463 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464
5465 if (n2 < 0)
5466 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005467 else if (n2 >= len)
5468 n2 = len - 1;
5469 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005470 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 l = list_alloc();
5472 if (l == NULL)
5473 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 n1 <= n2; ++n1)
5476 {
5477 if (list_append_tv(l, &item->li_tv) == FAIL)
5478 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005479 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 return FAIL;
5481 }
5482 item = item->li_next;
5483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 clear_tv(rettv);
5485 rettv->v_type = VAR_LIST;
5486 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005487 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 }
5489 else
5490 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005491 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 clear_tv(rettv);
5493 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 }
5495 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496
5497 case VAR_DICT:
5498 if (range)
5499 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005500 if (verbose)
5501 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 if (len == -1)
5503 clear_tv(&var1);
5504 return FAIL;
5505 }
5506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508
5509 if (len == -1)
5510 {
5511 key = get_tv_string(&var1);
5512 if (*key == NUL)
5513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 if (verbose)
5515 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 clear_tv(&var1);
5517 return FAIL;
5518 }
5519 }
5520
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005521 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005523 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005524 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005525 if (len == -1)
5526 clear_tv(&var1);
5527 if (item == NULL)
5528 return FAIL;
5529
5530 copy_tv(&item->di_tv, &var1);
5531 clear_tv(rettv);
5532 *rettv = var1;
5533 }
5534 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 }
5536 }
5537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 return OK;
5539}
5540
5541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 * Get an option value.
5543 * "arg" points to the '&' or '+' before the option name.
5544 * "arg" is advanced to character after the option name.
5545 * Return OK or FAIL.
5546 */
5547 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 int evaluate;
5552{
5553 char_u *option_end;
5554 long numval;
5555 char_u *stringval;
5556 int opt_type;
5557 int c;
5558 int working = (**arg == '+'); /* has("+option") */
5559 int ret = OK;
5560 int opt_flags;
5561
5562 /*
5563 * Isolate the option name and find its value.
5564 */
5565 option_end = find_option_end(arg, &opt_flags);
5566 if (option_end == NULL)
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 EMSG2(_("E112: Option name missing: %s"), *arg);
5570 return FAIL;
5571 }
5572
5573 if (!evaluate)
5574 {
5575 *arg = option_end;
5576 return OK;
5577 }
5578
5579 c = *option_end;
5580 *option_end = NUL;
5581 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 if (opt_type == -3) /* invalid name */
5585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 EMSG2(_("E113: Unknown option: %s"), *arg);
5588 ret = FAIL;
5589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 if (opt_type == -2) /* hidden string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == -1) /* hidden number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else if (opt_type == 1) /* number option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_NUMBER;
5605 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 else /* string option */
5608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609 rettv->v_type = VAR_STRING;
5610 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612 }
5613 else if (working && (opt_type == -2 || opt_type == -1))
5614 ret = FAIL;
5615
5616 *option_end = c; /* put back for error messages */
5617 *arg = option_end;
5618
5619 return ret;
5620}
5621
5622/*
5623 * Allocate a variable for a string constant.
5624 * Return OK or FAIL.
5625 */
5626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 int evaluate;
5631{
5632 char_u *p;
5633 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 int extra = 0;
5635
5636 /*
5637 * Find the end of the string, skipping backslashed characters.
5638 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 if (*p == '\\' && p[1] != NUL)
5642 {
5643 ++p;
5644 /* A "\<x>" form occupies at least 4 characters, and produces up
5645 * to 6 characters: reserve space for 2 extra */
5646 if (*p == '<')
5647 extra += 2;
5648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
5650
5651 if (*p != '"')
5652 {
5653 EMSG2(_("E114: Missing quote: %s"), *arg);
5654 return FAIL;
5655 }
5656
5657 /* If only parsing, set *arg and return here */
5658 if (!evaluate)
5659 {
5660 *arg = p + 1;
5661 return OK;
5662 }
5663
5664 /*
5665 * Copy the string into allocated memory, handling backslashed
5666 * characters.
5667 */
5668 name = alloc((unsigned)(p - *arg + extra));
5669 if (name == NULL)
5670 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 if (*p == '\\')
5677 {
5678 switch (*++p)
5679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 case 'b': *name++ = BS; ++p; break;
5681 case 'e': *name++ = ESC; ++p; break;
5682 case 'f': *name++ = FF; ++p; break;
5683 case 'n': *name++ = NL; ++p; break;
5684 case 'r': *name++ = CAR; ++p; break;
5685 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 case 'X': /* hex: "\x1", "\x12" */
5688 case 'x':
5689 case 'u': /* Unicode: "\u0023" */
5690 case 'U':
5691 if (vim_isxdigit(p[1]))
5692 {
5693 int n, nr;
5694 int c = toupper(*p);
5695
5696 if (c == 'X')
5697 n = 2;
5698 else
5699 n = 4;
5700 nr = 0;
5701 while (--n >= 0 && vim_isxdigit(p[1]))
5702 {
5703 ++p;
5704 nr = (nr << 4) + hex2nr(*p);
5705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_MBYTE
5708 /* For "\u" store the number according to
5709 * 'encoding'. */
5710 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else
5713#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 break;
5717
5718 /* octal: "\1", "\12", "\123" */
5719 case '0':
5720 case '1':
5721 case '2':
5722 case '3':
5723 case '4':
5724 case '5':
5725 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 case '7': *name = *p++ - '0';
5727 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 *name = (*name << 3) + *p++ - '0';
5730 if (*p >= '0' && *p <= '7')
5731 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 break;
5735
5736 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (extra != 0)
5739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 /* FALLTHROUGH */
5744
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
5747 }
5748 }
5749 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 *arg = p + 1;
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return OK;
5757}
5758
5759/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 * Return OK or FAIL.
5762 */
5763 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005764get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int evaluate;
5768{
5769 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 int reduce = 0;
5772
5773 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 */
5776 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5777 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 break;
5782 ++reduce;
5783 ++p;
5784 }
5785 }
5786
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 return FAIL;
5791 }
5792
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 if (!evaluate)
5795 {
5796 *arg = p + 1;
5797 return OK;
5798 }
5799
5800 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 */
5803 str = alloc((unsigned)((p - *arg) - reduce));
5804 if (str == NULL)
5805 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 rettv->v_type = VAR_STRING;
5807 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 break;
5815 ++p;
5816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 *arg = p + 1;
5821
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005822 return OK;
5823}
5824
5825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 * Allocate a variable for a List and fill it from "*arg".
5827 * Return OK or FAIL.
5828 */
5829 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 int evaluate;
5834{
Bram Moolenaar33570922005-01-25 22:26:29 +00005835 list_T *l = NULL;
5836 typval_T tv;
5837 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838
5839 if (evaluate)
5840 {
5841 l = list_alloc();
5842 if (l == NULL)
5843 return FAIL;
5844 }
5845
5846 *arg = skipwhite(*arg + 1);
5847 while (**arg != ']' && **arg != NUL)
5848 {
5849 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5850 goto failret;
5851 if (evaluate)
5852 {
5853 item = listitem_alloc();
5854 if (item != NULL)
5855 {
5856 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005857 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 list_append(l, item);
5859 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005860 else
5861 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 }
5863
5864 if (**arg == ']')
5865 break;
5866 if (**arg != ',')
5867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 goto failret;
5870 }
5871 *arg = skipwhite(*arg + 1);
5872 }
5873
5874 if (**arg != ']')
5875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877failret:
5878 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005879 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 return FAIL;
5881 }
5882
5883 *arg = skipwhite(*arg + 1);
5884 if (evaluate)
5885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005886 rettv->v_type = VAR_LIST;
5887 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 ++l->lv_refcount;
5889 }
5890
5891 return OK;
5892}
5893
5894/*
5895 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005896 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005898 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899list_alloc()
5900{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005901 list_T *l;
5902
5903 l = (list_T *)alloc_clear(sizeof(list_T));
5904 if (l != NULL)
5905 {
5906 /* Prepend the list to the list of lists for garbage collection. */
5907 if (first_list != NULL)
5908 first_list->lv_used_prev = l;
5909 l->lv_used_prev = NULL;
5910 l->lv_used_next = first_list;
5911 first_list = l;
5912 }
5913 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914}
5915
5916/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005917 * Allocate an empty list for a return value.
5918 * Returns OK or FAIL.
5919 */
5920 static int
5921rettv_list_alloc(rettv)
5922 typval_T *rettv;
5923{
5924 list_T *l = list_alloc();
5925
5926 if (l == NULL)
5927 return FAIL;
5928
5929 rettv->vval.v_list = l;
5930 rettv->v_type = VAR_LIST;
5931 ++l->lv_refcount;
5932 return OK;
5933}
5934
5935/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 * Unreference a list: decrement the reference count and free it when it
5937 * becomes zero.
5938 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005939 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943 if (l != NULL && --l->lv_refcount <= 0)
5944 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945}
5946
5947/*
5948 * Free a list, including all items it points to.
5949 * Ignores the reference count.
5950 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005951 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005952list_free(l, recurse)
5953 list_T *l;
5954 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955{
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005958 /* Remove the list from the list of lists for garbage collection. */
5959 if (l->lv_used_prev == NULL)
5960 first_list = l->lv_used_next;
5961 else
5962 l->lv_used_prev->lv_used_next = l->lv_used_next;
5963 if (l->lv_used_next != NULL)
5964 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5965
Bram Moolenaard9fba312005-06-26 22:34:35 +00005966 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005968 /* Remove the item before deleting it. */
5969 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005970 if (recurse || (item->li_tv.v_type != VAR_LIST
5971 && item->li_tv.v_type != VAR_DICT))
5972 clear_tv(&item->li_tv);
5973 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 }
5975 vim_free(l);
5976}
5977
5978/*
5979 * Allocate a list item.
5980 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005981 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982listitem_alloc()
5983{
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985}
5986
5987/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005988 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005990 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005994 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 vim_free(item);
5996}
5997
5998/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005999 * Remove a list item from a List and free it. Also clears the value.
6000 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006001 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 list_T *l;
6004 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006005{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006006 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006007 listitem_free(item);
6008}
6009
6010/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 * Get the number of items in a list.
6012 */
6013 static long
6014list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 if (l == NULL)
6018 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006019 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006023 * Return TRUE when two lists have exactly the same values.
6024 */
6025 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006026list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 list_T *l1;
6028 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006029 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006034 if (l1 == NULL || l2 == NULL)
6035 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006036 if (l1 == l2)
6037 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006038 if (list_len(l1) != list_len(l2))
6039 return FALSE;
6040
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006041 for (item1 = l1->lv_first, item2 = l2->lv_first;
6042 item1 != NULL && item2 != NULL;
6043 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006045 return FALSE;
6046 return item1 == NULL && item2 == NULL;
6047}
6048
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006049#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6050 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006051/*
6052 * Return the dictitem that an entry in a hashtable points to.
6053 */
6054 dictitem_T *
6055dict_lookup(hi)
6056 hashitem_T *hi;
6057{
6058 return HI2DI(hi);
6059}
6060#endif
6061
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006063 * Return TRUE when two dictionaries have exactly the same key/values.
6064 */
6065 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 dict_T *d1;
6068 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006070 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071{
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 hashitem_T *hi;
6073 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006076 if (d1 == NULL || d2 == NULL)
6077 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006078 if (d1 == d2)
6079 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 if (dict_len(d1) != dict_len(d2))
6081 return FALSE;
6082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006083 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006086 if (!HASHITEM_EMPTY(hi))
6087 {
6088 item2 = dict_find(d2, hi->hi_key, -1);
6089 if (item2 == NULL)
6090 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092 return FALSE;
6093 --todo;
6094 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006095 }
6096 return TRUE;
6097}
6098
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099static int tv_equal_recurse_limit;
6100
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006101/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006104 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105 */
6106 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 typval_T *tv1;
6109 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 int ic; /* ignore case */
6111 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112{
6113 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006114 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006116 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006118 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006121 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 * recursiveness to a limit. We guess they are equal then.
6123 * A fixed limit has the problem of still taking an awful long time.
6124 * Reduce the limit every time running into it. That should work fine for
6125 * deeply linked structures that are not recursively linked and catch
6126 * recursiveness quickly. */
6127 if (!recursive)
6128 tv_equal_recurse_limit = 1000;
6129 if (recursive_cnt >= tv_equal_recurse_limit)
6130 {
6131 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006132 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006134
6135 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138 ++recursive_cnt;
6139 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6140 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006141 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006142
6143 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006144 ++recursive_cnt;
6145 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6146 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006147 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006148
6149 case VAR_FUNC:
6150 return (tv1->vval.v_string != NULL
6151 && tv2->vval.v_string != NULL
6152 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6153
6154 case VAR_NUMBER:
6155 return tv1->vval.v_number == tv2->vval.v_number;
6156
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006157#ifdef FEAT_FLOAT
6158 case VAR_FLOAT:
6159 return tv1->vval.v_float == tv2->vval.v_float;
6160#endif
6161
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162 case VAR_STRING:
6163 s1 = get_tv_string_buf(tv1, buf1);
6164 s2 = get_tv_string_buf(tv2, buf2);
6165 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167
6168 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006169 return TRUE;
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Locate item with index "n" in list "l" and return it.
6174 * A negative index is counted from the end; -1 is the last item.
6175 * Returns NULL when "n" is out of range.
6176 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006177 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 long n;
6181{
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 long idx;
6184
6185 if (l == NULL)
6186 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006187
6188 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006190 n = l->lv_len + n;
6191
6192 /* Check for index out of range. */
6193 if (n < 0 || n >= l->lv_len)
6194 return NULL;
6195
6196 /* When there is a cached index may start search from there. */
6197 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199 if (n < l->lv_idx / 2)
6200 {
6201 /* closest to the start of the list */
6202 item = l->lv_first;
6203 idx = 0;
6204 }
6205 else if (n > (l->lv_idx + l->lv_len) / 2)
6206 {
6207 /* closest to the end of the list */
6208 item = l->lv_last;
6209 idx = l->lv_len - 1;
6210 }
6211 else
6212 {
6213 /* closest to the cached index */
6214 item = l->lv_idx_item;
6215 idx = l->lv_idx;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
6218 else
6219 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006220 if (n < l->lv_len / 2)
6221 {
6222 /* closest to the start of the list */
6223 item = l->lv_first;
6224 idx = 0;
6225 }
6226 else
6227 {
6228 /* closest to the end of the list */
6229 item = l->lv_last;
6230 idx = l->lv_len - 1;
6231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006233
6234 while (n > idx)
6235 {
6236 /* search forward */
6237 item = item->li_next;
6238 ++idx;
6239 }
6240 while (n < idx)
6241 {
6242 /* search backward */
6243 item = item->li_prev;
6244 --idx;
6245 }
6246
6247 /* cache the used index */
6248 l->lv_idx = idx;
6249 l->lv_idx_item = item;
6250
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 return item;
6252}
6253
6254/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006255 * Get list item "l[idx]" as a number.
6256 */
6257 static long
6258list_find_nr(l, idx, errorp)
6259 list_T *l;
6260 long idx;
6261 int *errorp; /* set to TRUE when something wrong */
6262{
6263 listitem_T *li;
6264
6265 li = list_find(l, idx);
6266 if (li == NULL)
6267 {
6268 if (errorp != NULL)
6269 *errorp = TRUE;
6270 return -1L;
6271 }
6272 return get_tv_number_chk(&li->li_tv, errorp);
6273}
6274
6275/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006276 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6277 */
6278 char_u *
6279list_find_str(l, idx)
6280 list_T *l;
6281 long idx;
6282{
6283 listitem_T *li;
6284
6285 li = list_find(l, idx - 1);
6286 if (li == NULL)
6287 {
6288 EMSGN(_(e_listidx), idx);
6289 return NULL;
6290 }
6291 return get_tv_string(&li->li_tv);
6292}
6293
6294/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006295 * Locate "item" list "l" and return its index.
6296 * Returns -1 when "item" is not in the list.
6297 */
6298 static long
6299list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 list_T *l;
6301 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006302{
6303 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006305
6306 if (l == NULL)
6307 return -1;
6308 idx = 0;
6309 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6310 ++idx;
6311 if (li == NULL)
6312 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006313 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006314}
6315
6316/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 * Append item "item" to the end of list "l".
6318 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006319 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l;
6322 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323{
6324 if (l->lv_last == NULL)
6325 {
6326 /* empty list */
6327 l->lv_first = item;
6328 l->lv_last = item;
6329 item->li_prev = NULL;
6330 }
6331 else
6332 {
6333 l->lv_last->li_next = item;
6334 item->li_prev = l->lv_last;
6335 l->lv_last = item;
6336 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006337 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 item->li_next = NULL;
6339}
6340
6341/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006343 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006345 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 list_T *l;
6348 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006350 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 copy_tv(tv, &li->li_tv);
6355 list_append(l, li);
6356 return OK;
6357}
6358
6359/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006360 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006361 * Return FAIL when out of memory.
6362 */
6363 int
6364list_append_dict(list, dict)
6365 list_T *list;
6366 dict_T *dict;
6367{
6368 listitem_T *li = listitem_alloc();
6369
6370 if (li == NULL)
6371 return FAIL;
6372 li->li_tv.v_type = VAR_DICT;
6373 li->li_tv.v_lock = 0;
6374 li->li_tv.vval.v_dict = dict;
6375 list_append(list, li);
6376 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 return OK;
6378}
6379
6380/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Returns FAIL when out of memory.
6384 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006385 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006386list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 list_T *l;
6388 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006389 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006390{
6391 listitem_T *li = listitem_alloc();
6392
6393 if (li == NULL)
6394 return FAIL;
6395 list_append(l, li);
6396 li->li_tv.v_type = VAR_STRING;
6397 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006398 if (str == NULL)
6399 li->li_tv.vval.v_string = NULL;
6400 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006401 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006402 return FAIL;
6403 return OK;
6404}
6405
6406/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006407 * Append "n" to list "l".
6408 * Returns FAIL when out of memory.
6409 */
6410 static int
6411list_append_number(l, n)
6412 list_T *l;
6413 varnumber_T n;
6414{
6415 listitem_T *li;
6416
6417 li = listitem_alloc();
6418 if (li == NULL)
6419 return FAIL;
6420 li->li_tv.v_type = VAR_NUMBER;
6421 li->li_tv.v_lock = 0;
6422 li->li_tv.vval.v_number = n;
6423 list_append(l, li);
6424 return OK;
6425}
6426
6427/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * If "item" is NULL append at the end.
6430 * Return FAIL when out of memory.
6431 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006432 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 list_T *l;
6435 typval_T *tv;
6436 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439
6440 if (ni == NULL)
6441 return FAIL;
6442 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006443 list_insert(l, ni, item);
6444 return OK;
6445}
6446
6447 void
6448list_insert(l, ni, item)
6449 list_T *l;
6450 listitem_T *ni;
6451 listitem_T *item;
6452{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 if (item == NULL)
6454 /* Append new item at end of list. */
6455 list_append(l, ni);
6456 else
6457 {
6458 /* Insert new item before existing item. */
6459 ni->li_prev = item->li_prev;
6460 ni->li_next = item;
6461 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 ++l->lv_idx;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006469 l->lv_idx_item = NULL;
6470 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006472 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474}
6475
6476/*
6477 * Extend "l1" with "l2".
6478 * If "bef" is NULL append at the end, otherwise insert before this item.
6479 * Returns FAIL when out of memory.
6480 */
6481 static int
6482list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 list_T *l1;
6484 list_T *l2;
6485 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486{
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006488 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006490 /* We also quit the loop when we have inserted the original item count of
6491 * the list, avoid a hang when we extend a list with itself. */
6492 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6494 return FAIL;
6495 return OK;
6496}
6497
6498/*
6499 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6500 * Return FAIL when out of memory.
6501 */
6502 static int
6503list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 list_T *l1;
6505 list_T *l2;
6506 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507{
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006510 if (l1 == NULL || l2 == NULL)
6511 return FAIL;
6512
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 if (l == NULL)
6516 return FAIL;
6517 tv->v_type = VAR_LIST;
6518 tv->vval.v_list = l;
6519
6520 /* append all items from the second list */
6521 return list_extend(l, l2, NULL);
6522}
6523
6524/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 * Returns NULL when out of memory.
6529 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006530 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535{
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 list_T *copy;
6537 listitem_T *item;
6538 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539
6540 if (orig == NULL)
6541 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542
6543 copy = list_alloc();
6544 if (copy != NULL)
6545 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006546 if (copyID != 0)
6547 {
6548 /* Do this before adding the items, because one of the items may
6549 * refer back to this list. */
6550 orig->lv_copyID = copyID;
6551 orig->lv_copylist = copy;
6552 }
6553 for (item = orig->lv_first; item != NULL && !got_int;
6554 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555 {
6556 ni = listitem_alloc();
6557 if (ni == NULL)
6558 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006559 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 {
6561 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6562 {
6563 vim_free(ni);
6564 break;
6565 }
6566 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006568 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 list_append(copy, ni);
6570 }
6571 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 if (item != NULL)
6573 {
6574 list_unref(copy);
6575 copy = NULL;
6576 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 }
6578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 return copy;
6580}
6581
6582/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006584 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006585 * This used to be called list_remove, but that conflicts with a Sun header
6586 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006588 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006589vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 list_T *l;
6591 listitem_T *item;
6592 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593{
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 /* notify watchers */
6597 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006599 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600 list_fix_watch(l, ip);
6601 if (ip == item2)
6602 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604
6605 if (item2->li_next == NULL)
6606 l->lv_last = item->li_prev;
6607 else
6608 item2->li_next->li_prev = item->li_prev;
6609 if (item->li_prev == NULL)
6610 l->lv_first = item2->li_next;
6611 else
6612 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614}
6615
6616/*
6617 * Return an allocated string with the string representation of a list.
6618 * May return NULL.
6619 */
6620 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006621list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006622 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624{
6625 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626
6627 if (tv->vval.v_list == NULL)
6628 return NULL;
6629 ga_init2(&ga, (int)sizeof(char), 80);
6630 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006631 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006632 {
6633 vim_free(ga.ga_data);
6634 return NULL;
6635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 ga_append(&ga, ']');
6637 ga_append(&ga, NUL);
6638 return (char_u *)ga.ga_data;
6639}
6640
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006641typedef struct join_S {
6642 char_u *s;
6643 char_u *tofree;
6644} join_T;
6645
6646 static int
6647list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6648 garray_T *gap; /* to store the result in */
6649 list_T *l;
6650 char_u *sep;
6651 int echo_style;
6652 int copyID;
6653 garray_T *join_gap; /* to keep each list item string */
6654{
6655 int i;
6656 join_T *p;
6657 int len;
6658 int sumlen = 0;
6659 int first = TRUE;
6660 char_u *tofree;
6661 char_u numbuf[NUMBUFLEN];
6662 listitem_T *item;
6663 char_u *s;
6664
6665 /* Stringify each item in the list. */
6666 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6667 {
6668 if (echo_style)
6669 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6670 else
6671 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6672 if (s == NULL)
6673 return FAIL;
6674
6675 len = (int)STRLEN(s);
6676 sumlen += len;
6677
6678 ga_grow(join_gap, 1);
6679 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6680 if (tofree != NULL || s != numbuf)
6681 {
6682 p->s = s;
6683 p->tofree = tofree;
6684 }
6685 else
6686 {
6687 p->s = vim_strnsave(s, len);
6688 p->tofree = p->s;
6689 }
6690
6691 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006692 if (did_echo_string_emsg) /* recursion error, bail out */
6693 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 }
6695
6696 /* Allocate result buffer with its total size, avoid re-allocation and
6697 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6698 if (join_gap->ga_len >= 2)
6699 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6700 if (ga_grow(gap, sumlen + 2) == FAIL)
6701 return FAIL;
6702
6703 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6704 {
6705 if (first)
6706 first = FALSE;
6707 else
6708 ga_concat(gap, sep);
6709 p = ((join_T *)join_gap->ga_data) + i;
6710
6711 if (p->s != NULL)
6712 ga_concat(gap, p->s);
6713 line_breakcheck();
6714 }
6715
6716 return OK;
6717}
6718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006721 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006722 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006725list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006726 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006729 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006730 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 garray_T join_ga;
6733 int retval;
6734 join_T *p;
6735 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006736
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6738 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6739
6740 /* Dispose each item in join_ga. */
6741 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006742 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006743 p = (join_T *)join_ga.ga_data;
6744 for (i = 0; i < join_ga.ga_len; ++i)
6745 {
6746 vim_free(p->tofree);
6747 ++p;
6748 }
6749 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006751
6752 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006753}
6754
6755/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 * Garbage collection for lists and dictionaries.
6757 *
6758 * We use reference counts to be able to free most items right away when they
6759 * are no longer used. But for composite items it's possible that it becomes
6760 * unused while the reference count is > 0: When there is a recursive
6761 * reference. Example:
6762 * :let l = [1, 2, 3]
6763 * :let d = {9: l}
6764 * :let l[1] = d
6765 *
6766 * Since this is quite unusual we handle this with garbage collection: every
6767 * once in a while find out which lists and dicts are not referenced from any
6768 * variable.
6769 *
6770 * Here is a good reference text about garbage collection (refers to Python
6771 * but it applies to all reference-counting mechanisms):
6772 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006773 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774
6775/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 * Do garbage collection for lists and dicts.
6777 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006778 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779 int
6780garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006781{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006782 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783 buf_T *buf;
6784 win_T *wp;
6785 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006786 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006787 int did_free;
6788 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006789#ifdef FEAT_WINDOWS
6790 tabpage_T *tp;
6791#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006793 /* Only do this once. */
6794 want_garbage_collect = FALSE;
6795 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006796 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006797
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798 /* We advance by two because we add one for items referenced through
6799 * previous_funccal. */
6800 current_copyID += COPYID_INC;
6801 copyID = current_copyID;
6802
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803 /*
6804 * 1. Go through all accessible variables and mark all lists and dicts
6805 * with copyID.
6806 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006807
6808 /* Don't free variables in the previous_funccal list unless they are only
6809 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006810 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006811 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6812 {
6813 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6814 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6815 }
6816
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817 /* script-local variables */
6818 for (i = 1; i <= ga_scripts.ga_len; ++i)
6819 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6820
6821 /* buffer-local variables */
6822 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006823 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824
6825 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006827 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006828#ifdef FEAT_AUTOCMD
6829 if (aucmd_win != NULL)
6830 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6831#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006833#ifdef FEAT_WINDOWS
6834 /* tabpage-local variables */
6835 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006836 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006837#endif
6838
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 /* global variables */
6840 set_ref_in_ht(&globvarht, copyID);
6841
6842 /* function-local variables */
6843 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6844 {
6845 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6846 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6847 }
6848
Bram Moolenaard812df62008-11-09 12:46:09 +00006849 /* v: vars */
6850 set_ref_in_ht(&vimvarht, copyID);
6851
Bram Moolenaar1dced572012-04-05 16:54:08 +02006852#ifdef FEAT_LUA
6853 set_ref_in_lua(copyID);
6854#endif
6855
Bram Moolenaardb913952012-06-29 12:54:53 +02006856#ifdef FEAT_PYTHON
6857 set_ref_in_python(copyID);
6858#endif
6859
6860#ifdef FEAT_PYTHON3
6861 set_ref_in_python3(copyID);
6862#endif
6863
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006864 /*
6865 * 2. Free lists and dictionaries that are not referenced.
6866 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 did_free = free_unref_items(copyID);
6868
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006869 /*
6870 * 3. Check if any funccal can be freed now.
6871 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 for (pfc = &previous_funccal; *pfc != NULL; )
6873 {
6874 if (can_free_funccal(*pfc, copyID))
6875 {
6876 fc = *pfc;
6877 *pfc = fc->caller;
6878 free_funccal(fc, TRUE);
6879 did_free = TRUE;
6880 did_free_funccal = TRUE;
6881 }
6882 else
6883 pfc = &(*pfc)->caller;
6884 }
6885 if (did_free_funccal)
6886 /* When a funccal was freed some more items might be garbage
6887 * collected, so run again. */
6888 (void)garbage_collect();
6889
6890 return did_free;
6891}
6892
6893/*
6894 * Free lists and dictionaries that are no longer referenced.
6895 */
6896 static int
6897free_unref_items(copyID)
6898 int copyID;
6899{
6900 dict_T *dd;
6901 list_T *ll;
6902 int did_free = FALSE;
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 */
6907 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006908 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006910 /* Free the Dictionary and ordinary items it contains, but don't
6911 * recurse into Lists and Dictionaries, they will be in the list
6912 * of dicts or list of lists. */
6913 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914 did_free = TRUE;
6915
6916 /* restart, next dict may also have been freed */
6917 dd = first_dict;
6918 }
6919 else
6920 dd = dd->dv_used_next;
6921
6922 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006923 * Go through the list of lists and free items without the copyID.
6924 * But don't free a list that has a watcher (used in a for loop), these
6925 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 */
6927 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6929 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006931 /* Free the List and ordinary items it contains, but don't recurse
6932 * into Lists and Dictionaries, they will be in the list of dicts
6933 * or list of lists. */
6934 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 did_free = TRUE;
6936
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006937 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 ll = first_list;
6939 }
6940 else
6941 ll = ll->lv_used_next;
6942
6943 return did_free;
6944}
6945
6946/*
6947 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6948 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006949 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950set_ref_in_ht(ht, copyID)
6951 hashtab_T *ht;
6952 int copyID;
6953{
6954 int todo;
6955 hashitem_T *hi;
6956
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006957 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 for (hi = ht->ht_array; todo > 0; ++hi)
6959 if (!HASHITEM_EMPTY(hi))
6960 {
6961 --todo;
6962 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6963 }
6964}
6965
6966/*
6967 * Mark all lists and dicts referenced through list "l" with "copyID".
6968 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006969 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970set_ref_in_list(l, copyID)
6971 list_T *l;
6972 int copyID;
6973{
6974 listitem_T *li;
6975
6976 for (li = l->lv_first; li != NULL; li = li->li_next)
6977 set_ref_in_item(&li->li_tv, copyID);
6978}
6979
6980/*
6981 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6982 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006983 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984set_ref_in_item(tv, copyID)
6985 typval_T *tv;
6986 int copyID;
6987{
6988 dict_T *dd;
6989 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006990
6991 switch (tv->v_type)
6992 {
6993 case VAR_DICT:
6994 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006995 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006996 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 /* Didn't see this dict yet. */
6998 dd->dv_copyID = copyID;
6999 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007000 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007002
7003 case VAR_LIST:
7004 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007005 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007006 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 /* Didn't see this list yet. */
7008 ll->lv_copyID = copyID;
7009 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007010 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007014}
7015
7016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 * Allocate an empty header for a dictionary.
7018 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007019 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020dict_alloc()
7021{
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007025 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007027 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 if (first_dict != NULL)
7029 first_dict->dv_used_prev = d;
7030 d->dv_used_next = first_dict;
7031 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007035 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007036 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007037 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007038 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007039 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041}
7042
7043/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007044 * Allocate an empty dict for a return value.
7045 * Returns OK or FAIL.
7046 */
7047 static int
7048rettv_dict_alloc(rettv)
7049 typval_T *rettv;
7050{
7051 dict_T *d = dict_alloc();
7052
7053 if (d == NULL)
7054 return FAIL;
7055
7056 rettv->vval.v_dict = d;
7057 rettv->v_type = VAR_DICT;
7058 ++d->dv_refcount;
7059 return OK;
7060}
7061
7062
7063/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 * Unreference a Dictionary: decrement the reference count and free it when it
7065 * becomes zero.
7066 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007067 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007071 if (d != NULL && --d->dv_refcount <= 0)
7072 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073}
7074
7075/*
7076 * Free a Dictionary, including all items it contains.
7077 * Ignores the reference count.
7078 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007079 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007080dict_free(d, recurse)
7081 dict_T *d;
7082 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007086 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 /* Remove the dict from the list of dicts for garbage collection. */
7089 if (d->dv_used_prev == NULL)
7090 first_dict = d->dv_used_next;
7091 else
7092 d->dv_used_prev->dv_used_next = d->dv_used_next;
7093 if (d->dv_used_next != NULL)
7094 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7095
7096 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007097 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007098 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 if (!HASHITEM_EMPTY(hi))
7102 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007103 /* Remove the item before deleting it, just in case there is
7104 * something recursive causing trouble. */
7105 di = HI2DI(hi);
7106 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007107 if (recurse || (di->di_tv.v_type != VAR_LIST
7108 && di->di_tv.v_type != VAR_DICT))
7109 clear_tv(&di->di_tv);
7110 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 --todo;
7112 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 vim_free(d);
7116}
7117
7118/*
7119 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 * The "key" is copied to the new item.
7121 * Note that the value of the item "di_tv" still needs to be initialized!
7122 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007124 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125dictitem_alloc(key)
7126 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127{
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007130 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 di->di_flags = 0;
7135 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137}
7138
7139/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 * Make a copy of a Dictionary item.
7141 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145{
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007148 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7149 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 if (di != NULL)
7151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007152 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007153 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 copy_tv(&org->di_tv, &di->di_tv);
7155 }
7156 return di;
7157}
7158
7159/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160 * Remove item "item" from Dictionary "dict" and free it.
7161 */
7162 static void
7163dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 dict_T *dict;
7165 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007166{
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 if (HASHITEM_EMPTY(hi))
7171 EMSG2(_(e_intern2), "dictitem_remove()");
7172 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 dictitem_free(item);
7175}
7176
7177/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178 * Free a dict item. Also clears the value.
7179 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007180 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 clear_tv(&item->di_tv);
7185 vim_free(item);
7186}
7187
7188/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7190 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 * Returns NULL when out of memory.
7193 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199{
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 dict_T *copy;
7201 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204
7205 if (orig == NULL)
7206 return NULL;
7207
7208 copy = dict_alloc();
7209 if (copy != NULL)
7210 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 if (copyID != 0)
7212 {
7213 orig->dv_copyID = copyID;
7214 orig->dv_copydict = copy;
7215 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007216 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 --todo;
7222
7223 di = dictitem_alloc(hi->hi_key);
7224 if (di == NULL)
7225 break;
7226 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 {
7228 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7229 copyID) == FAIL)
7230 {
7231 vim_free(di);
7232 break;
7233 }
7234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 else
7236 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7237 if (dict_add(copy, di) == FAIL)
7238 {
7239 dictitem_free(di);
7240 break;
7241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007243 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244
Bram Moolenaare9a41262005-01-15 22:18:47 +00007245 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 if (todo > 0)
7247 {
7248 dict_unref(copy);
7249 copy = NULL;
7250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007251 }
7252
7253 return copy;
7254}
7255
7256/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007258 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007260 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 dict_T *d;
7263 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264{
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266}
7267
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007269 * Add a number or string entry to dictionary "d".
7270 * When "str" is NULL use number "nr", otherwise use "str".
7271 * Returns FAIL when out of memory and when key already exists.
7272 */
7273 int
7274dict_add_nr_str(d, key, nr, str)
7275 dict_T *d;
7276 char *key;
7277 long nr;
7278 char_u *str;
7279{
7280 dictitem_T *item;
7281
7282 item = dictitem_alloc((char_u *)key);
7283 if (item == NULL)
7284 return FAIL;
7285 item->di_tv.v_lock = 0;
7286 if (str == NULL)
7287 {
7288 item->di_tv.v_type = VAR_NUMBER;
7289 item->di_tv.vval.v_number = nr;
7290 }
7291 else
7292 {
7293 item->di_tv.v_type = VAR_STRING;
7294 item->di_tv.vval.v_string = vim_strsave(str);
7295 }
7296 if (dict_add(d, item) == FAIL)
7297 {
7298 dictitem_free(item);
7299 return FAIL;
7300 }
7301 return OK;
7302}
7303
7304/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007305 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007306 * Returns FAIL when out of memory and when key already exists.
7307 */
7308 int
7309dict_add_list(d, key, list)
7310 dict_T *d;
7311 char *key;
7312 list_T *list;
7313{
7314 dictitem_T *item;
7315
7316 item = dictitem_alloc((char_u *)key);
7317 if (item == NULL)
7318 return FAIL;
7319 item->di_tv.v_lock = 0;
7320 item->di_tv.v_type = VAR_LIST;
7321 item->di_tv.vval.v_list = list;
7322 if (dict_add(d, item) == FAIL)
7323 {
7324 dictitem_free(item);
7325 return FAIL;
7326 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007327 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007328 return OK;
7329}
7330
7331/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 * Get the number of items in a Dictionary.
7333 */
7334 static long
7335dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 if (d == NULL)
7339 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007340 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341}
7342
7343/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 * Find item "key[len]" in Dictionary "d".
7345 * If "len" is negative use strlen(key).
7346 * Returns NULL when not found.
7347 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007348 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 char_u *key;
7352 int len;
7353{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354#define AKEYLEN 200
7355 char_u buf[AKEYLEN];
7356 char_u *akey;
7357 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 if (len < 0)
7361 akey = key;
7362 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 tofree = akey = vim_strnsave(key, len);
7365 if (akey == NULL)
7366 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007367 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 else
7369 {
7370 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007371 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 akey = buf;
7373 }
7374
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 vim_free(tofree);
7377 if (HASHITEM_EMPTY(hi))
7378 return NULL;
7379 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380}
7381
7382/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007383 * Get a string item from a dictionary.
7384 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007385 * Returns NULL if the entry doesn't exist or out of memory.
7386 */
7387 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007388get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007389 dict_T *d;
7390 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007391 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007392{
7393 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007394 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007395
7396 di = dict_find(d, key, -1);
7397 if (di == NULL)
7398 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007399 s = get_tv_string(&di->di_tv);
7400 if (save && s != NULL)
7401 s = vim_strsave(s);
7402 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007403}
7404
7405/*
7406 * Get a number item from a dictionary.
7407 * Returns 0 if the entry doesn't exist or out of memory.
7408 */
7409 long
7410get_dict_number(d, key)
7411 dict_T *d;
7412 char_u *key;
7413{
7414 dictitem_T *di;
7415
7416 di = dict_find(d, key, -1);
7417 if (di == NULL)
7418 return 0;
7419 return get_tv_number(&di->di_tv);
7420}
7421
7422/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 * Return an allocated string with the string representation of a Dictionary.
7424 * May return NULL.
7425 */
7426 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007427dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430{
7431 garray_T ga;
7432 int first = TRUE;
7433 char_u *tofree;
7434 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 return NULL;
7442 ga_init2(&ga, (int)sizeof(char), 80);
7443 ga_append(&ga, '{');
7444
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007445 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007446 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 --todo;
7451
7452 if (first)
7453 first = FALSE;
7454 else
7455 ga_concat(&ga, (char_u *)", ");
7456
7457 tofree = string_quote(hi->hi_key, FALSE);
7458 if (tofree != NULL)
7459 {
7460 ga_concat(&ga, tofree);
7461 vim_free(tofree);
7462 }
7463 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007464 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007465 if (s != NULL)
7466 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007468 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007469 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007470 line_breakcheck();
7471
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007474 if (todo > 0)
7475 {
7476 vim_free(ga.ga_data);
7477 return NULL;
7478 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479
7480 ga_append(&ga, '}');
7481 ga_append(&ga, NUL);
7482 return (char_u *)ga.ga_data;
7483}
7484
7485/*
7486 * Allocate a variable for a Dictionary and fill it from "*arg".
7487 * Return OK or FAIL. Returns NOTDONE for {expr}.
7488 */
7489 static int
7490get_dict_tv(arg, rettv, evaluate)
7491 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 int evaluate;
7494{
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 dict_T *d = NULL;
7496 typval_T tvkey;
7497 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007498 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502
7503 /*
7504 * First check if it's not a curly-braces thing: {expr}.
7505 * Must do this without evaluating, otherwise a function may be called
7506 * twice. Unfortunately this means we need to call eval1() twice for the
7507 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 if (*start != '}')
7511 {
7512 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7513 return FAIL;
7514 if (*start == '}')
7515 return NOTDONE;
7516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517
7518 if (evaluate)
7519 {
7520 d = dict_alloc();
7521 if (d == NULL)
7522 return FAIL;
7523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 tvkey.v_type = VAR_UNKNOWN;
7525 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526
7527 *arg = skipwhite(*arg + 1);
7528 while (**arg != '}' && **arg != NUL)
7529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 goto failret;
7532 if (**arg != ':')
7533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007534 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 goto failret;
7537 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007538 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007540 key = get_tv_string_buf_chk(&tvkey, buf);
7541 if (key == NULL || *key == NUL)
7542 {
7543 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7544 if (key != NULL)
7545 EMSG(_(e_emptykey));
7546 clear_tv(&tvkey);
7547 goto failret;
7548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550
7551 *arg = skipwhite(*arg + 1);
7552 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7553 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007554 if (evaluate)
7555 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 goto failret;
7557 }
7558 if (evaluate)
7559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 if (item != NULL)
7562 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007563 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 clear_tv(&tv);
7566 goto failret;
7567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568 item = dictitem_alloc(key);
7569 clear_tv(&tvkey);
7570 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007573 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if (dict_add(d, item) == FAIL)
7575 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 }
7577 }
7578
7579 if (**arg == '}')
7580 break;
7581 if (**arg != ',')
7582 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007583 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 goto failret;
7585 }
7586 *arg = skipwhite(*arg + 1);
7587 }
7588
7589 if (**arg != '}')
7590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007591 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592failret:
7593 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007594 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 return FAIL;
7596 }
7597
7598 *arg = skipwhite(*arg + 1);
7599 if (evaluate)
7600 {
7601 rettv->v_type = VAR_DICT;
7602 rettv->vval.v_dict = d;
7603 ++d->dv_refcount;
7604 }
7605
7606 return OK;
7607}
7608
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007610 * Return a string with the string representation of a variable.
7611 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007612 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007613 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007614 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007615 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 */
7617 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007621 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 static int recurse = 0;
7625 char_u *r = NULL;
7626
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007629 if (!did_echo_string_emsg)
7630 {
7631 /* Only give this message once for a recursive call to avoid
7632 * flooding the user with errors. And stop iterating over lists
7633 * and dicts. */
7634 did_echo_string_emsg = TRUE;
7635 EMSG(_("E724: variable nested too deep for displaying"));
7636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007638 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 }
7640 ++recurse;
7641
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 switch (tv->v_type)
7643 {
7644 case VAR_FUNC:
7645 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 r = tv->vval.v_string;
7647 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650 if (tv->vval.v_list == NULL)
7651 {
7652 *tofree = NULL;
7653 r = NULL;
7654 }
7655 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7656 {
7657 *tofree = NULL;
7658 r = (char_u *)"[...]";
7659 }
7660 else
7661 {
7662 tv->vval.v_list->lv_copyID = copyID;
7663 *tofree = list2string(tv, copyID);
7664 r = *tofree;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007667
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669 if (tv->vval.v_dict == NULL)
7670 {
7671 *tofree = NULL;
7672 r = NULL;
7673 }
7674 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7675 {
7676 *tofree = NULL;
7677 r = (char_u *)"{...}";
7678 }
7679 else
7680 {
7681 tv->vval.v_dict->dv_copyID = copyID;
7682 *tofree = dict2string(tv, copyID);
7683 r = *tofree;
7684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 case VAR_STRING:
7688 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 *tofree = NULL;
7690 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007691 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007692
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007693#ifdef FEAT_FLOAT
7694 case VAR_FLOAT:
7695 *tofree = NULL;
7696 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7697 r = numbuf;
7698 break;
7699#endif
7700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007703 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705
Bram Moolenaar8502c702014-06-17 12:51:16 +02007706 if (--recurse == 0)
7707 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007708 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007709}
7710
7711/*
7712 * Return a string with the string representation of a variable.
7713 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7714 * "numbuf" is used for a number.
7715 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007716 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 */
7718 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007719tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007720 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 char_u **tofree;
7722 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007723 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724{
7725 switch (tv->v_type)
7726 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 case VAR_FUNC:
7728 *tofree = string_quote(tv->vval.v_string, TRUE);
7729 return *tofree;
7730 case VAR_STRING:
7731 *tofree = string_quote(tv->vval.v_string, FALSE);
7732 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007733#ifdef FEAT_FLOAT
7734 case VAR_FLOAT:
7735 *tofree = NULL;
7736 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7737 return numbuf;
7738#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007740 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007746 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747}
7748
7749/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 * Return string "str" in ' quotes, doubling ' characters.
7751 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 */
7754 static char_u *
7755string_quote(str, function)
7756 char_u *str;
7757 int function;
7758{
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007760 char_u *p, *r, *s;
7761
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 len = (function ? 13 : 3);
7763 if (str != NULL)
7764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007765 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 for (p = str; *p != NUL; mb_ptr_adv(p))
7767 if (*p == '\'')
7768 ++len;
7769 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 s = r = alloc(len);
7771 if (r != NULL)
7772 {
7773 if (function)
7774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007776 r += 10;
7777 }
7778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 if (str != NULL)
7781 for (p = str; *p != NUL; )
7782 {
7783 if (*p == '\'')
7784 *r++ = '\'';
7785 MB_COPY_CHAR(p, r);
7786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007788 if (function)
7789 *r++ = ')';
7790 *r++ = NUL;
7791 }
7792 return s;
7793}
7794
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795#ifdef FEAT_FLOAT
7796/*
7797 * Convert the string "text" to a floating point number.
7798 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7799 * this always uses a decimal point.
7800 * Returns the length of the text that was consumed.
7801 */
7802 static int
7803string2float(text, value)
7804 char_u *text;
7805 float_T *value; /* result stored here */
7806{
7807 char *s = (char *)text;
7808 float_T f;
7809
7810 f = strtod(s, &s);
7811 *value = f;
7812 return (int)((char_u *)s - text);
7813}
7814#endif
7815
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 * Get the value of an environment variable.
7818 * "arg" is pointing to the '$'. It is advanced to after the name.
7819 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007820 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 */
7822 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 int evaluate;
7827{
7828 char_u *string = NULL;
7829 int len;
7830 int cc;
7831 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007832 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833
7834 ++*arg;
7835 name = *arg;
7836 len = get_env_len(arg);
7837 if (evaluate)
7838 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007839 if (len == 0)
7840 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007841
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007842 cc = name[len];
7843 name[len] = NUL;
7844 /* first try vim_getenv(), fast for normal environment vars */
7845 string = vim_getenv(name, &mustfree);
7846 if (string != NULL && *string != NUL)
7847 {
7848 if (!mustfree)
7849 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007851 else
7852 {
7853 if (mustfree)
7854 vim_free(string);
7855
7856 /* next try expanding things like $VIM and ${HOME} */
7857 string = expand_env_save(name - 1);
7858 if (string != NULL && *string == '$')
7859 {
7860 vim_free(string);
7861 string = NULL;
7862 }
7863 }
7864 name[len] = cc;
7865
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->v_type = VAR_STRING;
7867 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869
7870 return OK;
7871}
7872
7873/*
7874 * Array with names and number of arguments of all internal functions
7875 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7876 */
7877static struct fst
7878{
7879 char *f_name; /* function name */
7880 char f_min_argc; /* minimal number of arguments */
7881 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007883 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884} functions[] =
7885{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007890 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007891 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"append", 2, 2, f_append},
7893 {"argc", 0, 0, f_argc},
7894 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007895 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007896 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007898 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007900 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007903 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"bufexists", 1, 1, f_bufexists},
7905 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7906 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7907 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7908 {"buflisted", 1, 1, f_buflisted},
7909 {"bufloaded", 1, 1, f_bufloaded},
7910 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007911 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"bufwinnr", 1, 1, f_bufwinnr},
7913 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007914 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007915 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#ifdef FEAT_FLOAT
7918 {"ceil", 1, 1, f_ceil},
7919#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007920 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007921 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007923 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007925#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007926 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007927 {"complete_add", 1, 1, f_complete_add},
7928 {"complete_check", 0, 0, f_complete_check},
7929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932#ifdef FEAT_FLOAT
7933 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007938 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007939 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"delete", 1, 1, f_delete},
7941 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007942 {"diff_filler", 1, 1, f_diff_filler},
7943 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007944 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {"eventhandler", 0, 0, f_eventhandler},
7948 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007949 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007951#ifdef FEAT_FLOAT
7952 {"exp", 1, 1, f_exp},
7953#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007954 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007955 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007956 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7958 {"filereadable", 1, 1, f_filereadable},
7959 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007961 {"finddir", 1, 3, f_finddir},
7962 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963#ifdef FEAT_FLOAT
7964 {"float2nr", 1, 1, f_float2nr},
7965 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007966 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007968 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"fnamemodify", 2, 2, f_fnamemodify},
7970 {"foldclosed", 1, 1, f_foldclosed},
7971 {"foldclosedend", 1, 1, f_foldclosedend},
7972 {"foldlevel", 1, 1, f_foldlevel},
7973 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007974 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007977 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007978 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007979 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007980 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getchar", 0, 1, f_getchar},
7982 {"getcharmod", 0, 0, f_getcharmod},
7983 {"getcmdline", 0, 0, f_getcmdline},
7984 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007985 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02007986 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007988 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007989 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"getfsize", 1, 1, f_getfsize},
7991 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007992 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007993 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007994 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007995 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007996 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007997 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007998 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007999 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008001 {"gettabvar", 2, 3, f_gettabvar},
8002 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"getwinposx", 0, 0, f_getwinposx},
8004 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008005 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008006 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008007 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008010 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008011 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8013 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8014 {"histadd", 2, 2, f_histadd},
8015 {"histdel", 1, 2, f_histdel},
8016 {"histget", 1, 2, f_histget},
8017 {"histnr", 1, 1, f_histnr},
8018 {"hlID", 1, 1, f_hlID},
8019 {"hlexists", 1, 1, f_hlexists},
8020 {"hostname", 0, 0, f_hostname},
8021 {"iconv", 3, 3, f_iconv},
8022 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008023 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008024 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008026 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {"inputrestore", 0, 0, f_inputrestore},
8028 {"inputsave", 0, 0, f_inputsave},
8029 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008031 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008033 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008034 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008035 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008038 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"libcall", 3, 3, f_libcall},
8040 {"libcallnr", 3, 3, f_libcallnr},
8041 {"line", 1, 1, f_line},
8042 {"line2byte", 1, 1, f_line2byte},
8043 {"lispindent", 1, 1, f_lispindent},
8044 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008046 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047 {"log10", 1, 1, f_log10},
8048#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008049#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008050 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008051#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008053 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008054 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008055 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008056 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008057 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008058 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008059 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008060 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008061 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008062 {"max", 1, 1, f_max},
8063 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008064#ifdef vim_mkdir
8065 {"mkdir", 1, 3, f_mkdir},
8066#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008068#ifdef FEAT_MZSCHEME
8069 {"mzeval", 1, 1, f_mzeval},
8070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008072 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008073 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008074 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"pow", 2, 2, f_pow},
8077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008079 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008080 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008081#ifdef FEAT_PYTHON3
8082 {"py3eval", 1, 1, f_py3eval},
8083#endif
8084#ifdef FEAT_PYTHON
8085 {"pyeval", 1, 1, f_pyeval},
8086#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008087 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008088 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008089 {"reltime", 0, 2, f_reltime},
8090 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"remote_expr", 2, 3, f_remote_expr},
8092 {"remote_foreground", 1, 1, f_remote_foreground},
8093 {"remote_peek", 1, 2, f_remote_peek},
8094 {"remote_read", 1, 1, f_remote_read},
8095 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008096 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008098 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008100 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"round", 1, 1, f_round},
8103#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008104 {"screenattr", 2, 2, f_screenattr},
8105 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008106 {"screencol", 0, 0, f_screencol},
8107 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008108 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008109 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008110 {"searchpair", 3, 7, f_searchpair},
8111 {"searchpairpos", 3, 7, f_searchpairpos},
8112 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"server2client", 2, 2, f_server2client},
8114 {"serverlist", 0, 0, f_serverlist},
8115 {"setbufvar", 3, 3, f_setbufvar},
8116 {"setcmdpos", 1, 1, f_setcmdpos},
8117 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008118 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008119 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008120 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008121 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008123 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008124 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008126#ifdef FEAT_CRYPT
8127 {"sha256", 1, 1, f_sha256},
8128#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008129 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008130 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#ifdef FEAT_FLOAT
8133 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008134 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008136 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008137 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008138 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008139 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008140 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#ifdef FEAT_FLOAT
8142 {"sqrt", 1, 1, f_sqrt},
8143 {"str2float", 1, 1, f_str2float},
8144#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008145 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008146 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008147 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#ifdef HAVE_STRFTIME
8149 {"strftime", 1, 2, f_strftime},
8150#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"strlen", 1, 1, f_strlen},
8154 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008155 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008157 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008158 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"substitute", 4, 4, f_substitute},
8160 {"synID", 3, 3, f_synID},
8161 {"synIDattr", 2, 3, f_synIDattr},
8162 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008163 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008164 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008165 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008166 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008167 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008168 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008169 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008170 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008171 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008172#ifdef FEAT_FLOAT
8173 {"tan", 1, 1, f_tan},
8174 {"tanh", 1, 1, f_tanh},
8175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008177 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"tolower", 1, 1, f_tolower},
8179 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008180 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181#ifdef FEAT_FLOAT
8182 {"trunc", 1, 1, f_trunc},
8183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008185 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008186 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008187 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008188 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"virtcol", 1, 1, f_virtcol},
8190 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008191 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"winbufnr", 1, 1, f_winbufnr},
8193 {"wincol", 0, 0, f_wincol},
8194 {"winheight", 1, 1, f_winheight},
8195 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008196 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008198 {"winrestview", 1, 1, f_winrestview},
8199 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008201 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008202 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203};
8204
8205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8206
8207/*
8208 * Function given to ExpandGeneric() to obtain the list of internal
8209 * or user defined function names.
8210 */
8211 char_u *
8212get_function_name(xp, idx)
8213 expand_T *xp;
8214 int idx;
8215{
8216 static int intidx = -1;
8217 char_u *name;
8218
8219 if (idx == 0)
8220 intidx = -1;
8221 if (intidx < 0)
8222 {
8223 name = get_user_func_name(xp, idx);
8224 if (name != NULL)
8225 return name;
8226 }
8227 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8228 {
8229 STRCPY(IObuff, functions[intidx].f_name);
8230 STRCAT(IObuff, "(");
8231 if (functions[intidx].f_max_argc == 0)
8232 STRCAT(IObuff, ")");
8233 return IObuff;
8234 }
8235
8236 return NULL;
8237}
8238
8239/*
8240 * Function given to ExpandGeneric() to obtain the list of internal or
8241 * user defined variable or function names.
8242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 char_u *
8244get_expr_name(xp, idx)
8245 expand_T *xp;
8246 int idx;
8247{
8248 static int intidx = -1;
8249 char_u *name;
8250
8251 if (idx == 0)
8252 intidx = -1;
8253 if (intidx < 0)
8254 {
8255 name = get_function_name(xp, idx);
8256 if (name != NULL)
8257 return name;
8258 }
8259 return get_user_var_name(xp, ++intidx);
8260}
8261
8262#endif /* FEAT_CMDL_COMPL */
8263
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008264#if defined(EBCDIC) || defined(PROTO)
8265/*
8266 * Compare struct fst by function name.
8267 */
8268 static int
8269compare_func_name(s1, s2)
8270 const void *s1;
8271 const void *s2;
8272{
8273 struct fst *p1 = (struct fst *)s1;
8274 struct fst *p2 = (struct fst *)s2;
8275
8276 return STRCMP(p1->f_name, p2->f_name);
8277}
8278
8279/*
8280 * Sort the function table by function name.
8281 * The sorting of the table above is ASCII dependant.
8282 * On machines using EBCDIC we have to sort it.
8283 */
8284 static void
8285sortFunctions()
8286{
8287 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8288
8289 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8290}
8291#endif
8292
8293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294/*
8295 * Find internal function in table above.
8296 * Return index, or -1 if not found
8297 */
8298 static int
8299find_internal_func(name)
8300 char_u *name; /* name of the function */
8301{
8302 int first = 0;
8303 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8304 int cmp;
8305 int x;
8306
8307 /*
8308 * Find the function name in the table. Binary search.
8309 */
8310 while (first <= last)
8311 {
8312 x = first + ((unsigned)(last - first) >> 1);
8313 cmp = STRCMP(name, functions[x].f_name);
8314 if (cmp < 0)
8315 last = x - 1;
8316 else if (cmp > 0)
8317 first = x + 1;
8318 else
8319 return x;
8320 }
8321 return -1;
8322}
8323
8324/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8326 * name it contains, otherwise return "name".
8327 */
8328 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008329deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008330 char_u *name;
8331 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008332 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333{
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335 int cc;
8336
8337 cc = name[*lenp];
8338 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008339 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 {
8345 *lenp = 0;
8346 return (char_u *)""; /* just in case */
8347 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008348 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008350 }
8351
8352 return name;
8353}
8354
8355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 * Allocate a variable for the result of a function.
8357 * Return OK or FAIL.
8358 */
8359 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8361 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 char_u *name; /* name of the function */
8363 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 char_u **arg; /* argument, pointing to the '(' */
8366 linenr_T firstline; /* first line of range */
8367 linenr_T lastline; /* last line of range */
8368 int *doesrange; /* return: function handled range */
8369 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008370 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 char_u *argp;
8373 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008374 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 int argcount = 0; /* number of arguments found */
8376
8377 /*
8378 * Get the arguments.
8379 */
8380 argp = *arg;
8381 while (argcount < MAX_FUNC_ARGS)
8382 {
8383 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8384 if (*argp == ')' || *argp == ',' || *argp == NUL)
8385 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8387 {
8388 ret = FAIL;
8389 break;
8390 }
8391 ++argcount;
8392 if (*argp != ',')
8393 break;
8394 }
8395 if (*argp == ')')
8396 ++argp;
8397 else
8398 ret = FAIL;
8399
8400 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008402 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008404 {
8405 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008406 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008408 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 *arg = skipwhite(argp);
8415 return ret;
8416}
8417
8418
8419/*
8420 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008421 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008422 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 */
8424 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008425call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008426 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008427 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008431 typval_T *argvars; /* vars for arguments, must have "argcount"
8432 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 linenr_T firstline; /* first line of range */
8434 linenr_T lastline; /* last line of range */
8435 int *doesrange; /* return: function handled range */
8436 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#define ERROR_UNKNOWN 0
8441#define ERROR_TOOMANY 1
8442#define ERROR_TOOFEW 2
8443#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444#define ERROR_DICT 4
8445#define ERROR_NONE 5
8446#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 int error = ERROR_NONE;
8448 int i;
8449 int llen;
8450 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451#define FLEN_FIXED 40
8452 char_u fname_buf[FLEN_FIXED + 1];
8453 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008454 char_u *name;
8455
8456 /* Make a copy of the name, if it comes from a funcref variable it could
8457 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008458 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008459 if (name == NULL)
8460 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
8462 /*
8463 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8464 * Change <SNR>123_name() to K_SNR 123_name().
8465 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 llen = eval_fname_script(name);
8468 if (llen > 0)
8469 {
8470 fname_buf[0] = K_SPECIAL;
8471 fname_buf[1] = KS_EXTRA;
8472 fname_buf[2] = (int)KE_SNR;
8473 i = 3;
8474 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8475 {
8476 if (current_SID <= 0)
8477 error = ERROR_SCRIPT;
8478 else
8479 {
8480 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8481 i = (int)STRLEN(fname_buf);
8482 }
8483 }
8484 if (i + STRLEN(name + llen) < FLEN_FIXED)
8485 {
8486 STRCPY(fname_buf + i, name + llen);
8487 fname = fname_buf;
8488 }
8489 else
8490 {
8491 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8492 if (fname == NULL)
8493 error = ERROR_OTHER;
8494 else
8495 {
8496 mch_memmove(fname, fname_buf, (size_t)i);
8497 STRCPY(fname + i, name + llen);
8498 }
8499 }
8500 }
8501 else
8502 fname = name;
8503
8504 *doesrange = FALSE;
8505
8506
8507 /* execute the function if no errors detected and executing */
8508 if (evaluate && error == ERROR_NONE)
8509 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008510 char_u *rfname = fname;
8511
8512 /* Ignore "g:" before a function name. */
8513 if (fname[0] == 'g' && fname[1] == ':')
8514 rfname = fname + 2;
8515
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008516 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8517 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 error = ERROR_UNKNOWN;
8519
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008520 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
8522 /*
8523 * User defined function.
8524 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008525 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008528 /* Trigger FuncUndefined event, may load the function. */
8529 if (fp == NULL
8530 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008531 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008532 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008534 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008535 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 }
8537#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008538 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008539 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008540 {
8541 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008542 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008543 }
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (fp != NULL)
8546 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008547 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008549 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008551 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008553 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 else
8556 {
8557 /*
8558 * Call the user function.
8559 * Save and restore search patterns, script variables and
8560 * redo buffer.
8561 */
8562 save_search_patterns();
8563 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008564 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008566 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008567 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8568 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8569 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008570 /* Function was unreferenced while being used, free it
8571 * now. */
8572 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 restoreRedobuff();
8574 restore_search_patterns();
8575 error = ERROR_NONE;
8576 }
8577 }
8578 }
8579 else
8580 {
8581 /*
8582 * Find the function name in the table, call its implementation.
8583 */
8584 i = find_internal_func(fname);
8585 if (i >= 0)
8586 {
8587 if (argcount < functions[i].f_min_argc)
8588 error = ERROR_TOOFEW;
8589 else if (argcount > functions[i].f_max_argc)
8590 error = ERROR_TOOMANY;
8591 else
8592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 error = ERROR_NONE;
8596 }
8597 }
8598 }
8599 /*
8600 * The function call (or "FuncUndefined" autocommand sequence) might
8601 * have been aborted by an error, an interrupt, or an explicitly thrown
8602 * exception that has not been caught so far. This situation can be
8603 * tested for by calling aborting(). For an error in an internal
8604 * function or for the "E132" error in call_user_func(), however, the
8605 * throw point at which the "force_abort" flag (temporarily reset by
8606 * emsg()) is normally updated has not been reached yet. We need to
8607 * update that flag first to make aborting() reliable.
8608 */
8609 update_force_abort();
8610 }
8611 if (error == ERROR_NONE)
8612 ret = OK;
8613
8614 /*
8615 * Report an error unless the argument evaluation or function call has been
8616 * cancelled due to an aborting error, an interrupt, or an exception.
8617 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008618 if (!aborting())
8619 {
8620 switch (error)
8621 {
8622 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008623 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008624 break;
8625 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008626 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008627 break;
8628 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008629 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008630 name);
8631 break;
8632 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008633 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008634 name);
8635 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008636 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008637 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008638 name);
8639 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008640 }
8641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (fname != name && fname != fname_buf)
8644 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008645 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
8647 return ret;
8648}
8649
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008650/*
8651 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008652 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008653 */
8654 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008655emsg_funcname(ermsg, name)
8656 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 char_u *name;
8658{
8659 char_u *p;
8660
8661 if (*name == K_SPECIAL)
8662 p = concat_str((char_u *)"<SNR>", name + 3);
8663 else
8664 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008665 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 if (p != name)
8667 vim_free(p);
8668}
8669
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008670/*
8671 * Return TRUE for a non-zero Number and a non-empty String.
8672 */
8673 static int
8674non_zero_arg(argvars)
8675 typval_T *argvars;
8676{
8677 return ((argvars[0].v_type == VAR_NUMBER
8678 && argvars[0].vval.v_number != 0)
8679 || (argvars[0].v_type == VAR_STRING
8680 && argvars[0].vval.v_string != NULL
8681 && *argvars[0].vval.v_string != NUL));
8682}
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684/*********************************************
8685 * Implementation of the built-in functions
8686 */
8687
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008689static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8690
8691/*
8692 * Get the float value of "argvars[0]" into "f".
8693 * Returns FAIL when the argument is not a Number or Float.
8694 */
8695 static int
8696get_float_arg(argvars, f)
8697 typval_T *argvars;
8698 float_T *f;
8699{
8700 if (argvars[0].v_type == VAR_FLOAT)
8701 {
8702 *f = argvars[0].vval.v_float;
8703 return OK;
8704 }
8705 if (argvars[0].v_type == VAR_NUMBER)
8706 {
8707 *f = (float_T)argvars[0].vval.v_number;
8708 return OK;
8709 }
8710 EMSG(_("E808: Number or Float required"));
8711 return FAIL;
8712}
8713
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008714/*
8715 * "abs(expr)" function
8716 */
8717 static void
8718f_abs(argvars, rettv)
8719 typval_T *argvars;
8720 typval_T *rettv;
8721{
8722 if (argvars[0].v_type == VAR_FLOAT)
8723 {
8724 rettv->v_type = VAR_FLOAT;
8725 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8726 }
8727 else
8728 {
8729 varnumber_T n;
8730 int error = FALSE;
8731
8732 n = get_tv_number_chk(&argvars[0], &error);
8733 if (error)
8734 rettv->vval.v_number = -1;
8735 else if (n > 0)
8736 rettv->vval.v_number = n;
8737 else
8738 rettv->vval.v_number = -n;
8739 }
8740}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008741
8742/*
8743 * "acos()" function
8744 */
8745 static void
8746f_acos(argvars, rettv)
8747 typval_T *argvars;
8748 typval_T *rettv;
8749{
8750 float_T f;
8751
8752 rettv->v_type = VAR_FLOAT;
8753 if (get_float_arg(argvars, &f) == OK)
8754 rettv->vval.v_float = acos(f);
8755 else
8756 rettv->vval.v_float = 0.0;
8757}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008758#endif
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 */
8763 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008764f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *argvars;
8766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008771 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008773 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008774 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008775 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008777 }
8778 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008779 EMSG(_(e_listreq));
8780}
8781
8782/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008783 * "and(expr, expr)" function
8784 */
8785 static void
8786f_and(argvars, rettv)
8787 typval_T *argvars;
8788 typval_T *rettv;
8789{
8790 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8791 & get_tv_number_chk(&argvars[1], NULL);
8792}
8793
8794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008795 * "append(lnum, string/list)" function
8796 */
8797 static void
8798f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *argvars;
8800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801{
8802 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008803 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 list_T *l = NULL;
8805 listitem_T *li = NULL;
8806 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008807 long added = 0;
8808
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008809 /* When coming here from Insert mode, sync undo, so that this can be
8810 * undone separately from what was previously inserted. */
8811 if (u_sync_once == 2)
8812 {
8813 u_sync_once = 1; /* notify that u_sync() was called */
8814 u_sync(TRUE);
8815 }
8816
Bram Moolenaar0d660222005-01-07 21:51:51 +00008817 lnum = get_tv_lnum(argvars);
8818 if (lnum >= 0
8819 && lnum <= curbuf->b_ml.ml_line_count
8820 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008821 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008822 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008823 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008824 l = argvars[1].vval.v_list;
8825 if (l == NULL)
8826 return;
8827 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008829 for (;;)
8830 {
8831 if (l == NULL)
8832 tv = &argvars[1]; /* append a string */
8833 else if (li == NULL)
8834 break; /* end of list */
8835 else
8836 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008837 line = get_tv_string_chk(tv);
8838 if (line == NULL) /* type error */
8839 {
8840 rettv->vval.v_number = 1; /* Failed */
8841 break;
8842 }
8843 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008844 ++added;
8845 if (l == NULL)
8846 break;
8847 li = li->li_next;
8848 }
8849
8850 appended_lines_mark(lnum, added);
8851 if (curwin->w_cursor.lnum > lnum)
8852 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 else
8855 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
8858/*
8859 * "argc()" function
8860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867}
8868
8869/*
8870 * "argidx()" function
8871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878}
8879
8880/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008881 * "arglistid()" function
8882 */
8883 static void
8884f_arglistid(argvars, rettv)
8885 typval_T *argvars UNUSED;
8886 typval_T *rettv;
8887{
8888 win_T *wp;
8889 tabpage_T *tp = NULL;
8890 long n;
8891
8892 rettv->vval.v_number = -1;
8893 if (argvars[0].v_type != VAR_UNKNOWN)
8894 {
8895 if (argvars[1].v_type != VAR_UNKNOWN)
8896 {
8897 n = get_tv_number(&argvars[1]);
8898 if (n >= 0)
8899 tp = find_tabpage(n);
8900 }
8901 else
8902 tp = curtab;
8903
8904 if (tp != NULL)
8905 {
8906 wp = find_win_by_nr(&argvars[0], tp);
8907 if (wp != NULL)
8908 rettv->vval.v_number = wp->w_alist->id;
8909 }
8910 }
8911 else
8912 rettv->vval.v_number = curwin->w_alist->id;
8913}
8914
8915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 * "argv(nr)" function
8917 */
8918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *argvars;
8921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 int idx;
8924
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008925 if (argvars[0].v_type != VAR_UNKNOWN)
8926 {
8927 idx = get_tv_number_chk(&argvars[0], NULL);
8928 if (idx >= 0 && idx < ARGCOUNT)
8929 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8930 else
8931 rettv->vval.v_string = NULL;
8932 rettv->v_type = VAR_STRING;
8933 }
8934 else if (rettv_list_alloc(rettv) == OK)
8935 for (idx = 0; idx < ARGCOUNT; ++idx)
8936 list_append_string(rettv->vval.v_list,
8937 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008940#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008941/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008942 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008943 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008944 static void
8945f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008946 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008947 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008949 float_T f;
8950
8951 rettv->v_type = VAR_FLOAT;
8952 if (get_float_arg(argvars, &f) == OK)
8953 rettv->vval.v_float = asin(f);
8954 else
8955 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956}
8957
8958/*
8959 * "atan()" function
8960 */
8961 static void
8962f_atan(argvars, rettv)
8963 typval_T *argvars;
8964 typval_T *rettv;
8965{
8966 float_T f;
8967
8968 rettv->v_type = VAR_FLOAT;
8969 if (get_float_arg(argvars, &f) == OK)
8970 rettv->vval.v_float = atan(f);
8971 else
8972 rettv->vval.v_float = 0.0;
8973}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008974
8975/*
8976 * "atan2()" function
8977 */
8978 static void
8979f_atan2(argvars, rettv)
8980 typval_T *argvars;
8981 typval_T *rettv;
8982{
8983 float_T fx, fy;
8984
8985 rettv->v_type = VAR_FLOAT;
8986 if (get_float_arg(argvars, &fx) == OK
8987 && get_float_arg(&argvars[1], &fy) == OK)
8988 rettv->vval.v_float = atan2(fx, fy);
8989 else
8990 rettv->vval.v_float = 0.0;
8991}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008992#endif
8993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*
8995 * "browse(save, title, initdir, default)" function
8996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002#ifdef FEAT_BROWSE
9003 int save;
9004 char_u *title;
9005 char_u *initdir;
9006 char_u *defname;
9007 char_u buf[NUMBUFLEN];
9008 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 save = get_tv_number_chk(&argvars[0], &error);
9012 title = get_tv_string_chk(&argvars[1]);
9013 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9014 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 if (error || title == NULL || initdir == NULL || defname == NULL)
9017 rettv->vval.v_string = NULL;
9018 else
9019 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009020 do_browse(save ? BROWSE_SAVE : 0,
9021 title, defname, NULL, initdir, NULL, curbuf);
9022#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009024#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009026}
9027
9028/*
9029 * "browsedir(title, initdir)" function
9030 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009035{
9036#ifdef FEAT_BROWSE
9037 char_u *title;
9038 char_u *initdir;
9039 char_u buf[NUMBUFLEN];
9040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 title = get_tv_string_chk(&argvars[0]);
9042 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 if (title == NULL || initdir == NULL)
9045 rettv->vval.v_string = NULL;
9046 else
9047 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009048 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
Bram Moolenaar33570922005-01-25 22:26:29 +00009055static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057/*
9058 * Find a buffer by number or exact name.
9059 */
9060 static buf_T *
9061find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009066 if (avar->v_type == VAR_NUMBER)
9067 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009068 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009071 if (buf == NULL)
9072 {
9073 /* No full path name match, try a match with a URL or a "nofile"
9074 * buffer, these don't use the full path. */
9075 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9076 if (buf->b_fname != NULL
9077 && (path_with_url(buf->b_fname)
9078#ifdef FEAT_QUICKFIX
9079 || bt_nofile(buf)
9080#endif
9081 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009082 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009083 break;
9084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
9086 return buf;
9087}
9088
9089/*
9090 * "bufexists(expr)" function
9091 */
9092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *argvars;
9095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "buflisted(expr)" function
9102 */
9103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 buf_T *buf;
9109
9110 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
9114/*
9115 * "bufloaded(expr)" function
9116 */
9117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *argvars;
9120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 buf_T *buf;
9123
9124 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126}
9127
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009128static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130/*
9131 * Get buffer by number or pattern.
9132 */
9133 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009134get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009136 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 int save_magic;
9140 char_u *save_cpo;
9141 buf_T *buf;
9142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 if (tv->v_type == VAR_NUMBER)
9144 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009145 if (tv->v_type != VAR_STRING)
9146 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (name == NULL || *name == NUL)
9148 return curbuf;
9149 if (name[0] == '$' && name[1] == NUL)
9150 return lastbuf;
9151
9152 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9153 save_magic = p_magic;
9154 p_magic = TRUE;
9155 save_cpo = p_cpo;
9156 p_cpo = (char_u *)"";
9157
9158 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009159 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
9161 p_magic = save_magic;
9162 p_cpo = save_cpo;
9163
9164 /* If not found, try expanding the name, like done for bufexists(). */
9165 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
9168 return buf;
9169}
9170
9171/*
9172 * "bufname(expr)" function
9173 */
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 buf_T *buf;
9180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009183 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 --emsg_off;
9190}
9191
9192/*
9193 * "bufnr(expr)" function
9194 */
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009201 int error = FALSE;
9202 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009206 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009207 --emsg_off;
9208
9209 /* If the buffer isn't found and the second argument is not zero create a
9210 * new buffer. */
9211 if (buf == NULL
9212 && argvars[1].v_type != VAR_UNKNOWN
9213 && get_tv_number_chk(&argvars[1], &error) != 0
9214 && !error
9215 && (name = get_tv_string_chk(&argvars[0])) != NULL
9216 && !error)
9217 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223}
9224
9225/*
9226 * "bufwinnr(nr)" function
9227 */
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
9233#ifdef FEAT_WINDOWS
9234 win_T *wp;
9235 int winnr = 0;
9236#endif
9237 buf_T *buf;
9238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009241 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242#ifdef FEAT_WINDOWS
9243 for (wp = firstwin; wp; wp = wp->w_next)
9244 {
9245 ++winnr;
9246 if (wp->w_buffer == buf)
9247 break;
9248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252#endif
9253 --emsg_off;
9254}
9255
9256/*
9257 * "byte2line(byte)" function
9258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009261 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266#else
9267 long boff = 0;
9268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 (linenr_T)0, &boff);
9275#endif
9276}
9277
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009278 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009279byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T *argvars;
9281 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009282 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009283{
9284#ifdef FEAT_MBYTE
9285 char_u *t;
9286#endif
9287 char_u *str;
9288 long idx;
9289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 str = get_tv_string_chk(&argvars[0]);
9291 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009294 return;
9295
9296#ifdef FEAT_MBYTE
9297 t = str;
9298 for ( ; idx > 0; idx--)
9299 {
9300 if (*t == NUL) /* EOL reached */
9301 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009302 if (enc_utf8 && comp)
9303 t += utf_ptr2len(t);
9304 else
9305 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009306 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009307 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009308#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009309 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009311#endif
9312}
9313
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009314/*
9315 * "byteidx()" function
9316 */
9317 static void
9318f_byteidx(argvars, rettv)
9319 typval_T *argvars;
9320 typval_T *rettv;
9321{
9322 byteidx(argvars, rettv, FALSE);
9323}
9324
9325/*
9326 * "byteidxcomp()" function
9327 */
9328 static void
9329f_byteidxcomp(argvars, rettv)
9330 typval_T *argvars;
9331 typval_T *rettv;
9332{
9333 byteidx(argvars, rettv, TRUE);
9334}
9335
Bram Moolenaardb913952012-06-29 12:54:53 +02009336 int
9337func_call(name, args, selfdict, rettv)
9338 char_u *name;
9339 typval_T *args;
9340 dict_T *selfdict;
9341 typval_T *rettv;
9342{
9343 listitem_T *item;
9344 typval_T argv[MAX_FUNC_ARGS + 1];
9345 int argc = 0;
9346 int dummy;
9347 int r = 0;
9348
9349 for (item = args->vval.v_list->lv_first; item != NULL;
9350 item = item->li_next)
9351 {
9352 if (argc == MAX_FUNC_ARGS)
9353 {
9354 EMSG(_("E699: Too many arguments"));
9355 break;
9356 }
9357 /* Make a copy of each argument. This is needed to be able to set
9358 * v_lock to VAR_FIXED in the copy without changing the original list.
9359 */
9360 copy_tv(&item->li_tv, &argv[argc++]);
9361 }
9362
9363 if (item == NULL)
9364 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9365 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9366 &dummy, TRUE, selfdict);
9367
9368 /* Free the arguments. */
9369 while (argc > 0)
9370 clear_tv(&argv[--argc]);
9371
9372 return r;
9373}
9374
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009375/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009376 * "call(func, arglist)" function
9377 */
9378 static void
9379f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009382{
9383 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009385
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009386 if (argvars[1].v_type != VAR_LIST)
9387 {
9388 EMSG(_(e_listreq));
9389 return;
9390 }
9391 if (argvars[1].vval.v_list == NULL)
9392 return;
9393
9394 if (argvars[0].v_type == VAR_FUNC)
9395 func = argvars[0].vval.v_string;
9396 else
9397 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 if (*func == NUL)
9399 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009400
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 if (argvars[2].v_type != VAR_UNKNOWN)
9402 {
9403 if (argvars[2].v_type != VAR_DICT)
9404 {
9405 EMSG(_(e_dictreq));
9406 return;
9407 }
9408 selfdict = argvars[2].vval.v_dict;
9409 }
9410
Bram Moolenaardb913952012-06-29 12:54:53 +02009411 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009412}
9413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009414#ifdef FEAT_FLOAT
9415/*
9416 * "ceil({float})" function
9417 */
9418 static void
9419f_ceil(argvars, rettv)
9420 typval_T *argvars;
9421 typval_T *rettv;
9422{
9423 float_T f;
9424
9425 rettv->v_type = VAR_FLOAT;
9426 if (get_float_arg(argvars, &f) == OK)
9427 rettv->vval.v_float = ceil(f);
9428 else
9429 rettv->vval.v_float = 0.0;
9430}
9431#endif
9432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009433/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009434 * "changenr()" function
9435 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009436 static void
9437f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009438 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009439 typval_T *rettv;
9440{
9441 rettv->vval.v_number = curbuf->b_u_seq_cur;
9442}
9443
9444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * "char2nr(string)" function
9446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452#ifdef FEAT_MBYTE
9453 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009454 {
9455 int utf8 = 0;
9456
9457 if (argvars[1].v_type != VAR_UNKNOWN)
9458 utf8 = get_tv_number_chk(&argvars[1], NULL);
9459
9460 if (utf8)
9461 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9462 else
9463 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 else
9466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468}
9469
9470/*
9471 * "cindent(lnum)" function
9472 */
9473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
9478#ifdef FEAT_CINDENT
9479 pos_T pos;
9480 linenr_T lnum;
9481
9482 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9485 {
9486 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 curwin->w_cursor = pos;
9489 }
9490 else
9491#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009496 * "clearmatches()" function
9497 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009498 static void
9499f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009500 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009501 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009502{
9503#ifdef FEAT_SEARCH_EXTRA
9504 clear_matches(curwin);
9505#endif
9506}
9507
9508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 * "col(string)" function
9510 */
9511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *argvars;
9514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 colnr_T col = 0;
9517 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009518 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009520 fp = var2fpos(&argvars[0], FALSE, &fnum);
9521 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
9523 if (fp->col == MAXCOL)
9524 {
9525 /* '> can be MAXCOL, get the length of the line then */
9526 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009527 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 else
9529 col = MAXCOL;
9530 }
9531 else
9532 {
9533 col = fp->col + 1;
9534#ifdef FEAT_VIRTUALEDIT
9535 /* col(".") when the cursor is on the NUL at the end of the line
9536 * because of "coladd" can be seen as an extra column. */
9537 if (virtual_active() && fp == &curwin->w_cursor)
9538 {
9539 char_u *p = ml_get_cursor();
9540
9541 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9542 curwin->w_virtcol - curwin->w_cursor.coladd))
9543 {
9544# ifdef FEAT_MBYTE
9545 int l;
9546
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009547 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 col += l;
9549# else
9550 if (*p != NUL && p[1] == NUL)
9551 ++col;
9552# endif
9553 }
9554 }
9555#endif
9556 }
9557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
Bram Moolenaar572cb562005-08-05 21:35:02 +00009561#if defined(FEAT_INS_EXPAND)
9562/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009563 * "complete()" function
9564 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009565 static void
9566f_complete(argvars, rettv)
9567 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009568 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009569{
9570 int startcol;
9571
9572 if ((State & INSERT) == 0)
9573 {
9574 EMSG(_("E785: complete() can only be used in Insert mode"));
9575 return;
9576 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009577
9578 /* Check for undo allowed here, because if something was already inserted
9579 * the line was already saved for undo and this check isn't done. */
9580 if (!undo_allowed())
9581 return;
9582
Bram Moolenaarade00832006-03-10 21:46:58 +00009583 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9584 {
9585 EMSG(_(e_invarg));
9586 return;
9587 }
9588
9589 startcol = get_tv_number_chk(&argvars[0], NULL);
9590 if (startcol <= 0)
9591 return;
9592
9593 set_completion(startcol - 1, argvars[1].vval.v_list);
9594}
9595
9596/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009597 * "complete_add()" function
9598 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009599 static void
9600f_complete_add(argvars, rettv)
9601 typval_T *argvars;
9602 typval_T *rettv;
9603{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009604 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009605}
9606
9607/*
9608 * "complete_check()" function
9609 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009610 static void
9611f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009612 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009613 typval_T *rettv;
9614{
9615 int saved = RedrawingDisabled;
9616
9617 RedrawingDisabled = 0;
9618 ins_compl_check_keys(0);
9619 rettv->vval.v_number = compl_interrupted;
9620 RedrawingDisabled = saved;
9621}
9622#endif
9623
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624/*
9625 * "confirm(message, buttons[, default [, type]])" function
9626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009629 typval_T *argvars UNUSED;
9630 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9633 char_u *message;
9634 char_u *buttons = NULL;
9635 char_u buf[NUMBUFLEN];
9636 char_u buf2[NUMBUFLEN];
9637 int def = 1;
9638 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 char_u *typestr;
9640 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 message = get_tv_string_chk(&argvars[0]);
9643 if (message == NULL)
9644 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009645 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9648 if (buttons == NULL)
9649 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009653 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9656 if (typestr == NULL)
9657 error = TRUE;
9658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 switch (TOUPPER_ASC(*typestr))
9661 {
9662 case 'E': type = VIM_ERROR; break;
9663 case 'Q': type = VIM_QUESTION; break;
9664 case 'I': type = VIM_INFO; break;
9665 case 'W': type = VIM_WARNING; break;
9666 case 'G': type = VIM_GENERIC; break;
9667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 }
9669 }
9670 }
9671 }
9672
9673 if (buttons == NULL || *buttons == NUL)
9674 buttons = (char_u *)_("&Ok");
9675
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009676 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009677 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009678 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#endif
9680}
9681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009682/*
9683 * "copy()" function
9684 */
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009689{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009690 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009693#ifdef FEAT_FLOAT
9694/*
9695 * "cos()" function
9696 */
9697 static void
9698f_cos(argvars, rettv)
9699 typval_T *argvars;
9700 typval_T *rettv;
9701{
9702 float_T f;
9703
9704 rettv->v_type = VAR_FLOAT;
9705 if (get_float_arg(argvars, &f) == OK)
9706 rettv->vval.v_float = cos(f);
9707 else
9708 rettv->vval.v_float = 0.0;
9709}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009710
9711/*
9712 * "cosh()" function
9713 */
9714 static void
9715f_cosh(argvars, rettv)
9716 typval_T *argvars;
9717 typval_T *rettv;
9718{
9719 float_T f;
9720
9721 rettv->v_type = VAR_FLOAT;
9722 if (get_float_arg(argvars, &f) == OK)
9723 rettv->vval.v_float = cosh(f);
9724 else
9725 rettv->vval.v_float = 0.0;
9726}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009727#endif
9728
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009730 * "count()" function
9731 */
9732 static void
9733f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009736{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009737 long n = 0;
9738 int ic = FALSE;
9739
Bram Moolenaare9a41262005-01-15 22:18:47 +00009740 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009741 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009742 listitem_T *li;
9743 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009745
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 if ((l = argvars[0].vval.v_list) != NULL)
9747 {
9748 li = l->lv_first;
9749 if (argvars[2].v_type != VAR_UNKNOWN)
9750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009751 int error = FALSE;
9752
9753 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009754 if (argvars[3].v_type != VAR_UNKNOWN)
9755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009756 idx = get_tv_number_chk(&argvars[3], &error);
9757 if (!error)
9758 {
9759 li = list_find(l, idx);
9760 if (li == NULL)
9761 EMSGN(_(e_listidx), idx);
9762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009764 if (error)
9765 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 }
9767
9768 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009769 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 ++n;
9771 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009773 else if (argvars[0].v_type == VAR_DICT)
9774 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 int todo;
9776 dict_T *d;
9777 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009778
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009779 if ((d = argvars[0].vval.v_dict) != NULL)
9780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 int error = FALSE;
9782
Bram Moolenaare9a41262005-01-15 22:18:47 +00009783 if (argvars[2].v_type != VAR_UNKNOWN)
9784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009786 if (argvars[3].v_type != VAR_UNKNOWN)
9787 EMSG(_(e_invarg));
9788 }
9789
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009790 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009791 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009792 {
9793 if (!HASHITEM_EMPTY(hi))
9794 {
9795 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009796 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009797 ++n;
9798 }
9799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 }
9801 }
9802 else
9803 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804 rettv->vval.v_number = n;
9805}
9806
9807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9809 *
9810 * Checks the existence of a cscope connection.
9811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009814 typval_T *argvars UNUSED;
9815 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817#ifdef FEAT_CSCOPE
9818 int num = 0;
9819 char_u *dbpath = NULL;
9820 char_u *prepend = NULL;
9821 char_u buf[NUMBUFLEN];
9822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009823 if (argvars[0].v_type != VAR_UNKNOWN
9824 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826 num = (int)get_tv_number(&argvars[0]);
9827 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009828 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 }
9831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833#endif
9834}
9835
9836/*
9837 * "cursor(lnum, col)" function
9838 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009839 * Moves the cursor to the specified line and column.
9840 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 typval_T *argvars;
9845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009848#ifdef FEAT_VIRTUALEDIT
9849 long coladd = 0;
9850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009852 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009853 if (argvars[1].v_type == VAR_UNKNOWN)
9854 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009855 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009856 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009857
Bram Moolenaar493c1782014-05-28 14:34:46 +02009858 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009859 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009860 line = pos.lnum;
9861 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009862#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009863 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009864#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009865 if (curswant >= 0)
9866 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009867 }
9868 else
9869 {
9870 line = get_tv_lnum(argvars);
9871 col = get_tv_number_chk(&argvars[1], NULL);
9872#ifdef FEAT_VIRTUALEDIT
9873 if (argvars[2].v_type != VAR_UNKNOWN)
9874 coladd = get_tv_number_chk(&argvars[2], NULL);
9875#endif
9876 }
9877 if (line < 0 || col < 0
9878#ifdef FEAT_VIRTUALEDIT
9879 || coladd < 0
9880#endif
9881 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 if (line > 0)
9884 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (col > 0)
9886 curwin->w_cursor.col = col - 1;
9887#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009888 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#endif
9890
9891 /* Make sure the cursor is in a valid position. */
9892 check_cursor();
9893#ifdef FEAT_MBYTE
9894 /* Correct cursor for multi-byte character. */
9895 if (has_mbyte)
9896 mb_adjust_cursor();
9897#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009898
9899 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009900 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901}
9902
9903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009904 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 */
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009911 int noref = 0;
9912
9913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009915 if (noref < 0 || noref > 1)
9916 EMSG(_(e_invarg));
9917 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009918 {
9919 current_copyID += COPYID_INC;
9920 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922}
9923
9924/*
9925 * "delete()" function
9926 */
9927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009928f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009929 typval_T *argvars;
9930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931{
9932 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938/*
9939 * "did_filetype()" function
9940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009943 typval_T *argvars UNUSED;
9944 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009947 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948#endif
9949}
9950
9951/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009952 * "diff_filler()" function
9953 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009956 typval_T *argvars UNUSED;
9957 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009958{
9959#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009961#endif
9962}
9963
9964/*
9965 * "diff_hlID()" function
9966 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009969 typval_T *argvars UNUSED;
9970 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009971{
9972#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009974 static linenr_T prev_lnum = 0;
9975 static int changedtick = 0;
9976 static int fnum = 0;
9977 static int change_start = 0;
9978 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009979 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009980 int filler_lines;
9981 int col;
9982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 if (lnum < 0) /* ignore type error in {lnum} arg */
9984 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009985 if (lnum != prev_lnum
9986 || changedtick != curbuf->b_changedtick
9987 || fnum != curbuf->b_fnum)
9988 {
9989 /* New line, buffer, change: need to get the values. */
9990 filler_lines = diff_check(curwin, lnum);
9991 if (filler_lines < 0)
9992 {
9993 if (filler_lines == -1)
9994 {
9995 change_start = MAXCOL;
9996 change_end = -1;
9997 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9998 hlID = HLF_ADD; /* added line */
9999 else
10000 hlID = HLF_CHD; /* changed line */
10001 }
10002 else
10003 hlID = HLF_ADD; /* added line */
10004 }
10005 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010006 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010007 prev_lnum = lnum;
10008 changedtick = curbuf->b_changedtick;
10009 fnum = curbuf->b_fnum;
10010 }
10011
10012 if (hlID == HLF_CHD || hlID == HLF_TXD)
10013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010015 if (col >= change_start && col <= change_end)
10016 hlID = HLF_TXD; /* changed text */
10017 else
10018 hlID = HLF_CHD; /* changed line */
10019 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010020 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010021#endif
10022}
10023
10024/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010025 * "empty({expr})" function
10026 */
10027 static void
10028f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 typval_T *argvars;
10030 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010031{
10032 int n;
10033
10034 switch (argvars[0].v_type)
10035 {
10036 case VAR_STRING:
10037 case VAR_FUNC:
10038 n = argvars[0].vval.v_string == NULL
10039 || *argvars[0].vval.v_string == NUL;
10040 break;
10041 case VAR_NUMBER:
10042 n = argvars[0].vval.v_number == 0;
10043 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010044#ifdef FEAT_FLOAT
10045 case VAR_FLOAT:
10046 n = argvars[0].vval.v_float == 0.0;
10047 break;
10048#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010049 case VAR_LIST:
10050 n = argvars[0].vval.v_list == NULL
10051 || argvars[0].vval.v_list->lv_first == NULL;
10052 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010053 case VAR_DICT:
10054 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010055 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010056 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010057 default:
10058 EMSG2(_(e_intern2), "f_empty()");
10059 n = 0;
10060 }
10061
10062 rettv->vval.v_number = n;
10063}
10064
10065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 * "escape({string}, {chars})" function
10067 */
10068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010070 typval_T *argvars;
10071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
10073 char_u buf[NUMBUFLEN];
10074
Bram Moolenaar758711c2005-02-02 23:11:38 +000010075 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10076 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010081 * "eval()" function
10082 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010083 static void
10084f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010087{
10088 char_u *s;
10089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 s = get_tv_string_chk(&argvars[0]);
10091 if (s != NULL)
10092 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10095 {
10096 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010097 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010099 else if (*s != NUL)
10100 EMSG(_(e_trailing));
10101}
10102
10103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 * "eventhandler()" function
10105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010108 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112}
10113
10114/*
10115 * "executable()" function
10116 */
10117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010122 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10123}
10124
10125/*
10126 * "exepath()" function
10127 */
10128 static void
10129f_exepath(argvars, rettv)
10130 typval_T *argvars;
10131 typval_T *rettv;
10132{
10133 char_u *p = NULL;
10134
10135 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10136 rettv->v_type = VAR_STRING;
10137 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138}
10139
10140/*
10141 * "exists()" function
10142 */
10143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *argvars;
10146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 char_u *p;
10149 char_u *name;
10150 int n = FALSE;
10151 int len = 0;
10152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 if (*p == '$') /* environment variable */
10155 {
10156 /* first try "normal" environment variables (fast) */
10157 if (mch_getenv(p + 1) != NULL)
10158 n = TRUE;
10159 else
10160 {
10161 /* try expanding things like $VIM and ${HOME} */
10162 p = expand_env_save(p);
10163 if (p != NULL && *p != '$')
10164 n = TRUE;
10165 vim_free(p);
10166 }
10167 }
10168 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010171 if (*skipwhite(p) != NUL)
10172 n = FALSE; /* trailing garbage */
10173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 else if (*p == '*') /* internal or user defined function */
10175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010176 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 }
10178 else if (*p == ':')
10179 {
10180 n = cmd_exists(p + 1);
10181 }
10182 else if (*p == '#')
10183 {
10184#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010185 if (p[1] == '#')
10186 n = autocmd_supported(p + 2);
10187 else
10188 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189#endif
10190 }
10191 else /* internal variable */
10192 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010193 char_u *tofree;
10194 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010196 /* get_name_len() takes care of expanding curly braces */
10197 name = p;
10198 len = get_name_len(&p, &tofree, TRUE, FALSE);
10199 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010201 if (tofree != NULL)
10202 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010203 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010204 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010206 /* handle d.key, l[idx], f(expr) */
10207 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10208 if (n)
10209 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 }
10211 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010212 if (*p != NUL)
10213 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010215 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010218 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219}
10220
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010221#ifdef FEAT_FLOAT
10222/*
10223 * "exp()" function
10224 */
10225 static void
10226f_exp(argvars, rettv)
10227 typval_T *argvars;
10228 typval_T *rettv;
10229{
10230 float_T f;
10231
10232 rettv->v_type = VAR_FLOAT;
10233 if (get_float_arg(argvars, &f) == OK)
10234 rettv->vval.v_float = exp(f);
10235 else
10236 rettv->vval.v_float = 0.0;
10237}
10238#endif
10239
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240/*
10241 * "expand()" function
10242 */
10243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010245 typval_T *argvars;
10246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
10248 char_u *s;
10249 int len;
10250 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010251 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010254 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010257 if (argvars[1].v_type != VAR_UNKNOWN
10258 && argvars[2].v_type != VAR_UNKNOWN
10259 && get_tv_number_chk(&argvars[2], &error)
10260 && !error)
10261 {
10262 rettv->v_type = VAR_LIST;
10263 rettv->vval.v_list = NULL;
10264 }
10265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010266 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 if (*s == '%' || *s == '#' || *s == '<')
10268 {
10269 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010270 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010272 if (rettv->v_type == VAR_LIST)
10273 {
10274 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10275 list_append_string(rettv->vval.v_list, result, -1);
10276 else
10277 vim_free(result);
10278 }
10279 else
10280 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 }
10282 else
10283 {
10284 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010285 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 if (argvars[1].v_type != VAR_UNKNOWN
10287 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010288 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 if (!error)
10290 {
10291 ExpandInit(&xpc);
10292 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010293 if (p_wic)
10294 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010295 if (rettv->v_type == VAR_STRING)
10296 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10297 options, WILD_ALL);
10298 else if (rettv_list_alloc(rettv) != FAIL)
10299 {
10300 int i;
10301
10302 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10303 for (i = 0; i < xpc.xp_numfiles; i++)
10304 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10305 ExpandCleanup(&xpc);
10306 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 }
10308 else
10309 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 }
10311}
10312
10313/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010314 * Go over all entries in "d2" and add them to "d1".
10315 * When "action" is "error" then a duplicate key is an error.
10316 * When "action" is "force" then a duplicate key is overwritten.
10317 * Otherwise duplicate keys are ignored ("action" is "keep").
10318 */
10319 void
10320dict_extend(d1, d2, action)
10321 dict_T *d1;
10322 dict_T *d2;
10323 char_u *action;
10324{
10325 dictitem_T *di1;
10326 hashitem_T *hi2;
10327 int todo;
10328
10329 todo = (int)d2->dv_hashtab.ht_used;
10330 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10331 {
10332 if (!HASHITEM_EMPTY(hi2))
10333 {
10334 --todo;
10335 di1 = dict_find(d1, hi2->hi_key, -1);
10336 if (d1->dv_scope != 0)
10337 {
10338 /* Disallow replacing a builtin function in l: and g:.
10339 * Check the key to be valid when adding to any
10340 * scope. */
10341 if (d1->dv_scope == VAR_DEF_SCOPE
10342 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10343 && var_check_func_name(hi2->hi_key,
10344 di1 == NULL))
10345 break;
10346 if (!valid_varname(hi2->hi_key))
10347 break;
10348 }
10349 if (di1 == NULL)
10350 {
10351 di1 = dictitem_copy(HI2DI(hi2));
10352 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10353 dictitem_free(di1);
10354 }
10355 else if (*action == 'e')
10356 {
10357 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10358 break;
10359 }
10360 else if (*action == 'f' && HI2DI(hi2) != di1)
10361 {
10362 clear_tv(&di1->di_tv);
10363 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10364 }
10365 }
10366 }
10367}
10368
10369/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010370 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010372 */
10373 static void
10374f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 typval_T *argvars;
10376 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010377{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010378 char *arg_errmsg = N_("extend() argument");
10379
Bram Moolenaare9a41262005-01-15 22:18:47 +000010380 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010381 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 list_T *l1, *l2;
10383 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386
Bram Moolenaare9a41262005-01-15 22:18:47 +000010387 l1 = argvars[0].vval.v_list;
10388 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010389 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010390 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 {
10392 if (argvars[2].v_type != VAR_UNKNOWN)
10393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010394 before = get_tv_number_chk(&argvars[2], &error);
10395 if (error)
10396 return; /* type error; errmsg already given */
10397
Bram Moolenaar758711c2005-02-02 23:11:38 +000010398 if (before == l1->lv_len)
10399 item = NULL;
10400 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010401 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010402 item = list_find(l1, before);
10403 if (item == NULL)
10404 {
10405 EMSGN(_(e_listidx), before);
10406 return;
10407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 }
10409 }
10410 else
10411 item = NULL;
10412 list_extend(l1, l2, item);
10413
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 copy_tv(&argvars[0], rettv);
10415 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10418 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010419 dict_T *d1, *d2;
10420 char_u *action;
10421 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422
10423 d1 = argvars[0].vval.v_dict;
10424 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010425 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010426 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010427 {
10428 /* Check the third argument. */
10429 if (argvars[2].v_type != VAR_UNKNOWN)
10430 {
10431 static char *(av[]) = {"keep", "force", "error"};
10432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 action = get_tv_string_chk(&argvars[2]);
10434 if (action == NULL)
10435 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 for (i = 0; i < 3; ++i)
10437 if (STRCMP(action, av[i]) == 0)
10438 break;
10439 if (i == 3)
10440 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010441 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010442 return;
10443 }
10444 }
10445 else
10446 action = (char_u *)"force";
10447
Bram Moolenaara9922d62013-05-30 13:01:18 +020010448 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449
Bram Moolenaare9a41262005-01-15 22:18:47 +000010450 copy_tv(&argvars[0], rettv);
10451 }
10452 }
10453 else
10454 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010455}
10456
10457/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010458 * "feedkeys()" function
10459 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010460 static void
10461f_feedkeys(argvars, rettv)
10462 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010463 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010464{
10465 int remap = TRUE;
10466 char_u *keys, *flags;
10467 char_u nbuf[NUMBUFLEN];
10468 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010469 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010470
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010471 /* This is not allowed in the sandbox. If the commands would still be
10472 * executed in the sandbox it would be OK, but it probably happens later,
10473 * when "sandbox" is no longer set. */
10474 if (check_secure())
10475 return;
10476
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010477 keys = get_tv_string(&argvars[0]);
10478 if (*keys != NUL)
10479 {
10480 if (argvars[1].v_type != VAR_UNKNOWN)
10481 {
10482 flags = get_tv_string_buf(&argvars[1], nbuf);
10483 for ( ; *flags != NUL; ++flags)
10484 {
10485 switch (*flags)
10486 {
10487 case 'n': remap = FALSE; break;
10488 case 'm': remap = TRUE; break;
10489 case 't': typed = TRUE; break;
10490 }
10491 }
10492 }
10493
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010494 /* Need to escape K_SPECIAL and CSI before putting the string in the
10495 * typeahead buffer. */
10496 keys_esc = vim_strsave_escape_csi(keys);
10497 if (keys_esc != NULL)
10498 {
10499 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010500 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010501 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010502 if (vgetc_busy)
10503 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010504 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010505 }
10506}
10507
10508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 * "filereadable()" function
10510 */
10511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010513 typval_T *argvars;
10514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010516 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 char_u *p;
10518 int n;
10519
Bram Moolenaarc236c162008-07-13 17:41:49 +000010520#ifndef O_NONBLOCK
10521# define O_NONBLOCK 0
10522#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010524 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10525 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 {
10527 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010528 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
10530 else
10531 n = FALSE;
10532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534}
10535
10536/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010537 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 * rights to write into.
10539 */
10540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010542 typval_T *argvars;
10543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010545 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546}
10547
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010548static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010549
10550 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010551findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010552 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010553 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010554 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010555{
10556#ifdef FEAT_SEARCHPATH
10557 char_u *fname;
10558 char_u *fresult = NULL;
10559 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10560 char_u *p;
10561 char_u pathbuf[NUMBUFLEN];
10562 int count = 1;
10563 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010564 int error = FALSE;
10565#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010566
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010567 rettv->vval.v_string = NULL;
10568 rettv->v_type = VAR_STRING;
10569
10570#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010573 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10576 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010577 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 else
10579 {
10580 if (*p != NUL)
10581 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010584 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010586 }
10587
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010588 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10589 error = TRUE;
10590
10591 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 do
10594 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010595 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010596 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 fresult = find_file_in_path_option(first ? fname : NULL,
10598 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010599 0, first, path,
10600 find_what,
10601 curbuf->b_ffname,
10602 find_what == FINDFILE_DIR
10603 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010605
10606 if (fresult != NULL && rettv->v_type == VAR_LIST)
10607 list_append_string(rettv->vval.v_list, fresult, -1);
10608
10609 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010611
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010612 if (rettv->v_type == VAR_STRING)
10613 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010614#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010615}
10616
Bram Moolenaar33570922005-01-25 22:26:29 +000010617static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10618static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619
10620/*
10621 * Implementation of map() and filter().
10622 */
10623 static void
10624filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627 int map;
10628{
10629 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 listitem_T *li, *nli;
10632 list_T *l = NULL;
10633 dictitem_T *di;
10634 hashtab_T *ht;
10635 hashitem_T *hi;
10636 dict_T *d = NULL;
10637 typval_T save_val;
10638 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010640 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010641 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10642 char *arg_errmsg = (map ? N_("map() argument")
10643 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010644 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010645 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010646
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010649 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010650 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651 return;
10652 }
10653 else if (argvars[0].v_type == VAR_DICT)
10654 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010655 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010656 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010657 return;
10658 }
10659 else
10660 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010661 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010662 return;
10663 }
10664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 expr = get_tv_string_buf_chk(&argvars[1], buf);
10666 /* On type errors, the preceding call has already displayed an error
10667 * message. Avoid a misleading error message for an empty string that
10668 * was not passed as argument. */
10669 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 prepare_vimvar(VV_VAL, &save_val);
10672 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010673
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010674 /* We reset "did_emsg" to be able to detect whether an error
10675 * occurred during evaluation of the expression. */
10676 save_did_emsg = did_emsg;
10677 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010678
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010679 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010680 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010682 vimvars[VV_KEY].vv_type = VAR_STRING;
10683
10684 ht = &d->dv_hashtab;
10685 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010686 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 if (!HASHITEM_EMPTY(hi))
10690 {
10691 --todo;
10692 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010693 if (tv_check_lock(di->di_tv.v_lock,
10694 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 break;
10696 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010697 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010698 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 break;
10700 if (!map && rem)
10701 dictitem_remove(d, di);
10702 clear_tv(&vimvars[VV_KEY].vv_tv);
10703 }
10704 }
10705 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010706 }
10707 else
10708 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010709 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 for (li = l->lv_first; li != NULL; li = nli)
10712 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010713 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010714 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010716 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010717 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010718 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010719 break;
10720 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010722 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010723 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010724 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010725
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010726 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010728
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010729 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010730 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010731
10732 copy_tv(&argvars[0], rettv);
10733}
10734
10735 static int
10736filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 char_u *expr;
10739 int map;
10740 int *remp;
10741{
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010744 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010745
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 s = expr;
10748 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010749 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010750 if (*s != NUL) /* check for trailing chars after expr */
10751 {
10752 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010753 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010754 }
10755 if (map)
10756 {
10757 /* map(): replace the list item value */
10758 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010759 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 *tv = rettv;
10761 }
10762 else
10763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 int error = FALSE;
10765
Bram Moolenaare9a41262005-01-15 22:18:47 +000010766 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010768 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 /* On type error, nothing has been removed; return FAIL to stop the
10770 * loop. The error message was given by get_tv_number_chk(). */
10771 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010772 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010774 retval = OK;
10775theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010776 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010777 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010778}
10779
10780/*
10781 * "filter()" function
10782 */
10783 static void
10784f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010787{
10788 filter_map(argvars, rettv, FALSE);
10789}
10790
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792 * "finddir({fname}[, {path}[, {count}]])" function
10793 */
10794 static void
10795f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 typval_T *argvars;
10797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010799 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800}
10801
10802/*
10803 * "findfile({fname}[, {path}[, {count}]])" function
10804 */
10805 static void
10806f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *argvars;
10808 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010810 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811}
10812
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010813#ifdef FEAT_FLOAT
10814/*
10815 * "float2nr({float})" function
10816 */
10817 static void
10818f_float2nr(argvars, rettv)
10819 typval_T *argvars;
10820 typval_T *rettv;
10821{
10822 float_T f;
10823
10824 if (get_float_arg(argvars, &f) == OK)
10825 {
10826 if (f < -0x7fffffff)
10827 rettv->vval.v_number = -0x7fffffff;
10828 else if (f > 0x7fffffff)
10829 rettv->vval.v_number = 0x7fffffff;
10830 else
10831 rettv->vval.v_number = (varnumber_T)f;
10832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010833}
10834
10835/*
10836 * "floor({float})" function
10837 */
10838 static void
10839f_floor(argvars, rettv)
10840 typval_T *argvars;
10841 typval_T *rettv;
10842{
10843 float_T f;
10844
10845 rettv->v_type = VAR_FLOAT;
10846 if (get_float_arg(argvars, &f) == OK)
10847 rettv->vval.v_float = floor(f);
10848 else
10849 rettv->vval.v_float = 0.0;
10850}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010851
10852/*
10853 * "fmod()" function
10854 */
10855 static void
10856f_fmod(argvars, rettv)
10857 typval_T *argvars;
10858 typval_T *rettv;
10859{
10860 float_T fx, fy;
10861
10862 rettv->v_type = VAR_FLOAT;
10863 if (get_float_arg(argvars, &fx) == OK
10864 && get_float_arg(&argvars[1], &fy) == OK)
10865 rettv->vval.v_float = fmod(fx, fy);
10866 else
10867 rettv->vval.v_float = 0.0;
10868}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010869#endif
10870
Bram Moolenaar0d660222005-01-07 21:51:51 +000010871/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010872 * "fnameescape({string})" function
10873 */
10874 static void
10875f_fnameescape(argvars, rettv)
10876 typval_T *argvars;
10877 typval_T *rettv;
10878{
10879 rettv->vval.v_string = vim_strsave_fnameescape(
10880 get_tv_string(&argvars[0]), FALSE);
10881 rettv->v_type = VAR_STRING;
10882}
10883
10884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 * "fnamemodify({fname}, {mods})" function
10886 */
10887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T *argvars;
10890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
10892 char_u *fname;
10893 char_u *mods;
10894 int usedlen = 0;
10895 int len;
10896 char_u *fbuf = NULL;
10897 char_u buf[NUMBUFLEN];
10898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 fname = get_tv_string_chk(&argvars[0]);
10900 mods = get_tv_string_buf_chk(&argvars[1], buf);
10901 if (fname == NULL || mods == NULL)
10902 fname = NULL;
10903 else
10904 {
10905 len = (int)STRLEN(fname);
10906 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 vim_free(fbuf);
10915}
10916
Bram Moolenaar33570922005-01-25 22:26:29 +000010917static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918
10919/*
10920 * "foldclosed()" function
10921 */
10922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010926 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927{
10928#ifdef FEAT_FOLDING
10929 linenr_T lnum;
10930 linenr_T first, last;
10931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10934 {
10935 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10936 {
10937 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 return;
10942 }
10943 }
10944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010949 * "foldclosed()" function
10950 */
10951 static void
10952f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 typval_T *argvars;
10954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010955{
10956 foldclosed_both(argvars, rettv, FALSE);
10957}
10958
10959/*
10960 * "foldclosedend()" function
10961 */
10962 static void
10963f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010964 typval_T *argvars;
10965 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010966{
10967 foldclosed_both(argvars, rettv, TRUE);
10968}
10969
10970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 * "foldlevel()" function
10972 */
10973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010975 typval_T *argvars UNUSED;
10976 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978#ifdef FEAT_FOLDING
10979 linenr_T lnum;
10980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
10988 * "foldtext()" function
10989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010992 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994{
10995#ifdef FEAT_FOLDING
10996 linenr_T lnum;
10997 char_u *s;
10998 char_u *r;
10999 int len;
11000 char *txt;
11001#endif
11002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003 rettv->v_type = VAR_STRING;
11004 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11007 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11008 <= curbuf->b_ml.ml_line_count
11009 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 {
11011 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11013 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 {
11015 if (!linewhite(lnum))
11016 break;
11017 ++lnum;
11018 }
11019
11020 /* Find interesting text in this line. */
11021 s = skipwhite(ml_get(lnum));
11022 /* skip C comment-start */
11023 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011026 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011027 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011028 {
11029 s = skipwhite(ml_get(lnum + 1));
11030 if (*s == '*')
11031 s = skipwhite(s + 1);
11032 }
11033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 txt = _("+-%s%3ld lines: ");
11035 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011036 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 + 20 /* for %3ld */
11038 + STRLEN(s))); /* concatenated */
11039 if (r != NULL)
11040 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011041 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11042 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11043 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 len = (int)STRLEN(r);
11045 STRCAT(r, s);
11046 /* remove 'foldmarker' and 'commentstring' */
11047 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 }
11050 }
11051#endif
11052}
11053
11054/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011055 * "foldtextresult(lnum)" function
11056 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011060 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011061{
11062#ifdef FEAT_FOLDING
11063 linenr_T lnum;
11064 char_u *text;
11065 char_u buf[51];
11066 foldinfo_T foldinfo;
11067 int fold_count;
11068#endif
11069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->v_type = VAR_STRING;
11071 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011072#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 /* treat illegal types and illegal string values for {lnum} the same */
11075 if (lnum < 0)
11076 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011077 fold_count = foldedCount(curwin, lnum, &foldinfo);
11078 if (fold_count > 0)
11079 {
11080 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11081 &foldinfo, buf);
11082 if (text == buf)
11083 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011085 }
11086#endif
11087}
11088
11089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 * "foreground()" function
11091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011094 typval_T *argvars UNUSED;
11095 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097#ifdef FEAT_GUI
11098 if (gui.in_use)
11099 gui_mch_set_foreground();
11100#else
11101# ifdef WIN32
11102 win32_set_foreground();
11103# endif
11104#endif
11105}
11106
11107/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011108 * "function()" function
11109 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T *argvars;
11113 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114{
11115 char_u *s;
11116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011118 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011119 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011120 /* Don't check an autoload name for existence here. */
11121 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011122 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 else
11124 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011125 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011126 {
11127 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011128 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011129
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011130 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11131 * also be called from another script. Using trans_function_name()
11132 * would also work, but some plugins depend on the name being
11133 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011134 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011135 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011136 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011137 if (rettv->vval.v_string != NULL)
11138 {
11139 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011140 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011141 }
11142 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011143 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011144 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011146 }
11147}
11148
11149/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011150 * "garbagecollect()" function
11151 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011152 static void
11153f_garbagecollect(argvars, rettv)
11154 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011155 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011156{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011157 /* This is postponed until we are back at the toplevel, because we may be
11158 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11159 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011160
11161 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11162 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011163}
11164
11165/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011166 * "get()" function
11167 */
11168 static void
11169f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011170 typval_T *argvars;
11171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172{
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 listitem_T *li;
11174 list_T *l;
11175 dictitem_T *di;
11176 dict_T *d;
11177 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011178
Bram Moolenaare9a41262005-01-15 22:18:47 +000011179 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 int error = FALSE;
11184
11185 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11186 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011188 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011190 else if (argvars[0].v_type == VAR_DICT)
11191 {
11192 if ((d = argvars[0].vval.v_dict) != NULL)
11193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011194 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011195 if (di != NULL)
11196 tv = &di->di_tv;
11197 }
11198 }
11199 else
11200 EMSG2(_(e_listdictarg), "get()");
11201
11202 if (tv == NULL)
11203 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011205 copy_tv(&argvars[2], rettv);
11206 }
11207 else
11208 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209}
11210
Bram Moolenaar342337a2005-07-21 21:11:17 +000011211static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011212
11213/*
11214 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011215 * Return a range (from start to end) of lines in rettv from the specified
11216 * buffer.
11217 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011218 */
11219 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011220get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011221 buf_T *buf;
11222 linenr_T start;
11223 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011224 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011225 typval_T *rettv;
11226{
11227 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011228
Bram Moolenaar959a1432013-12-14 12:17:38 +010011229 rettv->v_type = VAR_STRING;
11230 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011231 if (retlist && rettv_list_alloc(rettv) == FAIL)
11232 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011233
11234 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11235 return;
11236
11237 if (!retlist)
11238 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011239 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11240 p = ml_get_buf(buf, start, FALSE);
11241 else
11242 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011243 rettv->vval.v_string = vim_strsave(p);
11244 }
11245 else
11246 {
11247 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011248 return;
11249
11250 if (start < 1)
11251 start = 1;
11252 if (end > buf->b_ml.ml_line_count)
11253 end = buf->b_ml.ml_line_count;
11254 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011255 if (list_append_string(rettv->vval.v_list,
11256 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011257 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011258 }
11259}
11260
11261/*
11262 * "getbufline()" function
11263 */
11264 static void
11265f_getbufline(argvars, rettv)
11266 typval_T *argvars;
11267 typval_T *rettv;
11268{
11269 linenr_T lnum;
11270 linenr_T end;
11271 buf_T *buf;
11272
11273 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11274 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011275 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011276 --emsg_off;
11277
Bram Moolenaar661b1822005-07-28 22:36:45 +000011278 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011279 if (argvars[2].v_type == VAR_UNKNOWN)
11280 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011281 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011282 end = get_tv_lnum_buf(&argvars[2], buf);
11283
Bram Moolenaar342337a2005-07-21 21:11:17 +000011284 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011285}
11286
Bram Moolenaar0d660222005-01-07 21:51:51 +000011287/*
11288 * "getbufvar()" function
11289 */
11290 static void
11291f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
11295 buf_T *buf;
11296 buf_T *save_curbuf;
11297 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011299 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11302 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011304 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011306 rettv->v_type = VAR_STRING;
11307 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011308
11309 if (buf != NULL && varname != NULL)
11310 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011311 /* set curbuf to be our buf, temporarily */
11312 save_curbuf = curbuf;
11313 curbuf = buf;
11314
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011316 {
11317 if (get_option_tv(&varname, rettv, TRUE) == OK)
11318 done = TRUE;
11319 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011320 else if (STRCMP(varname, "changedtick") == 0)
11321 {
11322 rettv->v_type = VAR_NUMBER;
11323 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011324 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011325 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326 else
11327 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011328 /* Look up the variable. */
11329 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11330 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11331 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011332 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011333 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011335 done = TRUE;
11336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011338
11339 /* restore previous notion of curbuf */
11340 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341 }
11342
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011343 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11344 /* use the default value */
11345 copy_tv(&argvars[2], rettv);
11346
Bram Moolenaar0d660222005-01-07 21:51:51 +000011347 --emsg_off;
11348}
11349
11350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 * "getchar()" function
11352 */
11353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 typval_T *argvars;
11356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
11358 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011361 /* Position the cursor. Needed after a message that ends in a space. */
11362 windgoto(msg_row, msg_col);
11363
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 ++no_mapping;
11365 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011366 for (;;)
11367 {
11368 if (argvars[0].v_type == VAR_UNKNOWN)
11369 /* getchar(): blocking wait. */
11370 n = safe_vgetc();
11371 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11372 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011373 n = vpeekc_any();
11374 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011375 /* illegal argument or getchar(0) and no char avail: return zero */
11376 n = 0;
11377 else
11378 /* getchar(0) and char avail: return char */
11379 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011380
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011381 if (n == K_IGNORE)
11382 continue;
11383 break;
11384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 --no_mapping;
11386 --allow_keys;
11387
Bram Moolenaar219b8702006-11-01 14:32:36 +000011388 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11389 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11390 vimvars[VV_MOUSE_COL].vv_nr = 0;
11391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 if (IS_SPECIAL(n) || mod_mask != 0)
11394 {
11395 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11396 int i = 0;
11397
11398 /* Turn a special key into three bytes, plus modifier. */
11399 if (mod_mask != 0)
11400 {
11401 temp[i++] = K_SPECIAL;
11402 temp[i++] = KS_MODIFIER;
11403 temp[i++] = mod_mask;
11404 }
11405 if (IS_SPECIAL(n))
11406 {
11407 temp[i++] = K_SPECIAL;
11408 temp[i++] = K_SECOND(n);
11409 temp[i++] = K_THIRD(n);
11410 }
11411#ifdef FEAT_MBYTE
11412 else if (has_mbyte)
11413 i += (*mb_char2bytes)(n, temp + i);
11414#endif
11415 else
11416 temp[i++] = n;
11417 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->v_type = VAR_STRING;
11419 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011420
11421#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011422 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011423 {
11424 int row = mouse_row;
11425 int col = mouse_col;
11426 win_T *win;
11427 linenr_T lnum;
11428# ifdef FEAT_WINDOWS
11429 win_T *wp;
11430# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011431 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011432
11433 if (row >= 0 && col >= 0)
11434 {
11435 /* Find the window at the mouse coordinates and compute the
11436 * text position. */
11437 win = mouse_find_win(&row, &col);
11438 (void)mouse_comp_pos(win, &row, &col, &lnum);
11439# ifdef FEAT_WINDOWS
11440 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011441 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011442# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011443 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011444 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11445 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11446 }
11447 }
11448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450}
11451
11452/*
11453 * "getcharmod()" function
11454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011457 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461}
11462
11463/*
11464 * "getcmdline()" function
11465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
11472 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473}
11474
11475/*
11476 * "getcmdpos()" function
11477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011480 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484}
11485
11486/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011487 * "getcmdtype()" function
11488 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011489 static void
11490f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011491 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011492 typval_T *rettv;
11493{
11494 rettv->v_type = VAR_STRING;
11495 rettv->vval.v_string = alloc(2);
11496 if (rettv->vval.v_string != NULL)
11497 {
11498 rettv->vval.v_string[0] = get_cmdline_type();
11499 rettv->vval.v_string[1] = NUL;
11500 }
11501}
11502
11503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 * "getcwd()" function
11505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011508 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011511 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011514 rettv->vval.v_string = NULL;
11515 cwd = alloc(MAXPATHL);
11516 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011518 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11519 {
11520 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011522 if (rettv->vval.v_string != NULL)
11523 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011525 }
11526 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 }
11528}
11529
11530/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011531 * "getfontname()" function
11532 */
11533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011535 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011536 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 rettv->v_type = VAR_STRING;
11539 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011540#ifdef FEAT_GUI
11541 if (gui.in_use)
11542 {
11543 GuiFont font;
11544 char_u *name = NULL;
11545
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011547 {
11548 /* Get the "Normal" font. Either the name saved by
11549 * hl_set_font_name() or from the font ID. */
11550 font = gui.norm_font;
11551 name = hl_get_font_name();
11552 }
11553 else
11554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011556 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11557 return;
11558 font = gui_mch_get_font(name, FALSE);
11559 if (font == NOFONT)
11560 return; /* Invalid font name, return empty string. */
11561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011563 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011564 gui_mch_free_font(font);
11565 }
11566#endif
11567}
11568
11569/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011570 * "getfperm({fname})" function
11571 */
11572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011576{
11577 char_u *fname;
11578 struct stat st;
11579 char_u *perm = NULL;
11580 char_u flags[] = "rwx";
11581 int i;
11582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011586 if (mch_stat((char *)fname, &st) >= 0)
11587 {
11588 perm = vim_strsave((char_u *)"---------");
11589 if (perm != NULL)
11590 {
11591 for (i = 0; i < 9; i++)
11592 {
11593 if (st.st_mode & (1 << (8 - i)))
11594 perm[i] = flags[i % 3];
11595 }
11596 }
11597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011599}
11600
11601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 * "getfsize({fname})" function
11603 */
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 char_u *fname;
11610 struct stat st;
11611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
11616 if (mch_stat((char *)fname, &st) >= 0)
11617 {
11618 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011623
11624 /* non-perfect check for overflow */
11625 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11626 rettv->vval.v_number = -2;
11627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 }
11629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631}
11632
11633/*
11634 * "getftime({fname})" function
11635 */
11636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
11641 char_u *fname;
11642 struct stat st;
11643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
11646 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650}
11651
11652/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011653 * "getftype({fname})" function
11654 */
11655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011657 typval_T *argvars;
11658 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011659{
11660 char_u *fname;
11661 struct stat st;
11662 char_u *type = NULL;
11663 char *t;
11664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011668 if (mch_lstat((char *)fname, &st) >= 0)
11669 {
11670#ifdef S_ISREG
11671 if (S_ISREG(st.st_mode))
11672 t = "file";
11673 else if (S_ISDIR(st.st_mode))
11674 t = "dir";
11675# ifdef S_ISLNK
11676 else if (S_ISLNK(st.st_mode))
11677 t = "link";
11678# endif
11679# ifdef S_ISBLK
11680 else if (S_ISBLK(st.st_mode))
11681 t = "bdev";
11682# endif
11683# ifdef S_ISCHR
11684 else if (S_ISCHR(st.st_mode))
11685 t = "cdev";
11686# endif
11687# ifdef S_ISFIFO
11688 else if (S_ISFIFO(st.st_mode))
11689 t = "fifo";
11690# endif
11691# ifdef S_ISSOCK
11692 else if (S_ISSOCK(st.st_mode))
11693 t = "fifo";
11694# endif
11695 else
11696 t = "other";
11697#else
11698# ifdef S_IFMT
11699 switch (st.st_mode & S_IFMT)
11700 {
11701 case S_IFREG: t = "file"; break;
11702 case S_IFDIR: t = "dir"; break;
11703# ifdef S_IFLNK
11704 case S_IFLNK: t = "link"; break;
11705# endif
11706# ifdef S_IFBLK
11707 case S_IFBLK: t = "bdev"; break;
11708# endif
11709# ifdef S_IFCHR
11710 case S_IFCHR: t = "cdev"; break;
11711# endif
11712# ifdef S_IFIFO
11713 case S_IFIFO: t = "fifo"; break;
11714# endif
11715# ifdef S_IFSOCK
11716 case S_IFSOCK: t = "socket"; break;
11717# endif
11718 default: t = "other";
11719 }
11720# else
11721 if (mch_isdir(fname))
11722 t = "dir";
11723 else
11724 t = "file";
11725# endif
11726#endif
11727 type = vim_strsave((char_u *)t);
11728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011730}
11731
11732/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011733 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011734 */
11735 static void
11736f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011737 typval_T *argvars;
11738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011739{
11740 linenr_T lnum;
11741 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011742 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011743
11744 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011745 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011746 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011747 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011748 retlist = FALSE;
11749 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011750 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011753 retlist = TRUE;
11754 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011755
Bram Moolenaar342337a2005-07-21 21:11:17 +000011756 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757}
11758
11759/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011760 * "getmatches()" function
11761 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011762 static void
11763f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011764 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011765 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011766{
11767#ifdef FEAT_SEARCH_EXTRA
11768 dict_T *dict;
11769 matchitem_T *cur = curwin->w_match_head;
11770
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011771 if (rettv_list_alloc(rettv) == OK)
11772 {
11773 while (cur != NULL)
11774 {
11775 dict = dict_alloc();
11776 if (dict == NULL)
11777 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011778 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11779 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11780 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11781 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11782 list_append_dict(rettv->vval.v_list, dict);
11783 cur = cur->next;
11784 }
11785 }
11786#endif
11787}
11788
11789/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011790 * "getpid()" function
11791 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011792 static void
11793f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011794 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011795 typval_T *rettv;
11796{
11797 rettv->vval.v_number = mch_get_pid();
11798}
11799
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011800static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11801
11802/*
11803 * "getcurpos()" function
11804 */
11805 static void
11806f_getcurpos(argvars, rettv)
11807 typval_T *argvars;
11808 typval_T *rettv;
11809{
11810 getpos_both(argvars, rettv, TRUE);
11811}
11812
Bram Moolenaar18081e32008-02-20 19:11:07 +000011813/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011814 * "getpos(string)" function
11815 */
11816 static void
11817f_getpos(argvars, rettv)
11818 typval_T *argvars;
11819 typval_T *rettv;
11820{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011821 getpos_both(argvars, rettv, FALSE);
11822}
11823
11824 static void
11825getpos_both(argvars, rettv, getcurpos)
11826 typval_T *argvars;
11827 typval_T *rettv;
11828 int getcurpos;
11829{
Bram Moolenaara5525202006-03-02 22:52:09 +000011830 pos_T *fp;
11831 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011832 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011833
11834 if (rettv_list_alloc(rettv) == OK)
11835 {
11836 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011837 if (getcurpos)
11838 fp = &curwin->w_cursor;
11839 else
11840 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011841 if (fnum != -1)
11842 list_append_number(l, (varnumber_T)fnum);
11843 else
11844 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011845 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11846 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011847 list_append_number(l, (fp != NULL)
11848 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011849 : (varnumber_T)0);
11850 list_append_number(l,
11851#ifdef FEAT_VIRTUALEDIT
11852 (fp != NULL) ? (varnumber_T)fp->coladd :
11853#endif
11854 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011855 if (getcurpos)
Bram Moolenaar493c1782014-05-28 14:34:46 +020011856 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011857 }
11858 else
11859 rettv->vval.v_number = FALSE;
11860}
11861
11862/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011863 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011864 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011865 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011866f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011867 typval_T *argvars UNUSED;
11868 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011869{
11870#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011871 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011872#endif
11873
Bram Moolenaar2641f772005-03-25 21:58:17 +000011874#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011875 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011876 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011877 wp = NULL;
11878 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11879 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011880 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011881 if (wp == NULL)
11882 return;
11883 }
11884
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011885 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011886 }
11887#endif
11888}
11889
11890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 * "getreg()" function
11892 */
11893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897{
11898 char_u *strregname;
11899 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011900 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011901 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011904 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906 strregname = get_tv_string_chk(&argvars[0]);
11907 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011908 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011911 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11912 return_list = get_tv_number_chk(&argvars[2], &error);
11913 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011916 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011917
11918 if (error)
11919 return;
11920
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 regname = (strregname == NULL ? '"' : *strregname);
11922 if (regname == 0)
11923 regname = '"';
11924
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011925 if (return_list)
11926 {
11927 rettv->v_type = VAR_LIST;
11928 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11929 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11930 }
11931 else
11932 {
11933 rettv->v_type = VAR_STRING;
11934 rettv->vval.v_string = get_reg_contents(regname,
11935 arg2 ? GREG_EXPR_SRC : 0);
11936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937}
11938
11939/*
11940 * "getregtype()" function
11941 */
11942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 typval_T *argvars;
11945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947 char_u *strregname;
11948 int regname;
11949 char_u buf[NUMBUFLEN + 2];
11950 long reglen = 0;
11951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011952 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 {
11954 strregname = get_tv_string_chk(&argvars[0]);
11955 if (strregname == NULL) /* type error; errmsg already given */
11956 {
11957 rettv->v_type = VAR_STRING;
11958 rettv->vval.v_string = NULL;
11959 return;
11960 }
11961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 else
11963 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011964 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965
11966 regname = (strregname == NULL ? '"' : *strregname);
11967 if (regname == 0)
11968 regname = '"';
11969
11970 buf[0] = NUL;
11971 buf[1] = NUL;
11972 switch (get_reg_type(regname, &reglen))
11973 {
11974 case MLINE: buf[0] = 'V'; break;
11975 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 case MBLOCK:
11977 buf[0] = Ctrl_V;
11978 sprintf((char *)buf + 1, "%ld", reglen + 1);
11979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
11982 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983}
11984
11985/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011986 * "gettabvar()" function
11987 */
11988 static void
11989f_gettabvar(argvars, rettv)
11990 typval_T *argvars;
11991 typval_T *rettv;
11992{
11993 tabpage_T *tp;
11994 dictitem_T *v;
11995 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011996 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011997
11998 rettv->v_type = VAR_STRING;
11999 rettv->vval.v_string = NULL;
12000
12001 varname = get_tv_string_chk(&argvars[1]);
12002 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12003 if (tp != NULL && varname != NULL)
12004 {
12005 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020012006 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012007 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012008 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012009 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012010 done = TRUE;
12011 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012012 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012013
12014 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012015 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012016}
12017
12018/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012019 * "gettabwinvar()" function
12020 */
12021 static void
12022f_gettabwinvar(argvars, rettv)
12023 typval_T *argvars;
12024 typval_T *rettv;
12025{
12026 getwinvar(argvars, rettv, 1);
12027}
12028
12029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 * "getwinposx()" function
12031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038#ifdef FEAT_GUI
12039 if (gui.in_use)
12040 {
12041 int x, y;
12042
12043 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 }
12046#endif
12047}
12048
12049/*
12050 * "getwinposy()" function
12051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058#ifdef FEAT_GUI
12059 if (gui.in_use)
12060 {
12061 int x, y;
12062
12063 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 }
12066#endif
12067}
12068
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012069/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012070 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012071 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012072 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012073find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012074 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012075 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012076{
12077#ifdef FEAT_WINDOWS
12078 win_T *wp;
12079#endif
12080 int nr;
12081
12082 nr = get_tv_number_chk(vp, NULL);
12083
12084#ifdef FEAT_WINDOWS
12085 if (nr < 0)
12086 return NULL;
12087 if (nr == 0)
12088 return curwin;
12089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012090 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12091 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012092 if (--nr <= 0)
12093 break;
12094 return wp;
12095#else
12096 if (nr == 0 || nr == 1)
12097 return curwin;
12098 return NULL;
12099#endif
12100}
12101
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102/*
12103 * "getwinvar()" function
12104 */
12105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 typval_T *argvars;
12108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012110 getwinvar(argvars, rettv, 0);
12111}
12112
12113/*
12114 * getwinvar() and gettabwinvar()
12115 */
12116 static void
12117getwinvar(argvars, rettv, off)
12118 typval_T *argvars;
12119 typval_T *rettv;
12120 int off; /* 1 for gettabwinvar() */
12121{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 win_T *win, *oldcurwin;
12123 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012125 tabpage_T *tp = NULL;
12126 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012127 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012129#ifdef FEAT_WINDOWS
12130 if (off == 1)
12131 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12132 else
12133 tp = curtab;
12134#endif
12135 win = find_win_by_nr(&argvars[off], tp);
12136 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012139 rettv->v_type = VAR_STRING;
12140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141
12142 if (win != NULL && varname != NULL)
12143 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012144 /* Set curwin to be our win, temporarily. Also set the tabpage,
12145 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012146 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012147
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012149 {
12150 if (get_option_tv(&varname, rettv, 1) == OK)
12151 done = TRUE;
12152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 else
12154 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012155 /* Look up the variable. */
12156 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12157 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012161 done = TRUE;
12162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012164
12165 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012166 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 }
12168
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012169 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12170 /* use the default return value */
12171 copy_tv(&argvars[off + 2], rettv);
12172
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 --emsg_off;
12174}
12175
12176/*
12177 * "glob()" function
12178 */
12179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012181 typval_T *argvars;
12182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012184 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012186 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012188 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012189 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012191 if (argvars[1].v_type != VAR_UNKNOWN)
12192 {
12193 if (get_tv_number_chk(&argvars[1], &error))
12194 options |= WILD_KEEP_ALL;
12195 if (argvars[2].v_type != VAR_UNKNOWN
12196 && get_tv_number_chk(&argvars[2], &error))
12197 {
12198 rettv->v_type = VAR_LIST;
12199 rettv->vval.v_list = NULL;
12200 }
12201 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012202 if (!error)
12203 {
12204 ExpandInit(&xpc);
12205 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012206 if (p_wic)
12207 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012208 if (rettv->v_type == VAR_STRING)
12209 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012210 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012211 else if (rettv_list_alloc(rettv) != FAIL)
12212 {
12213 int i;
12214
12215 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12216 NULL, options, WILD_ALL_KEEP);
12217 for (i = 0; i < xpc.xp_numfiles; i++)
12218 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12219
12220 ExpandCleanup(&xpc);
12221 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012222 }
12223 else
12224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225}
12226
12227/*
12228 * "globpath()" function
12229 */
12230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012232 typval_T *argvars;
12233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012235 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012238 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012239 garray_T ga;
12240 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012242 /* When the optional second argument is non-zero, don't remove matches
12243 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012245 if (argvars[2].v_type != VAR_UNKNOWN)
12246 {
12247 if (get_tv_number_chk(&argvars[2], &error))
12248 flags |= WILD_KEEP_ALL;
12249 if (argvars[3].v_type != VAR_UNKNOWN
12250 && get_tv_number_chk(&argvars[3], &error))
12251 {
12252 rettv->v_type = VAR_LIST;
12253 rettv->vval.v_list = NULL;
12254 }
12255 }
12256 if (file != NULL && !error)
12257 {
12258 ga_init2(&ga, (int)sizeof(char_u *), 10);
12259 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12260 if (rettv->v_type == VAR_STRING)
12261 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12262 else if (rettv_list_alloc(rettv) != FAIL)
12263 for (i = 0; i < ga.ga_len; ++i)
12264 list_append_string(rettv->vval.v_list,
12265 ((char_u **)(ga.ga_data))[i], -1);
12266 ga_clear_strings(&ga);
12267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012269 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270}
12271
12272/*
12273 * "has()" function
12274 */
12275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012277 typval_T *argvars;
12278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279{
12280 int i;
12281 char_u *name;
12282 int n = FALSE;
12283 static char *(has_list[]) =
12284 {
12285#ifdef AMIGA
12286 "amiga",
12287# ifdef FEAT_ARP
12288 "arp",
12289# endif
12290#endif
12291#ifdef __BEOS__
12292 "beos",
12293#endif
12294#ifdef MSDOS
12295# ifdef DJGPP
12296 "dos32",
12297# else
12298 "dos16",
12299# endif
12300#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012301#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 "mac",
12303#endif
12304#if defined(MACOS_X_UNIX)
12305 "macunix",
12306#endif
12307#ifdef OS2
12308 "os2",
12309#endif
12310#ifdef __QNX__
12311 "qnx",
12312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#ifdef UNIX
12314 "unix",
12315#endif
12316#ifdef VMS
12317 "vms",
12318#endif
12319#ifdef WIN16
12320 "win16",
12321#endif
12322#ifdef WIN32
12323 "win32",
12324#endif
12325#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12326 "win32unix",
12327#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012328#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 "win64",
12330#endif
12331#ifdef EBCDIC
12332 "ebcdic",
12333#endif
12334#ifndef CASE_INSENSITIVE_FILENAME
12335 "fname_case",
12336#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012337#ifdef HAVE_ACL
12338 "acl",
12339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340#ifdef FEAT_ARABIC
12341 "arabic",
12342#endif
12343#ifdef FEAT_AUTOCMD
12344 "autocmd",
12345#endif
12346#ifdef FEAT_BEVAL
12347 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012348# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12349 "balloon_multiline",
12350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#endif
12352#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12353 "builtin_terms",
12354# ifdef ALL_BUILTIN_TCAPS
12355 "all_builtin_terms",
12356# endif
12357#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012358#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12359 || defined(FEAT_GUI_W32) \
12360 || defined(FEAT_GUI_MOTIF))
12361 "browsefilter",
12362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363#ifdef FEAT_BYTEOFF
12364 "byte_offset",
12365#endif
12366#ifdef FEAT_CINDENT
12367 "cindent",
12368#endif
12369#ifdef FEAT_CLIENTSERVER
12370 "clientserver",
12371#endif
12372#ifdef FEAT_CLIPBOARD
12373 "clipboard",
12374#endif
12375#ifdef FEAT_CMDL_COMPL
12376 "cmdline_compl",
12377#endif
12378#ifdef FEAT_CMDHIST
12379 "cmdline_hist",
12380#endif
12381#ifdef FEAT_COMMENTS
12382 "comments",
12383#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012384#ifdef FEAT_CONCEAL
12385 "conceal",
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387#ifdef FEAT_CRYPT
12388 "cryptv",
12389#endif
12390#ifdef FEAT_CSCOPE
12391 "cscope",
12392#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012393#ifdef FEAT_CURSORBIND
12394 "cursorbind",
12395#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012396#ifdef CURSOR_SHAPE
12397 "cursorshape",
12398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399#ifdef DEBUG
12400 "debug",
12401#endif
12402#ifdef FEAT_CON_DIALOG
12403 "dialog_con",
12404#endif
12405#ifdef FEAT_GUI_DIALOG
12406 "dialog_gui",
12407#endif
12408#ifdef FEAT_DIFF
12409 "diff",
12410#endif
12411#ifdef FEAT_DIGRAPHS
12412 "digraphs",
12413#endif
12414#ifdef FEAT_DND
12415 "dnd",
12416#endif
12417#ifdef FEAT_EMACS_TAGS
12418 "emacs_tags",
12419#endif
12420 "eval", /* always present, of course! */
12421#ifdef FEAT_EX_EXTRA
12422 "ex_extra",
12423#endif
12424#ifdef FEAT_SEARCH_EXTRA
12425 "extra_search",
12426#endif
12427#ifdef FEAT_FKMAP
12428 "farsi",
12429#endif
12430#ifdef FEAT_SEARCHPATH
12431 "file_in_path",
12432#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012433#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012434 "filterpipe",
12435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436#ifdef FEAT_FIND_ID
12437 "find_in_path",
12438#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012439#ifdef FEAT_FLOAT
12440 "float",
12441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442#ifdef FEAT_FOLDING
12443 "folding",
12444#endif
12445#ifdef FEAT_FOOTER
12446 "footer",
12447#endif
12448#if !defined(USE_SYSTEM) && defined(UNIX)
12449 "fork",
12450#endif
12451#ifdef FEAT_GETTEXT
12452 "gettext",
12453#endif
12454#ifdef FEAT_GUI
12455 "gui",
12456#endif
12457#ifdef FEAT_GUI_ATHENA
12458# ifdef FEAT_GUI_NEXTAW
12459 "gui_neXtaw",
12460# else
12461 "gui_athena",
12462# endif
12463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464#ifdef FEAT_GUI_GTK
12465 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012468#ifdef FEAT_GUI_GNOME
12469 "gui_gnome",
12470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471#ifdef FEAT_GUI_MAC
12472 "gui_mac",
12473#endif
12474#ifdef FEAT_GUI_MOTIF
12475 "gui_motif",
12476#endif
12477#ifdef FEAT_GUI_PHOTON
12478 "gui_photon",
12479#endif
12480#ifdef FEAT_GUI_W16
12481 "gui_win16",
12482#endif
12483#ifdef FEAT_GUI_W32
12484 "gui_win32",
12485#endif
12486#ifdef FEAT_HANGULIN
12487 "hangul_input",
12488#endif
12489#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12490 "iconv",
12491#endif
12492#ifdef FEAT_INS_EXPAND
12493 "insert_expand",
12494#endif
12495#ifdef FEAT_JUMPLIST
12496 "jumplist",
12497#endif
12498#ifdef FEAT_KEYMAP
12499 "keymap",
12500#endif
12501#ifdef FEAT_LANGMAP
12502 "langmap",
12503#endif
12504#ifdef FEAT_LIBCALL
12505 "libcall",
12506#endif
12507#ifdef FEAT_LINEBREAK
12508 "linebreak",
12509#endif
12510#ifdef FEAT_LISP
12511 "lispindent",
12512#endif
12513#ifdef FEAT_LISTCMDS
12514 "listcmds",
12515#endif
12516#ifdef FEAT_LOCALMAP
12517 "localmap",
12518#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012519#ifdef FEAT_LUA
12520# ifndef DYNAMIC_LUA
12521 "lua",
12522# endif
12523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524#ifdef FEAT_MENU
12525 "menu",
12526#endif
12527#ifdef FEAT_SESSION
12528 "mksession",
12529#endif
12530#ifdef FEAT_MODIFY_FNAME
12531 "modify_fname",
12532#endif
12533#ifdef FEAT_MOUSE
12534 "mouse",
12535#endif
12536#ifdef FEAT_MOUSESHAPE
12537 "mouseshape",
12538#endif
12539#if defined(UNIX) || defined(VMS)
12540# ifdef FEAT_MOUSE_DEC
12541 "mouse_dec",
12542# endif
12543# ifdef FEAT_MOUSE_GPM
12544 "mouse_gpm",
12545# endif
12546# ifdef FEAT_MOUSE_JSB
12547 "mouse_jsbterm",
12548# endif
12549# ifdef FEAT_MOUSE_NET
12550 "mouse_netterm",
12551# endif
12552# ifdef FEAT_MOUSE_PTERM
12553 "mouse_pterm",
12554# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012555# ifdef FEAT_MOUSE_SGR
12556 "mouse_sgr",
12557# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012558# ifdef FEAT_SYSMOUSE
12559 "mouse_sysmouse",
12560# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012561# ifdef FEAT_MOUSE_URXVT
12562 "mouse_urxvt",
12563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564# ifdef FEAT_MOUSE_XTERM
12565 "mouse_xterm",
12566# endif
12567#endif
12568#ifdef FEAT_MBYTE
12569 "multi_byte",
12570#endif
12571#ifdef FEAT_MBYTE_IME
12572 "multi_byte_ime",
12573#endif
12574#ifdef FEAT_MULTI_LANG
12575 "multi_lang",
12576#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012577#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012578#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012579 "mzscheme",
12580#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582#ifdef FEAT_OLE
12583 "ole",
12584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#ifdef FEAT_PATH_EXTRA
12586 "path_extra",
12587#endif
12588#ifdef FEAT_PERL
12589#ifndef DYNAMIC_PERL
12590 "perl",
12591#endif
12592#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012593#ifdef FEAT_PERSISTENT_UNDO
12594 "persistent_undo",
12595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#ifdef FEAT_PYTHON
12597#ifndef DYNAMIC_PYTHON
12598 "python",
12599#endif
12600#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012601#ifdef FEAT_PYTHON3
12602#ifndef DYNAMIC_PYTHON3
12603 "python3",
12604#endif
12605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606#ifdef FEAT_POSTSCRIPT
12607 "postscript",
12608#endif
12609#ifdef FEAT_PRINTER
12610 "printer",
12611#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012612#ifdef FEAT_PROFILE
12613 "profile",
12614#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012615#ifdef FEAT_RELTIME
12616 "reltime",
12617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618#ifdef FEAT_QUICKFIX
12619 "quickfix",
12620#endif
12621#ifdef FEAT_RIGHTLEFT
12622 "rightleft",
12623#endif
12624#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12625 "ruby",
12626#endif
12627#ifdef FEAT_SCROLLBIND
12628 "scrollbind",
12629#endif
12630#ifdef FEAT_CMDL_INFO
12631 "showcmd",
12632 "cmdline_info",
12633#endif
12634#ifdef FEAT_SIGNS
12635 "signs",
12636#endif
12637#ifdef FEAT_SMARTINDENT
12638 "smartindent",
12639#endif
12640#ifdef FEAT_SNIFF
12641 "sniff",
12642#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012643#ifdef STARTUPTIME
12644 "startuptime",
12645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646#ifdef FEAT_STL_OPT
12647 "statusline",
12648#endif
12649#ifdef FEAT_SUN_WORKSHOP
12650 "sun_workshop",
12651#endif
12652#ifdef FEAT_NETBEANS_INTG
12653 "netbeans_intg",
12654#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012655#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012656 "spell",
12657#endif
12658#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 "syntax",
12660#endif
12661#if defined(USE_SYSTEM) || !defined(UNIX)
12662 "system",
12663#endif
12664#ifdef FEAT_TAG_BINS
12665 "tag_binary",
12666#endif
12667#ifdef FEAT_TAG_OLDSTATIC
12668 "tag_old_static",
12669#endif
12670#ifdef FEAT_TAG_ANYWHITE
12671 "tag_any_white",
12672#endif
12673#ifdef FEAT_TCL
12674# ifndef DYNAMIC_TCL
12675 "tcl",
12676# endif
12677#endif
12678#ifdef TERMINFO
12679 "terminfo",
12680#endif
12681#ifdef FEAT_TERMRESPONSE
12682 "termresponse",
12683#endif
12684#ifdef FEAT_TEXTOBJ
12685 "textobjects",
12686#endif
12687#ifdef HAVE_TGETENT
12688 "tgetent",
12689#endif
12690#ifdef FEAT_TITLE
12691 "title",
12692#endif
12693#ifdef FEAT_TOOLBAR
12694 "toolbar",
12695#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012696#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12697 "unnamedplus",
12698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699#ifdef FEAT_USR_CMDS
12700 "user-commands", /* was accidentally included in 5.4 */
12701 "user_commands",
12702#endif
12703#ifdef FEAT_VIMINFO
12704 "viminfo",
12705#endif
12706#ifdef FEAT_VERTSPLIT
12707 "vertsplit",
12708#endif
12709#ifdef FEAT_VIRTUALEDIT
12710 "virtualedit",
12711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713#ifdef FEAT_VISUALEXTRA
12714 "visualextra",
12715#endif
12716#ifdef FEAT_VREPLACE
12717 "vreplace",
12718#endif
12719#ifdef FEAT_WILDIGN
12720 "wildignore",
12721#endif
12722#ifdef FEAT_WILDMENU
12723 "wildmenu",
12724#endif
12725#ifdef FEAT_WINDOWS
12726 "windows",
12727#endif
12728#ifdef FEAT_WAK
12729 "winaltkeys",
12730#endif
12731#ifdef FEAT_WRITEBACKUP
12732 "writebackup",
12733#endif
12734#ifdef FEAT_XIM
12735 "xim",
12736#endif
12737#ifdef FEAT_XFONTSET
12738 "xfontset",
12739#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012740#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012741 "xpm",
12742 "xpm_w32", /* for backward compatibility */
12743#else
12744# if defined(HAVE_XPM)
12745 "xpm",
12746# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748#ifdef USE_XSMP
12749 "xsmp",
12750#endif
12751#ifdef USE_XSMP_INTERACT
12752 "xsmp_interact",
12753#endif
12754#ifdef FEAT_XCLIPBOARD
12755 "xterm_clipboard",
12756#endif
12757#ifdef FEAT_XTERM_SAVE
12758 "xterm_save",
12759#endif
12760#if defined(UNIX) && defined(FEAT_X11)
12761 "X11",
12762#endif
12763 NULL
12764 };
12765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 for (i = 0; has_list[i] != NULL; ++i)
12768 if (STRICMP(name, has_list[i]) == 0)
12769 {
12770 n = TRUE;
12771 break;
12772 }
12773
12774 if (n == FALSE)
12775 {
12776 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012777 {
12778 if (name[5] == '-'
12779 && STRLEN(name) > 11
12780 && vim_isdigit(name[6])
12781 && vim_isdigit(name[8])
12782 && vim_isdigit(name[10]))
12783 {
12784 int major = atoi((char *)name + 6);
12785 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012786
12787 /* Expect "patch-9.9.01234". */
12788 n = (major < VIM_VERSION_MAJOR
12789 || (major == VIM_VERSION_MAJOR
12790 && (minor < VIM_VERSION_MINOR
12791 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012792 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012793 }
12794 else
12795 n = has_patch(atoi((char *)name + 5));
12796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 else if (STRICMP(name, "vim_starting") == 0)
12798 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012799#ifdef FEAT_MBYTE
12800 else if (STRICMP(name, "multi_byte_encoding") == 0)
12801 n = has_mbyte;
12802#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012803#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12804 else if (STRICMP(name, "balloon_multiline") == 0)
12805 n = multiline_balloon_available();
12806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807#ifdef DYNAMIC_TCL
12808 else if (STRICMP(name, "tcl") == 0)
12809 n = tcl_enabled(FALSE);
12810#endif
12811#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12812 else if (STRICMP(name, "iconv") == 0)
12813 n = iconv_enabled(FALSE);
12814#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012815#ifdef DYNAMIC_LUA
12816 else if (STRICMP(name, "lua") == 0)
12817 n = lua_enabled(FALSE);
12818#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012819#ifdef DYNAMIC_MZSCHEME
12820 else if (STRICMP(name, "mzscheme") == 0)
12821 n = mzscheme_enabled(FALSE);
12822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823#ifdef DYNAMIC_RUBY
12824 else if (STRICMP(name, "ruby") == 0)
12825 n = ruby_enabled(FALSE);
12826#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012827#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828#ifdef DYNAMIC_PYTHON
12829 else if (STRICMP(name, "python") == 0)
12830 n = python_enabled(FALSE);
12831#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012832#endif
12833#ifdef FEAT_PYTHON3
12834#ifdef DYNAMIC_PYTHON3
12835 else if (STRICMP(name, "python3") == 0)
12836 n = python3_enabled(FALSE);
12837#endif
12838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839#ifdef DYNAMIC_PERL
12840 else if (STRICMP(name, "perl") == 0)
12841 n = perl_enabled(FALSE);
12842#endif
12843#ifdef FEAT_GUI
12844 else if (STRICMP(name, "gui_running") == 0)
12845 n = (gui.in_use || gui.starting);
12846# ifdef FEAT_GUI_W32
12847 else if (STRICMP(name, "gui_win32s") == 0)
12848 n = gui_is_win32s();
12849# endif
12850# ifdef FEAT_BROWSE
12851 else if (STRICMP(name, "browse") == 0)
12852 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12853# endif
12854#endif
12855#ifdef FEAT_SYN_HL
12856 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012857 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858#endif
12859#if defined(WIN3264)
12860 else if (STRICMP(name, "win95") == 0)
12861 n = mch_windows95();
12862#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012863#ifdef FEAT_NETBEANS_INTG
12864 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012865 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 }
12868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
12872/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012873 * "has_key()" function
12874 */
12875 static void
12876f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012879{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012880 if (argvars[0].v_type != VAR_DICT)
12881 {
12882 EMSG(_(e_dictreq));
12883 return;
12884 }
12885 if (argvars[0].vval.v_dict == NULL)
12886 return;
12887
12888 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012889 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012890}
12891
12892/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012893 * "haslocaldir()" function
12894 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012895 static void
12896f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012897 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012898 typval_T *rettv;
12899{
12900 rettv->vval.v_number = (curwin->w_localdir != NULL);
12901}
12902
12903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 * "hasmapto()" function
12905 */
12906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 typval_T *argvars;
12909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
12911 char_u *name;
12912 char_u *mode;
12913 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012914 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012917 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 mode = (char_u *)"nvo";
12919 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012922 if (argvars[2].v_type != VAR_UNKNOWN)
12923 abbr = get_tv_number(&argvars[2]);
12924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925
Bram Moolenaar2c932302006-03-18 21:42:09 +000012926 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930}
12931
12932/*
12933 * "histadd()" function
12934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012937 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939{
12940#ifdef FEAT_CMDHIST
12941 int histype;
12942 char_u *str;
12943 char_u buf[NUMBUFLEN];
12944#endif
12945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 if (check_restricted() || check_secure())
12948 return;
12949#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12951 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 if (histype >= 0)
12953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 if (*str != NUL)
12956 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012957 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 return;
12961 }
12962 }
12963#endif
12964}
12965
12966/*
12967 * "histdel()" function
12968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012971 typval_T *argvars UNUSED;
12972 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973{
12974#ifdef FEAT_CMDHIST
12975 int n;
12976 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012979 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12980 if (str == NULL)
12981 n = 0;
12982 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012984 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012985 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 else
12990 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012991 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992 get_tv_string_buf(&argvars[1], buf));
12993 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994#endif
12995}
12996
12997/*
12998 * "histget()" function
12999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004{
13005#ifdef FEAT_CMDHIST
13006 int type;
13007 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13011 if (str == NULL)
13012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 {
13015 type = get_histtype(str);
13016 if (argvars[1].v_type == VAR_UNKNOWN)
13017 idx = get_history_idx(type);
13018 else
13019 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13020 /* -1 on type error */
13021 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
13029/*
13030 * "histnr()" function
13031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036{
13037 int i;
13038
13039#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 char_u *history = get_tv_string_chk(&argvars[0]);
13041
13042 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 if (i >= HIST_CMD && i < HIST_COUNT)
13044 i = get_history_idx(i);
13045 else
13046#endif
13047 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049}
13050
13051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 * "highlightID(name)" function
13053 */
13054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 typval_T *argvars;
13057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060}
13061
13062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013063 * "highlight_exists()" function
13064 */
13065 static void
13066f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013067 typval_T *argvars;
13068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069{
13070 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13071}
13072
13073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 * "hostname()" function
13075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013078 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080{
13081 char_u hostname[256];
13082
13083 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 rettv->v_type = VAR_STRING;
13085 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086}
13087
13088/*
13089 * iconv() function
13090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013093 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095{
13096#ifdef FEAT_MBYTE
13097 char_u buf1[NUMBUFLEN];
13098 char_u buf2[NUMBUFLEN];
13099 char_u *from, *to, *str;
13100 vimconv_T vimconv;
13101#endif
13102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->v_type = VAR_STRING;
13104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105
13106#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 str = get_tv_string(&argvars[0]);
13108 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13109 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 vimconv.vc_type = CONV_NONE;
13111 convert_setup(&vimconv, from, to);
13112
13113 /* If the encodings are equal, no conversion needed. */
13114 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118
13119 convert_setup(&vimconv, NULL, NULL);
13120 vim_free(from);
13121 vim_free(to);
13122#endif
13123}
13124
13125/*
13126 * "indent()" function
13127 */
13128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013129f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 typval_T *argvars;
13131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132{
13133 linenr_T lnum;
13134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140}
13141
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013142/*
13143 * "index()" function
13144 */
13145 static void
13146f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013147 typval_T *argvars;
13148 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013149{
Bram Moolenaar33570922005-01-25 22:26:29 +000013150 list_T *l;
13151 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013152 long idx = 0;
13153 int ic = FALSE;
13154
13155 rettv->vval.v_number = -1;
13156 if (argvars[0].v_type != VAR_LIST)
13157 {
13158 EMSG(_(e_listreq));
13159 return;
13160 }
13161 l = argvars[0].vval.v_list;
13162 if (l != NULL)
13163 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013164 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013167 int error = FALSE;
13168
Bram Moolenaar758711c2005-02-02 23:11:38 +000013169 /* Start at specified item. Use the cached index that list_find()
13170 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013172 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013173 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013174 ic = get_tv_number_chk(&argvars[3], &error);
13175 if (error)
13176 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013177 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013178
Bram Moolenaar758711c2005-02-02 23:11:38 +000013179 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013180 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013181 {
13182 rettv->vval.v_number = idx;
13183 break;
13184 }
13185 }
13186}
13187
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188static int inputsecret_flag = 0;
13189
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013190static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13191
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013193 * This function is used by f_input() and f_inputdialog() functions. The third
13194 * argument to f_input() specifies the type of completion to use at the
13195 * prompt. The third argument to f_inputdialog() specifies the value to return
13196 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 */
13198 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013199get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013202 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 char_u *p = NULL;
13206 int c;
13207 char_u buf[NUMBUFLEN];
13208 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013210 int xp_type = EXPAND_NOTHING;
13211 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215
13216#ifdef NO_CONSOLE_INPUT
13217 /* While starting up, there is no place to enter text. */
13218 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220#endif
13221
13222 cmd_silent = FALSE; /* Want to see the prompt. */
13223 if (prompt != NULL)
13224 {
13225 /* Only the part of the message after the last NL is considered as
13226 * prompt for the command line */
13227 p = vim_strrchr(prompt, '\n');
13228 if (p == NULL)
13229 p = prompt;
13230 else
13231 {
13232 ++p;
13233 c = *p;
13234 *p = NUL;
13235 msg_start();
13236 msg_clr_eos();
13237 msg_puts_attr(prompt, echo_attr);
13238 msg_didout = FALSE;
13239 msg_starthere();
13240 *p = c;
13241 }
13242 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 if (argvars[1].v_type != VAR_UNKNOWN)
13245 {
13246 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13247 if (defstr != NULL)
13248 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013250 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013251 {
13252 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013253 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013254 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013255
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013256 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013257 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013258
Bram Moolenaar4463f292005-09-25 22:20:24 +000013259 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13260 if (xp_name == NULL)
13261 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013262
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013263 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013264
Bram Moolenaar4463f292005-09-25 22:20:24 +000013265 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13266 &xp_arg) == FAIL)
13267 return;
13268 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013269 }
13270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013272 {
13273# ifdef FEAT_EX_EXTRA
13274 int save_ex_normal_busy = ex_normal_busy;
13275 ex_normal_busy = 0;
13276# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013278 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13279 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013280# ifdef FEAT_EX_EXTRA
13281 ex_normal_busy = save_ex_normal_busy;
13282# endif
13283 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013284 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013285 && argvars[1].v_type != VAR_UNKNOWN
13286 && argvars[2].v_type != VAR_UNKNOWN)
13287 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13288 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013289
13290 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 /* since the user typed this, no need to wait for return */
13293 need_wait_return = FALSE;
13294 msg_didout = FALSE;
13295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 cmd_silent = cmd_silent_save;
13297}
13298
13299/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013300 * "input()" function
13301 * Also handles inputsecret() when inputsecret is set.
13302 */
13303 static void
13304f_input(argvars, rettv)
13305 typval_T *argvars;
13306 typval_T *rettv;
13307{
13308 get_user_input(argvars, rettv, FALSE);
13309}
13310
13311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 * "inputdialog()" function
13313 */
13314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013316 typval_T *argvars;
13317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318{
13319#if defined(FEAT_GUI_TEXTDIALOG)
13320 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13321 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13322 {
13323 char_u *message;
13324 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013325 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 message = get_tv_string_chk(&argvars[0]);
13328 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013329 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013330 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 else
13332 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013333 if (message != NULL && defstr != NULL
13334 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013335 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 else
13338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 if (message != NULL && defstr != NULL
13340 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013341 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342 rettv->vval.v_string = vim_strsave(
13343 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 }
13349 else
13350#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013351 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352}
13353
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013354/*
13355 * "inputlist()" function
13356 */
13357 static void
13358f_inputlist(argvars, rettv)
13359 typval_T *argvars;
13360 typval_T *rettv;
13361{
13362 listitem_T *li;
13363 int selected;
13364 int mouse_used;
13365
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013366#ifdef NO_CONSOLE_INPUT
13367 /* While starting up, there is no place to enter text. */
13368 if (no_console_input())
13369 return;
13370#endif
13371 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13372 {
13373 EMSG2(_(e_listarg), "inputlist()");
13374 return;
13375 }
13376
13377 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013378 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013379 lines_left = Rows; /* avoid more prompt */
13380 msg_scroll = TRUE;
13381 msg_clr_eos();
13382
13383 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13384 {
13385 msg_puts(get_tv_string(&li->li_tv));
13386 msg_putchar('\n');
13387 }
13388
13389 /* Ask for choice. */
13390 selected = prompt_for_number(&mouse_used);
13391 if (mouse_used)
13392 selected -= lines_left;
13393
13394 rettv->vval.v_number = selected;
13395}
13396
13397
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13399
13400/*
13401 * "inputrestore()" function
13402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
13408 if (ga_userinput.ga_len > 0)
13409 {
13410 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13412 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013413 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 }
13415 else if (p_verbose > 1)
13416 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013417 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 }
13420}
13421
13422/*
13423 * "inputsave()" function
13424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013430 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 if (ga_grow(&ga_userinput, 1) == OK)
13432 {
13433 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13434 + ga_userinput.ga_len);
13435 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013436 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 }
13438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440}
13441
13442/*
13443 * "inputsecret()" function
13444 */
13445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 typval_T *argvars;
13448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449{
13450 ++cmdline_star;
13451 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 --cmdline_star;
13454 --inputsecret_flag;
13455}
13456
13457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013458 * "insert()" function
13459 */
13460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *argvars;
13463 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013464{
13465 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 listitem_T *item;
13467 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013469
13470 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013471 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013472 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013473 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013474 {
13475 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 before = get_tv_number_chk(&argvars[2], &error);
13477 if (error)
13478 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479
Bram Moolenaar758711c2005-02-02 23:11:38 +000013480 if (before == l->lv_len)
13481 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 else
13483 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013484 item = list_find(l, before);
13485 if (item == NULL)
13486 {
13487 EMSGN(_(e_listidx), before);
13488 l = NULL;
13489 }
13490 }
13491 if (l != NULL)
13492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013493 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013494 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013495 }
13496 }
13497}
13498
13499/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013500 * "invert(expr)" function
13501 */
13502 static void
13503f_invert(argvars, rettv)
13504 typval_T *argvars;
13505 typval_T *rettv;
13506{
13507 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13508}
13509
13510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 * "isdirectory()" function
13512 */
13513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013515 typval_T *argvars;
13516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519}
13520
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013521/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013522 * "islocked()" function
13523 */
13524 static void
13525f_islocked(argvars, rettv)
13526 typval_T *argvars;
13527 typval_T *rettv;
13528{
13529 lval_T lv;
13530 char_u *end;
13531 dictitem_T *di;
13532
13533 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013534 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13535 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013536 if (end != NULL && lv.ll_name != NULL)
13537 {
13538 if (*end != NUL)
13539 EMSG(_(e_trailing));
13540 else
13541 {
13542 if (lv.ll_tv == NULL)
13543 {
13544 if (check_changedtick(lv.ll_name))
13545 rettv->vval.v_number = 1; /* always locked */
13546 else
13547 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013548 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013549 if (di != NULL)
13550 {
13551 /* Consider a variable locked when:
13552 * 1. the variable itself is locked
13553 * 2. the value of the variable is locked.
13554 * 3. the List or Dict value is locked.
13555 */
13556 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13557 || tv_islocked(&di->di_tv));
13558 }
13559 }
13560 }
13561 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013562 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013563 else if (lv.ll_newkey != NULL)
13564 EMSG2(_(e_dictkey), lv.ll_newkey);
13565 else if (lv.ll_list != NULL)
13566 /* List item. */
13567 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13568 else
13569 /* Dictionary item. */
13570 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13571 }
13572 }
13573
13574 clear_lval(&lv);
13575}
13576
Bram Moolenaar33570922005-01-25 22:26:29 +000013577static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013578
13579/*
13580 * Turn a dict into a list:
13581 * "what" == 0: list of keys
13582 * "what" == 1: list of values
13583 * "what" == 2: list of items
13584 */
13585 static void
13586dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013587 typval_T *argvars;
13588 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013589 int what;
13590{
Bram Moolenaar33570922005-01-25 22:26:29 +000013591 list_T *l2;
13592 dictitem_T *di;
13593 hashitem_T *hi;
13594 listitem_T *li;
13595 listitem_T *li2;
13596 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013597 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013598
Bram Moolenaar8c711452005-01-14 21:53:12 +000013599 if (argvars[0].v_type != VAR_DICT)
13600 {
13601 EMSG(_(e_dictreq));
13602 return;
13603 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013604 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013605 return;
13606
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013607 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013608 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013609
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013610 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013613 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013615 --todo;
13616 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013617
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013618 li = listitem_alloc();
13619 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013620 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013621 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013622
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013623 if (what == 0)
13624 {
13625 /* keys() */
13626 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013627 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013628 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13629 }
13630 else if (what == 1)
13631 {
13632 /* values() */
13633 copy_tv(&di->di_tv, &li->li_tv);
13634 }
13635 else
13636 {
13637 /* items() */
13638 l2 = list_alloc();
13639 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013640 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013641 li->li_tv.vval.v_list = l2;
13642 if (l2 == NULL)
13643 break;
13644 ++l2->lv_refcount;
13645
13646 li2 = listitem_alloc();
13647 if (li2 == NULL)
13648 break;
13649 list_append(l2, li2);
13650 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013651 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013652 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13653
13654 li2 = listitem_alloc();
13655 if (li2 == NULL)
13656 break;
13657 list_append(l2, li2);
13658 copy_tv(&di->di_tv, &li2->li_tv);
13659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013660 }
13661 }
13662}
13663
13664/*
13665 * "items(dict)" function
13666 */
13667 static void
13668f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013671{
13672 dict_list(argvars, rettv, 2);
13673}
13674
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013676 * "join()" function
13677 */
13678 static void
13679f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013682{
13683 garray_T ga;
13684 char_u *sep;
13685
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013686 if (argvars[0].v_type != VAR_LIST)
13687 {
13688 EMSG(_(e_listreq));
13689 return;
13690 }
13691 if (argvars[0].vval.v_list == NULL)
13692 return;
13693 if (argvars[1].v_type == VAR_UNKNOWN)
13694 sep = (char_u *)" ";
13695 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013697
13698 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699
13700 if (sep != NULL)
13701 {
13702 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013703 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 ga_append(&ga, NUL);
13705 rettv->vval.v_string = (char_u *)ga.ga_data;
13706 }
13707 else
13708 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013709}
13710
13711/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013712 * "keys()" function
13713 */
13714 static void
13715f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 typval_T *argvars;
13717 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013718{
13719 dict_list(argvars, rettv, 0);
13720}
13721
13722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 * "last_buffer_nr()" function.
13724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729{
13730 int n = 0;
13731 buf_T *buf;
13732
13733 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13734 if (n < buf->b_fnum)
13735 n = buf->b_fnum;
13736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013737 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013738}
13739
13740/*
13741 * "len()" function
13742 */
13743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013745 typval_T *argvars;
13746 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013747{
13748 switch (argvars[0].v_type)
13749 {
13750 case VAR_STRING:
13751 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_number = (varnumber_T)STRLEN(
13753 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013754 break;
13755 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013757 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013758 case VAR_DICT:
13759 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13760 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013761 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013762 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 break;
13764 }
13765}
13766
Bram Moolenaar33570922005-01-25 22:26:29 +000013767static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013768
13769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *argvars;
13772 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013773 int type;
13774{
13775#ifdef FEAT_LIBCALL
13776 char_u *string_in;
13777 char_u **string_result;
13778 int nr_result;
13779#endif
13780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013782 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013784
13785 if (check_restricted() || check_secure())
13786 return;
13787
13788#ifdef FEAT_LIBCALL
13789 /* The first two args must be strings, otherwise its meaningless */
13790 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13791 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013792 string_in = NULL;
13793 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013794 string_in = argvars[2].vval.v_string;
13795 if (type == VAR_NUMBER)
13796 string_result = NULL;
13797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799 if (mch_libcall(argvars[0].vval.v_string,
13800 argvars[1].vval.v_string,
13801 string_in,
13802 argvars[2].vval.v_number,
13803 string_result,
13804 &nr_result) == OK
13805 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013807 }
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809}
13810
13811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812 * "libcall()" function
13813 */
13814 static void
13815f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 typval_T *argvars;
13817 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818{
13819 libcall_common(argvars, rettv, VAR_STRING);
13820}
13821
13822/*
13823 * "libcallnr()" function
13824 */
13825 static void
13826f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 typval_T *argvars;
13828 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013829{
13830 libcall_common(argvars, rettv, VAR_NUMBER);
13831}
13832
13833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 * "line(string)" function
13835 */
13836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013838 typval_T *argvars;
13839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840{
13841 linenr_T lnum = 0;
13842 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013843 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013845 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 if (fp != NULL)
13847 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013848 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849}
13850
13851/*
13852 * "line2byte(lnum)" function
13853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013855f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858{
13859#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861#else
13862 linenr_T lnum;
13863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013864 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13869 if (rettv->vval.v_number >= 0)
13870 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871#endif
13872}
13873
13874/*
13875 * "lispindent(lnum)" function
13876 */
13877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013879 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
13882#ifdef FEAT_LISP
13883 pos_T pos;
13884 linenr_T lnum;
13885
13886 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13889 {
13890 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 curwin->w_cursor = pos;
13893 }
13894 else
13895#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897}
13898
13899/*
13900 * "localtime()" function
13901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013907 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908}
13909
Bram Moolenaar33570922005-01-25 22:26:29 +000013910static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911
13912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013914 typval_T *argvars;
13915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 int exact;
13917{
13918 char_u *keys;
13919 char_u *which;
13920 char_u buf[NUMBUFLEN];
13921 char_u *keys_buf = NULL;
13922 char_u *rhs;
13923 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013924 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013925 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013926 mapblock_T *mp;
13927 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
13929 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930 rettv->v_type = VAR_STRING;
13931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 if (*keys == NUL)
13935 return;
13936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013941 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013942 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013943 if (argvars[3].v_type != VAR_UNKNOWN)
13944 get_dict = get_tv_number(&argvars[3]);
13945 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 else
13948 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 if (which == NULL)
13950 return;
13951
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 mode = get_map_mode(&which, 0);
13953
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013954 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013955 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013957
13958 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013960 /* Return a string. */
13961 if (rhs != NULL)
13962 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963
Bram Moolenaarbd743252010-10-20 21:23:33 +020013964 }
13965 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13966 {
13967 /* Return a dictionary. */
13968 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13969 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13970 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971
Bram Moolenaarbd743252010-10-20 21:23:33 +020013972 dict_add_nr_str(dict, "lhs", 0L, lhs);
13973 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13974 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13975 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13976 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13977 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13978 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013979 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013980 dict_add_nr_str(dict, "mode", 0L, mapmode);
13981
13982 vim_free(lhs);
13983 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 }
13985}
13986
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013987#ifdef FEAT_FLOAT
13988/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013989 * "log()" function
13990 */
13991 static void
13992f_log(argvars, rettv)
13993 typval_T *argvars;
13994 typval_T *rettv;
13995{
13996 float_T f;
13997
13998 rettv->v_type = VAR_FLOAT;
13999 if (get_float_arg(argvars, &f) == OK)
14000 rettv->vval.v_float = log(f);
14001 else
14002 rettv->vval.v_float = 0.0;
14003}
14004
14005/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014006 * "log10()" function
14007 */
14008 static void
14009f_log10(argvars, rettv)
14010 typval_T *argvars;
14011 typval_T *rettv;
14012{
14013 float_T f;
14014
14015 rettv->v_type = VAR_FLOAT;
14016 if (get_float_arg(argvars, &f) == OK)
14017 rettv->vval.v_float = log10(f);
14018 else
14019 rettv->vval.v_float = 0.0;
14020}
14021#endif
14022
Bram Moolenaar1dced572012-04-05 16:54:08 +020014023#ifdef FEAT_LUA
14024/*
14025 * "luaeval()" function
14026 */
14027 static void
14028f_luaeval(argvars, rettv)
14029 typval_T *argvars;
14030 typval_T *rettv;
14031{
14032 char_u *str;
14033 char_u buf[NUMBUFLEN];
14034
14035 str = get_tv_string_buf(&argvars[0], buf);
14036 do_luaeval(str, argvars + 1, rettv);
14037}
14038#endif
14039
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014041 * "map()" function
14042 */
14043 static void
14044f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014045 typval_T *argvars;
14046 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014047{
14048 filter_map(argvars, rettv, TRUE);
14049}
14050
14051/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 */
14054 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014055f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 typval_T *argvars;
14057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060}
14061
14062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 */
14065 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014067 typval_T *argvars;
14068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071}
14072
Bram Moolenaar33570922005-01-25 22:26:29 +000014073static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074
14075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 int type;
14080{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014081 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014082 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014083 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 char_u *pat;
14085 regmatch_T regmatch;
14086 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014087 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 char_u *save_cpo;
14089 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014090 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014091 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014092 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014093 list_T *l = NULL;
14094 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014095 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014096 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
14098 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14099 save_cpo = p_cpo;
14100 p_cpo = (char_u *)"";
14101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014102 rettv->vval.v_number = -1;
14103 if (type == 3)
14104 {
14105 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014107 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108 }
14109 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 rettv->v_type = VAR_STRING;
14112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014115 if (argvars[0].v_type == VAR_LIST)
14116 {
14117 if ((l = argvars[0].vval.v_list) == NULL)
14118 goto theend;
14119 li = l->lv_first;
14120 }
14121 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014122 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014123 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014124 len = (long)STRLEN(str);
14125 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014127 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14128 if (pat == NULL)
14129 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014130
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 int error = FALSE;
14134
14135 start = get_tv_number_chk(&argvars[2], &error);
14136 if (error)
14137 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014138 if (l != NULL)
14139 {
14140 li = list_find(l, start);
14141 if (li == NULL)
14142 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014143 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014144 }
14145 else
14146 {
14147 if (start < 0)
14148 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014149 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014150 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014151 /* When "count" argument is there ignore matches before "start",
14152 * otherwise skip part of the string. Differs when pattern is "^"
14153 * or "\<". */
14154 if (argvars[3].v_type != VAR_UNKNOWN)
14155 startcol = start;
14156 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014157 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014158 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014159 len -= start;
14160 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014161 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014162
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014163 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 nth = get_tv_number_chk(&argvars[3], &error);
14165 if (error)
14166 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 }
14168
14169 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14170 if (regmatch.regprog != NULL)
14171 {
14172 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014173
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014174 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014175 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014176 if (l != NULL)
14177 {
14178 if (li == NULL)
14179 {
14180 match = FALSE;
14181 break;
14182 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014183 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014184 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014185 if (str == NULL)
14186 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014187 }
14188
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014189 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014190
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014191 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014192 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014193 if (l == NULL && !match)
14194 break;
14195
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014196 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197 if (l != NULL)
14198 {
14199 li = li->li_next;
14200 ++idx;
14201 }
14202 else
14203 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014204#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014205 startcol = (colnr_T)(regmatch.startp[0]
14206 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014207#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014208 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014209#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014210 if (startcol > (colnr_T)len
14211 || str + startcol <= regmatch.startp[0])
14212 {
14213 match = FALSE;
14214 break;
14215 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014216 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014217 }
14218
14219 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014221 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014223 int i;
14224
14225 /* return list with matched string and submatches */
14226 for (i = 0; i < NSUBEXP; ++i)
14227 {
14228 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014229 {
14230 if (list_append_string(rettv->vval.v_list,
14231 (char_u *)"", 0) == FAIL)
14232 break;
14233 }
14234 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014235 regmatch.startp[i],
14236 (int)(regmatch.endp[i] - regmatch.startp[i]))
14237 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014238 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014239 }
14240 }
14241 else if (type == 2)
14242 {
14243 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014244 if (l != NULL)
14245 copy_tv(&li->li_tv, rettv);
14246 else
14247 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014249 }
14250 else if (l != NULL)
14251 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 else
14253 {
14254 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 (varnumber_T)(regmatch.startp[0] - str);
14257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014260 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 }
14262 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014263 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 }
14265
14266theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014267 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 p_cpo = save_cpo;
14269}
14270
14271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272 * "match()" function
14273 */
14274 static void
14275f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278{
14279 find_some_match(argvars, rettv, 1);
14280}
14281
14282/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014283 * "matchadd()" function
14284 */
14285 static void
14286f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014287 typval_T *argvars UNUSED;
14288 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014289{
14290#ifdef FEAT_SEARCH_EXTRA
14291 char_u buf[NUMBUFLEN];
14292 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14293 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14294 int prio = 10; /* default priority */
14295 int id = -1;
14296 int error = FALSE;
14297
14298 rettv->vval.v_number = -1;
14299
14300 if (grp == NULL || pat == NULL)
14301 return;
14302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014303 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014304 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014305 if (argvars[3].v_type != VAR_UNKNOWN)
14306 id = get_tv_number_chk(&argvars[3], &error);
14307 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014308 if (error == TRUE)
14309 return;
14310 if (id >= 1 && id <= 3)
14311 {
14312 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14313 return;
14314 }
14315
14316 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14317#endif
14318}
14319
14320/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014321 * "matcharg()" function
14322 */
14323 static void
14324f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014325 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014326 typval_T *rettv;
14327{
14328 if (rettv_list_alloc(rettv) == OK)
14329 {
14330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014331 int id = get_tv_number(&argvars[0]);
14332 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014333
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014334 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014335 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014336 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14337 {
14338 list_append_string(rettv->vval.v_list,
14339 syn_id2name(m->hlg_id), -1);
14340 list_append_string(rettv->vval.v_list, m->pattern, -1);
14341 }
14342 else
14343 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014344 list_append_string(rettv->vval.v_list, NULL, -1);
14345 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014346 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014347 }
14348#endif
14349 }
14350}
14351
14352/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014353 * "matchdelete()" function
14354 */
14355 static void
14356f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014357 typval_T *argvars UNUSED;
14358 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014359{
14360#ifdef FEAT_SEARCH_EXTRA
14361 rettv->vval.v_number = match_delete(curwin,
14362 (int)get_tv_number(&argvars[0]), TRUE);
14363#endif
14364}
14365
14366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 * "matchend()" function
14368 */
14369 static void
14370f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373{
14374 find_some_match(argvars, rettv, 0);
14375}
14376
14377/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014378 * "matchlist()" function
14379 */
14380 static void
14381f_matchlist(argvars, rettv)
14382 typval_T *argvars;
14383 typval_T *rettv;
14384{
14385 find_some_match(argvars, rettv, 3);
14386}
14387
14388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 * "matchstr()" function
14390 */
14391 static void
14392f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395{
14396 find_some_match(argvars, rettv, 2);
14397}
14398
Bram Moolenaar33570922005-01-25 22:26:29 +000014399static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014400
14401 static void
14402max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 typval_T *argvars;
14404 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014405 int domax;
14406{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014407 long n = 0;
14408 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014410
14411 if (argvars[0].v_type == VAR_LIST)
14412 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 list_T *l;
14414 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014415
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014416 l = argvars[0].vval.v_list;
14417 if (l != NULL)
14418 {
14419 li = l->lv_first;
14420 if (li != NULL)
14421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014423 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014424 {
14425 li = li->li_next;
14426 if (li == NULL)
14427 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014429 if (domax ? i > n : i < n)
14430 n = i;
14431 }
14432 }
14433 }
14434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014435 else if (argvars[0].v_type == VAR_DICT)
14436 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014438 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014440 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014441
14442 d = argvars[0].vval.v_dict;
14443 if (d != NULL)
14444 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014445 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014446 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014447 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014448 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014450 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014452 if (first)
14453 {
14454 n = i;
14455 first = FALSE;
14456 }
14457 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014458 n = i;
14459 }
14460 }
14461 }
14462 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014463 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014464 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014466}
14467
14468/*
14469 * "max()" function
14470 */
14471 static void
14472f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 typval_T *argvars;
14474 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014475{
14476 max_min(argvars, rettv, TRUE);
14477}
14478
14479/*
14480 * "min()" function
14481 */
14482 static void
14483f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 typval_T *argvars;
14485 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014486{
14487 max_min(argvars, rettv, FALSE);
14488}
14489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014490static int mkdir_recurse __ARGS((char_u *dir, int prot));
14491
14492/*
14493 * Create the directory in which "dir" is located, and higher levels when
14494 * needed.
14495 */
14496 static int
14497mkdir_recurse(dir, prot)
14498 char_u *dir;
14499 int prot;
14500{
14501 char_u *p;
14502 char_u *updir;
14503 int r = FAIL;
14504
14505 /* Get end of directory name in "dir".
14506 * We're done when it's "/" or "c:/". */
14507 p = gettail_sep(dir);
14508 if (p <= get_past_head(dir))
14509 return OK;
14510
14511 /* If the directory exists we're done. Otherwise: create it.*/
14512 updir = vim_strnsave(dir, (int)(p - dir));
14513 if (updir == NULL)
14514 return FAIL;
14515 if (mch_isdir(updir))
14516 r = OK;
14517 else if (mkdir_recurse(updir, prot) == OK)
14518 r = vim_mkdir_emsg(updir, prot);
14519 vim_free(updir);
14520 return r;
14521}
14522
14523#ifdef vim_mkdir
14524/*
14525 * "mkdir()" function
14526 */
14527 static void
14528f_mkdir(argvars, rettv)
14529 typval_T *argvars;
14530 typval_T *rettv;
14531{
14532 char_u *dir;
14533 char_u buf[NUMBUFLEN];
14534 int prot = 0755;
14535
14536 rettv->vval.v_number = FAIL;
14537 if (check_restricted() || check_secure())
14538 return;
14539
14540 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014541 if (*dir == NUL)
14542 rettv->vval.v_number = FAIL;
14543 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014544 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014545 if (*gettail(dir) == NUL)
14546 /* remove trailing slashes */
14547 *gettail_sep(dir) = NUL;
14548
14549 if (argvars[1].v_type != VAR_UNKNOWN)
14550 {
14551 if (argvars[2].v_type != VAR_UNKNOWN)
14552 prot = get_tv_number_chk(&argvars[2], NULL);
14553 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14554 mkdir_recurse(dir, prot);
14555 }
14556 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014557 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014558}
14559#endif
14560
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 * "mode()" function
14563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014565f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 typval_T *argvars;
14567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014569 char_u buf[3];
14570
14571 buf[1] = NUL;
14572 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 if (VIsual_active)
14575 {
14576 if (VIsual_select)
14577 buf[0] = VIsual_mode + 's' - 'v';
14578 else
14579 buf[0] = VIsual_mode;
14580 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014581 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014582 || State == CONFIRM)
14583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014585 if (State == ASKMORE)
14586 buf[1] = 'm';
14587 else if (State == CONFIRM)
14588 buf[1] = '?';
14589 }
14590 else if (State == EXTERNCMD)
14591 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 else if (State & INSERT)
14593 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014594#ifdef FEAT_VREPLACE
14595 if (State & VREPLACE_FLAG)
14596 {
14597 buf[0] = 'R';
14598 buf[1] = 'v';
14599 }
14600 else
14601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 if (State & REPLACE_FLAG)
14603 buf[0] = 'R';
14604 else
14605 buf[0] = 'i';
14606 }
14607 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014610 if (exmode_active)
14611 buf[1] = 'v';
14612 }
14613 else if (exmode_active)
14614 {
14615 buf[0] = 'c';
14616 buf[1] = 'e';
14617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014621 if (finish_op)
14622 buf[1] = 'o';
14623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014625 /* Clear out the minor mode when the argument is not a non-zero number or
14626 * non-empty string. */
14627 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014628 buf[1] = NUL;
14629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->vval.v_string = vim_strsave(buf);
14631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632}
14633
Bram Moolenaar429fa852013-04-15 12:27:36 +020014634#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014635/*
14636 * "mzeval()" function
14637 */
14638 static void
14639f_mzeval(argvars, rettv)
14640 typval_T *argvars;
14641 typval_T *rettv;
14642{
14643 char_u *str;
14644 char_u buf[NUMBUFLEN];
14645
14646 str = get_tv_string_buf(&argvars[0], buf);
14647 do_mzeval(str, rettv);
14648}
Bram Moolenaar75676462013-01-30 14:55:42 +010014649
14650 void
14651mzscheme_call_vim(name, args, rettv)
14652 char_u *name;
14653 typval_T *args;
14654 typval_T *rettv;
14655{
14656 typval_T argvars[3];
14657
14658 argvars[0].v_type = VAR_STRING;
14659 argvars[0].vval.v_string = name;
14660 copy_tv(args, &argvars[1]);
14661 argvars[2].v_type = VAR_UNKNOWN;
14662 f_call(argvars, rettv);
14663 clear_tv(&argvars[1]);
14664}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014665#endif
14666
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 * "nextnonblank()" function
14669 */
14670 static void
14671f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674{
14675 linenr_T lnum;
14676
14677 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 {
14681 lnum = 0;
14682 break;
14683 }
14684 if (*skipwhite(ml_get(lnum)) != NUL)
14685 break;
14686 }
14687 rettv->vval.v_number = lnum;
14688}
14689
14690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 * "nr2char()" function
14692 */
14693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014694f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014695 typval_T *argvars;
14696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697{
14698 char_u buf[NUMBUFLEN];
14699
14700#ifdef FEAT_MBYTE
14701 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014702 {
14703 int utf8 = 0;
14704
14705 if (argvars[1].v_type != VAR_UNKNOWN)
14706 utf8 = get_tv_number_chk(&argvars[1], NULL);
14707 if (utf8)
14708 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14709 else
14710 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 else
14713#endif
14714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 buf[1] = NUL;
14717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 rettv->v_type = VAR_STRING;
14719 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014720}
14721
14722/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014723 * "or(expr, expr)" function
14724 */
14725 static void
14726f_or(argvars, rettv)
14727 typval_T *argvars;
14728 typval_T *rettv;
14729{
14730 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14731 | get_tv_number_chk(&argvars[1], NULL);
14732}
14733
14734/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014735 * "pathshorten()" function
14736 */
14737 static void
14738f_pathshorten(argvars, rettv)
14739 typval_T *argvars;
14740 typval_T *rettv;
14741{
14742 char_u *p;
14743
14744 rettv->v_type = VAR_STRING;
14745 p = get_tv_string_chk(&argvars[0]);
14746 if (p == NULL)
14747 rettv->vval.v_string = NULL;
14748 else
14749 {
14750 p = vim_strsave(p);
14751 rettv->vval.v_string = p;
14752 if (p != NULL)
14753 shorten_dir(p);
14754 }
14755}
14756
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014757#ifdef FEAT_FLOAT
14758/*
14759 * "pow()" function
14760 */
14761 static void
14762f_pow(argvars, rettv)
14763 typval_T *argvars;
14764 typval_T *rettv;
14765{
14766 float_T fx, fy;
14767
14768 rettv->v_type = VAR_FLOAT;
14769 if (get_float_arg(argvars, &fx) == OK
14770 && get_float_arg(&argvars[1], &fy) == OK)
14771 rettv->vval.v_float = pow(fx, fy);
14772 else
14773 rettv->vval.v_float = 0.0;
14774}
14775#endif
14776
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778 * "prevnonblank()" function
14779 */
14780 static void
14781f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 typval_T *argvars;
14783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014784{
14785 linenr_T lnum;
14786
14787 lnum = get_tv_lnum(argvars);
14788 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14789 lnum = 0;
14790 else
14791 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14792 --lnum;
14793 rettv->vval.v_number = lnum;
14794}
14795
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014796#ifdef HAVE_STDARG_H
14797/* This dummy va_list is here because:
14798 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14799 * - locally in the function results in a "used before set" warning
14800 * - using va_start() to initialize it gives "function with fixed args" error */
14801static va_list ap;
14802#endif
14803
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014805 * "printf()" function
14806 */
14807 static void
14808f_printf(argvars, rettv)
14809 typval_T *argvars;
14810 typval_T *rettv;
14811{
14812 rettv->v_type = VAR_STRING;
14813 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014814#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014815 {
14816 char_u buf[NUMBUFLEN];
14817 int len;
14818 char_u *s;
14819 int saved_did_emsg = did_emsg;
14820 char *fmt;
14821
14822 /* Get the required length, allocate the buffer and do it for real. */
14823 did_emsg = FALSE;
14824 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014825 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014826 if (!did_emsg)
14827 {
14828 s = alloc(len + 1);
14829 if (s != NULL)
14830 {
14831 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014832 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014833 }
14834 }
14835 did_emsg |= saved_did_emsg;
14836 }
14837#endif
14838}
14839
14840/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014841 * "pumvisible()" function
14842 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014843 static void
14844f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014845 typval_T *argvars UNUSED;
14846 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014847{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014848#ifdef FEAT_INS_EXPAND
14849 if (pum_visible())
14850 rettv->vval.v_number = 1;
14851#endif
14852}
14853
Bram Moolenaardb913952012-06-29 12:54:53 +020014854#ifdef FEAT_PYTHON3
14855/*
14856 * "py3eval()" function
14857 */
14858 static void
14859f_py3eval(argvars, rettv)
14860 typval_T *argvars;
14861 typval_T *rettv;
14862{
14863 char_u *str;
14864 char_u buf[NUMBUFLEN];
14865
14866 str = get_tv_string_buf(&argvars[0], buf);
14867 do_py3eval(str, rettv);
14868}
14869#endif
14870
14871#ifdef FEAT_PYTHON
14872/*
14873 * "pyeval()" function
14874 */
14875 static void
14876f_pyeval(argvars, rettv)
14877 typval_T *argvars;
14878 typval_T *rettv;
14879{
14880 char_u *str;
14881 char_u buf[NUMBUFLEN];
14882
14883 str = get_tv_string_buf(&argvars[0], buf);
14884 do_pyeval(str, rettv);
14885}
14886#endif
14887
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014888/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014889 * "range()" function
14890 */
14891 static void
14892f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 typval_T *argvars;
14894 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014895{
14896 long start;
14897 long end;
14898 long stride = 1;
14899 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014900 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014902 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014903 if (argvars[1].v_type == VAR_UNKNOWN)
14904 {
14905 end = start - 1;
14906 start = 0;
14907 }
14908 else
14909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014911 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014913 }
14914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014915 if (error)
14916 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014917 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014918 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014919 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014920 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014921 else
14922 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014923 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014924 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014925 if (list_append_number(rettv->vval.v_list,
14926 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014927 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014928 }
14929}
14930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014931/*
14932 * "readfile()" function
14933 */
14934 static void
14935f_readfile(argvars, rettv)
14936 typval_T *argvars;
14937 typval_T *rettv;
14938{
14939 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014940 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014941 char_u *fname;
14942 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014943 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14944 int io_size = sizeof(buf);
14945 int readlen; /* size of last fread() */
14946 char_u *prev = NULL; /* previously read bytes, if any */
14947 long prevlen = 0; /* length of data in prev */
14948 long prevsize = 0; /* size of prev buffer */
14949 long maxline = MAXLNUM;
14950 long cnt = 0;
14951 char_u *p; /* position in buf */
14952 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014953
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014954 if (argvars[1].v_type != VAR_UNKNOWN)
14955 {
14956 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14957 binary = TRUE;
14958 if (argvars[2].v_type != VAR_UNKNOWN)
14959 maxline = get_tv_number(&argvars[2]);
14960 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014962 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014964
14965 /* Always open the file in binary mode, library functions have a mind of
14966 * their own about CR-LF conversion. */
14967 fname = get_tv_string(&argvars[0]);
14968 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14969 {
14970 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14971 return;
14972 }
14973
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014974 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014977
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014978 /* This for loop processes what was read, but is also entered at end
14979 * of file so that either:
14980 * - an incomplete line gets written
14981 * - a "binary" file gets an empty line at the end if it ends in a
14982 * newline. */
14983 for (p = buf, start = buf;
14984 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14985 ++p)
14986 {
14987 if (*p == '\n' || readlen <= 0)
14988 {
14989 listitem_T *li;
14990 char_u *s = NULL;
14991 long_u len = p - start;
14992
14993 /* Finished a line. Remove CRs before NL. */
14994 if (readlen > 0 && !binary)
14995 {
14996 while (len > 0 && start[len - 1] == '\r')
14997 --len;
14998 /* removal may cross back to the "prev" string */
14999 if (len == 0)
15000 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15001 --prevlen;
15002 }
15003 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015004 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015005 else
15006 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015007 /* Change "prev" buffer to be the right size. This way
15008 * the bytes are only copied once, and very long lines are
15009 * allocated only once. */
15010 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015011 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015012 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015013 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015014 prev = NULL; /* the list will own the string */
15015 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015016 }
15017 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015018 if (s == NULL)
15019 {
15020 do_outofmem_msg((long_u) prevlen + len + 1);
15021 failed = TRUE;
15022 break;
15023 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015024
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015025 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015026 {
15027 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015028 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015029 break;
15030 }
15031 li->li_tv.v_type = VAR_STRING;
15032 li->li_tv.v_lock = 0;
15033 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015034 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015036 start = p + 1; /* step over newline */
15037 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015038 break;
15039 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015040 else if (*p == NUL)
15041 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015042#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015043 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15044 * when finding the BF and check the previous two bytes. */
15045 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015046 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015047 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15048 * + 1, these may be in the "prev" string. */
15049 char_u back1 = p >= buf + 1 ? p[-1]
15050 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15051 char_u back2 = p >= buf + 2 ? p[-2]
15052 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15053 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015054
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015055 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015056 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015057 char_u *dest = p - 2;
15058
15059 /* Usually a BOM is at the beginning of a file, and so at
15060 * the beginning of a line; then we can just step over it.
15061 */
15062 if (start == dest)
15063 start = p + 1;
15064 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015065 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015066 /* have to shuffle buf to close gap */
15067 int adjust_prevlen = 0;
15068
15069 if (dest < buf)
15070 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015071 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015072 dest = buf;
15073 }
15074 if (readlen > p - buf + 1)
15075 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15076 readlen -= 3 - adjust_prevlen;
15077 prevlen -= adjust_prevlen;
15078 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015079 }
15080 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015081 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015082#endif
15083 } /* for */
15084
15085 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15086 break;
15087 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015088 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015089 /* There's part of a line in buf, store it in "prev". */
15090 if (p - start + prevlen >= prevsize)
15091 {
15092 /* need bigger "prev" buffer */
15093 char_u *newprev;
15094
15095 /* A common use case is ordinary text files and "prev" gets a
15096 * fragment of a line, so the first allocation is made
15097 * small, to avoid repeatedly 'allocing' large and
15098 * 'reallocing' small. */
15099 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015100 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015101 else
15102 {
15103 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015104 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015105 prevsize = grow50pc > growmin ? grow50pc : growmin;
15106 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015107 newprev = prev == NULL ? alloc(prevsize)
15108 : vim_realloc(prev, prevsize);
15109 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015110 {
15111 do_outofmem_msg((long_u)prevsize);
15112 failed = TRUE;
15113 break;
15114 }
15115 prev = newprev;
15116 }
15117 /* Add the line part to end of "prev". */
15118 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015119 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015120 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015121 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015122
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015123 /*
15124 * For a negative line count use only the lines at the end of the file,
15125 * free the rest.
15126 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015127 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015128 while (cnt > -maxline)
15129 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015130 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015131 --cnt;
15132 }
15133
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015134 if (failed)
15135 {
15136 list_free(rettv->vval.v_list, TRUE);
15137 /* readfile doc says an empty list is returned on error */
15138 rettv->vval.v_list = list_alloc();
15139 }
15140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015141 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015142 fclose(fd);
15143}
15144
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015145#if defined(FEAT_RELTIME)
15146static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15147
15148/*
15149 * Convert a List to proftime_T.
15150 * Return FAIL when there is something wrong.
15151 */
15152 static int
15153list2proftime(arg, tm)
15154 typval_T *arg;
15155 proftime_T *tm;
15156{
15157 long n1, n2;
15158 int error = FALSE;
15159
15160 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15161 || arg->vval.v_list->lv_len != 2)
15162 return FAIL;
15163 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15164 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15165# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015166 tm->HighPart = n1;
15167 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015168# else
15169 tm->tv_sec = n1;
15170 tm->tv_usec = n2;
15171# endif
15172 return error ? FAIL : OK;
15173}
15174#endif /* FEAT_RELTIME */
15175
15176/*
15177 * "reltime()" function
15178 */
15179 static void
15180f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015181 typval_T *argvars UNUSED;
15182 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015183{
15184#ifdef FEAT_RELTIME
15185 proftime_T res;
15186 proftime_T start;
15187
15188 if (argvars[0].v_type == VAR_UNKNOWN)
15189 {
15190 /* No arguments: get current time. */
15191 profile_start(&res);
15192 }
15193 else if (argvars[1].v_type == VAR_UNKNOWN)
15194 {
15195 if (list2proftime(&argvars[0], &res) == FAIL)
15196 return;
15197 profile_end(&res);
15198 }
15199 else
15200 {
15201 /* Two arguments: compute the difference. */
15202 if (list2proftime(&argvars[0], &start) == FAIL
15203 || list2proftime(&argvars[1], &res) == FAIL)
15204 return;
15205 profile_sub(&res, &start);
15206 }
15207
15208 if (rettv_list_alloc(rettv) == OK)
15209 {
15210 long n1, n2;
15211
15212# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015213 n1 = res.HighPart;
15214 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015215# else
15216 n1 = res.tv_sec;
15217 n2 = res.tv_usec;
15218# endif
15219 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15220 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15221 }
15222#endif
15223}
15224
15225/*
15226 * "reltimestr()" function
15227 */
15228 static void
15229f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015230 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015231 typval_T *rettv;
15232{
15233#ifdef FEAT_RELTIME
15234 proftime_T tm;
15235#endif
15236
15237 rettv->v_type = VAR_STRING;
15238 rettv->vval.v_string = NULL;
15239#ifdef FEAT_RELTIME
15240 if (list2proftime(&argvars[0], &tm) == OK)
15241 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15242#endif
15243}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015244
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15246static void make_connection __ARGS((void));
15247static int check_connection __ARGS((void));
15248
15249 static void
15250make_connection()
15251{
15252 if (X_DISPLAY == NULL
15253# ifdef FEAT_GUI
15254 && !gui.in_use
15255# endif
15256 )
15257 {
15258 x_force_connect = TRUE;
15259 setup_term_clip();
15260 x_force_connect = FALSE;
15261 }
15262}
15263
15264 static int
15265check_connection()
15266{
15267 make_connection();
15268 if (X_DISPLAY == NULL)
15269 {
15270 EMSG(_("E240: No connection to Vim server"));
15271 return FAIL;
15272 }
15273 return OK;
15274}
15275#endif
15276
15277#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015278static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279
15280 static void
15281remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015282 typval_T *argvars;
15283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 int expr;
15285{
15286 char_u *server_name;
15287 char_u *keys;
15288 char_u *r = NULL;
15289 char_u buf[NUMBUFLEN];
15290# ifdef WIN32
15291 HWND w;
15292# else
15293 Window w;
15294# endif
15295
15296 if (check_restricted() || check_secure())
15297 return;
15298
15299# ifdef FEAT_X11
15300 if (check_connection() == FAIL)
15301 return;
15302# endif
15303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 server_name = get_tv_string_chk(&argvars[0]);
15305 if (server_name == NULL)
15306 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307 keys = get_tv_string_buf(&argvars[1], buf);
15308# ifdef WIN32
15309 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15310# else
15311 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15312 < 0)
15313# endif
15314 {
15315 if (r != NULL)
15316 EMSG(r); /* sending worked but evaluation failed */
15317 else
15318 EMSG2(_("E241: Unable to send to %s"), server_name);
15319 return;
15320 }
15321
15322 rettv->vval.v_string = r;
15323
15324 if (argvars[2].v_type != VAR_UNKNOWN)
15325 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015327 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015330 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 v.di_tv.v_type = VAR_STRING;
15332 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 idvar = get_tv_string_chk(&argvars[2]);
15334 if (idvar != NULL)
15335 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015336 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337 }
15338}
15339#endif
15340
15341/*
15342 * "remote_expr()" function
15343 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344 static void
15345f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348{
15349 rettv->v_type = VAR_STRING;
15350 rettv->vval.v_string = NULL;
15351#ifdef FEAT_CLIENTSERVER
15352 remote_common(argvars, rettv, TRUE);
15353#endif
15354}
15355
15356/*
15357 * "remote_foreground()" function
15358 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359 static void
15360f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015361 typval_T *argvars UNUSED;
15362 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015364#ifdef FEAT_CLIENTSERVER
15365# ifdef WIN32
15366 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 {
15368 char_u *server_name = get_tv_string_chk(&argvars[0]);
15369
15370 if (server_name != NULL)
15371 serverForeground(server_name);
15372 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373# else
15374 /* Send a foreground() expression to the server. */
15375 argvars[1].v_type = VAR_STRING;
15376 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15377 argvars[2].v_type = VAR_UNKNOWN;
15378 remote_common(argvars, rettv, TRUE);
15379 vim_free(argvars[1].vval.v_string);
15380# endif
15381#endif
15382}
15383
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 static void
15385f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015386 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388{
15389#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015390 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 char_u *s = NULL;
15392# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015393 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015395 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396
15397 if (check_restricted() || check_secure())
15398 {
15399 rettv->vval.v_number = -1;
15400 return;
15401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015402 serverid = get_tv_string_chk(&argvars[0]);
15403 if (serverid == NULL)
15404 {
15405 rettv->vval.v_number = -1;
15406 return; /* type error; errmsg already given */
15407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015408# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015409 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410 if (n == 0)
15411 rettv->vval.v_number = -1;
15412 else
15413 {
15414 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15415 rettv->vval.v_number = (s != NULL);
15416 }
15417# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015418 if (check_connection() == FAIL)
15419 return;
15420
15421 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015422 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015423# endif
15424
15425 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015427 char_u *retvar;
15428
Bram Moolenaar33570922005-01-25 22:26:29 +000015429 v.di_tv.v_type = VAR_STRING;
15430 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015431 retvar = get_tv_string_chk(&argvars[1]);
15432 if (retvar != NULL)
15433 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015434 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015435 }
15436#else
15437 rettv->vval.v_number = -1;
15438#endif
15439}
15440
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441 static void
15442f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015443 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015445{
15446 char_u *r = NULL;
15447
15448#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015449 char_u *serverid = get_tv_string_chk(&argvars[0]);
15450
15451 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015452 {
15453# ifdef WIN32
15454 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015455 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015456
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015457 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458 if (n != 0)
15459 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15460 if (r == NULL)
15461# else
15462 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015463 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015464# endif
15465 EMSG(_("E277: Unable to read a server reply"));
15466 }
15467#endif
15468 rettv->v_type = VAR_STRING;
15469 rettv->vval.v_string = r;
15470}
15471
15472/*
15473 * "remote_send()" function
15474 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015475 static void
15476f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015479{
15480 rettv->v_type = VAR_STRING;
15481 rettv->vval.v_string = NULL;
15482#ifdef FEAT_CLIENTSERVER
15483 remote_common(argvars, rettv, FALSE);
15484#endif
15485}
15486
15487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015488 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015489 */
15490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *argvars;
15493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015494{
Bram Moolenaar33570922005-01-25 22:26:29 +000015495 list_T *l;
15496 listitem_T *item, *item2;
15497 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015498 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015499 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015500 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015501 dict_T *d;
15502 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015503 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015504
Bram Moolenaar8c711452005-01-14 21:53:12 +000015505 if (argvars[0].v_type == VAR_DICT)
15506 {
15507 if (argvars[2].v_type != VAR_UNKNOWN)
15508 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015509 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015510 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015512 key = get_tv_string_chk(&argvars[1]);
15513 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 di = dict_find(d, key, -1);
15516 if (di == NULL)
15517 EMSG2(_(e_dictkey), key);
15518 else
15519 {
15520 *rettv = di->di_tv;
15521 init_tv(&di->di_tv);
15522 dictitem_remove(d, di);
15523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015525 }
15526 }
15527 else if (argvars[0].v_type != VAR_LIST)
15528 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015529 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015530 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015532 int error = FALSE;
15533
15534 idx = get_tv_number_chk(&argvars[1], &error);
15535 if (error)
15536 ; /* type error: do nothing, errmsg already given */
15537 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015538 EMSGN(_(e_listidx), idx);
15539 else
15540 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015541 if (argvars[2].v_type == VAR_UNKNOWN)
15542 {
15543 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015544 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015545 *rettv = item->li_tv;
15546 vim_free(item);
15547 }
15548 else
15549 {
15550 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015551 end = get_tv_number_chk(&argvars[2], &error);
15552 if (error)
15553 ; /* type error: do nothing */
15554 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015555 EMSGN(_(e_listidx), end);
15556 else
15557 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015558 int cnt = 0;
15559
15560 for (li = item; li != NULL; li = li->li_next)
15561 {
15562 ++cnt;
15563 if (li == item2)
15564 break;
15565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015566 if (li == NULL) /* didn't find "item2" after "item" */
15567 EMSG(_(e_invrange));
15568 else
15569 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015570 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015571 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015572 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015573 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015574 l->lv_first = item;
15575 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015576 item->li_prev = NULL;
15577 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015578 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015579 }
15580 }
15581 }
15582 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015583 }
15584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585}
15586
15587/*
15588 * "rename({from}, {to})" function
15589 */
15590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 typval_T *argvars;
15593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594{
15595 char_u buf[NUMBUFLEN];
15596
15597 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015600 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15601 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602}
15603
15604/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015605 * "repeat()" function
15606 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015607 static void
15608f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015609 typval_T *argvars;
15610 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015611{
15612 char_u *p;
15613 int n;
15614 int slen;
15615 int len;
15616 char_u *r;
15617 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015618
15619 n = get_tv_number(&argvars[1]);
15620 if (argvars[0].v_type == VAR_LIST)
15621 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015622 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015623 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015624 if (list_extend(rettv->vval.v_list,
15625 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015626 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015627 }
15628 else
15629 {
15630 p = get_tv_string(&argvars[0]);
15631 rettv->v_type = VAR_STRING;
15632 rettv->vval.v_string = NULL;
15633
15634 slen = (int)STRLEN(p);
15635 len = slen * n;
15636 if (len <= 0)
15637 return;
15638
15639 r = alloc(len + 1);
15640 if (r != NULL)
15641 {
15642 for (i = 0; i < n; i++)
15643 mch_memmove(r + i * slen, p, (size_t)slen);
15644 r[len] = NUL;
15645 }
15646
15647 rettv->vval.v_string = r;
15648 }
15649}
15650
15651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 * "resolve()" function
15653 */
15654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015655f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 typval_T *argvars;
15657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658{
15659 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015660#ifdef HAVE_READLINK
15661 char_u *buf = NULL;
15662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665#ifdef FEAT_SHORTCUT
15666 {
15667 char_u *v = NULL;
15668
15669 v = mch_resolve_shortcut(p);
15670 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
15675#else
15676# ifdef HAVE_READLINK
15677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 char_u *cpy;
15679 int len;
15680 char_u *remain = NULL;
15681 char_u *q;
15682 int is_relative_to_current = FALSE;
15683 int has_trailing_pathsep = FALSE;
15684 int limit = 100;
15685
15686 p = vim_strsave(p);
15687
15688 if (p[0] == '.' && (vim_ispathsep(p[1])
15689 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15690 is_relative_to_current = TRUE;
15691
15692 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015693 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015696 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
15699 q = getnextcomp(p);
15700 if (*q != NUL)
15701 {
15702 /* Separate the first path component in "p", and keep the
15703 * remainder (beginning with the path separator). */
15704 remain = vim_strsave(q - 1);
15705 q[-1] = NUL;
15706 }
15707
Bram Moolenaard9462e32011-04-11 21:35:11 +020015708 buf = alloc(MAXPATHL + 1);
15709 if (buf == NULL)
15710 goto fail;
15711
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 for (;;)
15713 {
15714 for (;;)
15715 {
15716 len = readlink((char *)p, (char *)buf, MAXPATHL);
15717 if (len <= 0)
15718 break;
15719 buf[len] = NUL;
15720
15721 if (limit-- == 0)
15722 {
15723 vim_free(p);
15724 vim_free(remain);
15725 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 goto fail;
15728 }
15729
15730 /* Ensure that the result will have a trailing path separator
15731 * if the argument has one. */
15732 if (remain == NULL && has_trailing_pathsep)
15733 add_pathsep(buf);
15734
15735 /* Separate the first path component in the link value and
15736 * concatenate the remainders. */
15737 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15738 if (*q != NUL)
15739 {
15740 if (remain == NULL)
15741 remain = vim_strsave(q - 1);
15742 else
15743 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015744 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 if (cpy != NULL)
15746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 vim_free(remain);
15748 remain = cpy;
15749 }
15750 }
15751 q[-1] = NUL;
15752 }
15753
15754 q = gettail(p);
15755 if (q > p && *q == NUL)
15756 {
15757 /* Ignore trailing path separator. */
15758 q[-1] = NUL;
15759 q = gettail(p);
15760 }
15761 if (q > p && !mch_isFullName(buf))
15762 {
15763 /* symlink is relative to directory of argument */
15764 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15765 if (cpy != NULL)
15766 {
15767 STRCPY(cpy, p);
15768 STRCPY(gettail(cpy), buf);
15769 vim_free(p);
15770 p = cpy;
15771 }
15772 }
15773 else
15774 {
15775 vim_free(p);
15776 p = vim_strsave(buf);
15777 }
15778 }
15779
15780 if (remain == NULL)
15781 break;
15782
15783 /* Append the first path component of "remain" to "p". */
15784 q = getnextcomp(remain + 1);
15785 len = q - remain - (*q != NUL);
15786 cpy = vim_strnsave(p, STRLEN(p) + len);
15787 if (cpy != NULL)
15788 {
15789 STRNCAT(cpy, remain, len);
15790 vim_free(p);
15791 p = cpy;
15792 }
15793 /* Shorten "remain". */
15794 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015795 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 else
15797 {
15798 vim_free(remain);
15799 remain = NULL;
15800 }
15801 }
15802
15803 /* If the result is a relative path name, make it explicitly relative to
15804 * the current directory if and only if the argument had this form. */
15805 if (!vim_ispathsep(*p))
15806 {
15807 if (is_relative_to_current
15808 && *p != NUL
15809 && !(p[0] == '.'
15810 && (p[1] == NUL
15811 || vim_ispathsep(p[1])
15812 || (p[1] == '.'
15813 && (p[2] == NUL
15814 || vim_ispathsep(p[2]))))))
15815 {
15816 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015817 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 if (cpy != NULL)
15819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 vim_free(p);
15821 p = cpy;
15822 }
15823 }
15824 else if (!is_relative_to_current)
15825 {
15826 /* Strip leading "./". */
15827 q = p;
15828 while (q[0] == '.' && vim_ispathsep(q[1]))
15829 q += 2;
15830 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015831 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 }
15833 }
15834
15835 /* Ensure that the result will have no trailing path separator
15836 * if the argument had none. But keep "/" or "//". */
15837 if (!has_trailing_pathsep)
15838 {
15839 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015840 if (after_pathsep(p, q))
15841 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 }
15843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 }
15846# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848# endif
15849#endif
15850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015851 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852
15853#ifdef HAVE_READLINK
15854fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015855 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015857 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858}
15859
15860/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 */
15863 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 typval_T *argvars;
15866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867{
Bram Moolenaar33570922005-01-25 22:26:29 +000015868 list_T *l;
15869 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870
Bram Moolenaar0d660222005-01-07 21:51:51 +000015871 if (argvars[0].v_type != VAR_LIST)
15872 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015873 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015874 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015875 {
15876 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015877 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015878 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879 while (li != NULL)
15880 {
15881 ni = li->li_prev;
15882 list_append(l, li);
15883 li = ni;
15884 }
15885 rettv->vval.v_list = l;
15886 rettv->v_type = VAR_LIST;
15887 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015888 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890}
15891
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015892#define SP_NOMOVE 0x01 /* don't move cursor */
15893#define SP_REPEAT 0x02 /* repeat to find outer pair */
15894#define SP_RETCOUNT 0x04 /* return matchcount */
15895#define SP_SETPCMARK 0x08 /* set previous context mark */
15896#define SP_START 0x10 /* accept match at start position */
15897#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15898#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015899
Bram Moolenaar33570922005-01-25 22:26:29 +000015900static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901
15902/*
15903 * Get flags for a search function.
15904 * Possibly sets "p_ws".
15905 * Returns BACKWARD, FORWARD or zero (for an error).
15906 */
15907 static int
15908get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015910 int *flagsp;
15911{
15912 int dir = FORWARD;
15913 char_u *flags;
15914 char_u nbuf[NUMBUFLEN];
15915 int mask;
15916
15917 if (varp->v_type != VAR_UNKNOWN)
15918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 flags = get_tv_string_buf_chk(varp, nbuf);
15920 if (flags == NULL)
15921 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 while (*flags != NUL)
15923 {
15924 switch (*flags)
15925 {
15926 case 'b': dir = BACKWARD; break;
15927 case 'w': p_ws = TRUE; break;
15928 case 'W': p_ws = FALSE; break;
15929 default: mask = 0;
15930 if (flagsp != NULL)
15931 switch (*flags)
15932 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015933 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015934 case 'e': mask = SP_END; break;
15935 case 'm': mask = SP_RETCOUNT; break;
15936 case 'n': mask = SP_NOMOVE; break;
15937 case 'p': mask = SP_SUBPAT; break;
15938 case 'r': mask = SP_REPEAT; break;
15939 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 }
15941 if (mask == 0)
15942 {
15943 EMSG2(_(e_invarg2), flags);
15944 dir = 0;
15945 }
15946 else
15947 *flagsp |= mask;
15948 }
15949 if (dir == 0)
15950 break;
15951 ++flags;
15952 }
15953 }
15954 return dir;
15955}
15956
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015961search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015962 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015964 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015966 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 char_u *pat;
15968 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015969 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 int save_p_ws = p_ws;
15971 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015972 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015973 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015974 proftime_T tm;
15975#ifdef FEAT_RELTIME
15976 long time_limit = 0;
15977#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015978 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015979 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015981 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015982 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015983 if (dir == 0)
15984 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015985 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015986 if (flags & SP_START)
15987 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015988 if (flags & SP_END)
15989 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015990
Bram Moolenaar76929292008-01-06 19:07:36 +000015991 /* Optional arguments: line number to stop searching and timeout. */
15992 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015993 {
15994 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15995 if (lnum_stop < 0)
15996 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015997#ifdef FEAT_RELTIME
15998 if (argvars[3].v_type != VAR_UNKNOWN)
15999 {
16000 time_limit = get_tv_number_chk(&argvars[3], NULL);
16001 if (time_limit < 0)
16002 goto theend;
16003 }
16004#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016005 }
16006
Bram Moolenaar76929292008-01-06 19:07:36 +000016007#ifdef FEAT_RELTIME
16008 /* Set the time limit, if there is one. */
16009 profile_setlimit(time_limit, &tm);
16010#endif
16011
Bram Moolenaar231334e2005-07-25 20:46:57 +000016012 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016013 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016014 * Check to make sure only those flags are set.
16015 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16016 * flags cannot be set. Check for that condition also.
16017 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016018 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016019 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016022 goto theend;
16023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016025 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016026 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016027 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016028 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016030 if (flags & SP_SUBPAT)
16031 retval = subpatnum;
16032 else
16033 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016034 if (flags & SP_SETPCMARK)
16035 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016037 if (match_pos != NULL)
16038 {
16039 /* Store the match cursor position */
16040 match_pos->lnum = pos.lnum;
16041 match_pos->col = pos.col + 1;
16042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 /* "/$" will put the cursor after the end of the line, may need to
16044 * correct that here */
16045 check_cursor();
16046 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016047
16048 /* If 'n' flag is used: restore cursor position. */
16049 if (flags & SP_NOMOVE)
16050 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016051 else
16052 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016053theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016055
16056 return retval;
16057}
16058
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016059#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016060
16061/*
16062 * round() is not in C90, use ceil() or floor() instead.
16063 */
16064 float_T
16065vim_round(f)
16066 float_T f;
16067{
16068 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16069}
16070
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016071/*
16072 * "round({float})" function
16073 */
16074 static void
16075f_round(argvars, rettv)
16076 typval_T *argvars;
16077 typval_T *rettv;
16078{
16079 float_T f;
16080
16081 rettv->v_type = VAR_FLOAT;
16082 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016083 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016084 else
16085 rettv->vval.v_float = 0.0;
16086}
16087#endif
16088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016089/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016090 * "screenattr()" function
16091 */
16092 static void
16093f_screenattr(argvars, rettv)
16094 typval_T *argvars UNUSED;
16095 typval_T *rettv;
16096{
16097 int row;
16098 int col;
16099 int c;
16100
16101 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16102 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16103 if (row < 0 || row >= screen_Rows
16104 || col < 0 || col >= screen_Columns)
16105 c = -1;
16106 else
16107 c = ScreenAttrs[LineOffset[row] + col];
16108 rettv->vval.v_number = c;
16109}
16110
16111/*
16112 * "screenchar()" function
16113 */
16114 static void
16115f_screenchar(argvars, rettv)
16116 typval_T *argvars UNUSED;
16117 typval_T *rettv;
16118{
16119 int row;
16120 int col;
16121 int off;
16122 int c;
16123
16124 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16125 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16126 if (row < 0 || row >= screen_Rows
16127 || col < 0 || col >= screen_Columns)
16128 c = -1;
16129 else
16130 {
16131 off = LineOffset[row] + col;
16132#ifdef FEAT_MBYTE
16133 if (enc_utf8 && ScreenLinesUC[off] != 0)
16134 c = ScreenLinesUC[off];
16135 else
16136#endif
16137 c = ScreenLines[off];
16138 }
16139 rettv->vval.v_number = c;
16140}
16141
16142/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016143 * "screencol()" function
16144 *
16145 * First column is 1 to be consistent with virtcol().
16146 */
16147 static void
16148f_screencol(argvars, rettv)
16149 typval_T *argvars UNUSED;
16150 typval_T *rettv;
16151{
16152 rettv->vval.v_number = screen_screencol() + 1;
16153}
16154
16155/*
16156 * "screenrow()" function
16157 */
16158 static void
16159f_screenrow(argvars, rettv)
16160 typval_T *argvars UNUSED;
16161 typval_T *rettv;
16162{
16163 rettv->vval.v_number = screen_screenrow() + 1;
16164}
16165
16166/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016167 * "search()" function
16168 */
16169 static void
16170f_search(argvars, rettv)
16171 typval_T *argvars;
16172 typval_T *rettv;
16173{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016174 int flags = 0;
16175
16176 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177}
16178
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016180 * "searchdecl()" function
16181 */
16182 static void
16183f_searchdecl(argvars, rettv)
16184 typval_T *argvars;
16185 typval_T *rettv;
16186{
16187 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016188 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016189 int error = FALSE;
16190 char_u *name;
16191
16192 rettv->vval.v_number = 1; /* default: FAIL */
16193
16194 name = get_tv_string_chk(&argvars[0]);
16195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016196 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016197 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016198 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16199 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16200 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016201 if (!error && name != NULL)
16202 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016203 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016204}
16205
16206/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016207 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016209 static int
16210searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016212 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213{
16214 char_u *spat, *mpat, *epat;
16215 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 int dir;
16218 int flags = 0;
16219 char_u nbuf1[NUMBUFLEN];
16220 char_u nbuf2[NUMBUFLEN];
16221 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016222 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016223 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016224 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 spat = get_tv_string_chk(&argvars[0]);
16228 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16229 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16230 if (spat == NULL || mpat == NULL || epat == NULL)
16231 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 /* Handle the optional fourth argument: flags */
16234 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016235 if (dir == 0)
16236 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016237
16238 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016239 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16240 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016241 if ((flags & (SP_END | SP_SUBPAT)) != 0
16242 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016243 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016244 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016245 goto theend;
16246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016248 /* Using 'r' implies 'W', otherwise it doesn't work. */
16249 if (flags & SP_REPEAT)
16250 p_ws = FALSE;
16251
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016252 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016253 if (argvars[3].v_type == VAR_UNKNOWN
16254 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 skip = (char_u *)"";
16256 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016259 if (argvars[5].v_type != VAR_UNKNOWN)
16260 {
16261 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16262 if (lnum_stop < 0)
16263 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016264#ifdef FEAT_RELTIME
16265 if (argvars[6].v_type != VAR_UNKNOWN)
16266 {
16267 time_limit = get_tv_number_chk(&argvars[6], NULL);
16268 if (time_limit < 0)
16269 goto theend;
16270 }
16271#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016272 }
16273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 if (skip == NULL)
16275 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016277 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016278 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016279
16280theend:
16281 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016282
16283 return retval;
16284}
16285
16286/*
16287 * "searchpair()" function
16288 */
16289 static void
16290f_searchpair(argvars, rettv)
16291 typval_T *argvars;
16292 typval_T *rettv;
16293{
16294 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16295}
16296
16297/*
16298 * "searchpairpos()" function
16299 */
16300 static void
16301f_searchpairpos(argvars, rettv)
16302 typval_T *argvars;
16303 typval_T *rettv;
16304{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016305 pos_T match_pos;
16306 int lnum = 0;
16307 int col = 0;
16308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016309 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016310 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016311
16312 if (searchpair_cmn(argvars, &match_pos) > 0)
16313 {
16314 lnum = match_pos.lnum;
16315 col = match_pos.col;
16316 }
16317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016318 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16319 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016320}
16321
16322/*
16323 * Search for a start/middle/end thing.
16324 * Used by searchpair(), see its documentation for the details.
16325 * Returns 0 or -1 for no match,
16326 */
16327 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016328do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16329 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016330 char_u *spat; /* start pattern */
16331 char_u *mpat; /* middle pattern */
16332 char_u *epat; /* end pattern */
16333 int dir; /* BACKWARD or FORWARD */
16334 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016335 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016336 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016337 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016338 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016339{
16340 char_u *save_cpo;
16341 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16342 long retval = 0;
16343 pos_T pos;
16344 pos_T firstpos;
16345 pos_T foundpos;
16346 pos_T save_cursor;
16347 pos_T save_pos;
16348 int n;
16349 int r;
16350 int nest = 1;
16351 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016352 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016353 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016354
16355 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16356 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016357 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016358
Bram Moolenaar76929292008-01-06 19:07:36 +000016359#ifdef FEAT_RELTIME
16360 /* Set the time limit, if there is one. */
16361 profile_setlimit(time_limit, &tm);
16362#endif
16363
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016364 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16365 * start/middle/end (pat3, for the top pair). */
16366 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16367 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16368 if (pat2 == NULL || pat3 == NULL)
16369 goto theend;
16370 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16371 if (*mpat == NUL)
16372 STRCPY(pat3, pat2);
16373 else
16374 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16375 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016376 if (flags & SP_START)
16377 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016378
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 save_cursor = curwin->w_cursor;
16380 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016381 clearpos(&firstpos);
16382 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 pat = pat3;
16384 for (;;)
16385 {
16386 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016387 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16389 /* didn't find it or found the first match again: FAIL */
16390 break;
16391
16392 if (firstpos.lnum == 0)
16393 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016394 if (equalpos(pos, foundpos))
16395 {
16396 /* Found the same position again. Can happen with a pattern that
16397 * has "\zs" at the end and searching backwards. Advance one
16398 * character and try again. */
16399 if (dir == BACKWARD)
16400 decl(&pos);
16401 else
16402 incl(&pos);
16403 }
16404 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016406 /* clear the start flag to avoid getting stuck here */
16407 options &= ~SEARCH_START;
16408
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 /* If the skip pattern matches, ignore this match. */
16410 if (*skip != NUL)
16411 {
16412 save_pos = curwin->w_cursor;
16413 curwin->w_cursor = pos;
16414 r = eval_to_bool(skip, &err, NULL, FALSE);
16415 curwin->w_cursor = save_pos;
16416 if (err)
16417 {
16418 /* Evaluating {skip} caused an error, break here. */
16419 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016420 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 break;
16422 }
16423 if (r)
16424 continue;
16425 }
16426
16427 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16428 {
16429 /* Found end when searching backwards or start when searching
16430 * forward: nested pair. */
16431 ++nest;
16432 pat = pat2; /* nested, don't search for middle */
16433 }
16434 else
16435 {
16436 /* Found end when searching forward or start when searching
16437 * backward: end of (nested) pair; or found middle in outer pair. */
16438 if (--nest == 1)
16439 pat = pat3; /* outer level, search for middle */
16440 }
16441
16442 if (nest == 0)
16443 {
16444 /* Found the match: return matchcount or line number. */
16445 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016446 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016448 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016449 if (flags & SP_SETPCMARK)
16450 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 curwin->w_cursor = pos;
16452 if (!(flags & SP_REPEAT))
16453 break;
16454 nest = 1; /* search for next unmatched */
16455 }
16456 }
16457
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016458 if (match_pos != NULL)
16459 {
16460 /* Store the match cursor position */
16461 match_pos->lnum = curwin->w_cursor.lnum;
16462 match_pos->col = curwin->w_cursor.col + 1;
16463 }
16464
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016466 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 curwin->w_cursor = save_cursor;
16468
16469theend:
16470 vim_free(pat2);
16471 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016472 if (p_cpo == empty_option)
16473 p_cpo = save_cpo;
16474 else
16475 /* Darn, evaluating the {skip} expression changed the value. */
16476 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016477
16478 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479}
16480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016481/*
16482 * "searchpos()" function
16483 */
16484 static void
16485f_searchpos(argvars, rettv)
16486 typval_T *argvars;
16487 typval_T *rettv;
16488{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016489 pos_T match_pos;
16490 int lnum = 0;
16491 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016492 int n;
16493 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016495 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016496 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016497
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016498 n = search_cmn(argvars, &match_pos, &flags);
16499 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016500 {
16501 lnum = match_pos.lnum;
16502 col = match_pos.col;
16503 }
16504
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016505 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16506 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016507 if (flags & SP_SUBPAT)
16508 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016509}
16510
16511
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 static void
16513f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016514 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517#ifdef FEAT_CLIENTSERVER
16518 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 char_u *server = get_tv_string_chk(&argvars[0]);
16520 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016523 if (server == NULL || reply == NULL)
16524 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 if (check_restricted() || check_secure())
16526 return;
16527# ifdef FEAT_X11
16528 if (check_connection() == FAIL)
16529 return;
16530# endif
16531
16532 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534 EMSG(_("E258: Unable to send to client"));
16535 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537 rettv->vval.v_number = 0;
16538#else
16539 rettv->vval.v_number = -1;
16540#endif
16541}
16542
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 static void
16544f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016545 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016546 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547{
16548 char_u *r = NULL;
16549
16550#ifdef FEAT_CLIENTSERVER
16551# ifdef WIN32
16552 r = serverGetVimNames();
16553# else
16554 make_connection();
16555 if (X_DISPLAY != NULL)
16556 r = serverGetVimNames(X_DISPLAY);
16557# endif
16558#endif
16559 rettv->v_type = VAR_STRING;
16560 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561}
16562
16563/*
16564 * "setbufvar()" function
16565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016567f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016569 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570{
16571 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016574 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 char_u nbuf[NUMBUFLEN];
16576
16577 if (check_restricted() || check_secure())
16578 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16580 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016581 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 varp = &argvars[2];
16583
16584 if (buf != NULL && varname != NULL && varp != NULL)
16585 {
16586 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
16589 if (*varname == '&')
16590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016591 long numval;
16592 char_u *strval;
16593 int error = FALSE;
16594
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016596 numval = get_tv_number_chk(varp, &error);
16597 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 if (!error && strval != NULL)
16599 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 }
16601 else
16602 {
16603 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16604 if (bufvarname != NULL)
16605 {
16606 STRCPY(bufvarname, "b:");
16607 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016608 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 vim_free(bufvarname);
16610 }
16611 }
16612
16613 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616}
16617
16618/*
16619 * "setcmdpos()" function
16620 */
16621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 typval_T *argvars;
16624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016626 int pos = (int)get_tv_number(&argvars[0]) - 1;
16627
16628 if (pos >= 0)
16629 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630}
16631
16632/*
16633 * "setline()" function
16634 */
16635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 typval_T *argvars;
16638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639{
16640 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016641 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016642 list_T *l = NULL;
16643 listitem_T *li = NULL;
16644 long added = 0;
16645 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016647 lnum = get_tv_lnum(&argvars[0]);
16648 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016650 l = argvars[1].vval.v_list;
16651 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016653 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016654 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016655
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016656 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016657 for (;;)
16658 {
16659 if (l != NULL)
16660 {
16661 /* list argument, get next string */
16662 if (li == NULL)
16663 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016664 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016665 li = li->li_next;
16666 }
16667
16668 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016670 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016671
16672 /* When coming here from Insert mode, sync undo, so that this can be
16673 * undone separately from what was previously inserted. */
16674 if (u_sync_once == 2)
16675 {
16676 u_sync_once = 1; /* notify that u_sync() was called */
16677 u_sync(TRUE);
16678 }
16679
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016680 if (lnum <= curbuf->b_ml.ml_line_count)
16681 {
16682 /* existing line, replace it */
16683 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16684 {
16685 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016686 if (lnum == curwin->w_cursor.lnum)
16687 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016688 rettv->vval.v_number = 0; /* OK */
16689 }
16690 }
16691 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16692 {
16693 /* lnum is one past the last line, append the line */
16694 ++added;
16695 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16696 rettv->vval.v_number = 0; /* OK */
16697 }
16698
16699 if (l == NULL) /* only one string argument */
16700 break;
16701 ++lnum;
16702 }
16703
16704 if (added > 0)
16705 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706}
16707
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016708static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16709
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016711 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016712 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016713 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016714set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016715 win_T *wp UNUSED;
16716 typval_T *list_arg UNUSED;
16717 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016718 typval_T *rettv;
16719{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016720#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016721 char_u *act;
16722 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016723#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016724
Bram Moolenaar2641f772005-03-25 21:58:17 +000016725 rettv->vval.v_number = -1;
16726
16727#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016728 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016729 EMSG(_(e_listreq));
16730 else
16731 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016732 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016733
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016734 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016735 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016736 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 if (act == NULL)
16738 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016739 if (*act == 'a' || *act == 'r')
16740 action = *act;
16741 }
16742
Bram Moolenaar81484f42012-12-05 15:16:47 +010016743 if (l != NULL && set_errorlist(wp, l, action,
16744 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016745 rettv->vval.v_number = 0;
16746 }
16747#endif
16748}
16749
16750/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016751 * "setloclist()" function
16752 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016753 static void
16754f_setloclist(argvars, rettv)
16755 typval_T *argvars;
16756 typval_T *rettv;
16757{
16758 win_T *win;
16759
16760 rettv->vval.v_number = -1;
16761
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016762 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016763 if (win != NULL)
16764 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16765}
16766
16767/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016768 * "setmatches()" function
16769 */
16770 static void
16771f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016772 typval_T *argvars UNUSED;
16773 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016774{
16775#ifdef FEAT_SEARCH_EXTRA
16776 list_T *l;
16777 listitem_T *li;
16778 dict_T *d;
16779
16780 rettv->vval.v_number = -1;
16781 if (argvars[0].v_type != VAR_LIST)
16782 {
16783 EMSG(_(e_listreq));
16784 return;
16785 }
16786 if ((l = argvars[0].vval.v_list) != NULL)
16787 {
16788
16789 /* To some extent make sure that we are dealing with a list from
16790 * "getmatches()". */
16791 li = l->lv_first;
16792 while (li != NULL)
16793 {
16794 if (li->li_tv.v_type != VAR_DICT
16795 || (d = li->li_tv.vval.v_dict) == NULL)
16796 {
16797 EMSG(_(e_invarg));
16798 return;
16799 }
16800 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16801 && dict_find(d, (char_u *)"pattern", -1) != NULL
16802 && dict_find(d, (char_u *)"priority", -1) != NULL
16803 && dict_find(d, (char_u *)"id", -1) != NULL))
16804 {
16805 EMSG(_(e_invarg));
16806 return;
16807 }
16808 li = li->li_next;
16809 }
16810
16811 clear_matches(curwin);
16812 li = l->lv_first;
16813 while (li != NULL)
16814 {
16815 d = li->li_tv.vval.v_dict;
16816 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16817 get_dict_string(d, (char_u *)"pattern", FALSE),
16818 (int)get_dict_number(d, (char_u *)"priority"),
16819 (int)get_dict_number(d, (char_u *)"id"));
16820 li = li->li_next;
16821 }
16822 rettv->vval.v_number = 0;
16823 }
16824#endif
16825}
16826
16827/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016828 * "setpos()" function
16829 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016830 static void
16831f_setpos(argvars, rettv)
16832 typval_T *argvars;
16833 typval_T *rettv;
16834{
16835 pos_T pos;
16836 int fnum;
16837 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016838 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016839
Bram Moolenaar08250432008-02-13 11:42:46 +000016840 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016841 name = get_tv_string_chk(argvars);
16842 if (name != NULL)
16843 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016844 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016845 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016846 if (--pos.col < 0)
16847 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016848 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016849 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016850 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016851 if (fnum == curbuf->b_fnum)
16852 {
16853 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016854 if (curswant >= 0)
16855 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016856 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016857 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016858 }
16859 else
16860 EMSG(_(e_invarg));
16861 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016862 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16863 {
16864 /* set mark */
16865 if (setmark_pos(name[1], &pos, fnum) == OK)
16866 rettv->vval.v_number = 0;
16867 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016868 else
16869 EMSG(_(e_invarg));
16870 }
16871 }
16872}
16873
16874/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016875 * "setqflist()" function
16876 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016877 static void
16878f_setqflist(argvars, rettv)
16879 typval_T *argvars;
16880 typval_T *rettv;
16881{
16882 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16883}
16884
16885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 * "setreg()" function
16887 */
16888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 typval_T *argvars;
16891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892{
16893 int regname;
16894 char_u *strregname;
16895 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 int append;
16898 char_u yank_type;
16899 long block_len;
16900
16901 block_len = -1;
16902 yank_type = MAUTO;
16903 append = FALSE;
16904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 if (strregname == NULL)
16909 return; /* type error; errmsg already given */
16910 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 if (regname == 0 || regname == '@')
16912 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016914 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 stropt = get_tv_string_chk(&argvars[2]);
16917 if (stropt == NULL)
16918 return; /* type error */
16919 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 switch (*stropt)
16921 {
16922 case 'a': case 'A': /* append */
16923 append = TRUE;
16924 break;
16925 case 'v': case 'c': /* character-wise selection */
16926 yank_type = MCHAR;
16927 break;
16928 case 'V': case 'l': /* line-wise selection */
16929 yank_type = MLINE;
16930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 case 'b': case Ctrl_V: /* block-wise selection */
16932 yank_type = MBLOCK;
16933 if (VIM_ISDIGIT(stropt[1]))
16934 {
16935 ++stropt;
16936 block_len = getdigits(&stropt) - 1;
16937 --stropt;
16938 }
16939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 }
16941 }
16942
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016943 if (argvars[1].v_type == VAR_LIST)
16944 {
16945 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016946 char_u **allocval;
16947 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016948 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016949 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016950 int len = argvars[1].vval.v_list->lv_len;
16951 listitem_T *li;
16952
Bram Moolenaar7d647822014-04-05 21:28:56 +020016953 /* First half: use for pointers to result lines; second half: use for
16954 * pointers to allocated copies. */
16955 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016956 if (lstval == NULL)
16957 return;
16958 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016959 allocval = lstval + len + 2;
16960 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016961
16962 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16963 li = li->li_next)
16964 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016965 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016966 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016967 goto free_lstval;
16968 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016969 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016970 /* Need to make a copy, next get_tv_string_buf_chk() will
16971 * overwrite the string. */
16972 strval = vim_strsave(buf);
16973 if (strval == NULL)
16974 goto free_lstval;
16975 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016976 }
16977 *curval++ = strval;
16978 }
16979 *curval++ = NULL;
16980
16981 write_reg_contents_lst(regname, lstval, -1,
16982 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016983free_lstval:
16984 while (curallocval > allocval)
16985 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016986 vim_free(lstval);
16987 }
16988 else
16989 {
16990 strval = get_tv_string_chk(&argvars[1]);
16991 if (strval == NULL)
16992 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997}
16998
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016999/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017000 * "settabvar()" function
17001 */
17002 static void
17003f_settabvar(argvars, rettv)
17004 typval_T *argvars;
17005 typval_T *rettv;
17006{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017007#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017008 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017009 tabpage_T *tp;
17010#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017011 char_u *varname, *tabvarname;
17012 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017013
17014 rettv->vval.v_number = 0;
17015
17016 if (check_restricted() || check_secure())
17017 return;
17018
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017019#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017020 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017021#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017022 varname = get_tv_string_chk(&argvars[1]);
17023 varp = &argvars[2];
17024
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017025 if (varname != NULL && varp != NULL
17026#ifdef FEAT_WINDOWS
17027 && tp != NULL
17028#endif
17029 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017030 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017031#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017032 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017033 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017034#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017035
17036 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17037 if (tabvarname != NULL)
17038 {
17039 STRCPY(tabvarname, "t:");
17040 STRCPY(tabvarname + 2, varname);
17041 set_var(tabvarname, varp, TRUE);
17042 vim_free(tabvarname);
17043 }
17044
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017045#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017046 /* Restore current tabpage */
17047 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017048 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017049#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017050 }
17051}
17052
17053/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017054 * "settabwinvar()" function
17055 */
17056 static void
17057f_settabwinvar(argvars, rettv)
17058 typval_T *argvars;
17059 typval_T *rettv;
17060{
17061 setwinvar(argvars, rettv, 1);
17062}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063
17064/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017065 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017068f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 typval_T *argvars;
17070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017072 setwinvar(argvars, rettv, 0);
17073}
17074
17075/*
17076 * "setwinvar()" and "settabwinvar()" functions
17077 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017078
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017079 static void
17080setwinvar(argvars, rettv, off)
17081 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017082 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017083 int off;
17084{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 win_T *win;
17086#ifdef FEAT_WINDOWS
17087 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017088 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089#endif
17090 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017093 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094
17095 if (check_restricted() || check_secure())
17096 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017097
17098#ifdef FEAT_WINDOWS
17099 if (off == 1)
17100 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17101 else
17102 tp = curtab;
17103#endif
17104 win = find_win_by_nr(&argvars[off], tp);
17105 varname = get_tv_string_chk(&argvars[off + 1]);
17106 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
17108 if (win != NULL && varname != NULL && varp != NULL)
17109 {
17110#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017111 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017112 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113#endif
17114
17115 if (*varname == '&')
17116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 long numval;
17118 char_u *strval;
17119 int error = FALSE;
17120
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 numval = get_tv_number_chk(varp, &error);
17123 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017124 if (!error && strval != NULL)
17125 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 }
17127 else
17128 {
17129 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17130 if (winvarname != NULL)
17131 {
17132 STRCPY(winvarname, "w:");
17133 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017134 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 vim_free(winvarname);
17136 }
17137 }
17138
17139#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017140 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141#endif
17142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143}
17144
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017145#ifdef FEAT_CRYPT
17146/*
17147 * "sha256({string})" function
17148 */
17149 static void
17150f_sha256(argvars, rettv)
17151 typval_T *argvars;
17152 typval_T *rettv;
17153{
17154 char_u *p;
17155
17156 p = get_tv_string(&argvars[0]);
17157 rettv->vval.v_string = vim_strsave(
17158 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17159 rettv->v_type = VAR_STRING;
17160}
17161#endif /* FEAT_CRYPT */
17162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017164 * "shellescape({string})" function
17165 */
17166 static void
17167f_shellescape(argvars, rettv)
17168 typval_T *argvars;
17169 typval_T *rettv;
17170{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017171 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017172 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017173 rettv->v_type = VAR_STRING;
17174}
17175
17176/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017177 * shiftwidth() function
17178 */
17179 static void
17180f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017181 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017182 typval_T *rettv;
17183{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017184 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017185}
17186
17187/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 */
17190 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 typval_T *argvars;
17193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 p = get_tv_string(&argvars[0]);
17198 rettv->vval.v_string = vim_strsave(p);
17199 simplify_filename(rettv->vval.v_string); /* simplify in place */
17200 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201}
17202
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017203#ifdef FEAT_FLOAT
17204/*
17205 * "sin()" function
17206 */
17207 static void
17208f_sin(argvars, rettv)
17209 typval_T *argvars;
17210 typval_T *rettv;
17211{
17212 float_T f;
17213
17214 rettv->v_type = VAR_FLOAT;
17215 if (get_float_arg(argvars, &f) == OK)
17216 rettv->vval.v_float = sin(f);
17217 else
17218 rettv->vval.v_float = 0.0;
17219}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017220
17221/*
17222 * "sinh()" function
17223 */
17224 static void
17225f_sinh(argvars, rettv)
17226 typval_T *argvars;
17227 typval_T *rettv;
17228{
17229 float_T f;
17230
17231 rettv->v_type = VAR_FLOAT;
17232 if (get_float_arg(argvars, &f) == OK)
17233 rettv->vval.v_float = sinh(f);
17234 else
17235 rettv->vval.v_float = 0.0;
17236}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017237#endif
17238
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239static int
17240#ifdef __BORLANDC__
17241 _RTLENTRYF
17242#endif
17243 item_compare __ARGS((const void *s1, const void *s2));
17244static int
17245#ifdef __BORLANDC__
17246 _RTLENTRYF
17247#endif
17248 item_compare2 __ARGS((const void *s1, const void *s2));
17249
17250static int item_compare_ic;
17251static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017252static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017254static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017255#define ITEM_COMPARE_FAIL 999
17256
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017258 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 static int
17261#ifdef __BORLANDC__
17262_RTLENTRYF
17263#endif
17264item_compare(s1, s2)
17265 const void *s1;
17266 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268 char_u *p1, *p2;
17269 char_u *tofree1, *tofree2;
17270 int res;
17271 char_u numbuf1[NUMBUFLEN];
17272 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017274 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17275 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017276 if (p1 == NULL)
17277 p1 = (char_u *)"";
17278 if (p2 == NULL)
17279 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 if (item_compare_ic)
17281 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 res = STRCMP(p1, p2);
17284 vim_free(tofree1);
17285 vim_free(tofree2);
17286 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287}
17288
17289 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017290#ifdef __BORLANDC__
17291_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293item_compare2(s1, s2)
17294 const void *s1;
17295 const void *s2;
17296{
17297 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017298 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017299 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017300 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 /* shortcut after failure in previous call; compare all items equal */
17303 if (item_compare_func_err)
17304 return 0;
17305
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017306 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17307 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17309 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310
17311 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017312 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017313 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17314 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315 clear_tv(&argv[0]);
17316 clear_tv(&argv[1]);
17317
17318 if (res == FAIL)
17319 res = ITEM_COMPARE_FAIL;
17320 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017321 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17322 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017323 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017324 clear_tv(&rettv);
17325 return res;
17326}
17327
17328/*
17329 * "sort({list})" function
17330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017332do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 typval_T *argvars;
17334 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017335 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336{
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 list_T *l;
17338 listitem_T *li;
17339 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017340 long len;
17341 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017344 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 else
17346 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017347 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017348 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017349 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017350 return;
17351 rettv->vval.v_list = l;
17352 rettv->v_type = VAR_LIST;
17353 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar0d660222005-01-07 21:51:51 +000017355 len = list_len(l);
17356 if (len <= 1)
17357 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359 item_compare_ic = FALSE;
17360 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017361 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017362 if (argvars[1].v_type != VAR_UNKNOWN)
17363 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017364 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017365 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017367 else
17368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 int error = FALSE;
17370
17371 i = get_tv_number_chk(&argvars[1], &error);
17372 if (error)
17373 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374 if (i == 1)
17375 item_compare_ic = TRUE;
17376 else
17377 item_compare_func = get_tv_string(&argvars[1]);
17378 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017379
17380 if (argvars[2].v_type != VAR_UNKNOWN)
17381 {
17382 /* optional third argument: {dict} */
17383 if (argvars[2].v_type != VAR_DICT)
17384 {
17385 EMSG(_(e_dictreq));
17386 return;
17387 }
17388 item_compare_selfdict = argvars[2].vval.v_dict;
17389 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017393 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017394 if (ptrs == NULL)
17395 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396
Bram Moolenaar327aa022014-03-25 18:24:23 +010017397 i = 0;
17398 if (sort)
17399 {
17400 /* sort(): ptrs will be the list to sort */
17401 for (li = l->lv_first; li != NULL; li = li->li_next)
17402 ptrs[i++] = li;
17403
17404 item_compare_func_err = FALSE;
17405 /* test the compare function */
17406 if (item_compare_func != NULL
17407 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017409 EMSG(_("E702: Sort compare function failed"));
17410 else
17411 {
17412 /* Sort the array with item pointers. */
17413 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17414 item_compare_func == NULL ? item_compare : item_compare2);
17415
17416 if (!item_compare_func_err)
17417 {
17418 /* Clear the List and append the items in sorted order. */
17419 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17420 l->lv_len = 0;
17421 for (i = 0; i < len; ++i)
17422 list_append(l, ptrs[i]);
17423 }
17424 }
17425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017427 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017428 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17429
17430 /* f_uniq(): ptrs will be a stack of items to remove */
17431 item_compare_func_err = FALSE;
17432 item_compare_func_ptr = item_compare_func
17433 ? item_compare2 : item_compare;
17434
17435 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17436 li = li->li_next)
17437 {
17438 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17439 == 0)
17440 ptrs[i++] = li;
17441 if (item_compare_func_err)
17442 {
17443 EMSG(_("E882: Uniq compare function failed"));
17444 break;
17445 }
17446 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017448 if (!item_compare_func_err)
17449 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017450 while (--i >= 0)
17451 {
17452 li = ptrs[i]->li_next;
17453 ptrs[i]->li_next = li->li_next;
17454 if (li->li_next != NULL)
17455 li->li_next->li_prev = ptrs[i];
17456 else
17457 l->lv_last = ptrs[i];
17458 list_fix_watch(l, li);
17459 listitem_free(li);
17460 l->lv_len--;
17461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017462 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 }
17464
17465 vim_free(ptrs);
17466 }
17467}
17468
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017469/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017470 * "sort({list})" function
17471 */
17472 static void
17473f_sort(argvars, rettv)
17474 typval_T *argvars;
17475 typval_T *rettv;
17476{
17477 do_sort_uniq(argvars, rettv, TRUE);
17478}
17479
17480/*
17481 * "uniq({list})" function
17482 */
17483 static void
17484f_uniq(argvars, rettv)
17485 typval_T *argvars;
17486 typval_T *rettv;
17487{
17488 do_sort_uniq(argvars, rettv, FALSE);
17489}
17490
17491/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017492 * "soundfold({word})" function
17493 */
17494 static void
17495f_soundfold(argvars, rettv)
17496 typval_T *argvars;
17497 typval_T *rettv;
17498{
17499 char_u *s;
17500
17501 rettv->v_type = VAR_STRING;
17502 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017503#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017504 rettv->vval.v_string = eval_soundfold(s);
17505#else
17506 rettv->vval.v_string = vim_strsave(s);
17507#endif
17508}
17509
17510/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017511 * "spellbadword()" function
17512 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017513 static void
17514f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017515 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017516 typval_T *rettv;
17517{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017518 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017519 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017520 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017523 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017524
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017525#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017526 if (argvars[0].v_type == VAR_UNKNOWN)
17527 {
17528 /* Find the start and length of the badly spelled word. */
17529 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17530 if (len != 0)
17531 word = ml_get_cursor();
17532 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017533 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017534 {
17535 char_u *str = get_tv_string_chk(&argvars[0]);
17536 int capcol = -1;
17537
17538 if (str != NULL)
17539 {
17540 /* Check the argument for spelling. */
17541 while (*str != NUL)
17542 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017543 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017544 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017545 {
17546 word = str;
17547 break;
17548 }
17549 str += len;
17550 }
17551 }
17552 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017553#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017554
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017555 list_append_string(rettv->vval.v_list, word, len);
17556 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017557 attr == HLF_SPB ? "bad" :
17558 attr == HLF_SPR ? "rare" :
17559 attr == HLF_SPL ? "local" :
17560 attr == HLF_SPC ? "caps" :
17561 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017562}
17563
17564/*
17565 * "spellsuggest()" function
17566 */
17567 static void
17568f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017569 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017570 typval_T *rettv;
17571{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017572#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017573 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017574 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017575 int maxcount;
17576 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017577 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017578 listitem_T *li;
17579 int need_capital = FALSE;
17580#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017582 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017583 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017584
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017585#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017586 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017587 {
17588 str = get_tv_string(&argvars[0]);
17589 if (argvars[1].v_type != VAR_UNKNOWN)
17590 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017591 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017592 if (maxcount <= 0)
17593 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017594 if (argvars[2].v_type != VAR_UNKNOWN)
17595 {
17596 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17597 if (typeerr)
17598 return;
17599 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017600 }
17601 else
17602 maxcount = 25;
17603
Bram Moolenaar4770d092006-01-12 23:22:24 +000017604 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017605
17606 for (i = 0; i < ga.ga_len; ++i)
17607 {
17608 str = ((char_u **)ga.ga_data)[i];
17609
17610 li = listitem_alloc();
17611 if (li == NULL)
17612 vim_free(str);
17613 else
17614 {
17615 li->li_tv.v_type = VAR_STRING;
17616 li->li_tv.v_lock = 0;
17617 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017618 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017619 }
17620 }
17621 ga_clear(&ga);
17622 }
17623#endif
17624}
17625
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017627f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 typval_T *argvars;
17629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630{
17631 char_u *str;
17632 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017633 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 regmatch_T regmatch;
17635 char_u patbuf[NUMBUFLEN];
17636 char_u *save_cpo;
17637 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017638 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017639 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641
17642 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17643 save_cpo = p_cpo;
17644 p_cpo = (char_u *)"";
17645
17646 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017647 if (argvars[1].v_type != VAR_UNKNOWN)
17648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17650 if (pat == NULL)
17651 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017653 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017654 }
17655 if (pat == NULL || *pat == NUL)
17656 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017658 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017660 if (typeerr)
17661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662
Bram Moolenaar0d660222005-01-07 21:51:51 +000017663 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17664 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017666 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017667 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017669 if (*str == NUL)
17670 match = FALSE; /* empty item at the end */
17671 else
17672 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017673 if (match)
17674 end = regmatch.startp[0];
17675 else
17676 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017677 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17678 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017679 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017680 if (list_append_string(rettv->vval.v_list, str,
17681 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017682 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017683 }
17684 if (!match)
17685 break;
17686 /* Advance to just after the match. */
17687 if (regmatch.endp[0] > str)
17688 col = 0;
17689 else
17690 {
17691 /* Don't get stuck at the same match. */
17692#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017693 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694#else
17695 col = 1;
17696#endif
17697 }
17698 str = regmatch.endp[0];
17699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700
Bram Moolenaar473de612013-06-08 18:19:48 +020017701 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703
Bram Moolenaar0d660222005-01-07 21:51:51 +000017704 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705}
17706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017707#ifdef FEAT_FLOAT
17708/*
17709 * "sqrt()" function
17710 */
17711 static void
17712f_sqrt(argvars, rettv)
17713 typval_T *argvars;
17714 typval_T *rettv;
17715{
17716 float_T f;
17717
17718 rettv->v_type = VAR_FLOAT;
17719 if (get_float_arg(argvars, &f) == OK)
17720 rettv->vval.v_float = sqrt(f);
17721 else
17722 rettv->vval.v_float = 0.0;
17723}
17724
17725/*
17726 * "str2float()" function
17727 */
17728 static void
17729f_str2float(argvars, rettv)
17730 typval_T *argvars;
17731 typval_T *rettv;
17732{
17733 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17734
17735 if (*p == '+')
17736 p = skipwhite(p + 1);
17737 (void)string2float(p, &rettv->vval.v_float);
17738 rettv->v_type = VAR_FLOAT;
17739}
17740#endif
17741
Bram Moolenaar2c932302006-03-18 21:42:09 +000017742/*
17743 * "str2nr()" function
17744 */
17745 static void
17746f_str2nr(argvars, rettv)
17747 typval_T *argvars;
17748 typval_T *rettv;
17749{
17750 int base = 10;
17751 char_u *p;
17752 long n;
17753
17754 if (argvars[1].v_type != VAR_UNKNOWN)
17755 {
17756 base = get_tv_number(&argvars[1]);
17757 if (base != 8 && base != 10 && base != 16)
17758 {
17759 EMSG(_(e_invarg));
17760 return;
17761 }
17762 }
17763
17764 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017765 if (*p == '+')
17766 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017767 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17768 rettv->vval.v_number = n;
17769}
17770
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771#ifdef HAVE_STRFTIME
17772/*
17773 * "strftime({format}[, {time}])" function
17774 */
17775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
17780 char_u result_buf[256];
17781 struct tm *curtime;
17782 time_t seconds;
17783 char_u *p;
17784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017788 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 seconds = time(NULL);
17790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017791 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 curtime = localtime(&seconds);
17793 /* MSVC returns NULL for an invalid value of seconds. */
17794 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 else
17797 {
17798# ifdef FEAT_MBYTE
17799 vimconv_T conv;
17800 char_u *enc;
17801
17802 conv.vc_type = CONV_NONE;
17803 enc = enc_locale();
17804 convert_setup(&conv, p_enc, enc);
17805 if (conv.vc_type != CONV_NONE)
17806 p = string_convert(&conv, p, NULL);
17807# endif
17808 if (p != NULL)
17809 (void)strftime((char *)result_buf, sizeof(result_buf),
17810 (char *)p, curtime);
17811 else
17812 result_buf[0] = NUL;
17813
17814# ifdef FEAT_MBYTE
17815 if (conv.vc_type != CONV_NONE)
17816 vim_free(p);
17817 convert_setup(&conv, enc, p_enc);
17818 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 else
17821# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017822 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823
17824# ifdef FEAT_MBYTE
17825 /* Release conversion descriptors */
17826 convert_setup(&conv, NULL, NULL);
17827 vim_free(enc);
17828# endif
17829 }
17830}
17831#endif
17832
17833/*
17834 * "stridx()" function
17835 */
17836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 typval_T *argvars;
17839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840{
17841 char_u buf[NUMBUFLEN];
17842 char_u *needle;
17843 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017846 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017848 needle = get_tv_string_chk(&argvars[1]);
17849 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017851 if (needle == NULL || haystack == NULL)
17852 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 if (argvars[2].v_type != VAR_UNKNOWN)
17855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017856 int error = FALSE;
17857
17858 start_idx = get_tv_number_chk(&argvars[2], &error);
17859 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017860 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017861 if (start_idx >= 0)
17862 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017863 }
17864
17865 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17866 if (pos != NULL)
17867 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868}
17869
17870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017871 * "string()" function
17872 */
17873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017874f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 typval_T *argvars;
17876 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017877{
17878 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017879 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017882 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017883 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017884 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017885 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886}
17887
17888/*
17889 * "strlen()" function
17890 */
17891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017892f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 typval_T *argvars;
17894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 rettv->vval.v_number = (varnumber_T)(STRLEN(
17897 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898}
17899
17900/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017901 * "strchars()" function
17902 */
17903 static void
17904f_strchars(argvars, rettv)
17905 typval_T *argvars;
17906 typval_T *rettv;
17907{
17908 char_u *s = get_tv_string(&argvars[0]);
17909#ifdef FEAT_MBYTE
17910 varnumber_T len = 0;
17911
17912 while (*s != NUL)
17913 {
17914 mb_cptr2char_adv(&s);
17915 ++len;
17916 }
17917 rettv->vval.v_number = len;
17918#else
17919 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17920#endif
17921}
17922
17923/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017924 * "strdisplaywidth()" function
17925 */
17926 static void
17927f_strdisplaywidth(argvars, rettv)
17928 typval_T *argvars;
17929 typval_T *rettv;
17930{
17931 char_u *s = get_tv_string(&argvars[0]);
17932 int col = 0;
17933
17934 if (argvars[1].v_type != VAR_UNKNOWN)
17935 col = get_tv_number(&argvars[1]);
17936
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017937 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017938}
17939
17940/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017941 * "strwidth()" function
17942 */
17943 static void
17944f_strwidth(argvars, rettv)
17945 typval_T *argvars;
17946 typval_T *rettv;
17947{
17948 char_u *s = get_tv_string(&argvars[0]);
17949
17950 rettv->vval.v_number = (varnumber_T)(
17951#ifdef FEAT_MBYTE
17952 mb_string2cells(s, -1)
17953#else
17954 STRLEN(s)
17955#endif
17956 );
17957}
17958
17959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 * "strpart()" function
17961 */
17962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017964 typval_T *argvars;
17965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966{
17967 char_u *p;
17968 int n;
17969 int len;
17970 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017971 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017973 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 slen = (int)STRLEN(p);
17975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017976 n = get_tv_number_chk(&argvars[1], &error);
17977 if (error)
17978 len = 0;
17979 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017980 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 else
17982 len = slen - n; /* default len: all bytes that are available. */
17983
17984 /*
17985 * Only return the overlap between the specified part and the actual
17986 * string.
17987 */
17988 if (n < 0)
17989 {
17990 len += n;
17991 n = 0;
17992 }
17993 else if (n > slen)
17994 n = slen;
17995 if (len < 0)
17996 len = 0;
17997 else if (n + len > slen)
17998 len = slen - n;
17999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018000 rettv->v_type = VAR_STRING;
18001 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002}
18003
18004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 * "strridx()" function
18006 */
18007 static void
18008f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018009 typval_T *argvars;
18010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018011{
18012 char_u buf[NUMBUFLEN];
18013 char_u *needle;
18014 char_u *haystack;
18015 char_u *rest;
18016 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018017 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018019 needle = get_tv_string_chk(&argvars[1]);
18020 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018021
18022 rettv->vval.v_number = -1;
18023 if (needle == NULL || haystack == NULL)
18024 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018025
18026 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018027 if (argvars[2].v_type != VAR_UNKNOWN)
18028 {
18029 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018030 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018031 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018033 }
18034 else
18035 end_idx = haystack_len;
18036
Bram Moolenaar0d660222005-01-07 21:51:51 +000018037 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018038 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018040 lastmatch = haystack + end_idx;
18041 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018043 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018044 for (rest = haystack; *rest != '\0'; ++rest)
18045 {
18046 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018047 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048 break;
18049 lastmatch = rest;
18050 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018051 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052
18053 if (lastmatch == NULL)
18054 rettv->vval.v_number = -1;
18055 else
18056 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18057}
18058
18059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 * "strtrans()" function
18061 */
18062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018063f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018064 typval_T *argvars;
18065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067 rettv->v_type = VAR_STRING;
18068 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069}
18070
18071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 * "submatch()" function
18073 */
18074 static void
18075f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 typval_T *argvars;
18077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078{
Bram Moolenaar41571762014-04-02 19:00:58 +020018079 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018080 int no;
18081 int retList = 0;
18082
18083 no = (int)get_tv_number_chk(&argvars[0], &error);
18084 if (error)
18085 return;
18086 error = FALSE;
18087 if (argvars[1].v_type != VAR_UNKNOWN)
18088 retList = get_tv_number_chk(&argvars[1], &error);
18089 if (error)
18090 return;
18091
18092 if (retList == 0)
18093 {
18094 rettv->v_type = VAR_STRING;
18095 rettv->vval.v_string = reg_submatch(no);
18096 }
18097 else
18098 {
18099 rettv->v_type = VAR_LIST;
18100 rettv->vval.v_list = reg_submatch_list(no);
18101 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018102}
18103
18104/*
18105 * "substitute()" function
18106 */
18107 static void
18108f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 typval_T *argvars;
18110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018111{
18112 char_u patbuf[NUMBUFLEN];
18113 char_u subbuf[NUMBUFLEN];
18114 char_u flagsbuf[NUMBUFLEN];
18115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018116 char_u *str = get_tv_string_chk(&argvars[0]);
18117 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18118 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18119 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18120
Bram Moolenaar0d660222005-01-07 21:51:51 +000018121 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018122 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18123 rettv->vval.v_string = NULL;
18124 else
18125 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126}
18127
18128/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018129 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018133 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135{
18136 int id = 0;
18137#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018138 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 long col;
18140 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018141 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018143 lnum = get_tv_lnum(argvars); /* -1 on type error */
18144 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18145 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018148 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018149 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150#endif
18151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153}
18154
18155/*
18156 * "synIDattr(id, what [, mode])" function
18157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162{
18163 char_u *p = NULL;
18164#ifdef FEAT_SYN_HL
18165 int id;
18166 char_u *what;
18167 char_u *mode;
18168 char_u modebuf[NUMBUFLEN];
18169 int modec;
18170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 id = get_tv_number(&argvars[0]);
18172 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018175 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018177 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 modec = 0; /* replace invalid with current */
18179 }
18180 else
18181 {
18182#ifdef FEAT_GUI
18183 if (gui.in_use)
18184 modec = 'g';
18185 else
18186#endif
18187 if (t_colors > 1)
18188 modec = 'c';
18189 else
18190 modec = 't';
18191 }
18192
18193
18194 switch (TOLOWER_ASC(what[0]))
18195 {
18196 case 'b':
18197 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18198 p = highlight_color(id, what, modec);
18199 else /* bold */
18200 p = highlight_has_attr(id, HL_BOLD, modec);
18201 break;
18202
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018203 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 p = highlight_color(id, what, modec);
18205 break;
18206
18207 case 'i':
18208 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18209 p = highlight_has_attr(id, HL_INVERSE, modec);
18210 else /* italic */
18211 p = highlight_has_attr(id, HL_ITALIC, modec);
18212 break;
18213
18214 case 'n': /* name */
18215 p = get_highlight_name(NULL, id - 1);
18216 break;
18217
18218 case 'r': /* reverse */
18219 p = highlight_has_attr(id, HL_INVERSE, modec);
18220 break;
18221
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018222 case 's':
18223 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18224 p = highlight_color(id, what, modec);
18225 else /* standout */
18226 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 break;
18228
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018229 case 'u':
18230 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18231 /* underline */
18232 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18233 else
18234 /* undercurl */
18235 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 break;
18237 }
18238
18239 if (p != NULL)
18240 p = vim_strsave(p);
18241#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242 rettv->v_type = VAR_STRING;
18243 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244}
18245
18246/*
18247 * "synIDtrans(id)" function
18248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018250f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253{
18254 int id;
18255
18256#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
18259 if (id > 0)
18260 id = syn_get_final_id(id);
18261 else
18262#endif
18263 id = 0;
18264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018265 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266}
18267
18268/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018269 * "synconcealed(lnum, col)" function
18270 */
18271 static void
18272f_synconcealed(argvars, rettv)
18273 typval_T *argvars UNUSED;
18274 typval_T *rettv;
18275{
18276#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18277 long lnum;
18278 long col;
18279 int syntax_flags = 0;
18280 int cchar;
18281 int matchid = 0;
18282 char_u str[NUMBUFLEN];
18283#endif
18284
18285 rettv->v_type = VAR_LIST;
18286 rettv->vval.v_list = NULL;
18287
18288#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18289 lnum = get_tv_lnum(argvars); /* -1 on type error */
18290 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18291
18292 vim_memset(str, NUL, sizeof(str));
18293
18294 if (rettv_list_alloc(rettv) != FAIL)
18295 {
18296 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18297 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18298 && curwin->w_p_cole > 0)
18299 {
18300 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18301 syntax_flags = get_syntax_info(&matchid);
18302
18303 /* get the conceal character */
18304 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18305 {
18306 cchar = syn_get_sub_char();
18307 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18308 cchar = lcs_conceal;
18309 if (cchar != NUL)
18310 {
18311# ifdef FEAT_MBYTE
18312 if (has_mbyte)
18313 (*mb_char2bytes)(cchar, str);
18314 else
18315# endif
18316 str[0] = cchar;
18317 }
18318 }
18319 }
18320
18321 list_append_number(rettv->vval.v_list,
18322 (syntax_flags & HL_CONCEAL) != 0);
18323 /* -1 to auto-determine strlen */
18324 list_append_string(rettv->vval.v_list, str, -1);
18325 list_append_number(rettv->vval.v_list, matchid);
18326 }
18327#endif
18328}
18329
18330/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018331 * "synstack(lnum, col)" function
18332 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018333 static void
18334f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018335 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018336 typval_T *rettv;
18337{
18338#ifdef FEAT_SYN_HL
18339 long lnum;
18340 long col;
18341 int i;
18342 int id;
18343#endif
18344
18345 rettv->v_type = VAR_LIST;
18346 rettv->vval.v_list = NULL;
18347
18348#ifdef FEAT_SYN_HL
18349 lnum = get_tv_lnum(argvars); /* -1 on type error */
18350 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18351
18352 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018353 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018354 && rettv_list_alloc(rettv) != FAIL)
18355 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018356 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018357 for (i = 0; ; ++i)
18358 {
18359 id = syn_get_stack_item(i);
18360 if (id < 0)
18361 break;
18362 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18363 break;
18364 }
18365 }
18366#endif
18367}
18368
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018370get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 typval_T *argvars;
18372 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018373 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018375 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018377 char_u *infile = NULL;
18378 char_u buf[NUMBUFLEN];
18379 int err = FALSE;
18380 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018381 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018383 rettv->v_type = VAR_STRING;
18384 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018385 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018386 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018387
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018388 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018389 {
18390 /*
18391 * Write the string to a temp file, to be used for input of the shell
18392 * command.
18393 */
18394 if ((infile = vim_tempname('i')) == NULL)
18395 {
18396 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018397 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018398 }
18399
18400 fd = mch_fopen((char *)infile, WRITEBIN);
18401 if (fd == NULL)
18402 {
18403 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018404 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018405 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018406 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018407 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018408 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18409 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018410 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018411 else
18412 {
18413 p = get_tv_string_buf_chk(&argvars[1], buf);
18414 if (p == NULL)
18415 {
18416 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018417 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018418 }
18419 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18420 err = TRUE;
18421 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018422 if (fclose(fd) != 0)
18423 err = TRUE;
18424 if (err)
18425 {
18426 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018427 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018428 }
18429 }
18430
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018431 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018433 int len;
18434 listitem_T *li;
18435 char_u *s = NULL;
18436 char_u *start;
18437 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018438 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018440 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18441 SHELL_SILENT | SHELL_COOKED, &len);
18442 if (res == NULL)
18443 goto errret;
18444
18445 list = list_alloc();
18446 if (list == NULL)
18447 goto errret;
18448
18449 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018451 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018452 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018453 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018454 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018455
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018456 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018457 if (s == NULL)
18458 goto errret;
18459
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018460 for (p = s; start < end; ++p, ++start)
18461 *p = *start == NUL ? NL : *start;
18462 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018463
18464 li = listitem_alloc();
18465 if (li == NULL)
18466 {
18467 vim_free(s);
18468 goto errret;
18469 }
18470 li->li_tv.v_type = VAR_STRING;
18471 li->li_tv.vval.v_string = s;
18472 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018474
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018475 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018476 rettv->v_type = VAR_LIST;
18477 rettv->vval.v_list = list;
18478 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018480 else
18481 {
18482 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18483 SHELL_SILENT | SHELL_COOKED, NULL);
18484#ifdef USE_CR
18485 /* translate <CR> into <NL> */
18486 if (res != NULL)
18487 {
18488 char_u *s;
18489
18490 for (s = res; *s; ++s)
18491 {
18492 if (*s == CAR)
18493 *s = NL;
18494 }
18495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496#else
18497# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018498 /* translate <CR><NL> into <NL> */
18499 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018501 char_u *s, *d;
18502
18503 d = res;
18504 for (s = res; *s; ++s)
18505 {
18506 if (s[0] == CAR && s[1] == NL)
18507 ++s;
18508 *d++ = *s;
18509 }
18510 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512# endif
18513#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018514 rettv->vval.v_string = res;
18515 res = NULL;
18516 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018517
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018518errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018519 if (infile != NULL)
18520 {
18521 mch_remove(infile);
18522 vim_free(infile);
18523 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018524 if (res != NULL)
18525 vim_free(res);
18526 if (list != NULL)
18527 list_free(list, TRUE);
18528}
18529
18530/*
18531 * "system()" function
18532 */
18533 static void
18534f_system(argvars, rettv)
18535 typval_T *argvars;
18536 typval_T *rettv;
18537{
18538 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18539}
18540
18541/*
18542 * "systemlist()" function
18543 */
18544 static void
18545f_systemlist(argvars, rettv)
18546 typval_T *argvars;
18547 typval_T *rettv;
18548{
18549 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550}
18551
18552/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018553 * "tabpagebuflist()" function
18554 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018555 static void
18556f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018557 typval_T *argvars UNUSED;
18558 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018559{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018560#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018561 tabpage_T *tp;
18562 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018563
18564 if (argvars[0].v_type == VAR_UNKNOWN)
18565 wp = firstwin;
18566 else
18567 {
18568 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18569 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018570 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018571 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018572 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018573 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018574 for (; wp != NULL; wp = wp->w_next)
18575 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018576 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018577 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018578 }
18579#endif
18580}
18581
18582
18583/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018584 * "tabpagenr()" function
18585 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018586 static void
18587f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018588 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018589 typval_T *rettv;
18590{
18591 int nr = 1;
18592#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018593 char_u *arg;
18594
18595 if (argvars[0].v_type != VAR_UNKNOWN)
18596 {
18597 arg = get_tv_string_chk(&argvars[0]);
18598 nr = 0;
18599 if (arg != NULL)
18600 {
18601 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018602 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018603 else
18604 EMSG2(_(e_invexpr2), arg);
18605 }
18606 }
18607 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018608 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018609#endif
18610 rettv->vval.v_number = nr;
18611}
18612
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018613
18614#ifdef FEAT_WINDOWS
18615static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18616
18617/*
18618 * Common code for tabpagewinnr() and winnr().
18619 */
18620 static int
18621get_winnr(tp, argvar)
18622 tabpage_T *tp;
18623 typval_T *argvar;
18624{
18625 win_T *twin;
18626 int nr = 1;
18627 win_T *wp;
18628 char_u *arg;
18629
18630 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18631 if (argvar->v_type != VAR_UNKNOWN)
18632 {
18633 arg = get_tv_string_chk(argvar);
18634 if (arg == NULL)
18635 nr = 0; /* type error; errmsg already given */
18636 else if (STRCMP(arg, "$") == 0)
18637 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18638 else if (STRCMP(arg, "#") == 0)
18639 {
18640 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18641 if (twin == NULL)
18642 nr = 0;
18643 }
18644 else
18645 {
18646 EMSG2(_(e_invexpr2), arg);
18647 nr = 0;
18648 }
18649 }
18650
18651 if (nr > 0)
18652 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18653 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018654 {
18655 if (wp == NULL)
18656 {
18657 /* didn't find it in this tabpage */
18658 nr = 0;
18659 break;
18660 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018661 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018662 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018663 return nr;
18664}
18665#endif
18666
18667/*
18668 * "tabpagewinnr()" function
18669 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018670 static void
18671f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018672 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018673 typval_T *rettv;
18674{
18675 int nr = 1;
18676#ifdef FEAT_WINDOWS
18677 tabpage_T *tp;
18678
18679 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18680 if (tp == NULL)
18681 nr = 0;
18682 else
18683 nr = get_winnr(tp, &argvars[1]);
18684#endif
18685 rettv->vval.v_number = nr;
18686}
18687
18688
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018689/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018690 * "tagfiles()" function
18691 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018692 static void
18693f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018694 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018695 typval_T *rettv;
18696{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018697 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018698 tagname_T tn;
18699 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018702 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018703 fname = alloc(MAXPATHL);
18704 if (fname == NULL)
18705 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018706
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018707 for (first = TRUE; ; first = FALSE)
18708 if (get_tagfname(&tn, first, fname) == FAIL
18709 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018710 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018711 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018712 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018713}
18714
18715/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018716 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018717 */
18718 static void
18719f_taglist(argvars, rettv)
18720 typval_T *argvars;
18721 typval_T *rettv;
18722{
18723 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018724
18725 tag_pattern = get_tv_string(&argvars[0]);
18726
18727 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018728 if (*tag_pattern == NUL)
18729 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018730
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018731 if (rettv_list_alloc(rettv) == OK)
18732 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018733}
18734
18735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 * "tempname()" function
18737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742{
18743 static int x = 'A';
18744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 rettv->v_type = VAR_STRING;
18746 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747
18748 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18749 * names. Skip 'I' and 'O', they are used for shell redirection. */
18750 do
18751 {
18752 if (x == 'Z')
18753 x = '0';
18754 else if (x == '9')
18755 x = 'A';
18756 else
18757 {
18758#ifdef EBCDIC
18759 if (x == 'I')
18760 x = 'J';
18761 else if (x == 'R')
18762 x = 'S';
18763 else
18764#endif
18765 ++x;
18766 }
18767 } while (x == 'I' || x == 'O');
18768}
18769
18770/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018771 * "test(list)" function: Just checking the walls...
18772 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018773 static void
18774f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018775 typval_T *argvars UNUSED;
18776 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018777{
18778 /* Used for unit testing. Change the code below to your liking. */
18779#if 0
18780 listitem_T *li;
18781 list_T *l;
18782 char_u *bad, *good;
18783
18784 if (argvars[0].v_type != VAR_LIST)
18785 return;
18786 l = argvars[0].vval.v_list;
18787 if (l == NULL)
18788 return;
18789 li = l->lv_first;
18790 if (li == NULL)
18791 return;
18792 bad = get_tv_string(&li->li_tv);
18793 li = li->li_next;
18794 if (li == NULL)
18795 return;
18796 good = get_tv_string(&li->li_tv);
18797 rettv->vval.v_number = test_edit_score(bad, good);
18798#endif
18799}
18800
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018801#ifdef FEAT_FLOAT
18802/*
18803 * "tan()" function
18804 */
18805 static void
18806f_tan(argvars, rettv)
18807 typval_T *argvars;
18808 typval_T *rettv;
18809{
18810 float_T f;
18811
18812 rettv->v_type = VAR_FLOAT;
18813 if (get_float_arg(argvars, &f) == OK)
18814 rettv->vval.v_float = tan(f);
18815 else
18816 rettv->vval.v_float = 0.0;
18817}
18818
18819/*
18820 * "tanh()" function
18821 */
18822 static void
18823f_tanh(argvars, rettv)
18824 typval_T *argvars;
18825 typval_T *rettv;
18826{
18827 float_T f;
18828
18829 rettv->v_type = VAR_FLOAT;
18830 if (get_float_arg(argvars, &f) == OK)
18831 rettv->vval.v_float = tanh(f);
18832 else
18833 rettv->vval.v_float = 0.0;
18834}
18835#endif
18836
Bram Moolenaard52d9742005-08-21 22:20:28 +000018837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 * "tolower(string)" function
18839 */
18840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 typval_T *argvars;
18843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844{
18845 char_u *p;
18846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018847 p = vim_strsave(get_tv_string(&argvars[0]));
18848 rettv->v_type = VAR_STRING;
18849 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850
18851 if (p != NULL)
18852 while (*p != NUL)
18853 {
18854#ifdef FEAT_MBYTE
18855 int l;
18856
18857 if (enc_utf8)
18858 {
18859 int c, lc;
18860
18861 c = utf_ptr2char(p);
18862 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018863 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 /* TODO: reallocate string when byte count changes. */
18865 if (utf_char2len(lc) == l)
18866 utf_char2bytes(lc, p);
18867 p += l;
18868 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018869 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 p += l; /* skip multi-byte character */
18871 else
18872#endif
18873 {
18874 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18875 ++p;
18876 }
18877 }
18878}
18879
18880/*
18881 * "toupper(string)" function
18882 */
18883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 typval_T *argvars;
18886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018888 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018889 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890}
18891
18892/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018893 * "tr(string, fromstr, tostr)" function
18894 */
18895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018896f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 typval_T *argvars;
18898 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018899{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018900 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018901 char_u *fromstr;
18902 char_u *tostr;
18903 char_u *p;
18904#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018905 int inlen;
18906 int fromlen;
18907 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018908 int idx;
18909 char_u *cpstr;
18910 int cplen;
18911 int first = TRUE;
18912#endif
18913 char_u buf[NUMBUFLEN];
18914 char_u buf2[NUMBUFLEN];
18915 garray_T ga;
18916
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018917 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18919 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018920
18921 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018922 rettv->v_type = VAR_STRING;
18923 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018924 if (fromstr == NULL || tostr == NULL)
18925 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018926 ga_init2(&ga, (int)sizeof(char), 80);
18927
18928#ifdef FEAT_MBYTE
18929 if (!has_mbyte)
18930#endif
18931 /* not multi-byte: fromstr and tostr must be the same length */
18932 if (STRLEN(fromstr) != STRLEN(tostr))
18933 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018934#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018935error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018936#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018937 EMSG2(_(e_invarg2), fromstr);
18938 ga_clear(&ga);
18939 return;
18940 }
18941
18942 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018943 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018944 {
18945#ifdef FEAT_MBYTE
18946 if (has_mbyte)
18947 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018948 inlen = (*mb_ptr2len)(in_str);
18949 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018950 cplen = inlen;
18951 idx = 0;
18952 for (p = fromstr; *p != NUL; p += fromlen)
18953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018954 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018955 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018956 {
18957 for (p = tostr; *p != NUL; p += tolen)
18958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018959 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018960 if (idx-- == 0)
18961 {
18962 cplen = tolen;
18963 cpstr = p;
18964 break;
18965 }
18966 }
18967 if (*p == NUL) /* tostr is shorter than fromstr */
18968 goto error;
18969 break;
18970 }
18971 ++idx;
18972 }
18973
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018974 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018975 {
18976 /* Check that fromstr and tostr have the same number of
18977 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018978 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018979 first = FALSE;
18980 for (p = tostr; *p != NUL; p += tolen)
18981 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018982 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018983 --idx;
18984 }
18985 if (idx != 0)
18986 goto error;
18987 }
18988
18989 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018990 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018991 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018992
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018993 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018994 }
18995 else
18996#endif
18997 {
18998 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018999 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019000 if (p != NULL)
19001 ga_append(&ga, tostr[p - fromstr]);
19002 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019003 ga_append(&ga, *in_str);
19004 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019005 }
19006 }
19007
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019008 /* add a terminating NUL */
19009 ga_grow(&ga, 1);
19010 ga_append(&ga, NUL);
19011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019012 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019013}
19014
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019015#ifdef FEAT_FLOAT
19016/*
19017 * "trunc({float})" function
19018 */
19019 static void
19020f_trunc(argvars, rettv)
19021 typval_T *argvars;
19022 typval_T *rettv;
19023{
19024 float_T f;
19025
19026 rettv->v_type = VAR_FLOAT;
19027 if (get_float_arg(argvars, &f) == OK)
19028 /* trunc() is not in C90, use floor() or ceil() instead. */
19029 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19030 else
19031 rettv->vval.v_float = 0.0;
19032}
19033#endif
19034
Bram Moolenaar8299df92004-07-10 09:47:34 +000019035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 * "type(expr)" function
19037 */
19038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019039f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019040 typval_T *argvars;
19041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019043 int n;
19044
19045 switch (argvars[0].v_type)
19046 {
19047 case VAR_NUMBER: n = 0; break;
19048 case VAR_STRING: n = 1; break;
19049 case VAR_FUNC: n = 2; break;
19050 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019051 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019052#ifdef FEAT_FLOAT
19053 case VAR_FLOAT: n = 5; break;
19054#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019055 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19056 }
19057 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058}
19059
19060/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019061 * "undofile(name)" function
19062 */
19063 static void
19064f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019065 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019066 typval_T *rettv;
19067{
19068 rettv->v_type = VAR_STRING;
19069#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019070 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019071 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019072
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019073 if (*fname == NUL)
19074 {
19075 /* If there is no file name there will be no undo file. */
19076 rettv->vval.v_string = NULL;
19077 }
19078 else
19079 {
19080 char_u *ffname = FullName_save(fname, FALSE);
19081
19082 if (ffname != NULL)
19083 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19084 vim_free(ffname);
19085 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019086 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019087#else
19088 rettv->vval.v_string = NULL;
19089#endif
19090}
19091
19092/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019093 * "undotree()" function
19094 */
19095 static void
19096f_undotree(argvars, rettv)
19097 typval_T *argvars UNUSED;
19098 typval_T *rettv;
19099{
19100 if (rettv_dict_alloc(rettv) == OK)
19101 {
19102 dict_T *dict = rettv->vval.v_dict;
19103 list_T *list;
19104
Bram Moolenaar730cde92010-06-27 05:18:54 +020019105 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019106 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019107 dict_add_nr_str(dict, "save_last",
19108 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019109 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19110 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019111 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019112
19113 list = list_alloc();
19114 if (list != NULL)
19115 {
19116 u_eval_tree(curbuf->b_u_oldhead, list);
19117 dict_add_list(dict, "entries", list);
19118 }
19119 }
19120}
19121
19122/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019123 * "values(dict)" function
19124 */
19125 static void
19126f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019127 typval_T *argvars;
19128 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019129{
19130 dict_list(argvars, rettv, 1);
19131}
19132
19133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 * "virtcol(string)" function
19135 */
19136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T *argvars;
19139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140{
19141 colnr_T vcol = 0;
19142 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019143 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019145 fp = var2fpos(&argvars[0], FALSE, &fnum);
19146 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19147 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 {
19149 getvvcol(curwin, fp, NULL, NULL, &vcol);
19150 ++vcol;
19151 }
19152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154}
19155
19156/*
19157 * "visualmode()" function
19158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019161 typval_T *argvars UNUSED;
19162 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 char_u str[2];
19165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167 str[0] = curbuf->b_visual_mode_eval;
19168 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
19171 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019172 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174}
19175
19176/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019177 * "wildmenumode()" function
19178 */
19179 static void
19180f_wildmenumode(argvars, rettv)
19181 typval_T *argvars UNUSED;
19182 typval_T *rettv UNUSED;
19183{
19184#ifdef FEAT_WILDMENU
19185 if (wild_menu_showing)
19186 rettv->vval.v_number = 1;
19187#endif
19188}
19189
19190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 * "winbufnr(nr)" function
19192 */
19193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019195 typval_T *argvars;
19196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197{
19198 win_T *wp;
19199
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019200 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205}
19206
19207/*
19208 * "wincol()" function
19209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214{
19215 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217}
19218
19219/*
19220 * "winheight(nr)" function
19221 */
19222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 typval_T *argvars;
19225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226{
19227 win_T *wp;
19228
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019229 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234}
19235
19236/*
19237 * "winline()" function
19238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019241 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243{
19244 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019245 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
19249 * "winnr()" function
19250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255{
19256 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019257
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019259 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262}
19263
19264/*
19265 * "winrestcmd()" function
19266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019268f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271{
19272#ifdef FEAT_WINDOWS
19273 win_T *wp;
19274 int winnr = 1;
19275 garray_T ga;
19276 char_u buf[50];
19277
19278 ga_init2(&ga, (int)sizeof(char), 70);
19279 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19280 {
19281 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19282 ga_concat(&ga, buf);
19283# ifdef FEAT_VERTSPLIT
19284 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19285 ga_concat(&ga, buf);
19286# endif
19287 ++winnr;
19288 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019289 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019293 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019295 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296}
19297
19298/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019299 * "winrestview()" function
19300 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019301 static void
19302f_winrestview(argvars, rettv)
19303 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019304 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019305{
19306 dict_T *dict;
19307
19308 if (argvars[0].v_type != VAR_DICT
19309 || (dict = argvars[0].vval.v_dict) == NULL)
19310 EMSG(_(e_invarg));
19311 else
19312 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019313 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19314 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19315 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19316 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019317#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019318 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19319 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019320#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019321 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19322 {
19323 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19324 curwin->w_set_curswant = FALSE;
19325 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019326
Bram Moolenaar82c25852014-05-28 16:47:16 +020019327 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19328 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019329#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019330 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19331 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019332#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019333 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19334 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19335 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19336 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019337
19338 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019339 win_new_height(curwin, curwin->w_height);
19340# ifdef FEAT_VERTSPLIT
19341 win_new_width(curwin, W_WIDTH(curwin));
19342# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019343 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019344
19345 if (curwin->w_topline == 0)
19346 curwin->w_topline = 1;
19347 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19348 curwin->w_topline = curbuf->b_ml.ml_line_count;
19349#ifdef FEAT_DIFF
19350 check_topfill(curwin, TRUE);
19351#endif
19352 }
19353}
19354
19355/*
19356 * "winsaveview()" function
19357 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019358 static void
19359f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019360 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019361 typval_T *rettv;
19362{
19363 dict_T *dict;
19364
Bram Moolenaara800b422010-06-27 01:15:55 +020019365 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019366 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019367 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019368
19369 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19370 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19371#ifdef FEAT_VIRTUALEDIT
19372 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19373#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019374 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019375 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19376
19377 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19378#ifdef FEAT_DIFF
19379 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19380#endif
19381 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19382 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19383}
19384
19385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 * "winwidth(nr)" function
19387 */
19388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 typval_T *argvars;
19391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392{
19393 win_T *wp;
19394
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019395 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019397 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 else
19399#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019400 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403#endif
19404}
19405
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019407 * Write list of strings to file
19408 */
19409 static int
19410write_list(fd, list, binary)
19411 FILE *fd;
19412 list_T *list;
19413 int binary;
19414{
19415 listitem_T *li;
19416 int c;
19417 int ret = OK;
19418 char_u *s;
19419
19420 for (li = list->lv_first; li != NULL; li = li->li_next)
19421 {
19422 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19423 {
19424 if (*s == '\n')
19425 c = putc(NUL, fd);
19426 else
19427 c = putc(*s, fd);
19428 if (c == EOF)
19429 {
19430 ret = FAIL;
19431 break;
19432 }
19433 }
19434 if (!binary || li->li_next != NULL)
19435 if (putc('\n', fd) == EOF)
19436 {
19437 ret = FAIL;
19438 break;
19439 }
19440 if (ret == FAIL)
19441 {
19442 EMSG(_(e_write));
19443 break;
19444 }
19445 }
19446 return ret;
19447}
19448
19449/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019450 * "writefile()" function
19451 */
19452 static void
19453f_writefile(argvars, rettv)
19454 typval_T *argvars;
19455 typval_T *rettv;
19456{
19457 int binary = FALSE;
19458 char_u *fname;
19459 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019460 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019461
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019462 if (check_restricted() || check_secure())
19463 return;
19464
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019465 if (argvars[0].v_type != VAR_LIST)
19466 {
19467 EMSG2(_(e_listarg), "writefile()");
19468 return;
19469 }
19470 if (argvars[0].vval.v_list == NULL)
19471 return;
19472
19473 if (argvars[2].v_type != VAR_UNKNOWN
19474 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19475 binary = TRUE;
19476
19477 /* Always open the file in binary mode, library functions have a mind of
19478 * their own about CR-LF conversion. */
19479 fname = get_tv_string(&argvars[1]);
19480 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19481 {
19482 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19483 ret = -1;
19484 }
19485 else
19486 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019487 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19488 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019489 fclose(fd);
19490 }
19491
19492 rettv->vval.v_number = ret;
19493}
19494
19495/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019496 * "xor(expr, expr)" function
19497 */
19498 static void
19499f_xor(argvars, rettv)
19500 typval_T *argvars;
19501 typval_T *rettv;
19502{
19503 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19504 ^ get_tv_number_chk(&argvars[1], NULL);
19505}
19506
19507
19508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019510 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 */
19512 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019513var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019515 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019516 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019518 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019520 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
Bram Moolenaara5525202006-03-02 22:52:09 +000019522 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019523 if (varp->v_type == VAR_LIST)
19524 {
19525 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019526 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019527 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019528 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019529
19530 l = varp->vval.v_list;
19531 if (l == NULL)
19532 return NULL;
19533
19534 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019535 pos.lnum = list_find_nr(l, 0L, &error);
19536 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019537 return NULL; /* invalid line number */
19538
19539 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019540 pos.col = list_find_nr(l, 1L, &error);
19541 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019542 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019543 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019544
19545 /* We accept "$" for the column number: last column. */
19546 li = list_find(l, 1L);
19547 if (li != NULL && li->li_tv.v_type == VAR_STRING
19548 && li->li_tv.vval.v_string != NULL
19549 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19550 pos.col = len + 1;
19551
Bram Moolenaara5525202006-03-02 22:52:09 +000019552 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019553 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019554 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019555 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019556
Bram Moolenaara5525202006-03-02 22:52:09 +000019557#ifdef FEAT_VIRTUALEDIT
19558 /* Get the virtual offset. Defaults to zero. */
19559 pos.coladd = list_find_nr(l, 2L, &error);
19560 if (error)
19561 pos.coladd = 0;
19562#endif
19563
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019564 return &pos;
19565 }
19566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019567 name = get_tv_string_chk(varp);
19568 if (name == NULL)
19569 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019570 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019572 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19573 {
19574 if (VIsual_active)
19575 return &VIsual;
19576 return &curwin->w_cursor;
19577 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019578 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019580 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19582 return NULL;
19583 return pp;
19584 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019585
19586#ifdef FEAT_VIRTUALEDIT
19587 pos.coladd = 0;
19588#endif
19589
Bram Moolenaar477933c2007-07-17 14:32:23 +000019590 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019591 {
19592 pos.col = 0;
19593 if (name[1] == '0') /* "w0": first visible line */
19594 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019595 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019596 pos.lnum = curwin->w_topline;
19597 return &pos;
19598 }
19599 else if (name[1] == '$') /* "w$": last visible line */
19600 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019601 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019602 pos.lnum = curwin->w_botline - 1;
19603 return &pos;
19604 }
19605 }
19606 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019608 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 {
19610 pos.lnum = curbuf->b_ml.ml_line_count;
19611 pos.col = 0;
19612 }
19613 else
19614 {
19615 pos.lnum = curwin->w_cursor.lnum;
19616 pos.col = (colnr_T)STRLEN(ml_get_curline());
19617 }
19618 return &pos;
19619 }
19620 return NULL;
19621}
19622
19623/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019624 * Convert list in "arg" into a position and optional file number.
19625 * When "fnump" is NULL there is no file number, only 3 items.
19626 * Note that the column is passed on as-is, the caller may want to decrement
19627 * it to use 1 for the first column.
19628 * Return FAIL when conversion is not possible, doesn't check the position for
19629 * validity.
19630 */
19631 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019632list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019633 typval_T *arg;
19634 pos_T *posp;
19635 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019636 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019637{
19638 list_T *l = arg->vval.v_list;
19639 long i = 0;
19640 long n;
19641
Bram Moolenaar493c1782014-05-28 14:34:46 +020019642 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19643 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019644 if (arg->v_type != VAR_LIST
19645 || l == NULL
19646 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019647 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019648 return FAIL;
19649
19650 if (fnump != NULL)
19651 {
19652 n = list_find_nr(l, i++, NULL); /* fnum */
19653 if (n < 0)
19654 return FAIL;
19655 if (n == 0)
19656 n = curbuf->b_fnum; /* current buffer */
19657 *fnump = n;
19658 }
19659
19660 n = list_find_nr(l, i++, NULL); /* lnum */
19661 if (n < 0)
19662 return FAIL;
19663 posp->lnum = n;
19664
19665 n = list_find_nr(l, i++, NULL); /* col */
19666 if (n < 0)
19667 return FAIL;
19668 posp->col = n;
19669
19670#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019671 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019672 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019673 posp->coladd = 0;
19674 else
19675 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019676#endif
19677
Bram Moolenaar493c1782014-05-28 14:34:46 +020019678 if (curswantp != NULL)
19679 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19680
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019681 return OK;
19682}
19683
19684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 * Get the length of an environment variable name.
19686 * Advance "arg" to the first character after the name.
19687 * Return 0 for error.
19688 */
19689 static int
19690get_env_len(arg)
19691 char_u **arg;
19692{
19693 char_u *p;
19694 int len;
19695
19696 for (p = *arg; vim_isIDc(*p); ++p)
19697 ;
19698 if (p == *arg) /* no name found */
19699 return 0;
19700
19701 len = (int)(p - *arg);
19702 *arg = p;
19703 return len;
19704}
19705
19706/*
19707 * Get the length of the name of a function or internal variable.
19708 * "arg" is advanced to the first non-white character after the name.
19709 * Return 0 if something is wrong.
19710 */
19711 static int
19712get_id_len(arg)
19713 char_u **arg;
19714{
19715 char_u *p;
19716 int len;
19717
19718 /* Find the end of the name. */
19719 for (p = *arg; eval_isnamec(*p); ++p)
19720 ;
19721 if (p == *arg) /* no name found */
19722 return 0;
19723
19724 len = (int)(p - *arg);
19725 *arg = skipwhite(p);
19726
19727 return len;
19728}
19729
19730/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019731 * Get the length of the name of a variable or function.
19732 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019734 * Return -1 if curly braces expansion failed.
19735 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 * If the name contains 'magic' {}'s, expand them and return the
19737 * expanded name in an allocated string via 'alias' - caller must free.
19738 */
19739 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019740get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 char_u **arg;
19742 char_u **alias;
19743 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019744 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745{
19746 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 char_u *p;
19748 char_u *expr_start;
19749 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750
19751 *alias = NULL; /* default to no alias */
19752
19753 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19754 && (*arg)[2] == (int)KE_SNR)
19755 {
19756 /* hard coded <SNR>, already translated */
19757 *arg += 3;
19758 return get_id_len(arg) + 3;
19759 }
19760 len = eval_fname_script(*arg);
19761 if (len > 0)
19762 {
19763 /* literal "<SID>", "s:" or "<SNR>" */
19764 *arg += len;
19765 }
19766
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019770 p = find_name_end(*arg, &expr_start, &expr_end,
19771 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 if (expr_start != NULL)
19773 {
19774 char_u *temp_string;
19775
19776 if (!evaluate)
19777 {
19778 len += (int)(p - *arg);
19779 *arg = skipwhite(p);
19780 return len;
19781 }
19782
19783 /*
19784 * Include any <SID> etc in the expanded string:
19785 * Thus the -len here.
19786 */
19787 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19788 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019789 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 *alias = temp_string;
19791 *arg = skipwhite(p);
19792 return (int)STRLEN(temp_string);
19793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794
19795 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019796 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 EMSG2(_(e_invexpr2), *arg);
19798
19799 return len;
19800}
19801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802/*
19803 * Find the end of a variable or function name, taking care of magic braces.
19804 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19805 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019806 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 * Return a pointer to just after the name. Equal to "arg" if there is no
19808 * valid name.
19809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019811find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 char_u *arg;
19813 char_u **expr_start;
19814 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019815 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 int mb_nest = 0;
19818 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 char_u *p;
19820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 *expr_start = NULL;
19824 *expr_end = NULL;
19825 }
19826
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019827 /* Quick check for valid starting character. */
19828 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19829 return arg;
19830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 for (p = arg; *p != NUL
19832 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019833 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019834 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019836 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019838 if (*p == '\'')
19839 {
19840 /* skip over 'string' to avoid counting [ and ] inside it. */
19841 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19842 ;
19843 if (*p == NUL)
19844 break;
19845 }
19846 else if (*p == '"')
19847 {
19848 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19849 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19850 if (*p == '\\' && p[1] != NUL)
19851 ++p;
19852 if (*p == NUL)
19853 break;
19854 }
19855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858 if (*p == '[')
19859 ++br_nest;
19860 else if (*p == ']')
19861 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019864 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019866 if (*p == '{')
19867 {
19868 mb_nest++;
19869 if (expr_start != NULL && *expr_start == NULL)
19870 *expr_start = p;
19871 }
19872 else if (*p == '}')
19873 {
19874 mb_nest--;
19875 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19876 *expr_end = p;
19877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 }
19880
19881 return p;
19882}
19883
19884/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885 * Expands out the 'magic' {}'s in a variable/function name.
19886 * Note that this can call itself recursively, to deal with
19887 * constructs like foo{bar}{baz}{bam}
19888 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19889 * "in_start" ^
19890 * "expr_start" ^
19891 * "expr_end" ^
19892 * "in_end" ^
19893 *
19894 * Returns a new allocated string, which the caller must free.
19895 * Returns NULL for failure.
19896 */
19897 static char_u *
19898make_expanded_name(in_start, expr_start, expr_end, in_end)
19899 char_u *in_start;
19900 char_u *expr_start;
19901 char_u *expr_end;
19902 char_u *in_end;
19903{
19904 char_u c1;
19905 char_u *retval = NULL;
19906 char_u *temp_result;
19907 char_u *nextcmd = NULL;
19908
19909 if (expr_end == NULL || in_end == NULL)
19910 return NULL;
19911 *expr_start = NUL;
19912 *expr_end = NUL;
19913 c1 = *in_end;
19914 *in_end = NUL;
19915
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019916 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019917 if (temp_result != NULL && nextcmd == NULL)
19918 {
19919 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19920 + (in_end - expr_end) + 1));
19921 if (retval != NULL)
19922 {
19923 STRCPY(retval, in_start);
19924 STRCAT(retval, temp_result);
19925 STRCAT(retval, expr_end + 1);
19926 }
19927 }
19928 vim_free(temp_result);
19929
19930 *in_end = c1; /* put char back for error messages */
19931 *expr_start = '{';
19932 *expr_end = '}';
19933
19934 if (retval != NULL)
19935 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019936 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019937 if (expr_start != NULL)
19938 {
19939 /* Further expansion! */
19940 temp_result = make_expanded_name(retval, expr_start,
19941 expr_end, temp_result);
19942 vim_free(retval);
19943 retval = temp_result;
19944 }
19945 }
19946
19947 return retval;
19948}
19949
19950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019952 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 */
19954 static int
19955eval_isnamec(c)
19956 int c;
19957{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019958 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19959}
19960
19961/*
19962 * Return TRUE if character "c" can be used as the first character in a
19963 * variable or function name (excluding '{' and '}').
19964 */
19965 static int
19966eval_isnamec1(c)
19967 int c;
19968{
19969 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 * Set number v: variable to "val".
19974 */
19975 void
19976set_vim_var_nr(idx, val)
19977 int idx;
19978 long val;
19979{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019980 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981}
19982
19983/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019984 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 */
19986 long
19987get_vim_var_nr(idx)
19988 int idx;
19989{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019990 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991}
19992
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019993/*
19994 * Get string v: variable value. Uses a static buffer, can only be used once.
19995 */
19996 char_u *
19997get_vim_var_str(idx)
19998 int idx;
19999{
20000 return get_tv_string(&vimvars[idx].vv_tv);
20001}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020002
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020004 * Get List v: variable value. Caller must take care of reference count when
20005 * needed.
20006 */
20007 list_T *
20008get_vim_var_list(idx)
20009 int idx;
20010{
20011 return vimvars[idx].vv_list;
20012}
20013
20014/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020015 * Set v:char to character "c".
20016 */
20017 void
20018set_vim_var_char(c)
20019 int c;
20020{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020021 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020022
20023#ifdef FEAT_MBYTE
20024 if (has_mbyte)
20025 buf[(*mb_char2bytes)(c, buf)] = NUL;
20026 else
20027#endif
20028 {
20029 buf[0] = c;
20030 buf[1] = NUL;
20031 }
20032 set_vim_var_string(VV_CHAR, buf, -1);
20033}
20034
20035/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020036 * Set v:count to "count" and v:count1 to "count1".
20037 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 */
20039 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020040set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 long count;
20042 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020043 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020045 if (set_prevcount)
20046 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020047 vimvars[VV_COUNT].vv_nr = count;
20048 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049}
20050
20051/*
20052 * Set string v: variable to a copy of "val".
20053 */
20054 void
20055set_vim_var_string(idx, val, len)
20056 int idx;
20057 char_u *val;
20058 int len; /* length of "val" to use or -1 (whole string) */
20059{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020060 /* Need to do this (at least) once, since we can't initialize a union.
20061 * Will always be invoked when "v:progname" is set. */
20062 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20063
Bram Moolenaare9a41262005-01-15 22:18:47 +000020064 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020066 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020068 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020070 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071}
20072
20073/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020074 * Set List v: variable to "val".
20075 */
20076 void
20077set_vim_var_list(idx, val)
20078 int idx;
20079 list_T *val;
20080{
20081 list_unref(vimvars[idx].vv_list);
20082 vimvars[idx].vv_list = val;
20083 if (val != NULL)
20084 ++val->lv_refcount;
20085}
20086
20087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 * Set v:register if needed.
20089 */
20090 void
20091set_reg_var(c)
20092 int c;
20093{
20094 char_u regname;
20095
20096 if (c == 0 || c == ' ')
20097 regname = '"';
20098 else
20099 regname = c;
20100 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020101 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 set_vim_var_string(VV_REG, &regname, 1);
20103}
20104
20105/*
20106 * Get or set v:exception. If "oldval" == NULL, return the current value.
20107 * Otherwise, restore the value to "oldval" and return NULL.
20108 * Must always be called in pairs to save and restore v:exception! Does not
20109 * take care of memory allocations.
20110 */
20111 char_u *
20112v_exception(oldval)
20113 char_u *oldval;
20114{
20115 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020116 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117
Bram Moolenaare9a41262005-01-15 22:18:47 +000020118 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 return NULL;
20120}
20121
20122/*
20123 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20124 * Otherwise, restore the value to "oldval" and return NULL.
20125 * Must always be called in pairs to save and restore v:throwpoint! Does not
20126 * take care of memory allocations.
20127 */
20128 char_u *
20129v_throwpoint(oldval)
20130 char_u *oldval;
20131{
20132 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020133 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134
Bram Moolenaare9a41262005-01-15 22:18:47 +000020135 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 return NULL;
20137}
20138
20139#if defined(FEAT_AUTOCMD) || defined(PROTO)
20140/*
20141 * Set v:cmdarg.
20142 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20143 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20144 * Must always be called in pairs!
20145 */
20146 char_u *
20147set_cmdarg(eap, oldarg)
20148 exarg_T *eap;
20149 char_u *oldarg;
20150{
20151 char_u *oldval;
20152 char_u *newval;
20153 unsigned len;
20154
Bram Moolenaare9a41262005-01-15 22:18:47 +000020155 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020156 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020158 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020159 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020160 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 }
20162
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020163 if (eap->force_bin == FORCE_BIN)
20164 len = 6;
20165 else if (eap->force_bin == FORCE_NOBIN)
20166 len = 8;
20167 else
20168 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020169
20170 if (eap->read_edit)
20171 len += 7;
20172
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020173 if (eap->force_ff != 0)
20174 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20175# ifdef FEAT_MBYTE
20176 if (eap->force_enc != 0)
20177 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020178 if (eap->bad_char != 0)
20179 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020180# endif
20181
20182 newval = alloc(len + 1);
20183 if (newval == NULL)
20184 return NULL;
20185
20186 if (eap->force_bin == FORCE_BIN)
20187 sprintf((char *)newval, " ++bin");
20188 else if (eap->force_bin == FORCE_NOBIN)
20189 sprintf((char *)newval, " ++nobin");
20190 else
20191 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020192
20193 if (eap->read_edit)
20194 STRCAT(newval, " ++edit");
20195
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020196 if (eap->force_ff != 0)
20197 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20198 eap->cmd + eap->force_ff);
20199# ifdef FEAT_MBYTE
20200 if (eap->force_enc != 0)
20201 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20202 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020203 if (eap->bad_char == BAD_KEEP)
20204 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20205 else if (eap->bad_char == BAD_DROP)
20206 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20207 else if (eap->bad_char != 0)
20208 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020209# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020210 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020211 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212}
20213#endif
20214
20215/*
20216 * Get the value of internal variable "name".
20217 * Return OK or FAIL.
20218 */
20219 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020220get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 char_u *name;
20222 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020224 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020225 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226{
20227 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 typval_T *tv = NULL;
20229 typval_T atv;
20230 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232
20233 /* truncate the name, so that we can use strcmp() */
20234 cc = name[len];
20235 name[len] = NUL;
20236
20237 /*
20238 * Check for "b:changedtick".
20239 */
20240 if (STRCMP(name, "b:changedtick") == 0)
20241 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020242 atv.v_type = VAR_NUMBER;
20243 atv.vval.v_number = curbuf->b_changedtick;
20244 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 }
20246
20247 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 * Check for user-defined variables.
20249 */
20250 else
20251 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020252 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 }
20256
Bram Moolenaare9a41262005-01-15 22:18:47 +000020257 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020259 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020260 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 ret = FAIL;
20262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020264 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265
20266 name[len] = cc;
20267
20268 return ret;
20269}
20270
20271/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020272 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20273 * Also handle function call with Funcref variable: func(expr)
20274 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20275 */
20276 static int
20277handle_subscript(arg, rettv, evaluate, verbose)
20278 char_u **arg;
20279 typval_T *rettv;
20280 int evaluate; /* do more than finding the end */
20281 int verbose; /* give error messages */
20282{
20283 int ret = OK;
20284 dict_T *selfdict = NULL;
20285 char_u *s;
20286 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020287 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020288
20289 while (ret == OK
20290 && (**arg == '['
20291 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020292 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020293 && !vim_iswhite(*(*arg - 1)))
20294 {
20295 if (**arg == '(')
20296 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020297 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020298 if (evaluate)
20299 {
20300 functv = *rettv;
20301 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020302
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020303 /* Invoke the function. Recursive! */
20304 s = functv.vval.v_string;
20305 }
20306 else
20307 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020308 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020309 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20310 &len, evaluate, selfdict);
20311
20312 /* Clear the funcref afterwards, so that deleting it while
20313 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020314 if (evaluate)
20315 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020316
20317 /* Stop the expression evaluation when immediately aborting on
20318 * error, or when an interrupt occurred or an exception was thrown
20319 * but not caught. */
20320 if (aborting())
20321 {
20322 if (ret == OK)
20323 clear_tv(rettv);
20324 ret = FAIL;
20325 }
20326 dict_unref(selfdict);
20327 selfdict = NULL;
20328 }
20329 else /* **arg == '[' || **arg == '.' */
20330 {
20331 dict_unref(selfdict);
20332 if (rettv->v_type == VAR_DICT)
20333 {
20334 selfdict = rettv->vval.v_dict;
20335 if (selfdict != NULL)
20336 ++selfdict->dv_refcount;
20337 }
20338 else
20339 selfdict = NULL;
20340 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20341 {
20342 clear_tv(rettv);
20343 ret = FAIL;
20344 }
20345 }
20346 }
20347 dict_unref(selfdict);
20348 return ret;
20349}
20350
20351/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020352 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020353 * value).
20354 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020357{
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020359}
20360
20361/*
20362 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 * The string "s" must have been allocated, it is consumed.
20364 * Return NULL for out of memory, the variable otherwise.
20365 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 char_u *s;
20369{
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020372 rettv = alloc_tv();
20373 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 rettv->v_type = VAR_STRING;
20376 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 }
20378 else
20379 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381}
20382
20383/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020384 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020386 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020388 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
20390 if (varp != NULL)
20391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020392 switch (varp->v_type)
20393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020394 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020395 func_unref(varp->vval.v_string);
20396 /*FALLTHROUGH*/
20397 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020398 vim_free(varp->vval.v_string);
20399 break;
20400 case VAR_LIST:
20401 list_unref(varp->vval.v_list);
20402 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020403 case VAR_DICT:
20404 dict_unref(varp->vval.v_dict);
20405 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020406 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020407#ifdef FEAT_FLOAT
20408 case VAR_FLOAT:
20409#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020410 case VAR_UNKNOWN:
20411 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020412 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020413 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020414 break;
20415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 vim_free(varp);
20417 }
20418}
20419
20420/*
20421 * Free the memory for a variable value and set the value to NULL or 0.
20422 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020423 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020425 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426{
20427 if (varp != NULL)
20428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020429 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020431 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020432 func_unref(varp->vval.v_string);
20433 /*FALLTHROUGH*/
20434 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020435 vim_free(varp->vval.v_string);
20436 varp->vval.v_string = NULL;
20437 break;
20438 case VAR_LIST:
20439 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020440 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020441 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020442 case VAR_DICT:
20443 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020444 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020445 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020446 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020447 varp->vval.v_number = 0;
20448 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020449#ifdef FEAT_FLOAT
20450 case VAR_FLOAT:
20451 varp->vval.v_float = 0.0;
20452 break;
20453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454 case VAR_UNKNOWN:
20455 break;
20456 default:
20457 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020459 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 }
20461}
20462
20463/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020464 * Set the value of a variable to NULL without freeing items.
20465 */
20466 static void
20467init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020468 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469{
20470 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020472}
20473
20474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 * Get the number value of a variable.
20476 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020477 * For incompatible types, return 0.
20478 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20479 * caller of incompatible types: it sets *denote to TRUE if "denote"
20480 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 */
20482 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020486 int error = FALSE;
20487
20488 return get_tv_number_chk(varp, &error); /* return 0L on error */
20489}
20490
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020491 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020492get_tv_number_chk(varp, denote)
20493 typval_T *varp;
20494 int *denote;
20495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020500 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020501 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020502#ifdef FEAT_FLOAT
20503 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020504 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020505 break;
20506#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020507 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020508 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 break;
20510 case VAR_STRING:
20511 if (varp->vval.v_string != NULL)
20512 vim_str2nr(varp->vval.v_string, NULL, NULL,
20513 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020514 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020515 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020516 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020517 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020518 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020519 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020520 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020521 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020522 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020525 if (denote == NULL) /* useful for values that must be unsigned */
20526 n = -1;
20527 else
20528 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020529 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530}
20531
20532/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020533 * Get the lnum from the first argument.
20534 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020535 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 */
20537 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020539 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540{
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 linenr_T lnum;
20543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020544 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 if (lnum == 0) /* no valid number, try using line() */
20546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547 rettv.v_type = VAR_NUMBER;
20548 f_line(argvars, &rettv);
20549 lnum = rettv.vval.v_number;
20550 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 }
20552 return lnum;
20553}
20554
20555/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020556 * Get the lnum from the first argument.
20557 * Also accepts "$", then "buf" is used.
20558 * Returns 0 on error.
20559 */
20560 static linenr_T
20561get_tv_lnum_buf(argvars, buf)
20562 typval_T *argvars;
20563 buf_T *buf;
20564{
20565 if (argvars[0].v_type == VAR_STRING
20566 && argvars[0].vval.v_string != NULL
20567 && argvars[0].vval.v_string[0] == '$'
20568 && buf != NULL)
20569 return buf->b_ml.ml_line_count;
20570 return get_tv_number_chk(&argvars[0], NULL);
20571}
20572
20573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 * Get the string value of a variable.
20575 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020576 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20577 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 * If the String variable has never been set, return an empty string.
20579 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020580 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20581 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 */
20583 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020584get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020585 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586{
20587 static char_u mybuf[NUMBUFLEN];
20588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590}
20591
20592 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020594 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020595 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020597 char_u *res = get_tv_string_buf_chk(varp, buf);
20598
20599 return res != NULL ? res : (char_u *)"";
20600}
20601
Bram Moolenaar7d647822014-04-05 21:28:56 +020020602/*
20603 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20604 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020605 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020606get_tv_string_chk(varp)
20607 typval_T *varp;
20608{
20609 static char_u mybuf[NUMBUFLEN];
20610
20611 return get_tv_string_buf_chk(varp, mybuf);
20612}
20613
20614 static char_u *
20615get_tv_string_buf_chk(varp, buf)
20616 typval_T *varp;
20617 char_u *buf;
20618{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020619 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020621 case VAR_NUMBER:
20622 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20623 return buf;
20624 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020625 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020626 break;
20627 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020628 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020629 break;
20630 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020632 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020633#ifdef FEAT_FLOAT
20634 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020635 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020636 break;
20637#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020638 case VAR_STRING:
20639 if (varp->vval.v_string != NULL)
20640 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020641 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020642 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020643 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020646 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647}
20648
20649/*
20650 * Find variable "name" in the list of variables.
20651 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020652 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020653 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020654 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020656 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020657find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020659 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020660 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664
Bram Moolenaara7043832005-01-21 11:56:39 +000020665 ht = find_var_ht(name, &varname);
20666 if (htp != NULL)
20667 *htp = ht;
20668 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020670 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671}
20672
20673/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020674 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020675 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020677 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020678find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020680 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020681 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020682 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020683{
Bram Moolenaar33570922005-01-25 22:26:29 +000020684 hashitem_T *hi;
20685
20686 if (*varname == NUL)
20687 {
20688 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020689 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020691 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 case 'g': return &globvars_var;
20693 case 'v': return &vimvars_var;
20694 case 'b': return &curbuf->b_bufvar;
20695 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020696#ifdef FEAT_WINDOWS
20697 case 't': return &curtab->tp_winvar;
20698#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020699 case 'l': return current_funccal == NULL
20700 ? NULL : &current_funccal->l_vars_var;
20701 case 'a': return current_funccal == NULL
20702 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020703 }
20704 return NULL;
20705 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020706
20707 hi = hash_find(ht, varname);
20708 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020709 {
20710 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020711 * worked find the variable again. Don't auto-load a script if it was
20712 * loaded already, otherwise it would be loaded every time when
20713 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020714 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020715 {
20716 /* Note: script_autoload() may make "hi" invalid. It must either
20717 * be obtained again or not used. */
20718 if (!script_autoload(varname, FALSE) || aborting())
20719 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020720 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020721 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020722 if (HASHITEM_EMPTY(hi))
20723 return NULL;
20724 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020725 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020726}
20727
20728/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020729 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020730 * Set "varname" to the start of name without ':'.
20731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020732 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020733find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 char_u *name;
20735 char_u **varname;
20736{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020737 hashitem_T *hi;
20738
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 if (name[1] != ':')
20740 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020741 /* The name must not start with a colon or #. */
20742 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 return NULL;
20744 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020745
20746 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020747 hi = hash_find(&compat_hashtab, name);
20748 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020749 return &compat_hashtab;
20750
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020752 return &globvarht; /* global variable */
20753 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 }
20755 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020756 if (*name == 'g') /* global variable */
20757 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020758 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20759 */
20760 if (vim_strchr(name + 2, ':') != NULL
20761 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020762 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020764 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020766 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020767#ifdef FEAT_WINDOWS
20768 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020769 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020770#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020771 if (*name == 'v') /* v: variable */
20772 return &vimvarht;
20773 if (*name == 'a' && current_funccal != NULL) /* function argument */
20774 return &current_funccal->l_avars.dv_hashtab;
20775 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20776 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 if (*name == 's' /* script variable */
20778 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20779 return &SCRIPT_VARS(current_SID);
20780 return NULL;
20781}
20782
20783/*
20784 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020785 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 * Returns NULL when it doesn't exist.
20787 */
20788 char_u *
20789get_var_value(name)
20790 char_u *name;
20791{
Bram Moolenaar33570922005-01-25 22:26:29 +000020792 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020794 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 if (v == NULL)
20796 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020797 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798}
20799
20800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 * sourcing this script and when executing functions defined in the script.
20803 */
20804 void
20805new_script_vars(id)
20806 scid_T id;
20807{
Bram Moolenaara7043832005-01-21 11:56:39 +000020808 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 hashtab_T *ht;
20810 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020811
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20813 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020814 /* Re-allocating ga_data means that an ht_array pointing to
20815 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020816 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020817 for (i = 1; i <= ga_scripts.ga_len; ++i)
20818 {
20819 ht = &SCRIPT_VARS(i);
20820 if (ht->ht_mask == HT_INIT_SIZE - 1)
20821 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020822 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020823 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020824 }
20825
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 while (ga_scripts.ga_len < id)
20827 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020828 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020829 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020830 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 }
20833 }
20834}
20835
20836/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20838 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 */
20840 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020841init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020842 dict_T *dict;
20843 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020844 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020847 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020848 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020849 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020850 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020851 dict_var->di_tv.vval.v_dict = dict;
20852 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020853 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020854 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20855 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856}
20857
20858/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020859 * Unreference a dictionary initialized by init_var_dict().
20860 */
20861 void
20862unref_var_dict(dict)
20863 dict_T *dict;
20864{
20865 /* Now the dict needs to be freed if no one else is using it, go back to
20866 * normal reference counting. */
20867 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20868 dict_unref(dict);
20869}
20870
20871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020873 * Frees all allocated variables and the value they contain.
20874 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 */
20876 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020877vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020878 hashtab_T *ht;
20879{
20880 vars_clear_ext(ht, TRUE);
20881}
20882
20883/*
20884 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20885 */
20886 static void
20887vars_clear_ext(ht, free_val)
20888 hashtab_T *ht;
20889 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890{
Bram Moolenaara7043832005-01-21 11:56:39 +000020891 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020892 hashitem_T *hi;
20893 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894
Bram Moolenaar33570922005-01-25 22:26:29 +000020895 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020896 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020897 for (hi = ht->ht_array; todo > 0; ++hi)
20898 {
20899 if (!HASHITEM_EMPTY(hi))
20900 {
20901 --todo;
20902
Bram Moolenaar33570922005-01-25 22:26:29 +000020903 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020904 * ht_array might change then. hash_clear() takes care of it
20905 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020906 v = HI2DI(hi);
20907 if (free_val)
20908 clear_tv(&v->di_tv);
20909 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20910 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020911 }
20912 }
20913 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020914 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915}
20916
Bram Moolenaara7043832005-01-21 11:56:39 +000020917/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020918 * Delete a variable from hashtab "ht" at item "hi".
20919 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020922delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020923 hashtab_T *ht;
20924 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925{
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020927
20928 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 clear_tv(&di->di_tv);
20930 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931}
20932
20933/*
20934 * List the value of one internal variable.
20935 */
20936 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020937list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020940 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020942 char_u *tofree;
20943 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020944 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020945
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020946 current_copyID += COPYID_INC;
20947 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020948 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020949 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020950 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951}
20952
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020954list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 char_u *prefix;
20956 char_u *name;
20957 int type;
20958 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020959 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960{
Bram Moolenaar31859182007-08-14 20:41:13 +000020961 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20962 msg_start();
20963 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (name != NULL) /* "a:" vars don't have a name stored */
20965 msg_puts(name);
20966 msg_putchar(' ');
20967 msg_advance(22);
20968 if (type == VAR_NUMBER)
20969 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020970 else if (type == VAR_FUNC)
20971 msg_putchar('*');
20972 else if (type == VAR_LIST)
20973 {
20974 msg_putchar('[');
20975 if (*string == '[')
20976 ++string;
20977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020978 else if (type == VAR_DICT)
20979 {
20980 msg_putchar('{');
20981 if (*string == '{')
20982 ++string;
20983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 else
20985 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020986
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020988
20989 if (type == VAR_FUNC)
20990 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020991 if (*first)
20992 {
20993 msg_clr_eos();
20994 *first = FALSE;
20995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996}
20997
20998/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 * If the variable already exists, the value is updated.
21001 * Otherwise the variable is created.
21002 */
21003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021007 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008{
Bram Moolenaar33570922005-01-25 22:26:29 +000021009 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021011 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021013 ht = find_var_ht(name, &varname);
21014 if (ht == NULL || *varname == NUL)
21015 {
21016 EMSG2(_(e_illvar), name);
21017 return;
21018 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021019 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021020
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021021 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21022 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021023
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021026 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021027 if (var_check_ro(v->di_flags, name)
21028 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021029 return;
21030 if (v->di_tv.v_type != tv->v_type
21031 && !((v->di_tv.v_type == VAR_STRING
21032 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021033 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021034 || tv->v_type == VAR_NUMBER))
21035#ifdef FEAT_FLOAT
21036 && !((v->di_tv.v_type == VAR_NUMBER
21037 || v->di_tv.v_type == VAR_FLOAT)
21038 && (tv->v_type == VAR_NUMBER
21039 || tv->v_type == VAR_FLOAT))
21040#endif
21041 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021043 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021044 return;
21045 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021046
21047 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021048 * Handle setting internal v: variables separately: we don't change
21049 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021050 */
21051 if (ht == &vimvarht)
21052 {
21053 if (v->di_tv.v_type == VAR_STRING)
21054 {
21055 vim_free(v->di_tv.vval.v_string);
21056 if (copy || tv->v_type != VAR_STRING)
21057 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21058 else
21059 {
21060 /* Take over the string to avoid an extra alloc/free. */
21061 v->di_tv.vval.v_string = tv->vval.v_string;
21062 tv->vval.v_string = NULL;
21063 }
21064 }
21065 else if (v->di_tv.v_type != VAR_NUMBER)
21066 EMSG2(_(e_intern2), "set_var()");
21067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021068 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021070 if (STRCMP(varname, "searchforward") == 0)
21071 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021072#ifdef FEAT_SEARCH_EXTRA
21073 else if (STRCMP(varname, "hlsearch") == 0)
21074 {
21075 no_hlsearch = !v->di_tv.vval.v_number;
21076 redraw_all_later(SOME_VALID);
21077 }
21078#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021079 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021080 return;
21081 }
21082
21083 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 }
21085 else /* add a new variable */
21086 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021087 /* Can't add "v:" variable. */
21088 if (ht == &vimvarht)
21089 {
21090 EMSG2(_(e_illvar), name);
21091 return;
21092 }
21093
Bram Moolenaar92124a32005-06-17 22:03:40 +000021094 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021095 if (!valid_varname(varname))
21096 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021097
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021098 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21099 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021100 if (v == NULL)
21101 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021102 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021103 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021105 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 return;
21107 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021108 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021110
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021111 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021112 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021113 else
21114 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021115 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021116 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021117 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119}
21120
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021121/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021122 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021123 * Also give an error message.
21124 */
21125 static int
21126var_check_ro(flags, name)
21127 int flags;
21128 char_u *name;
21129{
21130 if (flags & DI_FLAGS_RO)
21131 {
21132 EMSG2(_(e_readonlyvar), name);
21133 return TRUE;
21134 }
21135 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21136 {
21137 EMSG2(_(e_readonlysbx), name);
21138 return TRUE;
21139 }
21140 return FALSE;
21141}
21142
21143/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021144 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21145 * Also give an error message.
21146 */
21147 static int
21148var_check_fixed(flags, name)
21149 int flags;
21150 char_u *name;
21151{
21152 if (flags & DI_FLAGS_FIX)
21153 {
21154 EMSG2(_("E795: Cannot delete variable %s"), name);
21155 return TRUE;
21156 }
21157 return FALSE;
21158}
21159
21160/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021161 * Check if a funcref is assigned to a valid variable name.
21162 * Return TRUE and give an error if not.
21163 */
21164 static int
21165var_check_func_name(name, new_var)
21166 char_u *name; /* points to start of variable name */
21167 int new_var; /* TRUE when creating the variable */
21168{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021169 /* Allow for w: b: s: and t:. */
21170 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021171 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21172 ? name[2] : name[0]))
21173 {
21174 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21175 name);
21176 return TRUE;
21177 }
21178 /* Don't allow hiding a function. When "v" is not NULL we might be
21179 * assigning another function to the same var, the type is checked
21180 * below. */
21181 if (new_var && function_exists(name))
21182 {
21183 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21184 name);
21185 return TRUE;
21186 }
21187 return FALSE;
21188}
21189
21190/*
21191 * Check if a variable name is valid.
21192 * Return FALSE and give an error if not.
21193 */
21194 static int
21195valid_varname(varname)
21196 char_u *varname;
21197{
21198 char_u *p;
21199
21200 for (p = varname; *p != NUL; ++p)
21201 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21202 && *p != AUTOLOAD_CHAR)
21203 {
21204 EMSG2(_(e_illvar), varname);
21205 return FALSE;
21206 }
21207 return TRUE;
21208}
21209
21210/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021211 * Return TRUE if typeval "tv" is set to be locked (immutable).
21212 * Also give an error message, using "name".
21213 */
21214 static int
21215tv_check_lock(lock, name)
21216 int lock;
21217 char_u *name;
21218{
21219 if (lock & VAR_LOCKED)
21220 {
21221 EMSG2(_("E741: Value is locked: %s"),
21222 name == NULL ? (char_u *)_("Unknown") : name);
21223 return TRUE;
21224 }
21225 if (lock & VAR_FIXED)
21226 {
21227 EMSG2(_("E742: Cannot change value of %s"),
21228 name == NULL ? (char_u *)_("Unknown") : name);
21229 return TRUE;
21230 }
21231 return FALSE;
21232}
21233
21234/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021235 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021236 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021238 * It is OK for "from" and "to" to point to the same item. This is used to
21239 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021240 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021241 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021242copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021243 typval_T *from;
21244 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021246 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021247 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021248 switch (from->v_type)
21249 {
21250 case VAR_NUMBER:
21251 to->vval.v_number = from->vval.v_number;
21252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021253#ifdef FEAT_FLOAT
21254 case VAR_FLOAT:
21255 to->vval.v_float = from->vval.v_float;
21256 break;
21257#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 case VAR_STRING:
21259 case VAR_FUNC:
21260 if (from->vval.v_string == NULL)
21261 to->vval.v_string = NULL;
21262 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021263 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021264 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021265 if (from->v_type == VAR_FUNC)
21266 func_ref(to->vval.v_string);
21267 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 break;
21269 case VAR_LIST:
21270 if (from->vval.v_list == NULL)
21271 to->vval.v_list = NULL;
21272 else
21273 {
21274 to->vval.v_list = from->vval.v_list;
21275 ++to->vval.v_list->lv_refcount;
21276 }
21277 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021278 case VAR_DICT:
21279 if (from->vval.v_dict == NULL)
21280 to->vval.v_dict = NULL;
21281 else
21282 {
21283 to->vval.v_dict = from->vval.v_dict;
21284 ++to->vval.v_dict->dv_refcount;
21285 }
21286 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021287 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021288 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 break;
21290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021294 * Make a copy of an item.
21295 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021296 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21297 * reference to an already copied list/dict can be used.
21298 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021299 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021300 static int
21301item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 typval_T *from;
21303 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021304 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021305 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021306{
21307 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021308 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021309
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021311 {
21312 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021313 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021314 }
21315 ++recurse;
21316
21317 switch (from->v_type)
21318 {
21319 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021320#ifdef FEAT_FLOAT
21321 case VAR_FLOAT:
21322#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021323 case VAR_STRING:
21324 case VAR_FUNC:
21325 copy_tv(from, to);
21326 break;
21327 case VAR_LIST:
21328 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021329 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021330 if (from->vval.v_list == NULL)
21331 to->vval.v_list = NULL;
21332 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21333 {
21334 /* use the copy made earlier */
21335 to->vval.v_list = from->vval.v_list->lv_copylist;
21336 ++to->vval.v_list->lv_refcount;
21337 }
21338 else
21339 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21340 if (to->vval.v_list == NULL)
21341 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021342 break;
21343 case VAR_DICT:
21344 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021345 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021346 if (from->vval.v_dict == NULL)
21347 to->vval.v_dict = NULL;
21348 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21349 {
21350 /* use the copy made earlier */
21351 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21352 ++to->vval.v_dict->dv_refcount;
21353 }
21354 else
21355 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21356 if (to->vval.v_dict == NULL)
21357 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021358 break;
21359 default:
21360 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021361 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021362 }
21363 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021364 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021365}
21366
21367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 * ":echo expr1 ..." print each argument separated with a space, add a
21369 * newline at the end.
21370 * ":echon expr1 ..." print each argument plain.
21371 */
21372 void
21373ex_echo(eap)
21374 exarg_T *eap;
21375{
21376 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021378 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 char_u *p;
21380 int needclr = TRUE;
21381 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021382 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383
21384 if (eap->skip)
21385 ++emsg_skip;
21386 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21387 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021388 /* If eval1() causes an error message the text from the command may
21389 * still need to be cleared. E.g., "echo 22,44". */
21390 need_clr_eos = needclr;
21391
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021393 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 {
21395 /*
21396 * Report the invalid expression unless the expression evaluation
21397 * has been cancelled due to an aborting error, an interrupt, or an
21398 * exception.
21399 */
21400 if (!aborting())
21401 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021402 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 break;
21404 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021405 need_clr_eos = FALSE;
21406
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 if (!eap->skip)
21408 {
21409 if (atstart)
21410 {
21411 atstart = FALSE;
21412 /* Call msg_start() after eval1(), evaluating the expression
21413 * may cause a message to appear. */
21414 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021415 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021416 /* Mark the saved text as finishing the line, so that what
21417 * follows is displayed on a new line when scrolling back
21418 * at the more prompt. */
21419 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 }
21423 else if (eap->cmdidx == CMD_echo)
21424 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021425 current_copyID += COPYID_INC;
21426 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021427 if (p != NULL)
21428 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021430 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021432 if (*p != TAB && needclr)
21433 {
21434 /* remove any text still there from the command */
21435 msg_clr_eos();
21436 needclr = FALSE;
21437 }
21438 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 }
21440 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021441 {
21442#ifdef FEAT_MBYTE
21443 if (has_mbyte)
21444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021445 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021446
21447 (void)msg_outtrans_len_attr(p, i, echo_attr);
21448 p += i - 1;
21449 }
21450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021452 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 arg = skipwhite(arg);
21459 }
21460 eap->nextcmd = check_nextcmd(arg);
21461
21462 if (eap->skip)
21463 --emsg_skip;
21464 else
21465 {
21466 /* remove text that may still be there from the command */
21467 if (needclr)
21468 msg_clr_eos();
21469 if (eap->cmdidx == CMD_echo)
21470 msg_end();
21471 }
21472}
21473
21474/*
21475 * ":echohl {name}".
21476 */
21477 void
21478ex_echohl(eap)
21479 exarg_T *eap;
21480{
21481 int id;
21482
21483 id = syn_name2id(eap->arg);
21484 if (id == 0)
21485 echo_attr = 0;
21486 else
21487 echo_attr = syn_id2attr(id);
21488}
21489
21490/*
21491 * ":execute expr1 ..." execute the result of an expression.
21492 * ":echomsg expr1 ..." Print a message
21493 * ":echoerr expr1 ..." Print an error
21494 * Each gets spaces around each argument and a newline at the end for
21495 * echo commands
21496 */
21497 void
21498ex_execute(eap)
21499 exarg_T *eap;
21500{
21501 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 int ret = OK;
21504 char_u *p;
21505 garray_T ga;
21506 int len;
21507 int save_did_emsg;
21508
21509 ga_init2(&ga, 1, 80);
21510
21511 if (eap->skip)
21512 ++emsg_skip;
21513 while (*arg != NUL && *arg != '|' && *arg != '\n')
21514 {
21515 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021516 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 {
21518 /*
21519 * Report the invalid expression unless the expression evaluation
21520 * has been cancelled due to an aborting error, an interrupt, or an
21521 * exception.
21522 */
21523 if (!aborting())
21524 EMSG2(_(e_invexpr2), p);
21525 ret = FAIL;
21526 break;
21527 }
21528
21529 if (!eap->skip)
21530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021531 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 len = (int)STRLEN(p);
21533 if (ga_grow(&ga, len + 2) == FAIL)
21534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 ret = FAIL;
21537 break;
21538 }
21539 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 ga.ga_len += len;
21543 }
21544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 arg = skipwhite(arg);
21547 }
21548
21549 if (ret != FAIL && ga.ga_data != NULL)
21550 {
21551 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021554 out_flush();
21555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 else if (eap->cmdidx == CMD_echoerr)
21557 {
21558 /* We don't want to abort following commands, restore did_emsg. */
21559 save_did_emsg = did_emsg;
21560 EMSG((char_u *)ga.ga_data);
21561 if (!force_abort)
21562 did_emsg = save_did_emsg;
21563 }
21564 else if (eap->cmdidx == CMD_execute)
21565 do_cmdline((char_u *)ga.ga_data,
21566 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21567 }
21568
21569 ga_clear(&ga);
21570
21571 if (eap->skip)
21572 --emsg_skip;
21573
21574 eap->nextcmd = check_nextcmd(arg);
21575}
21576
21577/*
21578 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21579 * "arg" points to the "&" or '+' when called, to "option" when returning.
21580 * Returns NULL when no option name found. Otherwise pointer to the char
21581 * after the option name.
21582 */
21583 static char_u *
21584find_option_end(arg, opt_flags)
21585 char_u **arg;
21586 int *opt_flags;
21587{
21588 char_u *p = *arg;
21589
21590 ++p;
21591 if (*p == 'g' && p[1] == ':')
21592 {
21593 *opt_flags = OPT_GLOBAL;
21594 p += 2;
21595 }
21596 else if (*p == 'l' && p[1] == ':')
21597 {
21598 *opt_flags = OPT_LOCAL;
21599 p += 2;
21600 }
21601 else
21602 *opt_flags = 0;
21603
21604 if (!ASCII_ISALPHA(*p))
21605 return NULL;
21606 *arg = p;
21607
21608 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21609 p += 4; /* termcap option */
21610 else
21611 while (ASCII_ISALPHA(*p))
21612 ++p;
21613 return p;
21614}
21615
21616/*
21617 * ":function"
21618 */
21619 void
21620ex_function(eap)
21621 exarg_T *eap;
21622{
21623 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021624 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 int j;
21626 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021628 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 char_u *name = NULL;
21630 char_u *p;
21631 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021632 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 garray_T newargs;
21634 garray_T newlines;
21635 int varargs = FALSE;
21636 int mustend = FALSE;
21637 int flags = 0;
21638 ufunc_T *fp;
21639 int indent;
21640 int nesting;
21641 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 dictitem_T *v;
21643 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644 static int func_nr = 0; /* number for nameless function */
21645 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021646 hashtab_T *ht;
21647 int todo;
21648 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021649 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650
21651 /*
21652 * ":function" without argument: list functions.
21653 */
21654 if (ends_excmd(*eap->arg))
21655 {
21656 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021657 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021658 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021659 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 {
21661 if (!HASHITEM_EMPTY(hi))
21662 {
21663 --todo;
21664 fp = HI2UF(hi);
21665 if (!isdigit(*fp->uf_name))
21666 list_func_head(fp, FALSE);
21667 }
21668 }
21669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 eap->nextcmd = check_nextcmd(eap->arg);
21671 return;
21672 }
21673
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021674 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021675 * ":function /pat": list functions matching pattern.
21676 */
21677 if (*eap->arg == '/')
21678 {
21679 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21680 if (!eap->skip)
21681 {
21682 regmatch_T regmatch;
21683
21684 c = *p;
21685 *p = NUL;
21686 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21687 *p = c;
21688 if (regmatch.regprog != NULL)
21689 {
21690 regmatch.rm_ic = p_ic;
21691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021692 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021693 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21694 {
21695 if (!HASHITEM_EMPTY(hi))
21696 {
21697 --todo;
21698 fp = HI2UF(hi);
21699 if (!isdigit(*fp->uf_name)
21700 && vim_regexec(&regmatch, fp->uf_name, 0))
21701 list_func_head(fp, FALSE);
21702 }
21703 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021704 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021705 }
21706 }
21707 if (*p == '/')
21708 ++p;
21709 eap->nextcmd = check_nextcmd(p);
21710 return;
21711 }
21712
21713 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714 * Get the function name. There are these situations:
21715 * func normal function name
21716 * "name" == func, "fudi.fd_dict" == NULL
21717 * dict.func new dictionary entry
21718 * "name" == NULL, "fudi.fd_dict" set,
21719 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21720 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021721 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21723 * dict.func existing dict entry that's not a Funcref
21724 * "name" == NULL, "fudi.fd_dict" set,
21725 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021726 * s:func script-local function name
21727 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021730 name = trans_function_name(&p, eap->skip, 0, &fudi);
21731 paren = (vim_strchr(p, '(') != NULL);
21732 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 {
21734 /*
21735 * Return on an invalid expression in braces, unless the expression
21736 * evaluation has been cancelled due to an aborting error, an
21737 * interrupt, or an exception.
21738 */
21739 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021741 if (!eap->skip && fudi.fd_newkey != NULL)
21742 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021743 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 else
21747 eap->skip = TRUE;
21748 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021749
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 /* An error in a function call during evaluation of an expression in magic
21751 * braces should not cause the function not to be defined. */
21752 saved_did_emsg = did_emsg;
21753 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754
21755 /*
21756 * ":function func" with only function name: list function.
21757 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021758 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 {
21760 if (!ends_excmd(*skipwhite(p)))
21761 {
21762 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021763 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 }
21765 eap->nextcmd = check_nextcmd(p);
21766 if (eap->nextcmd != NULL)
21767 *p = NUL;
21768 if (!eap->skip && !got_int)
21769 {
21770 fp = find_func(name);
21771 if (fp != NULL)
21772 {
21773 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021774 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021776 if (FUNCLINE(fp, j) == NULL)
21777 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 msg_putchar('\n');
21779 msg_outnum((long)(j + 1));
21780 if (j < 9)
21781 msg_putchar(' ');
21782 if (j < 99)
21783 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021784 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 out_flush(); /* show a line at a time */
21786 ui_breakcheck();
21787 }
21788 if (!got_int)
21789 {
21790 msg_putchar('\n');
21791 msg_puts((char_u *)" endfunction");
21792 }
21793 }
21794 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021795 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021797 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 }
21799
21800 /*
21801 * ":function name(arg1, arg2)" Define function.
21802 */
21803 p = skipwhite(p);
21804 if (*p != '(')
21805 {
21806 if (!eap->skip)
21807 {
21808 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 }
21811 /* attempt to continue by skipping some text */
21812 if (vim_strchr(p, '(') != NULL)
21813 p = vim_strchr(p, '(');
21814 }
21815 p = skipwhite(p + 1);
21816
21817 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21818 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21819
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021820 if (!eap->skip)
21821 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021822 /* Check the name of the function. Unless it's a dictionary function
21823 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021824 if (name != NULL)
21825 arg = name;
21826 else
21827 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021828 if (arg != NULL && (fudi.fd_di == NULL
21829 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021830 {
21831 if (*arg == K_SPECIAL)
21832 j = 3;
21833 else
21834 j = 0;
21835 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21836 : eval_isnamec(arg[j])))
21837 ++j;
21838 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021839 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021840 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021841 /* Disallow using the g: dict. */
21842 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21843 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021844 }
21845
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 /*
21847 * Isolate the arguments: "arg1, arg2, ...)"
21848 */
21849 while (*p != ')')
21850 {
21851 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21852 {
21853 varargs = TRUE;
21854 p += 3;
21855 mustend = TRUE;
21856 }
21857 else
21858 {
21859 arg = p;
21860 while (ASCII_ISALNUM(*p) || *p == '_')
21861 ++p;
21862 if (arg == p || isdigit(*arg)
21863 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21864 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21865 {
21866 if (!eap->skip)
21867 EMSG2(_("E125: Illegal argument: %s"), arg);
21868 break;
21869 }
21870 if (ga_grow(&newargs, 1) == FAIL)
21871 goto erret;
21872 c = *p;
21873 *p = NUL;
21874 arg = vim_strsave(arg);
21875 if (arg == NULL)
21876 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021877
21878 /* Check for duplicate argument name. */
21879 for (i = 0; i < newargs.ga_len; ++i)
21880 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21881 {
21882 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021883 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021884 goto erret;
21885 }
21886
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21888 *p = c;
21889 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 if (*p == ',')
21891 ++p;
21892 else
21893 mustend = TRUE;
21894 }
21895 p = skipwhite(p);
21896 if (mustend && *p != ')')
21897 {
21898 if (!eap->skip)
21899 EMSG2(_(e_invarg2), eap->arg);
21900 break;
21901 }
21902 }
21903 ++p; /* skip the ')' */
21904
Bram Moolenaare9a41262005-01-15 22:18:47 +000021905 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 for (;;)
21907 {
21908 p = skipwhite(p);
21909 if (STRNCMP(p, "range", 5) == 0)
21910 {
21911 flags |= FC_RANGE;
21912 p += 5;
21913 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021914 else if (STRNCMP(p, "dict", 4) == 0)
21915 {
21916 flags |= FC_DICT;
21917 p += 4;
21918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 else if (STRNCMP(p, "abort", 5) == 0)
21920 {
21921 flags |= FC_ABORT;
21922 p += 5;
21923 }
21924 else
21925 break;
21926 }
21927
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021928 /* When there is a line break use what follows for the function body.
21929 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21930 if (*p == '\n')
21931 line_arg = p + 1;
21932 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 EMSG(_(e_trailing));
21934
21935 /*
21936 * Read the body of the function, until ":endfunction" is found.
21937 */
21938 if (KeyTyped)
21939 {
21940 /* Check if the function already exists, don't let the user type the
21941 * whole function before telling him it doesn't work! For a script we
21942 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021943 if (!eap->skip && !eap->forceit)
21944 {
21945 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21946 EMSG(_(e_funcdict));
21947 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021948 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021951 if (!eap->skip && did_emsg)
21952 goto erret;
21953
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 msg_putchar('\n'); /* don't overwrite the function name */
21955 cmdline_row = msg_row;
21956 }
21957
21958 indent = 2;
21959 nesting = 0;
21960 for (;;)
21961 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021962 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021963 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021964 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021965 saved_wait_return = FALSE;
21966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021968 sourcing_lnum_off = sourcing_lnum;
21969
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021970 if (line_arg != NULL)
21971 {
21972 /* Use eap->arg, split up in parts by line breaks. */
21973 theline = line_arg;
21974 p = vim_strchr(theline, '\n');
21975 if (p == NULL)
21976 line_arg += STRLEN(line_arg);
21977 else
21978 {
21979 *p = NUL;
21980 line_arg = p + 1;
21981 }
21982 }
21983 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 theline = getcmdline(':', 0L, indent);
21985 else
21986 theline = eap->getline(':', eap->cookie, indent);
21987 if (KeyTyped)
21988 lines_left = Rows - 1;
21989 if (theline == NULL)
21990 {
21991 EMSG(_("E126: Missing :endfunction"));
21992 goto erret;
21993 }
21994
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021995 /* Detect line continuation: sourcing_lnum increased more than one. */
21996 if (sourcing_lnum > sourcing_lnum_off + 1)
21997 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21998 else
21999 sourcing_lnum_off = 0;
22000
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 if (skip_until != NULL)
22002 {
22003 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22004 * don't check for ":endfunc". */
22005 if (STRCMP(theline, skip_until) == 0)
22006 {
22007 vim_free(skip_until);
22008 skip_until = NULL;
22009 }
22010 }
22011 else
22012 {
22013 /* skip ':' and blanks*/
22014 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22015 ;
22016
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022017 /* Check for "endfunction". */
22018 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022020 if (line_arg == NULL)
22021 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 break;
22023 }
22024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022025 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 * at "end". */
22027 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22028 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022029 else if (STRNCMP(p, "if", 2) == 0
22030 || STRNCMP(p, "wh", 2) == 0
22031 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 || STRNCMP(p, "try", 3) == 0)
22033 indent += 2;
22034
22035 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022036 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022038 if (*p == '!')
22039 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 p += eval_fname_script(p);
22041 if (ASCII_ISALPHA(*p))
22042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 if (*skipwhite(p) == '(')
22045 {
22046 ++nesting;
22047 indent += 2;
22048 }
22049 }
22050 }
22051
22052 /* Check for ":append" or ":insert". */
22053 p = skip_range(p, NULL);
22054 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22055 || (p[0] == 'i'
22056 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22057 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22058 skip_until = vim_strsave((char_u *)".");
22059
22060 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22061 arg = skipwhite(skiptowhite(p));
22062 if (arg[0] == '<' && arg[1] =='<'
22063 && ((p[0] == 'p' && p[1] == 'y'
22064 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22065 || (p[0] == 'p' && p[1] == 'e'
22066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22067 || (p[0] == 't' && p[1] == 'c'
22068 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022069 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22070 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22072 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022073 || (p[0] == 'm' && p[1] == 'z'
22074 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 ))
22076 {
22077 /* ":python <<" continues until a dot, like ":append" */
22078 p = skipwhite(arg + 2);
22079 if (*p == NUL)
22080 skip_until = vim_strsave((char_u *)".");
22081 else
22082 skip_until = vim_strsave(p);
22083 }
22084 }
22085
22086 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022087 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022088 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022089 if (line_arg == NULL)
22090 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022092 }
22093
22094 /* Copy the line to newly allocated memory. get_one_sourceline()
22095 * allocates 250 bytes per line, this saves 80% on average. The cost
22096 * is an extra alloc/free. */
22097 p = vim_strsave(theline);
22098 if (p != NULL)
22099 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022100 if (line_arg == NULL)
22101 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022102 theline = p;
22103 }
22104
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022105 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22106
22107 /* Add NULL lines for continuation lines, so that the line count is
22108 * equal to the index in the growarray. */
22109 while (sourcing_lnum_off-- > 0)
22110 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022111
22112 /* Check for end of eap->arg. */
22113 if (line_arg != NULL && *line_arg == NUL)
22114 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 }
22116
22117 /* Don't define the function when skipping commands or when an error was
22118 * detected. */
22119 if (eap->skip || did_emsg)
22120 goto erret;
22121
22122 /*
22123 * If there are no errors, add the function
22124 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022125 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022127 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022128 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022129 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022130 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022131 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022132 goto erret;
22133 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022135 fp = find_func(name);
22136 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022138 if (!eap->forceit)
22139 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022140 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022141 goto erret;
22142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022143 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022144 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022145 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022146 name);
22147 goto erret;
22148 }
22149 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022150 ga_clear_strings(&(fp->uf_args));
22151 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152 vim_free(name);
22153 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 }
22156 else
22157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022158 char numbuf[20];
22159
22160 fp = NULL;
22161 if (fudi.fd_newkey == NULL && !eap->forceit)
22162 {
22163 EMSG(_(e_funcdict));
22164 goto erret;
22165 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022166 if (fudi.fd_di == NULL)
22167 {
22168 /* Can't add a function to a locked dictionary */
22169 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22170 goto erret;
22171 }
22172 /* Can't change an existing function if it is locked */
22173 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22174 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022175
22176 /* Give the function a sequential number. Can only be used with a
22177 * Funcref! */
22178 vim_free(name);
22179 sprintf(numbuf, "%d", ++func_nr);
22180 name = vim_strsave((char_u *)numbuf);
22181 if (name == NULL)
22182 goto erret;
22183 }
22184
22185 if (fp == NULL)
22186 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022187 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022188 {
22189 int slen, plen;
22190 char_u *scriptname;
22191
22192 /* Check that the autoload name matches the script name. */
22193 j = FAIL;
22194 if (sourcing_name != NULL)
22195 {
22196 scriptname = autoload_name(name);
22197 if (scriptname != NULL)
22198 {
22199 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022200 plen = (int)STRLEN(p);
22201 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022202 if (slen > plen && fnamecmp(p,
22203 sourcing_name + slen - plen) == 0)
22204 j = OK;
22205 vim_free(scriptname);
22206 }
22207 }
22208 if (j == FAIL)
22209 {
22210 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22211 goto erret;
22212 }
22213 }
22214
22215 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 if (fp == NULL)
22217 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022218
22219 if (fudi.fd_dict != NULL)
22220 {
22221 if (fudi.fd_di == NULL)
22222 {
22223 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022224 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022225 if (fudi.fd_di == NULL)
22226 {
22227 vim_free(fp);
22228 goto erret;
22229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022230 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22231 {
22232 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022233 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022234 goto erret;
22235 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022236 }
22237 else
22238 /* overwrite existing dict entry */
22239 clear_tv(&fudi.fd_di->di_tv);
22240 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022241 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022242 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022243 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022244
22245 /* behave like "dict" was used */
22246 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022247 }
22248
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 STRCPY(fp->uf_name, name);
22251 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022253 fp->uf_args = newargs;
22254 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022255#ifdef FEAT_PROFILE
22256 fp->uf_tml_count = NULL;
22257 fp->uf_tml_total = NULL;
22258 fp->uf_tml_self = NULL;
22259 fp->uf_profiling = FALSE;
22260 if (prof_def_func())
22261 func_do_profile(fp);
22262#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022263 fp->uf_varargs = varargs;
22264 fp->uf_flags = flags;
22265 fp->uf_calls = 0;
22266 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022267 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268
22269erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 ga_clear_strings(&newargs);
22271 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022272ret_free:
22273 vim_free(skip_until);
22274 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022277 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278}
22279
22280/*
22281 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022282 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022284 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022285 * TFN_INT: internal function name OK
22286 * TFN_QUIET: be quiet
22287 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 * Advances "pp" to just after the function name (if no error).
22289 */
22290 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022291trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 char_u **pp;
22293 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022294 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022297 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 char_u *start;
22299 char_u *end;
22300 int lead;
22301 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022304
22305 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022308
22309 /* Check for hard coded <SNR>: already translated function ID (from a user
22310 * command). */
22311 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22312 && (*pp)[2] == (int)KE_SNR)
22313 {
22314 *pp += 3;
22315 len = get_id_len(pp) + 3;
22316 return vim_strnsave(start, len);
22317 }
22318
22319 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22320 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022322 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022324
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022325 /* Note that TFN_ flags use the same values as GLV_ flags. */
22326 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022327 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022328 if (end == start)
22329 {
22330 if (!skip)
22331 EMSG(_("E129: Function name required"));
22332 goto theend;
22333 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022334 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022335 {
22336 /*
22337 * Report an invalid expression in braces, unless the expression
22338 * evaluation has been cancelled due to an aborting error, an
22339 * interrupt, or an exception.
22340 */
22341 if (!aborting())
22342 {
22343 if (end != NULL)
22344 EMSG2(_(e_invarg2), start);
22345 }
22346 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022347 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022348 goto theend;
22349 }
22350
22351 if (lv.ll_tv != NULL)
22352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022353 if (fdp != NULL)
22354 {
22355 fdp->fd_dict = lv.ll_dict;
22356 fdp->fd_newkey = lv.ll_newkey;
22357 lv.ll_newkey = NULL;
22358 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022359 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022360 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22361 {
22362 name = vim_strsave(lv.ll_tv->vval.v_string);
22363 *pp = end;
22364 }
22365 else
22366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022367 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22368 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022369 EMSG(_(e_funcref));
22370 else
22371 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022372 name = NULL;
22373 }
22374 goto theend;
22375 }
22376
22377 if (lv.ll_name == NULL)
22378 {
22379 /* Error found, but continue after the function name. */
22380 *pp = end;
22381 goto theend;
22382 }
22383
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022384 /* Check if the name is a Funcref. If so, use the value. */
22385 if (lv.ll_exp_name != NULL)
22386 {
22387 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022388 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022389 if (name == lv.ll_exp_name)
22390 name = NULL;
22391 }
22392 else
22393 {
22394 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022395 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022396 if (name == *pp)
22397 name = NULL;
22398 }
22399 if (name != NULL)
22400 {
22401 name = vim_strsave(name);
22402 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022403 if (STRNCMP(name, "<SNR>", 5) == 0)
22404 {
22405 /* Change "<SNR>" to the byte sequence. */
22406 name[0] = K_SPECIAL;
22407 name[1] = KS_EXTRA;
22408 name[2] = (int)KE_SNR;
22409 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22410 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022411 goto theend;
22412 }
22413
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022414 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022415 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022416 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022417 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22418 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22419 {
22420 /* When there was "s:" already or the name expanded to get a
22421 * leading "s:" then remove it. */
22422 lv.ll_name += 2;
22423 len -= 2;
22424 lead = 2;
22425 }
22426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022427 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022428 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022429 /* skip over "s:" and "g:" */
22430 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022431 lv.ll_name += 2;
22432 len = (int)(end - lv.ll_name);
22433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022434
22435 /*
22436 * Copy the function name to allocated memory.
22437 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22438 * Accept <SNR>123_name() outside a script.
22439 */
22440 if (skip)
22441 lead = 0; /* do nothing */
22442 else if (lead > 0)
22443 {
22444 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022445 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22446 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022447 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022448 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022449 if (current_SID <= 0)
22450 {
22451 EMSG(_(e_usingsid));
22452 goto theend;
22453 }
22454 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22455 lead += (int)STRLEN(sid_buf);
22456 }
22457 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022458 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022459 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022460 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022461 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022462 goto theend;
22463 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022464 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022465 {
22466 char_u *cp = vim_strchr(lv.ll_name, ':');
22467
22468 if (cp != NULL && cp < end)
22469 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022470 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022471 goto theend;
22472 }
22473 }
22474
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022475 name = alloc((unsigned)(len + lead + 1));
22476 if (name != NULL)
22477 {
22478 if (lead > 0)
22479 {
22480 name[0] = K_SPECIAL;
22481 name[1] = KS_EXTRA;
22482 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022483 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022484 STRCPY(name + 3, sid_buf);
22485 }
22486 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022487 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022488 }
22489 *pp = end;
22490
22491theend:
22492 clear_lval(&lv);
22493 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494}
22495
22496/*
22497 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22498 * Return 2 if "p" starts with "s:".
22499 * Return 0 otherwise.
22500 */
22501 static int
22502eval_fname_script(p)
22503 char_u *p;
22504{
22505 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22506 || STRNICMP(p + 1, "SNR>", 4) == 0))
22507 return 5;
22508 if (p[0] == 's' && p[1] == ':')
22509 return 2;
22510 return 0;
22511}
22512
22513/*
22514 * Return TRUE if "p" starts with "<SID>" or "s:".
22515 * Only works if eval_fname_script() returned non-zero for "p"!
22516 */
22517 static int
22518eval_fname_sid(p)
22519 char_u *p;
22520{
22521 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22522}
22523
22524/*
22525 * List the head of the function: "name(arg1, arg2)".
22526 */
22527 static void
22528list_func_head(fp, indent)
22529 ufunc_T *fp;
22530 int indent;
22531{
22532 int j;
22533
22534 msg_start();
22535 if (indent)
22536 MSG_PUTS(" ");
22537 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022538 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 {
22540 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022541 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 }
22543 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022546 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 {
22548 if (j)
22549 MSG_PUTS(", ");
22550 msg_puts(FUNCARG(fp, j));
22551 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022552 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 {
22554 if (j)
22555 MSG_PUTS(", ");
22556 MSG_PUTS("...");
22557 }
22558 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022559 if (fp->uf_flags & FC_ABORT)
22560 MSG_PUTS(" abort");
22561 if (fp->uf_flags & FC_RANGE)
22562 MSG_PUTS(" range");
22563 if (fp->uf_flags & FC_DICT)
22564 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022565 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022566 if (p_verbose > 0)
22567 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568}
22569
22570/*
22571 * Find a function by name, return pointer to it in ufuncs.
22572 * Return NULL for unknown function.
22573 */
22574 static ufunc_T *
22575find_func(name)
22576 char_u *name;
22577{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022580 hi = hash_find(&func_hashtab, name);
22581 if (!HASHITEM_EMPTY(hi))
22582 return HI2UF(hi);
22583 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584}
22585
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022586#if defined(EXITFREE) || defined(PROTO)
22587 void
22588free_all_functions()
22589{
22590 hashitem_T *hi;
22591
22592 /* Need to start all over every time, because func_free() may change the
22593 * hash table. */
22594 while (func_hashtab.ht_used > 0)
22595 for (hi = func_hashtab.ht_array; ; ++hi)
22596 if (!HASHITEM_EMPTY(hi))
22597 {
22598 func_free(HI2UF(hi));
22599 break;
22600 }
22601}
22602#endif
22603
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022604 int
22605translated_function_exists(name)
22606 char_u *name;
22607{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022608 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022609 return find_internal_func(name) >= 0;
22610 return find_func(name) != NULL;
22611}
22612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022613/*
22614 * Return TRUE if a function "name" exists.
22615 */
22616 static int
22617function_exists(name)
22618 char_u *name;
22619{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022620 char_u *nm = name;
22621 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022622 int n = FALSE;
22623
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022624 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22625 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022626 nm = skipwhite(nm);
22627
22628 /* Only accept "funcname", "funcname ", "funcname (..." and
22629 * "funcname(...", not "funcname!...". */
22630 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022631 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022632 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022633 return n;
22634}
22635
Bram Moolenaara1544c02013-05-30 12:35:52 +020022636 char_u *
22637get_expanded_name(name, check)
22638 char_u *name;
22639 int check;
22640{
22641 char_u *nm = name;
22642 char_u *p;
22643
22644 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22645
22646 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022647 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022648 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022649
Bram Moolenaara1544c02013-05-30 12:35:52 +020022650 vim_free(p);
22651 return NULL;
22652}
22653
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022654/*
22655 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022656 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22657 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022658 */
22659 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022660builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022661 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022662 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022663{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022664 char_u *p;
22665
22666 if (!ASCII_ISLOWER(name[0]))
22667 return FALSE;
22668 p = vim_strchr(name, AUTOLOAD_CHAR);
22669 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022670}
22671
Bram Moolenaar05159a02005-02-26 23:04:13 +000022672#if defined(FEAT_PROFILE) || defined(PROTO)
22673/*
22674 * Start profiling function "fp".
22675 */
22676 static void
22677func_do_profile(fp)
22678 ufunc_T *fp;
22679{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022680 int len = fp->uf_lines.ga_len;
22681
22682 if (len == 0)
22683 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022684 fp->uf_tm_count = 0;
22685 profile_zero(&fp->uf_tm_self);
22686 profile_zero(&fp->uf_tm_total);
22687 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022688 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022689 if (fp->uf_tml_total == NULL)
22690 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022691 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022692 if (fp->uf_tml_self == NULL)
22693 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022694 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022695 fp->uf_tml_idx = -1;
22696 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22697 || fp->uf_tml_self == NULL)
22698 return; /* out of memory */
22699
22700 fp->uf_profiling = TRUE;
22701}
22702
22703/*
22704 * Dump the profiling results for all functions in file "fd".
22705 */
22706 void
22707func_dump_profile(fd)
22708 FILE *fd;
22709{
22710 hashitem_T *hi;
22711 int todo;
22712 ufunc_T *fp;
22713 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022714 ufunc_T **sorttab;
22715 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022716
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022717 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022718 if (todo == 0)
22719 return; /* nothing to dump */
22720
Bram Moolenaar73830342005-02-28 22:48:19 +000022721 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22722
Bram Moolenaar05159a02005-02-26 23:04:13 +000022723 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22724 {
22725 if (!HASHITEM_EMPTY(hi))
22726 {
22727 --todo;
22728 fp = HI2UF(hi);
22729 if (fp->uf_profiling)
22730 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022731 if (sorttab != NULL)
22732 sorttab[st_len++] = fp;
22733
Bram Moolenaar05159a02005-02-26 23:04:13 +000022734 if (fp->uf_name[0] == K_SPECIAL)
22735 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22736 else
22737 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22738 if (fp->uf_tm_count == 1)
22739 fprintf(fd, "Called 1 time\n");
22740 else
22741 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22742 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22743 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22744 fprintf(fd, "\n");
22745 fprintf(fd, "count total (s) self (s)\n");
22746
22747 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22748 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022749 if (FUNCLINE(fp, i) == NULL)
22750 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022751 prof_func_line(fd, fp->uf_tml_count[i],
22752 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022753 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22754 }
22755 fprintf(fd, "\n");
22756 }
22757 }
22758 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022759
22760 if (sorttab != NULL && st_len > 0)
22761 {
22762 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22763 prof_total_cmp);
22764 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22765 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22766 prof_self_cmp);
22767 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22768 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022769
22770 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022771}
Bram Moolenaar73830342005-02-28 22:48:19 +000022772
22773 static void
22774prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22775 FILE *fd;
22776 ufunc_T **sorttab;
22777 int st_len;
22778 char *title;
22779 int prefer_self; /* when equal print only self time */
22780{
22781 int i;
22782 ufunc_T *fp;
22783
22784 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22785 fprintf(fd, "count total (s) self (s) function\n");
22786 for (i = 0; i < 20 && i < st_len; ++i)
22787 {
22788 fp = sorttab[i];
22789 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22790 prefer_self);
22791 if (fp->uf_name[0] == K_SPECIAL)
22792 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22793 else
22794 fprintf(fd, " %s()\n", fp->uf_name);
22795 }
22796 fprintf(fd, "\n");
22797}
22798
22799/*
22800 * Print the count and times for one function or function line.
22801 */
22802 static void
22803prof_func_line(fd, count, total, self, prefer_self)
22804 FILE *fd;
22805 int count;
22806 proftime_T *total;
22807 proftime_T *self;
22808 int prefer_self; /* when equal print only self time */
22809{
22810 if (count > 0)
22811 {
22812 fprintf(fd, "%5d ", count);
22813 if (prefer_self && profile_equal(total, self))
22814 fprintf(fd, " ");
22815 else
22816 fprintf(fd, "%s ", profile_msg(total));
22817 if (!prefer_self && profile_equal(total, self))
22818 fprintf(fd, " ");
22819 else
22820 fprintf(fd, "%s ", profile_msg(self));
22821 }
22822 else
22823 fprintf(fd, " ");
22824}
22825
22826/*
22827 * Compare function for total time sorting.
22828 */
22829 static int
22830#ifdef __BORLANDC__
22831_RTLENTRYF
22832#endif
22833prof_total_cmp(s1, s2)
22834 const void *s1;
22835 const void *s2;
22836{
22837 ufunc_T *p1, *p2;
22838
22839 p1 = *(ufunc_T **)s1;
22840 p2 = *(ufunc_T **)s2;
22841 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22842}
22843
22844/*
22845 * Compare function for self time sorting.
22846 */
22847 static int
22848#ifdef __BORLANDC__
22849_RTLENTRYF
22850#endif
22851prof_self_cmp(s1, s2)
22852 const void *s1;
22853 const void *s2;
22854{
22855 ufunc_T *p1, *p2;
22856
22857 p1 = *(ufunc_T **)s1;
22858 p2 = *(ufunc_T **)s2;
22859 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22860}
22861
Bram Moolenaar05159a02005-02-26 23:04:13 +000022862#endif
22863
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022864/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022865 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022866 * Return TRUE if a package was loaded.
22867 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022868 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022869script_autoload(name, reload)
22870 char_u *name;
22871 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022872{
22873 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022874 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022875 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022876 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022877
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022878 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022879 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022880 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022881 return FALSE;
22882
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022883 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022884
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022885 /* Find the name in the list of previously loaded package names. Skip
22886 * "autoload/", it's always the same. */
22887 for (i = 0; i < ga_loaded.ga_len; ++i)
22888 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22889 break;
22890 if (!reload && i < ga_loaded.ga_len)
22891 ret = FALSE; /* was loaded already */
22892 else
22893 {
22894 /* Remember the name if it wasn't loaded already. */
22895 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22896 {
22897 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22898 tofree = NULL;
22899 }
22900
22901 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022902 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022903 ret = TRUE;
22904 }
22905
22906 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022907 return ret;
22908}
22909
22910/*
22911 * Return the autoload script name for a function or variable name.
22912 * Returns NULL when out of memory.
22913 */
22914 static char_u *
22915autoload_name(name)
22916 char_u *name;
22917{
22918 char_u *p;
22919 char_u *scriptname;
22920
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022921 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022922 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22923 if (scriptname == NULL)
22924 return FALSE;
22925 STRCPY(scriptname, "autoload/");
22926 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022927 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022928 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022929 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022930 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022931 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022932}
22933
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22935
22936/*
22937 * Function given to ExpandGeneric() to obtain the list of user defined
22938 * function names.
22939 */
22940 char_u *
22941get_user_func_name(xp, idx)
22942 expand_T *xp;
22943 int idx;
22944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022945 static long_u done;
22946 static hashitem_T *hi;
22947 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948
22949 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022951 done = 0;
22952 hi = func_hashtab.ht_array;
22953 }
22954 if (done < func_hashtab.ht_used)
22955 {
22956 if (done++ > 0)
22957 ++hi;
22958 while (HASHITEM_EMPTY(hi))
22959 ++hi;
22960 fp = HI2UF(hi);
22961
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022962 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022963 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022964
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022965 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22966 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967
22968 cat_func_name(IObuff, fp);
22969 if (xp->xp_context != EXPAND_USER_FUNC)
22970 {
22971 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022972 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 STRCAT(IObuff, ")");
22974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 return IObuff;
22976 }
22977 return NULL;
22978}
22979
22980#endif /* FEAT_CMDL_COMPL */
22981
22982/*
22983 * Copy the function name of "fp" to buffer "buf".
22984 * "buf" must be able to hold the function name plus three bytes.
22985 * Takes care of script-local function names.
22986 */
22987 static void
22988cat_func_name(buf, fp)
22989 char_u *buf;
22990 ufunc_T *fp;
22991{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022992 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 {
22994 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022995 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 }
22997 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022998 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999}
23000
23001/*
23002 * ":delfunction {name}"
23003 */
23004 void
23005ex_delfunction(eap)
23006 exarg_T *eap;
23007{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023008 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 char_u *p;
23010 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012
23013 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023014 name = trans_function_name(&p, eap->skip, 0, &fudi);
23015 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023017 {
23018 if (fudi.fd_dict != NULL && !eap->skip)
23019 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 if (!ends_excmd(*skipwhite(p)))
23023 {
23024 vim_free(name);
23025 EMSG(_(e_trailing));
23026 return;
23027 }
23028 eap->nextcmd = check_nextcmd(p);
23029 if (eap->nextcmd != NULL)
23030 *p = NUL;
23031
23032 if (!eap->skip)
23033 fp = find_func(name);
23034 vim_free(name);
23035
23036 if (!eap->skip)
23037 {
23038 if (fp == NULL)
23039 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023040 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 return;
23042 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023043 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 {
23045 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23046 return;
23047 }
23048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023049 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023051 /* Delete the dict item that refers to the function, it will
23052 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023053 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023055 else
23056 func_free(fp);
23057 }
23058}
23059
23060/*
23061 * Free a function and remove it from the list of functions.
23062 */
23063 static void
23064func_free(fp)
23065 ufunc_T *fp;
23066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023067 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068
23069 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023070 ga_clear_strings(&(fp->uf_args));
23071 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023072#ifdef FEAT_PROFILE
23073 vim_free(fp->uf_tml_count);
23074 vim_free(fp->uf_tml_total);
23075 vim_free(fp->uf_tml_self);
23076#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023077
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023078 /* remove the function from the function hashtable */
23079 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23080 if (HASHITEM_EMPTY(hi))
23081 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023082 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023083 hash_remove(&func_hashtab, hi);
23084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023085 vim_free(fp);
23086}
23087
23088/*
23089 * Unreference a Function: decrement the reference count and free it when it
23090 * becomes zero. Only for numbered functions.
23091 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023092 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023093func_unref(name)
23094 char_u *name;
23095{
23096 ufunc_T *fp;
23097
23098 if (name != NULL && isdigit(*name))
23099 {
23100 fp = find_func(name);
23101 if (fp == NULL)
23102 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023103 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 {
23105 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023106 * when "uf_calls" becomes zero. */
23107 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023108 func_free(fp);
23109 }
23110 }
23111}
23112
23113/*
23114 * Count a reference to a Function.
23115 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023116 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023117func_ref(name)
23118 char_u *name;
23119{
23120 ufunc_T *fp;
23121
23122 if (name != NULL && isdigit(*name))
23123 {
23124 fp = find_func(name);
23125 if (fp == NULL)
23126 EMSG2(_(e_intern2), "func_ref()");
23127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023128 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 }
23130}
23131
23132/*
23133 * Call a user function.
23134 */
23135 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023136call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 ufunc_T *fp; /* pointer to function */
23138 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 typval_T *argvars; /* arguments */
23140 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 linenr_T firstline; /* first line of range */
23142 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144{
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 char_u *save_sourcing_name;
23146 linenr_T save_sourcing_lnum;
23147 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023148 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023149 int save_did_emsg;
23150 static int depth = 0;
23151 dictitem_T *v;
23152 int fixvar_idx = 0; /* index in fixvar[] */
23153 int i;
23154 int ai;
23155 char_u numbuf[NUMBUFLEN];
23156 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023157#ifdef FEAT_PROFILE
23158 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023159 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161
23162 /* If depth of calling is getting too high, don't execute the function */
23163 if (depth >= p_mfd)
23164 {
23165 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023166 rettv->v_type = VAR_NUMBER;
23167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 return;
23169 }
23170 ++depth;
23171
23172 line_breakcheck(); /* check for CTRL-C hit */
23173
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023174 fc = (funccall_T *)alloc(sizeof(funccall_T));
23175 fc->caller = current_funccal;
23176 current_funccal = fc;
23177 fc->func = fp;
23178 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023179 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023180 fc->linenr = 0;
23181 fc->returned = FALSE;
23182 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023184 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23185 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186
Bram Moolenaar33570922005-01-25 22:26:29 +000023187 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023188 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023189 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23190 * each argument variable and saves a lot of time.
23191 */
23192 /*
23193 * Init l: variables.
23194 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023195 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023196 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023197 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023198 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23199 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023200 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023201 name = v->di_key;
23202 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023204 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023205 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023206 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 v->di_tv.vval.v_dict = selfdict;
23208 ++selfdict->dv_refcount;
23209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023210
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 /*
23212 * Init a: variables.
23213 * Set a:0 to "argcount".
23214 * Set a:000 to a list with room for the "..." arguments.
23215 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023216 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023217 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023218 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023219 /* Use "name" to avoid a warning from some compiler that checks the
23220 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023221 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023222 name = v->di_key;
23223 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023224 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023225 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023227 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023228 v->di_tv.vval.v_list = &fc->l_varlist;
23229 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23230 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23231 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023232
23233 /*
23234 * Set a:firstline to "firstline" and a:lastline to "lastline".
23235 * Set a:name to named arguments.
23236 * Set a:N to the "..." arguments.
23237 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023238 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023239 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023240 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023241 (varnumber_T)lastline);
23242 for (i = 0; i < argcount; ++i)
23243 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023244 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 if (ai < 0)
23246 /* named argument a:name */
23247 name = FUNCARG(fp, i);
23248 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023249 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 /* "..." argument a:1, a:2, etc. */
23251 sprintf((char *)numbuf, "%d", ai + 1);
23252 name = numbuf;
23253 }
23254 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23255 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023256 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23258 }
23259 else
23260 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023261 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23262 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 if (v == NULL)
23264 break;
23265 v->di_flags = DI_FLAGS_RO;
23266 }
23267 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023268 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023269
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023270 /* Note: the values are copied directly to avoid alloc/free.
23271 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023273 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023274
23275 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23276 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023277 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23278 fc->l_listitems[ai].li_tv = argvars[i];
23279 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023280 }
23281 }
23282
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 /* Don't redraw while executing the function. */
23284 ++RedrawingDisabled;
23285 save_sourcing_name = sourcing_name;
23286 save_sourcing_lnum = sourcing_lnum;
23287 sourcing_lnum = 1;
23288 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023289 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 if (sourcing_name != NULL)
23291 {
23292 if (save_sourcing_name != NULL
23293 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23294 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23295 else
23296 STRCPY(sourcing_name, "function ");
23297 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23298
23299 if (p_verbose >= 12)
23300 {
23301 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023302 verbose_enter_scroll();
23303
Bram Moolenaar555b2802005-05-19 21:08:39 +000023304 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 if (p_verbose >= 14)
23306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023308 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023309 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023310 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311
23312 msg_puts((char_u *)"(");
23313 for (i = 0; i < argcount; ++i)
23314 {
23315 if (i > 0)
23316 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023317 if (argvars[i].v_type == VAR_NUMBER)
23318 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 else
23320 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023321 /* Do not want errors such as E724 here. */
23322 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023323 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023324 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023325 if (s != NULL)
23326 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023327 if (vim_strsize(s) > MSG_BUF_CLEN)
23328 {
23329 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23330 s = buf;
23331 }
23332 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023333 vim_free(tofree);
23334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 }
23336 }
23337 msg_puts((char_u *)")");
23338 }
23339 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023340
23341 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 --no_wait_return;
23343 }
23344 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023345#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023346 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 {
23348 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23349 func_do_profile(fp);
23350 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023351 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023352 {
23353 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023354 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355 profile_zero(&fp->uf_tm_children);
23356 }
23357 script_prof_save(&wait_start);
23358 }
23359#endif
23360
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023362 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 save_did_emsg = did_emsg;
23364 did_emsg = FALSE;
23365
23366 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023367 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23369
23370 --RedrawingDisabled;
23371
23372 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023373 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023375 clear_tv(rettv);
23376 rettv->v_type = VAR_NUMBER;
23377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 }
23379
Bram Moolenaar05159a02005-02-26 23:04:13 +000023380#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023381 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023382 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023383 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023384 profile_end(&call_start);
23385 profile_sub_wait(&wait_start, &call_start);
23386 profile_add(&fp->uf_tm_total, &call_start);
23387 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023388 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023389 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023390 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23391 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023392 }
23393 }
23394#endif
23395
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 /* when being verbose, mention the return value */
23397 if (p_verbose >= 12)
23398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023400 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023403 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023404 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023405 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023406 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023409 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023410 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023411 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023412 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023413
Bram Moolenaar555b2802005-05-19 21:08:39 +000023414 /* The value may be very long. Skip the middle part, so that we
23415 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023416 * truncate it at the end. Don't want errors such as E724 here. */
23417 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023418 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023419 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023420 if (s != NULL)
23421 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023422 if (vim_strsize(s) > MSG_BUF_CLEN)
23423 {
23424 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23425 s = buf;
23426 }
23427 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023428 vim_free(tofree);
23429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 }
23431 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023432
23433 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 --no_wait_return;
23435 }
23436
23437 vim_free(sourcing_name);
23438 sourcing_name = save_sourcing_name;
23439 sourcing_lnum = save_sourcing_lnum;
23440 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023441#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023442 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023443 script_prof_restore(&wait_start);
23444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445
23446 if (p_verbose >= 12 && sourcing_name != NULL)
23447 {
23448 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023449 verbose_enter_scroll();
23450
Bram Moolenaar555b2802005-05-19 21:08:39 +000023451 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023453
23454 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 --no_wait_return;
23456 }
23457
23458 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023459 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023461
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023462 /* If the a:000 list and the l: and a: dicts are not referenced we can
23463 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023464 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23465 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23466 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23467 {
23468 free_funccal(fc, FALSE);
23469 }
23470 else
23471 {
23472 hashitem_T *hi;
23473 listitem_T *li;
23474 int todo;
23475
23476 /* "fc" is still in use. This can happen when returning "a:000" or
23477 * assigning "l:" to a global variable.
23478 * Link "fc" in the list for garbage collection later. */
23479 fc->caller = previous_funccal;
23480 previous_funccal = fc;
23481
23482 /* Make a copy of the a: variables, since we didn't do that above. */
23483 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23484 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23485 {
23486 if (!HASHITEM_EMPTY(hi))
23487 {
23488 --todo;
23489 v = HI2DI(hi);
23490 copy_tv(&v->di_tv, &v->di_tv);
23491 }
23492 }
23493
23494 /* Make a copy of the a:000 items, since we didn't do that above. */
23495 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23496 copy_tv(&li->li_tv, &li->li_tv);
23497 }
23498}
23499
23500/*
23501 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023502 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023503 */
23504 static int
23505can_free_funccal(fc, copyID)
23506 funccall_T *fc;
23507 int copyID;
23508{
23509 return (fc->l_varlist.lv_copyID != copyID
23510 && fc->l_vars.dv_copyID != copyID
23511 && fc->l_avars.dv_copyID != copyID);
23512}
23513
23514/*
23515 * Free "fc" and what it contains.
23516 */
23517 static void
23518free_funccal(fc, free_val)
23519 funccall_T *fc;
23520 int free_val; /* a: vars were allocated */
23521{
23522 listitem_T *li;
23523
23524 /* The a: variables typevals may not have been allocated, only free the
23525 * allocated variables. */
23526 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23527
23528 /* free all l: variables */
23529 vars_clear(&fc->l_vars.dv_hashtab);
23530
23531 /* Free the a:000 variables if they were allocated. */
23532 if (free_val)
23533 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23534 clear_tv(&li->li_tv);
23535
23536 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537}
23538
23539/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023540 * Add a number variable "name" to dict "dp" with value "nr".
23541 */
23542 static void
23543add_nr_var(dp, v, name, nr)
23544 dict_T *dp;
23545 dictitem_T *v;
23546 char *name;
23547 varnumber_T nr;
23548{
23549 STRCPY(v->di_key, name);
23550 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23551 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23552 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023554 v->di_tv.vval.v_number = nr;
23555}
23556
23557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 * ":return [expr]"
23559 */
23560 void
23561ex_return(eap)
23562 exarg_T *eap;
23563{
23564 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023565 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 int returning = FALSE;
23567
23568 if (current_funccal == NULL)
23569 {
23570 EMSG(_("E133: :return not inside a function"));
23571 return;
23572 }
23573
23574 if (eap->skip)
23575 ++emsg_skip;
23576
23577 eap->nextcmd = NULL;
23578 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023579 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 {
23581 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023582 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023584 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 }
23586 /* It's safer to return also on error. */
23587 else if (!eap->skip)
23588 {
23589 /*
23590 * Return unless the expression evaluation has been cancelled due to an
23591 * aborting error, an interrupt, or an exception.
23592 */
23593 if (!aborting())
23594 returning = do_return(eap, FALSE, TRUE, NULL);
23595 }
23596
23597 /* When skipping or the return gets pending, advance to the next command
23598 * in this line (!returning). Otherwise, ignore the rest of the line.
23599 * Following lines will be ignored by get_func_line(). */
23600 if (returning)
23601 eap->nextcmd = NULL;
23602 else if (eap->nextcmd == NULL) /* no argument */
23603 eap->nextcmd = check_nextcmd(arg);
23604
23605 if (eap->skip)
23606 --emsg_skip;
23607}
23608
23609/*
23610 * Return from a function. Possibly makes the return pending. Also called
23611 * for a pending return at the ":endtry" or after returning from an extra
23612 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023613 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023614 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 * FALSE when the return gets pending.
23616 */
23617 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023618do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 exarg_T *eap;
23620 int reanimate;
23621 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023622 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623{
23624 int idx;
23625 struct condstack *cstack = eap->cstack;
23626
23627 if (reanimate)
23628 /* Undo the return. */
23629 current_funccal->returned = FALSE;
23630
23631 /*
23632 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23633 * not in its finally clause (which then is to be executed next) is found.
23634 * In this case, make the ":return" pending for execution at the ":endtry".
23635 * Otherwise, return normally.
23636 */
23637 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23638 if (idx >= 0)
23639 {
23640 cstack->cs_pending[idx] = CSTP_RETURN;
23641
23642 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023643 /* A pending return again gets pending. "rettv" points to an
23644 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023646 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 else
23648 {
23649 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023650 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023652 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023654 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 {
23656 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023657 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023658 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 else
23660 EMSG(_(e_outofmem));
23661 }
23662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023663 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664
23665 if (reanimate)
23666 {
23667 /* The pending return value could be overwritten by a ":return"
23668 * without argument in a finally clause; reset the default
23669 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023670 current_funccal->rettv->v_type = VAR_NUMBER;
23671 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 }
23673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023674 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 }
23676 else
23677 {
23678 current_funccal->returned = TRUE;
23679
23680 /* If the return is carried out now, store the return value. For
23681 * a return immediately after reanimation, the value is already
23682 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023683 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023685 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023686 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023688 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689 }
23690 }
23691
23692 return idx < 0;
23693}
23694
23695/*
23696 * Free the variable with a pending return value.
23697 */
23698 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023699discard_pending_return(rettv)
23700 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701{
Bram Moolenaar33570922005-01-25 22:26:29 +000023702 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703}
23704
23705/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023706 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 * is an allocated string. Used by report_pending() for verbose messages.
23708 */
23709 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023710get_return_cmd(rettv)
23711 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023713 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023714 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023715 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023717 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023718 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023719 if (s == NULL)
23720 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023721
23722 STRCPY(IObuff, ":return ");
23723 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23724 if (STRLEN(s) + 8 >= IOSIZE)
23725 STRCPY(IObuff + IOSIZE - 4, "...");
23726 vim_free(tofree);
23727 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728}
23729
23730/*
23731 * Get next function line.
23732 * Called by do_cmdline() to get the next line.
23733 * Returns allocated string, or NULL for end of function.
23734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 char_u *
23736get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023737 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023739 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740{
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023742 ufunc_T *fp = fcp->func;
23743 char_u *retval;
23744 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745
23746 /* If breakpoints have been added/deleted need to check for it. */
23747 if (fcp->dbg_tick != debug_tick)
23748 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 sourcing_lnum);
23751 fcp->dbg_tick = debug_tick;
23752 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023753#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023754 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023755 func_line_end(cookie);
23756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757
Bram Moolenaar05159a02005-02-26 23:04:13 +000023758 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023759 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23760 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 retval = NULL;
23762 else
23763 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023764 /* Skip NULL lines (continuation lines). */
23765 while (fcp->linenr < gap->ga_len
23766 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23767 ++fcp->linenr;
23768 if (fcp->linenr >= gap->ga_len)
23769 retval = NULL;
23770 else
23771 {
23772 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23773 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023774#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023775 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023776 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023777#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 }
23780
23781 /* Did we encounter a breakpoint? */
23782 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23783 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023786 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 sourcing_lnum);
23788 fcp->dbg_tick = debug_tick;
23789 }
23790
23791 return retval;
23792}
23793
Bram Moolenaar05159a02005-02-26 23:04:13 +000023794#if defined(FEAT_PROFILE) || defined(PROTO)
23795/*
23796 * Called when starting to read a function line.
23797 * "sourcing_lnum" must be correct!
23798 * When skipping lines it may not actually be executed, but we won't find out
23799 * until later and we need to store the time now.
23800 */
23801 void
23802func_line_start(cookie)
23803 void *cookie;
23804{
23805 funccall_T *fcp = (funccall_T *)cookie;
23806 ufunc_T *fp = fcp->func;
23807
23808 if (fp->uf_profiling && sourcing_lnum >= 1
23809 && sourcing_lnum <= fp->uf_lines.ga_len)
23810 {
23811 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023812 /* Skip continuation lines. */
23813 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23814 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023815 fp->uf_tml_execed = FALSE;
23816 profile_start(&fp->uf_tml_start);
23817 profile_zero(&fp->uf_tml_children);
23818 profile_get_wait(&fp->uf_tml_wait);
23819 }
23820}
23821
23822/*
23823 * Called when actually executing a function line.
23824 */
23825 void
23826func_line_exec(cookie)
23827 void *cookie;
23828{
23829 funccall_T *fcp = (funccall_T *)cookie;
23830 ufunc_T *fp = fcp->func;
23831
23832 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23833 fp->uf_tml_execed = TRUE;
23834}
23835
23836/*
23837 * Called when done with a function line.
23838 */
23839 void
23840func_line_end(cookie)
23841 void *cookie;
23842{
23843 funccall_T *fcp = (funccall_T *)cookie;
23844 ufunc_T *fp = fcp->func;
23845
23846 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23847 {
23848 if (fp->uf_tml_execed)
23849 {
23850 ++fp->uf_tml_count[fp->uf_tml_idx];
23851 profile_end(&fp->uf_tml_start);
23852 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023853 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023854 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23855 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023856 }
23857 fp->uf_tml_idx = -1;
23858 }
23859}
23860#endif
23861
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862/*
23863 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023864 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 */
23866 int
23867func_has_ended(cookie)
23868 void *cookie;
23869{
Bram Moolenaar33570922005-01-25 22:26:29 +000023870 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871
23872 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23873 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023874 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 || fcp->returned);
23876}
23877
23878/*
23879 * return TRUE if cookie indicates a function which "abort"s on errors.
23880 */
23881 int
23882func_has_abort(cookie)
23883 void *cookie;
23884{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886}
23887
23888#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23889typedef enum
23890{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023891 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23892 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23893 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894} var_flavour_T;
23895
23896static var_flavour_T var_flavour __ARGS((char_u *varname));
23897
23898 static var_flavour_T
23899var_flavour(varname)
23900 char_u *varname;
23901{
23902 char_u *p = varname;
23903
23904 if (ASCII_ISUPPER(*p))
23905 {
23906 while (*(++p))
23907 if (ASCII_ISLOWER(*p))
23908 return VAR_FLAVOUR_SESSION;
23909 return VAR_FLAVOUR_VIMINFO;
23910 }
23911 else
23912 return VAR_FLAVOUR_DEFAULT;
23913}
23914#endif
23915
23916#if defined(FEAT_VIMINFO) || defined(PROTO)
23917/*
23918 * Restore global vars that start with a capital from the viminfo file
23919 */
23920 int
23921read_viminfo_varlist(virp, writing)
23922 vir_T *virp;
23923 int writing;
23924{
23925 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023926 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023927 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928
23929 if (!writing && (find_viminfo_parameter('!') != NULL))
23930 {
23931 tab = vim_strchr(virp->vir_line + 1, '\t');
23932 if (tab != NULL)
23933 {
23934 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023935 switch (*tab)
23936 {
23937 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023938#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023939 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023940#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023941 case 'D': type = VAR_DICT; break;
23942 case 'L': type = VAR_LIST; break;
23943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944
23945 tab = vim_strchr(tab, '\t');
23946 if (tab != NULL)
23947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023948 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023949 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023950 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023952#ifdef FEAT_FLOAT
23953 else if (type == VAR_FLOAT)
23954 (void)string2float(tab + 1, &tv.vval.v_float);
23955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023957 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023958 if (type == VAR_DICT || type == VAR_LIST)
23959 {
23960 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23961
23962 if (etv == NULL)
23963 /* Failed to parse back the dict or list, use it as a
23964 * string. */
23965 tv.v_type = VAR_STRING;
23966 else
23967 {
23968 vim_free(tv.vval.v_string);
23969 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023970 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023971 }
23972 }
23973
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023974 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023975
23976 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023977 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023978 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23979 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 }
23981 }
23982 }
23983
23984 return viminfo_readline(virp);
23985}
23986
23987/*
23988 * Write global vars that start with a capital to the viminfo file
23989 */
23990 void
23991write_viminfo_varlist(fp)
23992 FILE *fp;
23993{
Bram Moolenaar33570922005-01-25 22:26:29 +000023994 hashitem_T *hi;
23995 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023996 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023997 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023998 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023999 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024000 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001
24002 if (find_viminfo_parameter('!') == NULL)
24003 return;
24004
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024005 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024006
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024007 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024008 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024010 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024012 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024013 this_var = HI2DI(hi);
24014 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024015 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024016 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024017 {
24018 case VAR_STRING: s = "STR"; break;
24019 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024020#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024021 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024022#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024023 case VAR_DICT: s = "DIC"; break;
24024 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024025 default: continue;
24026 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024027 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024028 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024029 if (p != NULL)
24030 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024031 vim_free(tofree);
24032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 }
24034 }
24035}
24036#endif
24037
24038#if defined(FEAT_SESSION) || defined(PROTO)
24039 int
24040store_session_globals(fd)
24041 FILE *fd;
24042{
Bram Moolenaar33570922005-01-25 22:26:29 +000024043 hashitem_T *hi;
24044 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024045 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 char_u *p, *t;
24047
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024048 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024049 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024051 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024053 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024054 this_var = HI2DI(hi);
24055 if ((this_var->di_tv.v_type == VAR_NUMBER
24056 || this_var->di_tv.v_type == VAR_STRING)
24057 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024059 /* Escape special characters with a backslash. Turn a LF and
24060 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024061 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024062 (char_u *)"\\\"\n\r");
24063 if (p == NULL) /* out of memory */
24064 break;
24065 for (t = p; *t != NUL; ++t)
24066 if (*t == '\n')
24067 *t = 'n';
24068 else if (*t == '\r')
24069 *t = 'r';
24070 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024071 this_var->di_key,
24072 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24073 : ' ',
24074 p,
24075 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24076 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024077 || put_eol(fd) == FAIL)
24078 {
24079 vim_free(p);
24080 return FAIL;
24081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 vim_free(p);
24083 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024084#ifdef FEAT_FLOAT
24085 else if (this_var->di_tv.v_type == VAR_FLOAT
24086 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24087 {
24088 float_T f = this_var->di_tv.vval.v_float;
24089 int sign = ' ';
24090
24091 if (f < 0)
24092 {
24093 f = -f;
24094 sign = '-';
24095 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024096 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024097 this_var->di_key, sign, f) < 0)
24098 || put_eol(fd) == FAIL)
24099 return FAIL;
24100 }
24101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 }
24103 }
24104 return OK;
24105}
24106#endif
24107
Bram Moolenaar661b1822005-07-28 22:36:45 +000024108/*
24109 * Display script name where an item was last set.
24110 * Should only be invoked when 'verbose' is non-zero.
24111 */
24112 void
24113last_set_msg(scriptID)
24114 scid_T scriptID;
24115{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024116 char_u *p;
24117
Bram Moolenaar661b1822005-07-28 22:36:45 +000024118 if (scriptID != 0)
24119 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024120 p = home_replace_save(NULL, get_scriptname(scriptID));
24121 if (p != NULL)
24122 {
24123 verbose_enter();
24124 MSG_PUTS(_("\n\tLast set from "));
24125 MSG_PUTS(p);
24126 vim_free(p);
24127 verbose_leave();
24128 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024129 }
24130}
24131
Bram Moolenaard812df62008-11-09 12:46:09 +000024132/*
24133 * List v:oldfiles in a nice way.
24134 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024135 void
24136ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024137 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024138{
24139 list_T *l = vimvars[VV_OLDFILES].vv_list;
24140 listitem_T *li;
24141 int nr = 0;
24142
24143 if (l == NULL)
24144 msg((char_u *)_("No old files"));
24145 else
24146 {
24147 msg_start();
24148 msg_scroll = TRUE;
24149 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24150 {
24151 msg_outnum((long)++nr);
24152 MSG_PUTS(": ");
24153 msg_outtrans(get_tv_string(&li->li_tv));
24154 msg_putchar('\n');
24155 out_flush(); /* output one line at a time */
24156 ui_breakcheck();
24157 }
24158 /* Assume "got_int" was set to truncate the listing. */
24159 got_int = FALSE;
24160
24161#ifdef FEAT_BROWSE_CMD
24162 if (cmdmod.browse)
24163 {
24164 quit_more = FALSE;
24165 nr = prompt_for_number(FALSE);
24166 msg_starthere();
24167 if (nr > 0)
24168 {
24169 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24170 (long)nr);
24171
24172 if (p != NULL)
24173 {
24174 p = expand_env_save(p);
24175 eap->arg = p;
24176 eap->cmdidx = CMD_edit;
24177 cmdmod.browse = FALSE;
24178 do_exedit(eap, NULL);
24179 vim_free(p);
24180 }
24181 }
24182 }
24183#endif
24184 }
24185}
24186
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187#endif /* FEAT_EVAL */
24188
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024190#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191
24192#ifdef WIN3264
24193/*
24194 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24195 */
24196static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24197static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24198static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24199
24200/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024201 * Get the short path (8.3) for the filename in "fnamep".
24202 * Only works for a valid file name.
24203 * When the path gets longer "fnamep" is changed and the allocated buffer
24204 * is put in "bufp".
24205 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24206 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 */
24208 static int
24209get_short_pathname(fnamep, bufp, fnamelen)
24210 char_u **fnamep;
24211 char_u **bufp;
24212 int *fnamelen;
24213{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024214 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 char_u *newbuf;
24216
24217 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 l = GetShortPathName(*fnamep, *fnamep, len);
24219 if (l > len - 1)
24220 {
24221 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024222 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 newbuf = vim_strnsave(*fnamep, l);
24224 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226
24227 vim_free(*bufp);
24228 *fnamep = *bufp = newbuf;
24229
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024230 /* Really should always succeed, as the buffer is big enough. */
24231 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 }
24233
24234 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024235 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236}
24237
24238/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024239 * Get the short path (8.3) for the filename in "fname". The converted
24240 * path is returned in "bufp".
24241 *
24242 * Some of the directories specified in "fname" may not exist. This function
24243 * will shorten the existing directories at the beginning of the path and then
24244 * append the remaining non-existing path.
24245 *
24246 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024247 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024248 * bufp - Pointer to an allocated buffer for the filename.
24249 * fnamelen - Length of the filename pointed to by fname
24250 *
24251 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 */
24253 static int
24254shortpath_for_invalid_fname(fname, bufp, fnamelen)
24255 char_u **fname;
24256 char_u **bufp;
24257 int *fnamelen;
24258{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024259 char_u *short_fname, *save_fname, *pbuf_unused;
24260 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024262 int old_len, len;
24263 int new_len, sfx_len;
24264 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265
24266 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024267 old_len = *fnamelen;
24268 save_fname = vim_strnsave(*fname, old_len);
24269 pbuf_unused = NULL;
24270 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024272 endp = save_fname + old_len - 1; /* Find the end of the copy */
24273 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024275 /*
24276 * Try shortening the supplied path till it succeeds by removing one
24277 * directory at a time from the tail of the path.
24278 */
24279 len = 0;
24280 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024282 /* go back one path-separator */
24283 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24284 --endp;
24285 if (endp <= save_fname)
24286 break; /* processed the complete path */
24287
24288 /*
24289 * Replace the path separator with a NUL and try to shorten the
24290 * resulting path.
24291 */
24292 ch = *endp;
24293 *endp = 0;
24294 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024295 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024296 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24297 {
24298 retval = FAIL;
24299 goto theend;
24300 }
24301 *endp = ch; /* preserve the string */
24302
24303 if (len > 0)
24304 break; /* successfully shortened the path */
24305
24306 /* failed to shorten the path. Skip the path separator */
24307 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 }
24309
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024310 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024312 /*
24313 * Succeeded in shortening the path. Now concatenate the shortened
24314 * path with the remaining path at the tail.
24315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024317 /* Compute the length of the new path. */
24318 sfx_len = (int)(save_endp - endp) + 1;
24319 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024321 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024323 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024325 /* There is not enough space in the currently allocated string,
24326 * copy it to a buffer big enough. */
24327 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024329 {
24330 retval = FAIL;
24331 goto theend;
24332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 }
24334 else
24335 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024336 /* Transfer short_fname to the main buffer (it's big enough),
24337 * unless get_short_pathname() did its work in-place. */
24338 *fname = *bufp = save_fname;
24339 if (short_fname != save_fname)
24340 vim_strncpy(save_fname, short_fname, len);
24341 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024343
24344 /* concat the not-shortened part of the path */
24345 vim_strncpy(*fname + len, endp, sfx_len);
24346 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024348
24349theend:
24350 vim_free(pbuf_unused);
24351 vim_free(save_fname);
24352
24353 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354}
24355
24356/*
24357 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024358 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 */
24360 static int
24361shortpath_for_partial(fnamep, bufp, fnamelen)
24362 char_u **fnamep;
24363 char_u **bufp;
24364 int *fnamelen;
24365{
24366 int sepcount, len, tflen;
24367 char_u *p;
24368 char_u *pbuf, *tfname;
24369 int hasTilde;
24370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024371 /* Count up the path separators from the RHS.. so we know which part
24372 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024374 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 if (vim_ispathsep(*p))
24376 ++sepcount;
24377
24378 /* Need full path first (use expand_env() to remove a "~/") */
24379 hasTilde = (**fnamep == '~');
24380 if (hasTilde)
24381 pbuf = tfname = expand_env_save(*fnamep);
24382 else
24383 pbuf = tfname = FullName_save(*fnamep, FALSE);
24384
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024385 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024387 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389
24390 if (len == 0)
24391 {
24392 /* Don't have a valid filename, so shorten the rest of the
24393 * path if we can. This CAN give us invalid 8.3 filenames, but
24394 * there's not a lot of point in guessing what it might be.
24395 */
24396 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024397 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 }
24400
24401 /* Count the paths backward to find the beginning of the desired string. */
24402 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024403 {
24404#ifdef FEAT_MBYTE
24405 if (has_mbyte)
24406 p -= mb_head_off(tfname, p);
24407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 if (vim_ispathsep(*p))
24409 {
24410 if (sepcount == 0 || (hasTilde && sepcount == 1))
24411 break;
24412 else
24413 sepcount --;
24414 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 if (hasTilde)
24417 {
24418 --p;
24419 if (p >= tfname)
24420 *p = '~';
24421 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423 }
24424 else
24425 ++p;
24426
24427 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24428 vim_free(*bufp);
24429 *fnamelen = (int)STRLEN(p);
24430 *bufp = pbuf;
24431 *fnamep = p;
24432
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024433 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434}
24435#endif /* WIN3264 */
24436
24437/*
24438 * Adjust a filename, according to a string of modifiers.
24439 * *fnamep must be NUL terminated when called. When returning, the length is
24440 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024441 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 * When there is an error, *fnamep is set to NULL.
24443 */
24444 int
24445modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24446 char_u *src; /* string with modifiers */
24447 int *usedlen; /* characters after src that are used */
24448 char_u **fnamep; /* file name so far */
24449 char_u **bufp; /* buffer for allocated file name or NULL */
24450 int *fnamelen; /* length of fnamep */
24451{
24452 int valid = 0;
24453 char_u *tail;
24454 char_u *s, *p, *pbuf;
24455 char_u dirname[MAXPATHL];
24456 int c;
24457 int has_fullname = 0;
24458#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024459 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 int has_shortname = 0;
24461#endif
24462
24463repeat:
24464 /* ":p" - full path/file_name */
24465 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24466 {
24467 has_fullname = 1;
24468
24469 valid |= VALID_PATH;
24470 *usedlen += 2;
24471
24472 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24473 if ((*fnamep)[0] == '~'
24474#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24475 && ((*fnamep)[1] == '/'
24476# ifdef BACKSLASH_IN_FILENAME
24477 || (*fnamep)[1] == '\\'
24478# endif
24479 || (*fnamep)[1] == NUL)
24480
24481#endif
24482 )
24483 {
24484 *fnamep = expand_env_save(*fnamep);
24485 vim_free(*bufp); /* free any allocated file name */
24486 *bufp = *fnamep;
24487 if (*fnamep == NULL)
24488 return -1;
24489 }
24490
24491 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024492 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 {
24494 if (vim_ispathsep(*p)
24495 && p[1] == '.'
24496 && (p[2] == NUL
24497 || vim_ispathsep(p[2])
24498 || (p[2] == '.'
24499 && (p[3] == NUL || vim_ispathsep(p[3])))))
24500 break;
24501 }
24502
24503 /* FullName_save() is slow, don't use it when not needed. */
24504 if (*p != NUL || !vim_isAbsName(*fnamep))
24505 {
24506 *fnamep = FullName_save(*fnamep, *p != NUL);
24507 vim_free(*bufp); /* free any allocated file name */
24508 *bufp = *fnamep;
24509 if (*fnamep == NULL)
24510 return -1;
24511 }
24512
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024513#ifdef WIN3264
24514# if _WIN32_WINNT >= 0x0500
24515 if (vim_strchr(*fnamep, '~') != NULL)
24516 {
24517 /* Expand 8.3 filename to full path. Needed to make sure the same
24518 * file does not have two different names.
24519 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24520 p = alloc(_MAX_PATH + 1);
24521 if (p != NULL)
24522 {
24523 if (GetLongPathName(*fnamep, p, MAXPATHL))
24524 {
24525 vim_free(*bufp);
24526 *bufp = *fnamep = p;
24527 }
24528 else
24529 vim_free(p);
24530 }
24531 }
24532# endif
24533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 /* Append a path separator to a directory. */
24535 if (mch_isdir(*fnamep))
24536 {
24537 /* Make room for one or two extra characters. */
24538 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24539 vim_free(*bufp); /* free any allocated file name */
24540 *bufp = *fnamep;
24541 if (*fnamep == NULL)
24542 return -1;
24543 add_pathsep(*fnamep);
24544 }
24545 }
24546
24547 /* ":." - path relative to the current directory */
24548 /* ":~" - path relative to the home directory */
24549 /* ":8" - shortname path - postponed till after */
24550 while (src[*usedlen] == ':'
24551 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24552 {
24553 *usedlen += 2;
24554 if (c == '8')
24555 {
24556#ifdef WIN3264
24557 has_shortname = 1; /* Postpone this. */
24558#endif
24559 continue;
24560 }
24561 pbuf = NULL;
24562 /* Need full path first (use expand_env() to remove a "~/") */
24563 if (!has_fullname)
24564 {
24565 if (c == '.' && **fnamep == '~')
24566 p = pbuf = expand_env_save(*fnamep);
24567 else
24568 p = pbuf = FullName_save(*fnamep, FALSE);
24569 }
24570 else
24571 p = *fnamep;
24572
24573 has_fullname = 0;
24574
24575 if (p != NULL)
24576 {
24577 if (c == '.')
24578 {
24579 mch_dirname(dirname, MAXPATHL);
24580 s = shorten_fname(p, dirname);
24581 if (s != NULL)
24582 {
24583 *fnamep = s;
24584 if (pbuf != NULL)
24585 {
24586 vim_free(*bufp); /* free any allocated file name */
24587 *bufp = pbuf;
24588 pbuf = NULL;
24589 }
24590 }
24591 }
24592 else
24593 {
24594 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24595 /* Only replace it when it starts with '~' */
24596 if (*dirname == '~')
24597 {
24598 s = vim_strsave(dirname);
24599 if (s != NULL)
24600 {
24601 *fnamep = s;
24602 vim_free(*bufp);
24603 *bufp = s;
24604 }
24605 }
24606 }
24607 vim_free(pbuf);
24608 }
24609 }
24610
24611 tail = gettail(*fnamep);
24612 *fnamelen = (int)STRLEN(*fnamep);
24613
24614 /* ":h" - head, remove "/file_name", can be repeated */
24615 /* Don't remove the first "/" or "c:\" */
24616 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24617 {
24618 valid |= VALID_HEAD;
24619 *usedlen += 2;
24620 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024621 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024622 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 *fnamelen = (int)(tail - *fnamep);
24624#ifdef VMS
24625 if (*fnamelen > 0)
24626 *fnamelen += 1; /* the path separator is part of the path */
24627#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024628 if (*fnamelen == 0)
24629 {
24630 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24631 p = vim_strsave((char_u *)".");
24632 if (p == NULL)
24633 return -1;
24634 vim_free(*bufp);
24635 *bufp = *fnamep = tail = p;
24636 *fnamelen = 1;
24637 }
24638 else
24639 {
24640 while (tail > s && !after_pathsep(s, tail))
24641 mb_ptr_back(*fnamep, tail);
24642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 }
24644
24645 /* ":8" - shortname */
24646 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24647 {
24648 *usedlen += 2;
24649#ifdef WIN3264
24650 has_shortname = 1;
24651#endif
24652 }
24653
24654#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024655 /*
24656 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 */
24658 if (has_shortname)
24659 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024660 /* Copy the string if it is shortened by :h and when it wasn't copied
24661 * yet, because we are going to change it in place. Avoids changing
24662 * the buffer name for "%:8". */
24663 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 {
24665 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024666 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 return -1;
24668 vim_free(*bufp);
24669 *bufp = *fnamep = p;
24670 }
24671
24672 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024673 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 if (!has_fullname && !vim_isAbsName(*fnamep))
24675 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024676 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 return -1;
24678 }
24679 else
24680 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024681 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682
Bram Moolenaardc935552011-08-17 15:23:23 +020024683 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024685 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 return -1;
24687
24688 if (l == 0)
24689 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024690 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024692 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 return -1;
24694 }
24695 *fnamelen = l;
24696 }
24697 }
24698#endif /* WIN3264 */
24699
24700 /* ":t" - tail, just the basename */
24701 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24702 {
24703 *usedlen += 2;
24704 *fnamelen -= (int)(tail - *fnamep);
24705 *fnamep = tail;
24706 }
24707
24708 /* ":e" - extension, can be repeated */
24709 /* ":r" - root, without extension, can be repeated */
24710 while (src[*usedlen] == ':'
24711 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24712 {
24713 /* find a '.' in the tail:
24714 * - for second :e: before the current fname
24715 * - otherwise: The last '.'
24716 */
24717 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24718 s = *fnamep - 2;
24719 else
24720 s = *fnamep + *fnamelen - 1;
24721 for ( ; s > tail; --s)
24722 if (s[0] == '.')
24723 break;
24724 if (src[*usedlen + 1] == 'e') /* :e */
24725 {
24726 if (s > tail)
24727 {
24728 *fnamelen += (int)(*fnamep - (s + 1));
24729 *fnamep = s + 1;
24730#ifdef VMS
24731 /* cut version from the extension */
24732 s = *fnamep + *fnamelen - 1;
24733 for ( ; s > *fnamep; --s)
24734 if (s[0] == ';')
24735 break;
24736 if (s > *fnamep)
24737 *fnamelen = s - *fnamep;
24738#endif
24739 }
24740 else if (*fnamep <= tail)
24741 *fnamelen = 0;
24742 }
24743 else /* :r */
24744 {
24745 if (s > tail) /* remove one extension */
24746 *fnamelen = (int)(s - *fnamep);
24747 }
24748 *usedlen += 2;
24749 }
24750
24751 /* ":s?pat?foo?" - substitute */
24752 /* ":gs?pat?foo?" - global substitute */
24753 if (src[*usedlen] == ':'
24754 && (src[*usedlen + 1] == 's'
24755 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24756 {
24757 char_u *str;
24758 char_u *pat;
24759 char_u *sub;
24760 int sep;
24761 char_u *flags;
24762 int didit = FALSE;
24763
24764 flags = (char_u *)"";
24765 s = src + *usedlen + 2;
24766 if (src[*usedlen + 1] == 'g')
24767 {
24768 flags = (char_u *)"g";
24769 ++s;
24770 }
24771
24772 sep = *s++;
24773 if (sep)
24774 {
24775 /* find end of pattern */
24776 p = vim_strchr(s, sep);
24777 if (p != NULL)
24778 {
24779 pat = vim_strnsave(s, (int)(p - s));
24780 if (pat != NULL)
24781 {
24782 s = p + 1;
24783 /* find end of substitution */
24784 p = vim_strchr(s, sep);
24785 if (p != NULL)
24786 {
24787 sub = vim_strnsave(s, (int)(p - s));
24788 str = vim_strnsave(*fnamep, *fnamelen);
24789 if (sub != NULL && str != NULL)
24790 {
24791 *usedlen = (int)(p + 1 - src);
24792 s = do_string_sub(str, pat, sub, flags);
24793 if (s != NULL)
24794 {
24795 *fnamep = s;
24796 *fnamelen = (int)STRLEN(s);
24797 vim_free(*bufp);
24798 *bufp = s;
24799 didit = TRUE;
24800 }
24801 }
24802 vim_free(sub);
24803 vim_free(str);
24804 }
24805 vim_free(pat);
24806 }
24807 }
24808 /* after using ":s", repeat all the modifiers */
24809 if (didit)
24810 goto repeat;
24811 }
24812 }
24813
Bram Moolenaar26df0922014-02-23 23:39:13 +010024814 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24815 {
24816 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24817 if (p == NULL)
24818 return -1;
24819 vim_free(*bufp);
24820 *bufp = *fnamep = p;
24821 *fnamelen = (int)STRLEN(p);
24822 *usedlen += 2;
24823 }
24824
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825 return valid;
24826}
24827
24828/*
24829 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24830 * "flags" can be "g" to do a global substitute.
24831 * Returns an allocated string, NULL for error.
24832 */
24833 char_u *
24834do_string_sub(str, pat, sub, flags)
24835 char_u *str;
24836 char_u *pat;
24837 char_u *sub;
24838 char_u *flags;
24839{
24840 int sublen;
24841 regmatch_T regmatch;
24842 int i;
24843 int do_all;
24844 char_u *tail;
24845 garray_T ga;
24846 char_u *ret;
24847 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024848 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849
24850 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24851 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024852 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853
24854 ga_init2(&ga, 1, 200);
24855
24856 do_all = (flags[0] == 'g');
24857
24858 regmatch.rm_ic = p_ic;
24859 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24860 if (regmatch.regprog != NULL)
24861 {
24862 tail = str;
24863 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24864 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024865 /* Skip empty match except for first match. */
24866 if (regmatch.startp[0] == regmatch.endp[0])
24867 {
24868 if (zero_width == regmatch.startp[0])
24869 {
24870 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020024871 i = MB_PTR2LEN(tail);
24872 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
24873 (size_t)i);
24874 ga.ga_len += i;
24875 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024876 continue;
24877 }
24878 zero_width = regmatch.startp[0];
24879 }
24880
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 /*
24882 * Get some space for a temporary buffer to do the substitution
24883 * into. It will contain:
24884 * - The text up to where the match is.
24885 * - The substituted text.
24886 * - The text after the match.
24887 */
24888 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24889 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24890 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24891 {
24892 ga_clear(&ga);
24893 break;
24894 }
24895
24896 /* copy the text up to where the match is */
24897 i = (int)(regmatch.startp[0] - tail);
24898 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24899 /* add the substituted text */
24900 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24901 + ga.ga_len + i, TRUE, TRUE, FALSE);
24902 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024903 tail = regmatch.endp[0];
24904 if (*tail == NUL)
24905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 if (!do_all)
24907 break;
24908 }
24909
24910 if (ga.ga_data != NULL)
24911 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24912
Bram Moolenaar473de612013-06-08 18:19:48 +020024913 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 }
24915
24916 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24917 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024918 if (p_cpo == empty_option)
24919 p_cpo = save_cpo;
24920 else
24921 /* Darn, evaluating {sub} expression changed the value. */
24922 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923
24924 return ret;
24925}
24926
24927#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */