blob: ae8331d5aaa92a71e26f374dd075827f0b4e7250 [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 Moolenaar8c1329c2014-08-06 13:36:59 +0200557static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000565static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000566static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200567static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000568static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000569static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200572static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000573static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static 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 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static 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 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static 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 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static 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 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
805static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000806static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000809static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static 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 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
2811 else if (var_check_ro(lp->ll_di->di_flags, name))
2812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
2944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
2948 /*
2949 * Assign the List values to the list items.
2950 */
2951 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 if (op != NULL && *op != '=')
2954 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2955 else
2956 {
2957 clear_tv(&lp->ll_li->li_tv);
2958 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 ri = ri->li_next;
2961 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2962 break;
2963 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002966 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 ri = NULL;
2969 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002970 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 lp->ll_li = lp->ll_li->li_next;
2973 ++lp->ll_n1;
2974 }
2975 if (ri != NULL)
2976 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 else if (lp->ll_empty2
2978 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 : lp->ll_n1 != lp->ll_n2)
2980 EMSG(_("E711: List value has not enough items"));
2981 }
2982 else
2983 {
2984 /*
2985 * Assign to a List or Dictionary item.
2986 */
2987 if (lp->ll_newkey != NULL)
2988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 if (op != NULL && *op != '=')
2990 {
2991 EMSG2(_(e_letwrong), op);
2992 return;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002996 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 if (di == NULL)
2998 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002999 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3000 {
3001 vim_free(di);
3002 return;
3003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 else if (op != NULL && *op != '=')
3007 {
3008 tv_op(lp->ll_tv, rettv, op);
3009 return;
3010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003011 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 /*
3015 * Assign the value to the variable or list item.
3016 */
3017 if (copy)
3018 copy_tv(rettv, lp->ll_tv);
3019 else
3020 {
3021 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003022 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024 }
3025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003026}
3027
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3030 * Returns OK or FAIL.
3031 */
3032 static int
3033tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 typval_T *tv1;
3035 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 char_u *op;
3037{
3038 long n;
3039 char_u numbuf[NUMBUFLEN];
3040 char_u *s;
3041
3042 /* Can't do anything with a Funcref or a Dict on the right. */
3043 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3044 {
3045 switch (tv1->v_type)
3046 {
3047 case VAR_DICT:
3048 case VAR_FUNC:
3049 break;
3050
3051 case VAR_LIST:
3052 if (*op != '+' || tv2->v_type != VAR_LIST)
3053 break;
3054 /* List += List */
3055 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3056 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3057 return OK;
3058
3059 case VAR_NUMBER:
3060 case VAR_STRING:
3061 if (tv2->v_type == VAR_LIST)
3062 break;
3063 if (*op == '+' || *op == '-')
3064 {
3065 /* nr += nr or nr -= nr*/
3066 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#ifdef FEAT_FLOAT
3068 if (tv2->v_type == VAR_FLOAT)
3069 {
3070 float_T f = n;
3071
3072 if (*op == '+')
3073 f += tv2->vval.v_float;
3074 else
3075 f -= tv2->vval.v_float;
3076 clear_tv(tv1);
3077 tv1->v_type = VAR_FLOAT;
3078 tv1->vval.v_float = f;
3079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003081#endif
3082 {
3083 if (*op == '+')
3084 n += get_tv_number(tv2);
3085 else
3086 n -= get_tv_number(tv2);
3087 clear_tv(tv1);
3088 tv1->v_type = VAR_NUMBER;
3089 tv1->vval.v_number = n;
3090 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 }
3092 else
3093 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 if (tv2->v_type == VAR_FLOAT)
3095 break;
3096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 /* str .= str */
3098 s = get_tv_string(tv1);
3099 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3100 clear_tv(tv1);
3101 tv1->v_type = VAR_STRING;
3102 tv1->vval.v_string = s;
3103 }
3104 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105
3106#ifdef FEAT_FLOAT
3107 case VAR_FLOAT:
3108 {
3109 float_T f;
3110
3111 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3112 && tv2->v_type != VAR_NUMBER
3113 && tv2->v_type != VAR_STRING))
3114 break;
3115 if (tv2->v_type == VAR_FLOAT)
3116 f = tv2->vval.v_float;
3117 else
3118 f = get_tv_number(tv2);
3119 if (*op == '+')
3120 tv1->vval.v_float += f;
3121 else
3122 tv1->vval.v_float -= f;
3123 }
3124 return OK;
3125#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 }
3127 }
3128
3129 EMSG2(_(e_letwrong), op);
3130 return FAIL;
3131}
3132
3133/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 * Add a watcher to a list.
3135 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003136 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 list_T *l;
3139 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140{
3141 lw->lw_next = l->lv_watch;
3142 l->lv_watch = lw;
3143}
3144
3145/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003146 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 * No warning when it isn't found...
3148 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003149 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 list_T *l;
3152 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155
3156 lwp = &l->lv_watch;
3157 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3158 {
3159 if (lw == lwrem)
3160 {
3161 *lwp = lw->lw_next;
3162 break;
3163 }
3164 lwp = &lw->lw_next;
3165 }
3166}
3167
3168/*
3169 * Just before removing an item from a list: advance watchers to the next
3170 * item.
3171 */
3172 static void
3173list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 list_T *l;
3175 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176{
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3180 if (lw->lw_item == item)
3181 lw->lw_item = item->li_next;
3182}
3183
3184/*
3185 * Evaluate the expression used in a ":for var in expr" command.
3186 * "arg" points to "var".
3187 * Set "*errp" to TRUE for an error, FALSE otherwise;
3188 * Return a pointer that holds the info. Null when there is an error.
3189 */
3190 void *
3191eval_for_line(arg, errp, nextcmdp, skip)
3192 char_u *arg;
3193 int *errp;
3194 char_u **nextcmdp;
3195 int skip;
3196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 typval_T tv;
3200 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 *errp = TRUE; /* default: there is an error */
3203
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 if (fi == NULL)
3206 return NULL;
3207
3208 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3209 if (expr == NULL)
3210 return fi;
3211
3212 expr = skipwhite(expr);
3213 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3214 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003215 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 return fi;
3217 }
3218
3219 if (skip)
3220 ++emsg_skip;
3221 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3222 {
3223 *errp = FALSE;
3224 if (!skip)
3225 {
3226 l = tv.vval.v_list;
3227 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003230 clear_tv(&tv);
3231 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 else
3233 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003234 /* No need to increment the refcount, it's already set for the
3235 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 fi->fi_list = l;
3237 list_add_watch(l, &fi->fi_lw);
3238 fi->fi_lw.lw_item = l->lv_first;
3239 }
3240 }
3241 }
3242 if (skip)
3243 --emsg_skip;
3244
3245 return fi;
3246}
3247
3248/*
3249 * Use the first item in a ":for" list. Advance to the next.
3250 * Assign the values to the variable (list). "arg" points to the first one.
3251 * Return TRUE when a valid item was found, FALSE when at end of list or
3252 * something wrong.
3253 */
3254 int
3255next_for_item(fi_void, arg)
3256 void *fi_void;
3257 char_u *arg;
3258{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003259 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262
3263 item = fi->fi_lw.lw_item;
3264 if (item == NULL)
3265 result = FALSE;
3266 else
3267 {
3268 fi->fi_lw.lw_item = item->li_next;
3269 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3270 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3271 }
3272 return result;
3273}
3274
3275/*
3276 * Free the structure used to store info used by ":for".
3277 */
3278 void
3279free_for_info(fi_void)
3280 void *fi_void;
3281{
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003284 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 list_unref(fi->fi_list);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 vim_free(fi);
3290}
3291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3293
3294 void
3295set_context_for_expression(xp, arg, cmdidx)
3296 expand_T *xp;
3297 char_u *arg;
3298 cmdidx_T cmdidx;
3299{
3300 int got_eq = FALSE;
3301 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 if (cmdidx == CMD_let)
3305 {
3306 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003307 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 {
3309 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003310 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 {
3312 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003313 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 if (vim_iswhite(*p))
3315 break;
3316 }
3317 return;
3318 }
3319 }
3320 else
3321 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3322 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 while ((xp->xp_pattern = vim_strpbrk(arg,
3324 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3325 {
3326 c = *xp->xp_pattern;
3327 if (c == '&')
3328 {
3329 c = xp->xp_pattern[1];
3330 if (c == '&')
3331 {
3332 ++xp->xp_pattern;
3333 xp->xp_context = cmdidx != CMD_let || got_eq
3334 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3335 }
3336 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003339 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3340 xp->xp_pattern += 2;
3341
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 else if (c == '$')
3345 {
3346 /* environment variable */
3347 xp->xp_context = EXPAND_ENV_VARS;
3348 }
3349 else if (c == '=')
3350 {
3351 got_eq = TRUE;
3352 xp->xp_context = EXPAND_EXPRESSION;
3353 }
3354 else if (c == '<'
3355 && xp->xp_context == EXPAND_FUNCTIONS
3356 && vim_strchr(xp->xp_pattern, '(') == NULL)
3357 {
3358 /* Function name can start with "<SNR>" */
3359 break;
3360 }
3361 else if (cmdidx != CMD_let || got_eq)
3362 {
3363 if (c == '"') /* string */
3364 {
3365 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3366 if (c == '\\' && xp->xp_pattern[1] != NUL)
3367 ++xp->xp_pattern;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '\'') /* literal string */
3371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3374 /* skip */ ;
3375 xp->xp_context = EXPAND_NOTHING;
3376 }
3377 else if (c == '|')
3378 {
3379 if (xp->xp_pattern[1] == '|')
3380 {
3381 ++xp->xp_pattern;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
3384 else
3385 xp->xp_context = EXPAND_COMMANDS;
3386 }
3387 else
3388 xp->xp_context = EXPAND_EXPRESSION;
3389 }
3390 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 /* Doesn't look like something valid, expand as an expression
3392 * anyway. */
3393 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 arg = xp->xp_pattern;
3395 if (*arg != NUL)
3396 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3397 /* skip */ ;
3398 }
3399 xp->xp_pattern = arg;
3400}
3401
3402#endif /* FEAT_CMDL_COMPL */
3403
3404/*
3405 * ":1,25call func(arg1, arg2)" function call.
3406 */
3407 void
3408ex_call(eap)
3409 exarg_T *eap;
3410{
3411 char_u *arg = eap->arg;
3412 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 linenr_T lnum;
3418 int doesrange;
3419 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003420 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003422 if (eap->skip)
3423 {
3424 /* trans_function_name() doesn't work well when skipping, use eval0()
3425 * instead to skip to any following command, e.g. for:
3426 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003427 ++emsg_skip;
3428 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3429 clear_tv(&rettv);
3430 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003431 return;
3432 }
3433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003435 if (fudi.fd_newkey != NULL)
3436 {
3437 /* Still need to give an error message for missing key. */
3438 EMSG2(_(e_dictkey), fudi.fd_newkey);
3439 vim_free(fudi.fd_newkey);
3440 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003441 if (tofree == NULL)
3442 return;
3443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003444 /* Increase refcount on dictionary, it could get deleted when evaluating
3445 * the arguments. */
3446 if (fudi.fd_dict != NULL)
3447 ++fudi.fd_dict->dv_refcount;
3448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003449 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003450 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003451 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar532c7802005-01-27 14:44:31 +00003453 /* Skip white space to allow ":call func ()". Not good, but required for
3454 * backward compatibility. */
3455 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 if (*startarg != '(')
3459 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003460 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 goto end;
3462 }
3463
3464 /*
3465 * When skipping, evaluate the function once, to find the end of the
3466 * arguments.
3467 * When the function takes a range, this is discovered after the first
3468 * call, and the loop is broken.
3469 */
3470 if (eap->skip)
3471 {
3472 ++emsg_skip;
3473 lnum = eap->line2; /* do it once, also with an invalid range */
3474 }
3475 else
3476 lnum = eap->line1;
3477 for ( ; lnum <= eap->line2; ++lnum)
3478 {
3479 if (!eap->skip && eap->addr_count > 0)
3480 {
3481 curwin->w_cursor.lnum = lnum;
3482 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003483#ifdef FEAT_VIRTUALEDIT
3484 curwin->w_cursor.coladd = 0;
3485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 eap->line1, eap->line2, &doesrange,
3490 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 failed = TRUE;
3493 break;
3494 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003495
3496 /* Handle a function returning a Funcref, Dictionary or List. */
3497 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3498 {
3499 failed = TRUE;
3500 break;
3501 }
3502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (doesrange || eap->skip)
3505 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003510 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (aborting())
3512 break;
3513 }
3514 if (eap->skip)
3515 --emsg_skip;
3516
3517 if (!failed)
3518 {
3519 /* Check for trailing illegal characters and a following command. */
3520 if (!ends_excmd(*arg))
3521 {
3522 emsg_severe = TRUE;
3523 EMSG(_(e_trailing));
3524 }
3525 else
3526 eap->nextcmd = check_nextcmd(arg);
3527 }
3528
3529end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003531 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532}
3533
3534/*
3535 * ":unlet[!] var1 ... " command.
3536 */
3537 void
3538ex_unlet(eap)
3539 exarg_T *eap;
3540{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003541 ex_unletlock(eap, eap->arg, 0);
3542}
3543
3544/*
3545 * ":lockvar" and ":unlockvar" commands
3546 */
3547 void
3548ex_lockvar(eap)
3549 exarg_T *eap;
3550{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 int deep = 2;
3553
3554 if (eap->forceit)
3555 deep = -1;
3556 else if (vim_isdigit(*arg))
3557 {
3558 deep = getdigits(&arg);
3559 arg = skipwhite(arg);
3560 }
3561
3562 ex_unletlock(eap, arg, deep);
3563}
3564
3565/*
3566 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3567 */
3568 static void
3569ex_unletlock(eap, argstart, deep)
3570 exarg_T *eap;
3571 char_u *argstart;
3572 int deep;
3573{
3574 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 do
3580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003582 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003583 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 if (lv.ll_name == NULL)
3585 error = TRUE; /* error but continue parsing */
3586 if (name_end == NULL || (!vim_iswhite(*name_end)
3587 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589 if (name_end != NULL)
3590 {
3591 emsg_severe = TRUE;
3592 EMSG(_(e_trailing));
3593 }
3594 if (!(eap->skip || error))
3595 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 break;
3597 }
3598
3599 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 {
3601 if (eap->cmdidx == CMD_unlet)
3602 {
3603 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3604 error = TRUE;
3605 }
3606 else
3607 {
3608 if (do_lock_var(&lv, name_end, deep,
3609 eap->cmdidx == CMD_lockvar) == FAIL)
3610 error = TRUE;
3611 }
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (!eap->skip)
3615 clear_lval(&lv);
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 arg = skipwhite(name_end);
3618 } while (!ends_excmd(*arg));
3619
3620 eap->nextcmd = check_nextcmd(arg);
3621}
3622
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 int forceit;
3628{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 int ret = OK;
3630 int cc;
3631
3632 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 cc = *name_end;
3635 *name_end = NUL;
3636
3637 /* Normal name or expanded name. */
3638 if (check_changedtick(lp->ll_name))
3639 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3645 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 else if (lp->ll_range)
3647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649
3650 /* Delete a range of List items. */
3651 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3652 {
3653 li = lp->ll_li->li_next;
3654 listitem_remove(lp->ll_list, lp->ll_li);
3655 lp->ll_li = li;
3656 ++lp->ll_n1;
3657 }
3658 }
3659 else
3660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 /* unlet a List item. */
3663 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003666 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 }
3668
3669 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670}
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672/*
3673 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 */
3676 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 hashtab_T *ht;
3682 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003684 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 ht = find_var_ht(name, &varname);
3687 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 hi = hash_find(ht, varname);
3690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003692 di = HI2DI(hi);
3693 if (var_check_fixed(di->di_flags, name)
3694 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 return FAIL;
3696 delete_var(ht, hi);
3697 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003700 if (forceit)
3701 return OK;
3702 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return FAIL;
3704}
3705
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003706/*
3707 * Lock or unlock variable indicated by "lp".
3708 * "deep" is the levels to go (-1 for unlimited);
3709 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3710 */
3711 static int
3712do_lock_var(lp, name_end, deep, lock)
3713 lval_T *lp;
3714 char_u *name_end;
3715 int deep;
3716 int lock;
3717{
3718 int ret = OK;
3719 int cc;
3720 dictitem_T *di;
3721
3722 if (deep == 0) /* nothing to do */
3723 return OK;
3724
3725 if (lp->ll_tv == NULL)
3726 {
3727 cc = *name_end;
3728 *name_end = NUL;
3729
3730 /* Normal name or expanded name. */
3731 if (check_changedtick(lp->ll_name))
3732 ret = FAIL;
3733 else
3734 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003735 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 if (di == NULL)
3737 ret = FAIL;
3738 else
3739 {
3740 if (lock)
3741 di->di_flags |= DI_FLAGS_LOCK;
3742 else
3743 di->di_flags &= ~DI_FLAGS_LOCK;
3744 item_lock(&di->di_tv, deep, lock);
3745 }
3746 }
3747 *name_end = cc;
3748 }
3749 else if (lp->ll_range)
3750 {
3751 listitem_T *li = lp->ll_li;
3752
3753 /* (un)lock a range of List items. */
3754 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3755 {
3756 item_lock(&li->li_tv, deep, lock);
3757 li = li->li_next;
3758 ++lp->ll_n1;
3759 }
3760 }
3761 else if (lp->ll_list != NULL)
3762 /* (un)lock a List item. */
3763 item_lock(&lp->ll_li->li_tv, deep, lock);
3764 else
3765 /* un(lock) a Dictionary item. */
3766 item_lock(&lp->ll_di->di_tv, deep, lock);
3767
3768 return ret;
3769}
3770
3771/*
3772 * Lock or unlock an item. "deep" is nr of levels to go.
3773 */
3774 static void
3775item_lock(tv, deep, lock)
3776 typval_T *tv;
3777 int deep;
3778 int lock;
3779{
3780 static int recurse = 0;
3781 list_T *l;
3782 listitem_T *li;
3783 dict_T *d;
3784 hashitem_T *hi;
3785 int todo;
3786
3787 if (recurse >= DICT_MAXNEST)
3788 {
3789 EMSG(_("E743: variable nested too deep for (un)lock"));
3790 return;
3791 }
3792 if (deep == 0)
3793 return;
3794 ++recurse;
3795
3796 /* lock/unlock the item itself */
3797 if (lock)
3798 tv->v_lock |= VAR_LOCKED;
3799 else
3800 tv->v_lock &= ~VAR_LOCKED;
3801
3802 switch (tv->v_type)
3803 {
3804 case VAR_LIST:
3805 if ((l = tv->vval.v_list) != NULL)
3806 {
3807 if (lock)
3808 l->lv_lock |= VAR_LOCKED;
3809 else
3810 l->lv_lock &= ~VAR_LOCKED;
3811 if (deep < 0 || deep > 1)
3812 /* recursive: lock/unlock the items the List contains */
3813 for (li = l->lv_first; li != NULL; li = li->li_next)
3814 item_lock(&li->li_tv, deep - 1, lock);
3815 }
3816 break;
3817 case VAR_DICT:
3818 if ((d = tv->vval.v_dict) != NULL)
3819 {
3820 if (lock)
3821 d->dv_lock |= VAR_LOCKED;
3822 else
3823 d->dv_lock &= ~VAR_LOCKED;
3824 if (deep < 0 || deep > 1)
3825 {
3826 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003827 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3829 {
3830 if (!HASHITEM_EMPTY(hi))
3831 {
3832 --todo;
3833 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3834 }
3835 }
3836 }
3837 }
3838 }
3839 --recurse;
3840}
3841
Bram Moolenaara40058a2005-07-11 22:42:07 +00003842/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003843 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3844 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003845 */
3846 static int
3847tv_islocked(tv)
3848 typval_T *tv;
3849{
3850 return (tv->v_lock & VAR_LOCKED)
3851 || (tv->v_type == VAR_LIST
3852 && tv->vval.v_list != NULL
3853 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3854 || (tv->v_type == VAR_DICT
3855 && tv->vval.v_dict != NULL
3856 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3857}
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3860/*
3861 * Delete all "menutrans_" variables.
3862 */
3863 void
3864del_menutrans_vars()
3865{
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003867 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003872 {
3873 if (!HASHITEM_EMPTY(hi))
3874 {
3875 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3877 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003878 }
3879 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003880 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881}
3882#endif
3883
3884#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3885
3886/*
3887 * Local string buffer for the next two functions to store a variable name
3888 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3889 * get_user_var_name().
3890 */
3891
3892static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3893
3894static char_u *varnamebuf = NULL;
3895static int varnamebuflen = 0;
3896
3897/*
3898 * Function to concatenate a prefix and a variable name.
3899 */
3900 static char_u *
3901cat_prefix_varname(prefix, name)
3902 int prefix;
3903 char_u *name;
3904{
3905 int len;
3906
3907 len = (int)STRLEN(name) + 3;
3908 if (len > varnamebuflen)
3909 {
3910 vim_free(varnamebuf);
3911 len += 10; /* some additional space */
3912 varnamebuf = alloc(len);
3913 if (varnamebuf == NULL)
3914 {
3915 varnamebuflen = 0;
3916 return NULL;
3917 }
3918 varnamebuflen = len;
3919 }
3920 *varnamebuf = prefix;
3921 varnamebuf[1] = ':';
3922 STRCPY(varnamebuf + 2, name);
3923 return varnamebuf;
3924}
3925
3926/*
3927 * Function given to ExpandGeneric() to obtain the list of user defined
3928 * (global/buffer/window/built-in) variable names.
3929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 char_u *
3931get_user_var_name(xp, idx)
3932 expand_T *xp;
3933 int idx;
3934{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003935 static long_u gdone;
3936 static long_u bdone;
3937 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003938#ifdef FEAT_WINDOWS
3939 static long_u tdone;
3940#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 static int vidx;
3942 static hashitem_T *hi;
3943 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003946 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003948#ifdef FEAT_WINDOWS
3949 tdone = 0;
3950#endif
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952
3953 /* Global variables */
3954 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003958 else
3959 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 while (HASHITEM_EMPTY(hi))
3961 ++hi;
3962 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3963 return cat_prefix_varname('g', hi->hi_key);
3964 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
3967 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003968 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003971 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003973 else
3974 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 while (HASHITEM_EMPTY(hi))
3976 ++hi;
3977 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003981 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return (char_u *)"b:changedtick";
3983 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003984
3985 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003986 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003989 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 else
3992 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003993 while (HASHITEM_EMPTY(hi))
3994 ++hi;
3995 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998#ifdef FEAT_WINDOWS
3999 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004000 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 if (tdone < ht->ht_used)
4002 {
4003 if (tdone++ == 0)
4004 hi = ht->ht_array;
4005 else
4006 ++hi;
4007 while (HASHITEM_EMPTY(hi))
4008 ++hi;
4009 return cat_prefix_varname('t', hi->hi_key);
4010 }
4011#endif
4012
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 /* v: variables */
4014 if (vidx < VV_LEN)
4015 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 vim_free(varnamebuf);
4018 varnamebuf = NULL;
4019 varnamebuflen = 0;
4020 return NULL;
4021}
4022
4023#endif /* FEAT_CMDL_COMPL */
4024
4025/*
4026 * types for expressions.
4027 */
4028typedef enum
4029{
4030 TYPE_UNKNOWN = 0
4031 , TYPE_EQUAL /* == */
4032 , TYPE_NEQUAL /* != */
4033 , TYPE_GREATER /* > */
4034 , TYPE_GEQUAL /* >= */
4035 , TYPE_SMALLER /* < */
4036 , TYPE_SEQUAL /* <= */
4037 , TYPE_MATCH /* =~ */
4038 , TYPE_NOMATCH /* !~ */
4039} exptype_T;
4040
4041/*
4042 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4045 */
4046
4047/*
4048 * Handle zero level expression.
4049 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004051 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 char_u **nextcmd;
4059 int evaluate;
4060{
4061 int ret;
4062 char_u *p;
4063
4064 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (ret == FAIL || !ends_excmd(*p))
4067 {
4068 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 /*
4071 * Report the invalid expression unless the expression evaluation has
4072 * been cancelled due to an aborting error, an interrupt, or an
4073 * exception.
4074 */
4075 if (!aborting())
4076 EMSG2(_(e_invexpr2), arg);
4077 ret = FAIL;
4078 }
4079 if (nextcmd != NULL)
4080 *nextcmd = check_nextcmd(p);
4081
4082 return ret;
4083}
4084
4085/*
4086 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004087 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 *
4089 * "arg" must point to the first non-white of the expression.
4090 * "arg" is advanced to the next non-white after the recognized expression.
4091 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004092 * Note: "rettv.v_lock" is not set.
4093 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 * Return OK or FAIL.
4095 */
4096 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 int evaluate;
4101{
4102 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 /*
4106 * Get the first variable.
4107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110
4111 if ((*arg)[0] == '?')
4112 {
4113 result = FALSE;
4114 if (evaluate)
4115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116 int error = FALSE;
4117
4118 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 if (error)
4122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124
4125 /*
4126 * Get the second variable.
4127 */
4128 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Check for the ":".
4134 */
4135 if ((*arg)[0] != ':')
4136 {
4137 EMSG(_("E109: Missing ':' after '?'"));
4138 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142
4143 /*
4144 * Get the third variable.
4145 */
4146 *arg = skipwhite(*arg + 1);
4147 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4148 {
4149 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return FAIL;
4152 }
4153 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 return OK;
4158}
4159
4160/*
4161 * Handle first level expression:
4162 * expr2 || expr2 || expr2 logical OR
4163 *
4164 * "arg" must point to the first non-white of the expression.
4165 * "arg" is advanced to the next non-white after the recognized expression.
4166 *
4167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int evaluate;
4174{
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 long result;
4177 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 /*
4181 * Get the first variable.
4182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 /*
4187 * Repeat until there is no following "||".
4188 */
4189 first = TRUE;
4190 result = FALSE;
4191 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4192 {
4193 if (evaluate && first)
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (error)
4199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 first = FALSE;
4201 }
4202
4203 /*
4204 * Get the second variable.
4205 */
4206 *arg = skipwhite(*arg + 2);
4207 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4208 return FAIL;
4209
4210 /*
4211 * Compute the result.
4212 */
4213 if (evaluate && !result)
4214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 if (error)
4219 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 if (evaluate)
4222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 rettv->v_type = VAR_NUMBER;
4224 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227
4228 return OK;
4229}
4230
4231/*
4232 * Handle second level expression:
4233 * expr3 && expr3 && expr3 logical AND
4234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
4238 * Return OK or FAIL.
4239 */
4240 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 int evaluate;
4245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 long result;
4248 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Repeat until there is no following "&&".
4259 */
4260 first = TRUE;
4261 result = TRUE;
4262 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4263 {
4264 if (evaluate && first)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 first = FALSE;
4272 }
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 2);
4278 if (eval4(arg, &var2, evaluate && result) == FAIL)
4279 return FAIL;
4280
4281 /*
4282 * Compute the result.
4283 */
4284 if (evaluate && result)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (evaluate)
4293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 rettv->v_type = VAR_NUMBER;
4295 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298
4299 return OK;
4300}
4301
4302/*
4303 * Handle third level expression:
4304 * var1 == var2
4305 * var1 =~ var2
4306 * var1 != var2
4307 * var1 !~ var2
4308 * var1 > var2
4309 * var1 >= var2
4310 * var1 < var2
4311 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004312 * var1 is var2
4313 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int evaluate;
4325{
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 char_u *p;
4328 int i;
4329 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int len = 2;
4332 long n1, n2;
4333 char_u *s1, *s2;
4334 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4335 regmatch_T regmatch;
4336 int ic;
4337 char_u *save_cpo;
4338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 p = *arg;
4346 switch (p[0])
4347 {
4348 case '=': if (p[1] == '=')
4349 type = TYPE_EQUAL;
4350 else if (p[1] == '~')
4351 type = TYPE_MATCH;
4352 break;
4353 case '!': if (p[1] == '=')
4354 type = TYPE_NEQUAL;
4355 else if (p[1] == '~')
4356 type = TYPE_NOMATCH;
4357 break;
4358 case '>': if (p[1] != '=')
4359 {
4360 type = TYPE_GREATER;
4361 len = 1;
4362 }
4363 else
4364 type = TYPE_GEQUAL;
4365 break;
4366 case '<': if (p[1] != '=')
4367 {
4368 type = TYPE_SMALLER;
4369 len = 1;
4370 }
4371 else
4372 type = TYPE_SEQUAL;
4373 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 case 'i': if (p[1] == 's')
4375 {
4376 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4377 len = 5;
4378 if (!vim_isIDc(p[len]))
4379 {
4380 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4381 type_is = TRUE;
4382 }
4383 }
4384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386
4387 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 if (type != TYPE_UNKNOWN)
4391 {
4392 /* extra question mark appended: ignore case */
4393 if (p[len] == '?')
4394 {
4395 ic = TRUE;
4396 ++len;
4397 }
4398 /* extra '#' appended: match case */
4399 else if (p[len] == '#')
4400 {
4401 ic = FALSE;
4402 ++len;
4403 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004404 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 else
4406 ic = p_ic;
4407
4408 /*
4409 * Get the second variable.
4410 */
4411 *arg = skipwhite(p + len);
4412 if (eval5(arg, &var2, evaluate) == FAIL)
4413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416 }
4417
4418 if (evaluate)
4419 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 if (type_is && rettv->v_type != var2.v_type)
4421 {
4422 /* For "is" a different type always means FALSE, for "notis"
4423 * it means TRUE. */
4424 n1 = (type == TYPE_NEQUAL);
4425 }
4426 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4427 {
4428 if (type_is)
4429 {
4430 n1 = (rettv->v_type == var2.v_type
4431 && rettv->vval.v_list == var2.vval.v_list);
4432 if (type == TYPE_NEQUAL)
4433 n1 = !n1;
4434 }
4435 else if (rettv->v_type != var2.v_type
4436 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4437 {
4438 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004439 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004441 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 clear_tv(rettv);
4443 clear_tv(&var2);
4444 return FAIL;
4445 }
4446 else
4447 {
4448 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004449 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4450 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 if (type == TYPE_NEQUAL)
4452 n1 = !n1;
4453 }
4454 }
4455
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004456 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4457 {
4458 if (type_is)
4459 {
4460 n1 = (rettv->v_type == var2.v_type
4461 && rettv->vval.v_dict == var2.vval.v_dict);
4462 if (type == TYPE_NEQUAL)
4463 n1 = !n1;
4464 }
4465 else if (rettv->v_type != var2.v_type
4466 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4467 {
4468 if (rettv->v_type != var2.v_type)
4469 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4470 else
4471 EMSG(_("E736: Invalid operation for Dictionary"));
4472 clear_tv(rettv);
4473 clear_tv(&var2);
4474 return FAIL;
4475 }
4476 else
4477 {
4478 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004479 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4480 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004481 if (type == TYPE_NEQUAL)
4482 n1 = !n1;
4483 }
4484 }
4485
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4487 {
4488 if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004494 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Funcrefs for being equal or unequal. */
4502 if (rettv->vval.v_string == NULL
4503 || var2.vval.v_string == NULL)
4504 n1 = FALSE;
4505 else
4506 n1 = STRCMP(rettv->vval.v_string,
4507 var2.vval.v_string) == 0;
4508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004513#ifdef FEAT_FLOAT
4514 /*
4515 * If one of the two variables is a float, compare as a float.
4516 * When using "=~" or "!~", always compare as string.
4517 */
4518 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4519 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 {
4521 float_T f1, f2;
4522
4523 if (rettv->v_type == VAR_FLOAT)
4524 f1 = rettv->vval.v_float;
4525 else
4526 f1 = get_tv_number(rettv);
4527 if (var2.v_type == VAR_FLOAT)
4528 f2 = var2.vval.v_float;
4529 else
4530 f2 = get_tv_number(&var2);
4531 n1 = FALSE;
4532 switch (type)
4533 {
4534 case TYPE_EQUAL: n1 = (f1 == f2); break;
4535 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4536 case TYPE_GREATER: n1 = (f1 > f2); break;
4537 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4538 case TYPE_SMALLER: n1 = (f1 < f2); break;
4539 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4540 case TYPE_UNKNOWN:
4541 case TYPE_MATCH:
4542 case TYPE_NOMATCH: break; /* avoid gcc warning */
4543 }
4544 }
4545#endif
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /*
4548 * If one of the two variables is a number, compare as a number.
4549 * When using "=~" or "!~", always compare as string.
4550 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 n1 = get_tv_number(rettv);
4555 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 switch (type)
4557 {
4558 case TYPE_EQUAL: n1 = (n1 == n2); break;
4559 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4560 case TYPE_GREATER: n1 = (n1 > n2); break;
4561 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4562 case TYPE_SMALLER: n1 = (n1 < n2); break;
4563 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4564 case TYPE_UNKNOWN:
4565 case TYPE_MATCH:
4566 case TYPE_NOMATCH: break; /* avoid gcc warning */
4567 }
4568 }
4569 else
4570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 s1 = get_tv_string_buf(rettv, buf1);
4572 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4574 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4575 else
4576 i = 0;
4577 n1 = FALSE;
4578 switch (type)
4579 {
4580 case TYPE_EQUAL: n1 = (i == 0); break;
4581 case TYPE_NEQUAL: n1 = (i != 0); break;
4582 case TYPE_GREATER: n1 = (i > 0); break;
4583 case TYPE_GEQUAL: n1 = (i >= 0); break;
4584 case TYPE_SMALLER: n1 = (i < 0); break;
4585 case TYPE_SEQUAL: n1 = (i <= 0); break;
4586
4587 case TYPE_MATCH:
4588 case TYPE_NOMATCH:
4589 /* avoid 'l' flag in 'cpoptions' */
4590 save_cpo = p_cpo;
4591 p_cpo = (char_u *)"";
4592 regmatch.regprog = vim_regcomp(s2,
4593 RE_MAGIC + RE_STRING);
4594 regmatch.rm_ic = ic;
4595 if (regmatch.regprog != NULL)
4596 {
4597 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004598 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (type == TYPE_NOMATCH)
4600 n1 = !n1;
4601 }
4602 p_cpo = save_cpo;
4603 break;
4604
4605 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4606 }
4607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 rettv->v_type = VAR_NUMBER;
4611 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 }
4614
4615 return OK;
4616}
4617
4618/*
4619 * Handle fourth level expression:
4620 * + number addition
4621 * - number subtraction
4622 * . string concatenation
4623 *
4624 * "arg" must point to the first non-white of the expression.
4625 * "arg" is advanced to the next non-white after the recognized expression.
4626 *
4627 * Return OK or FAIL.
4628 */
4629 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 int evaluate;
4634{
Bram Moolenaar33570922005-01-25 22:26:29 +00004635 typval_T var2;
4636 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 int op;
4638 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639#ifdef FEAT_FLOAT
4640 float_T f1 = 0, f2 = 0;
4641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 char_u *s1, *s2;
4643 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4644 char_u *p;
4645
4646 /*
4647 * Get the first variable.
4648 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004649 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return FAIL;
4651
4652 /*
4653 * Repeat computing, until no '+', '-' or '.' is following.
4654 */
4655 for (;;)
4656 {
4657 op = **arg;
4658 if (op != '+' && op != '-' && op != '.')
4659 break;
4660
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661 if ((op != '+' || rettv->v_type != VAR_LIST)
4662#ifdef FEAT_FLOAT
4663 && (op == '.' || rettv->v_type != VAR_FLOAT)
4664#endif
4665 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 {
4667 /* For "list + ...", an illegal use of the first operand as
4668 * a number cannot be determined before evaluating the 2nd
4669 * operand: if this is also a list, all is ok.
4670 * For "something . ...", "something - ..." or "non-list + ...",
4671 * we know that the first operand needs to be a string or number
4672 * without evaluating the 2nd operand. So check before to avoid
4673 * side effects after an error. */
4674 if (evaluate && get_tv_string_chk(rettv) == NULL)
4675 {
4676 clear_tv(rettv);
4677 return FAIL;
4678 }
4679 }
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 /*
4682 * Get the second variable.
4683 */
4684 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004685 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
4689 }
4690
4691 if (evaluate)
4692 {
4693 /*
4694 * Compute the result.
4695 */
4696 if (op == '.')
4697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4699 s2 = get_tv_string_buf_chk(&var2, buf2);
4700 if (s2 == NULL) /* type error ? */
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004706 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(rettv);
4708 rettv->v_type = VAR_STRING;
4709 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004711 else if (op == '+' && rettv->v_type == VAR_LIST
4712 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004713 {
4714 /* concatenate Lists */
4715 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4716 &var3) == FAIL)
4717 {
4718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 return FAIL;
4721 }
4722 clear_tv(rettv);
4723 *rettv = var3;
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
4726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 int error = FALSE;
4728
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729#ifdef FEAT_FLOAT
4730 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732 f1 = rettv->vval.v_float;
4733 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735 else
4736#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004738 n1 = get_tv_number_chk(rettv, &error);
4739 if (error)
4740 {
4741 /* This can only happen for "list + non-list". For
4742 * "non-list + ..." or "something - ...", we returned
4743 * before evaluating the 2nd operand. */
4744 clear_tv(rettv);
4745 return FAIL;
4746 }
4747#ifdef FEAT_FLOAT
4748 if (var2.v_type == VAR_FLOAT)
4749 f1 = n1;
4750#endif
4751 }
4752#ifdef FEAT_FLOAT
4753 if (var2.v_type == VAR_FLOAT)
4754 {
4755 f2 = var2.vval.v_float;
4756 n2 = 0;
4757 }
4758 else
4759#endif
4760 {
4761 n2 = get_tv_number_chk(&var2, &error);
4762 if (error)
4763 {
4764 clear_tv(rettv);
4765 clear_tv(&var2);
4766 return FAIL;
4767 }
4768#ifdef FEAT_FLOAT
4769 if (rettv->v_type == VAR_FLOAT)
4770 f2 = n2;
4771#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774
4775#ifdef FEAT_FLOAT
4776 /* If there is a float on either side the result is a float. */
4777 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4778 {
4779 if (op == '+')
4780 f1 = f1 + f2;
4781 else
4782 f1 = f1 - f2;
4783 rettv->v_type = VAR_FLOAT;
4784 rettv->vval.v_float = f1;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787#endif
4788 {
4789 if (op == '+')
4790 n1 = n1 + n2;
4791 else
4792 n1 = n1 - n2;
4793 rettv->v_type = VAR_NUMBER;
4794 rettv->vval.v_number = n1;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
4800 return OK;
4801}
4802
4803/*
4804 * Handle fifth level expression:
4805 * * number multiplication
4806 * / number division
4807 * % number modulo
4808 *
4809 * "arg" must point to the first non-white of the expression.
4810 * "arg" is advanced to the next non-white after the recognized expression.
4811 *
4812 * Return OK or FAIL.
4813 */
4814 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004815eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004819 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
Bram Moolenaar33570922005-01-25 22:26:29 +00004821 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 int op;
4823 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824#ifdef FEAT_FLOAT
4825 int use_float = FALSE;
4826 float_T f1 = 0, f2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 /*
4831 * Get the first variable.
4832 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004833 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return FAIL;
4835
4836 /*
4837 * Repeat computing, until no '*', '/' or '%' is following.
4838 */
4839 for (;;)
4840 {
4841 op = **arg;
4842 if (op != '*' && op != '/' && op != '%')
4843 break;
4844
4845 if (evaluate)
4846 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847#ifdef FEAT_FLOAT
4848 if (rettv->v_type == VAR_FLOAT)
4849 {
4850 f1 = rettv->vval.v_float;
4851 use_float = TRUE;
4852 n1 = 0;
4853 }
4854 else
4855#endif
4856 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858 if (error)
4859 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 else
4862 n1 = 0;
4863
4864 /*
4865 * Get the second variable.
4866 */
4867 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004868 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return FAIL;
4870
4871 if (evaluate)
4872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (var2.v_type == VAR_FLOAT)
4875 {
4876 if (!use_float)
4877 {
4878 f1 = n1;
4879 use_float = TRUE;
4880 }
4881 f2 = var2.vval.v_float;
4882 n2 = 0;
4883 }
4884 else
4885#endif
4886 {
4887 n2 = get_tv_number_chk(&var2, &error);
4888 clear_tv(&var2);
4889 if (error)
4890 return FAIL;
4891#ifdef FEAT_FLOAT
4892 if (use_float)
4893 f2 = n2;
4894#endif
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904 if (op == '*')
4905 f1 = f1 * f2;
4906 else if (op == '/')
4907 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# ifdef VMS
4909 /* VMS crashes on divide by zero, work around it */
4910 if (f2 == 0.0)
4911 {
4912 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004913 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004914 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004915 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004916 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004917 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918 }
4919 else
4920 f1 = f1 / f2;
4921# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 /* We rely on the floating point library to handle divide
4923 * by zero to result in "inf" and not a crash. */
4924 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004925# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004929 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930 return FAIL;
4931 }
4932 rettv->v_type = VAR_FLOAT;
4933 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 if (op == '*')
4939 n1 = n1 * n2;
4940 else if (op == '/')
4941 {
4942 if (n2 == 0) /* give an error message? */
4943 {
4944 if (n1 == 0)
4945 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4946 else if (n1 < 0)
4947 n1 = -0x7fffffffL;
4948 else
4949 n1 = 0x7fffffffL;
4950 }
4951 else
4952 n1 = n1 / n2;
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 {
4956 if (n2 == 0) /* give an error message? */
4957 n1 = 0;
4958 else
4959 n1 = n1 % n2;
4960 }
4961 rettv->v_type = VAR_NUMBER;
4962 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 }
4966
4967 return OK;
4968}
4969
4970/*
4971 * Handle sixth level expression:
4972 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004973 * "string" string constant
4974 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 * &option-name option value
4976 * @r register contents
4977 * identifier variable value
4978 * function() function call
4979 * $VAR environment variable
4980 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004981 * [expr, expr] List
4982 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * Also handle:
4985 * ! in front logical NOT
4986 * - in front unary minus
4987 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 * trailing [] subscript in String or List
4989 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 *
4991 * "arg" must point to the first non-white of the expression.
4992 * "arg" is advanced to the next non-white after the recognized expression.
4993 *
4994 * Return OK or FAIL.
4995 */
4996 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004997eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005001 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 long n;
5004 int len;
5005 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 char_u *start_leader, *end_leader;
5007 int ret = OK;
5008 char_u *alias;
5009
5010 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 /*
5017 * Skip '!' and '-' characters. They are handled later.
5018 */
5019 start_leader = *arg;
5020 while (**arg == '!' || **arg == '-' || **arg == '+')
5021 *arg = skipwhite(*arg + 1);
5022 end_leader = *arg;
5023
5024 switch (**arg)
5025 {
5026 /*
5027 * Number constant.
5028 */
5029 case '0':
5030 case '1':
5031 case '2':
5032 case '3':
5033 case '4':
5034 case '5':
5035 case '6':
5036 case '7':
5037 case '8':
5038 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 {
5040#ifdef FEAT_FLOAT
5041 char_u *p = skipdigits(*arg + 1);
5042 int get_float = FALSE;
5043
5044 /* We accept a float when the format matches
5045 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005046 * strict to avoid backwards compatibility problems.
5047 * Don't look for a float after the "." operator, so that
5048 * ":let vers = 1.2.3" doesn't fail. */
5049 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005051 get_float = TRUE;
5052 p = skipdigits(p + 2);
5053 if (*p == 'e' || *p == 'E')
5054 {
5055 ++p;
5056 if (*p == '-' || *p == '+')
5057 ++p;
5058 if (!vim_isdigit(*p))
5059 get_float = FALSE;
5060 else
5061 p = skipdigits(p + 1);
5062 }
5063 if (ASCII_ISALPHA(*p) || *p == '.')
5064 get_float = FALSE;
5065 }
5066 if (get_float)
5067 {
5068 float_T f;
5069
5070 *arg += string2float(*arg, &f);
5071 if (evaluate)
5072 {
5073 rettv->v_type = VAR_FLOAT;
5074 rettv->vval.v_float = f;
5075 }
5076 }
5077 else
5078#endif
5079 {
5080 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5081 *arg += len;
5082 if (evaluate)
5083 {
5084 rettv->v_type = VAR_NUMBER;
5085 rettv->vval.v_number = n;
5086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 /*
5092 * String constant: "string".
5093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 break;
5102
5103 /*
5104 * List: [expr, expr]
5105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108
5109 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 * Dictionary: {key: val, key: val}
5111 */
5112 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5113 break;
5114
5115 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005116 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 break;
5120
5121 /*
5122 * Environment variable: $VAR.
5123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005124 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126
5127 /*
5128 * Register contents: @r.
5129 */
5130 case '@': ++*arg;
5131 if (evaluate)
5132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005134 rettv->vval.v_string = get_reg_contents(**arg,
5135 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137 if (**arg != NUL)
5138 ++*arg;
5139 break;
5140
5141 /*
5142 * nested expression: (expression).
5143 */
5144 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (**arg == ')')
5147 ++*arg;
5148 else if (ret == OK)
5149 {
5150 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 ret = FAIL;
5153 }
5154 break;
5155
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159
5160 if (ret == NOTDONE)
5161 {
5162 /*
5163 * Must be a variable or function name.
5164 * Can also be a curly-braces kind of name: {expr}.
5165 */
5166 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005167 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 if (alias != NULL)
5169 s = alias;
5170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005171 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 ret = FAIL;
5173 else
5174 {
5175 if (**arg == '(') /* recursive! */
5176 {
5177 /* If "s" is the name of a variable of type VAR_FUNC
5178 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005179 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180
5181 /* Invoke the function. */
5182 ret = get_func_tv(s, len, rettv, arg,
5183 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185
5186 /* If evaluate is FALSE rettv->v_type was not set in
5187 * get_func_tv, but it's needed in handle_subscript() to parse
5188 * what follows. So set it here. */
5189 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5190 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005191 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005192 rettv->v_type = VAR_FUNC;
5193 }
5194
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 /* Stop the expression evaluation when immediately
5196 * aborting on error, or when an interrupt occurred or
5197 * an exception was thrown but not caught. */
5198 if (aborting())
5199 {
5200 if (ret == OK)
5201 clear_tv(rettv);
5202 ret = FAIL;
5203 }
5204 }
5205 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005206 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005207 else
5208 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005210 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 }
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 *arg = skipwhite(*arg);
5214
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5216 * expr(expr). */
5217 if (ret == OK)
5218 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 /*
5221 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5222 */
5223 if (ret == OK && evaluate && end_leader > start_leader)
5224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 int val = 0;
5227#ifdef FEAT_FLOAT
5228 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230 if (rettv->v_type == VAR_FLOAT)
5231 f = rettv->vval.v_float;
5232 else
5233#endif
5234 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 clear_tv(rettv);
5238 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005240 else
5241 {
5242 while (end_leader > start_leader)
5243 {
5244 --end_leader;
5245 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 {
5247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 f = !f;
5250 else
5251#endif
5252 val = !val;
5253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005255 {
5256#ifdef FEAT_FLOAT
5257 if (rettv->v_type == VAR_FLOAT)
5258 f = -f;
5259 else
5260#endif
5261 val = -val;
5262 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005264#ifdef FEAT_FLOAT
5265 if (rettv->v_type == VAR_FLOAT)
5266 {
5267 clear_tv(rettv);
5268 rettv->vval.v_float = f;
5269 }
5270 else
5271#endif
5272 {
5273 clear_tv(rettv);
5274 rettv->v_type = VAR_NUMBER;
5275 rettv->vval.v_number = val;
5276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279
5280 return ret;
5281}
5282
5283/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005284 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5285 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5287 */
5288 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005293 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294{
5295 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 long len = -1;
5299 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005303 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005305 if (verbose)
5306 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 return FAIL;
5308 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005309#ifdef FEAT_FLOAT
5310 else if (rettv->v_type == VAR_FLOAT)
5311 {
5312 if (verbose)
5313 EMSG(_(e_float_as_string));
5314 return FAIL;
5315 }
5316#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 /*
5321 * dict.name
5322 */
5323 key = *arg + 1;
5324 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5325 ;
5326 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 }
5330 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 /*
5333 * something[idx]
5334 *
5335 * Get the (first) variable from inside the [].
5336 */
5337 *arg = skipwhite(*arg + 1);
5338 if (**arg == ':')
5339 empty1 = TRUE;
5340 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5341 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5343 {
5344 /* not a number or string */
5345 clear_tv(&var1);
5346 return FAIL;
5347 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348
5349 /*
5350 * Get the second variable from inside the [:].
5351 */
5352 if (**arg == ':')
5353 {
5354 range = TRUE;
5355 *arg = skipwhite(*arg + 1);
5356 if (**arg == ']')
5357 empty2 = TRUE;
5358 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 if (!empty1)
5361 clear_tv(&var1);
5362 return FAIL;
5363 }
5364 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5365 {
5366 /* not a number or string */
5367 if (!empty1)
5368 clear_tv(&var1);
5369 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 return FAIL;
5371 }
5372 }
5373
5374 /* Check for the ']'. */
5375 if (**arg != ']')
5376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005377 if (verbose)
5378 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 clear_tv(&var1);
5380 if (range)
5381 clear_tv(&var2);
5382 return FAIL;
5383 }
5384 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386
5387 if (evaluate)
5388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 n1 = 0;
5390 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 n1 = get_tv_number(&var1);
5393 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 }
5395 if (range)
5396 {
5397 if (empty2)
5398 n2 = -1;
5399 else
5400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 n2 = get_tv_number(&var2);
5402 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 }
5405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
5408 case VAR_NUMBER:
5409 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 len = (long)STRLEN(s);
5412 if (range)
5413 {
5414 /* The resulting variable is a substring. If the indexes
5415 * are out of range the result is empty. */
5416 if (n1 < 0)
5417 {
5418 n1 = len + n1;
5419 if (n1 < 0)
5420 n1 = 0;
5421 }
5422 if (n2 < 0)
5423 n2 = len + n2;
5424 else if (n2 >= len)
5425 n2 = len;
5426 if (n1 >= len || n2 < 0 || n1 > n2)
5427 s = NULL;
5428 else
5429 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5430 }
5431 else
5432 {
5433 /* The resulting variable is a string of a single
5434 * character. If the index is too big or negative the
5435 * result is empty. */
5436 if (n1 >= len || n1 < 0)
5437 s = NULL;
5438 else
5439 s = vim_strnsave(s + n1, 1);
5440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 clear_tv(rettv);
5442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 break;
5445
5446 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 if (n1 < 0)
5449 n1 = len + n1;
5450 if (!empty1 && (n1 < 0 || n1 >= len))
5451 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005452 /* For a range we allow invalid values and return an empty
5453 * list. A list index out of range is an error. */
5454 if (!range)
5455 {
5456 if (verbose)
5457 EMSGN(_(e_listidx), n1);
5458 return FAIL;
5459 }
5460 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 if (range)
5463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 list_T *l;
5465 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466
5467 if (n2 < 0)
5468 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005469 else if (n2 >= len)
5470 n2 = len - 1;
5471 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005472 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 l = list_alloc();
5474 if (l == NULL)
5475 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 n1 <= n2; ++n1)
5478 {
5479 if (list_append_tv(l, &item->li_tv) == FAIL)
5480 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005481 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 return FAIL;
5483 }
5484 item = item->li_next;
5485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 clear_tv(rettv);
5487 rettv->v_type = VAR_LIST;
5488 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005489 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 }
5491 else
5492 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005493 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 clear_tv(rettv);
5495 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
5499 case VAR_DICT:
5500 if (range)
5501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005502 if (verbose)
5503 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 if (len == -1)
5505 clear_tv(&var1);
5506 return FAIL;
5507 }
5508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510
5511 if (len == -1)
5512 {
5513 key = get_tv_string(&var1);
5514 if (*key == NUL)
5515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005516 if (verbose)
5517 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 clear_tv(&var1);
5519 return FAIL;
5520 }
5521 }
5522
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005523 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005526 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 if (len == -1)
5528 clear_tv(&var1);
5529 if (item == NULL)
5530 return FAIL;
5531
5532 copy_tv(&item->di_tv, &var1);
5533 clear_tv(rettv);
5534 *rettv = var1;
5535 }
5536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 }
5538 }
5539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 return OK;
5541}
5542
5543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 * Get an option value.
5545 * "arg" points to the '&' or '+' before the option name.
5546 * "arg" is advanced to character after the option name.
5547 * Return OK or FAIL.
5548 */
5549 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 int evaluate;
5554{
5555 char_u *option_end;
5556 long numval;
5557 char_u *stringval;
5558 int opt_type;
5559 int c;
5560 int working = (**arg == '+'); /* has("+option") */
5561 int ret = OK;
5562 int opt_flags;
5563
5564 /*
5565 * Isolate the option name and find its value.
5566 */
5567 option_end = find_option_end(arg, &opt_flags);
5568 if (option_end == NULL)
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 EMSG2(_("E112: Option name missing: %s"), *arg);
5572 return FAIL;
5573 }
5574
5575 if (!evaluate)
5576 {
5577 *arg = option_end;
5578 return OK;
5579 }
5580
5581 c = *option_end;
5582 *option_end = NUL;
5583 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 if (opt_type == -3) /* invalid name */
5587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005588 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 EMSG2(_("E113: Unknown option: %s"), *arg);
5590 ret = FAIL;
5591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
5594 if (opt_type == -2) /* hidden string option */
5595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 rettv->v_type = VAR_STRING;
5597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else if (opt_type == -1) /* hidden number option */
5600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 rettv->v_type = VAR_NUMBER;
5602 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604 else if (opt_type == 1) /* number option */
5605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606 rettv->v_type = VAR_NUMBER;
5607 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609 else /* string option */
5610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 rettv->v_type = VAR_STRING;
5612 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 }
5615 else if (working && (opt_type == -2 || opt_type == -1))
5616 ret = FAIL;
5617
5618 *option_end = c; /* put back for error messages */
5619 *arg = option_end;
5620
5621 return ret;
5622}
5623
5624/*
5625 * Allocate a variable for a string constant.
5626 * Return OK or FAIL.
5627 */
5628 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 int evaluate;
5633{
5634 char_u *p;
5635 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 int extra = 0;
5637
5638 /*
5639 * Find the end of the string, skipping backslashed characters.
5640 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 if (*p == '\\' && p[1] != NUL)
5644 {
5645 ++p;
5646 /* A "\<x>" form occupies at least 4 characters, and produces up
5647 * to 6 characters: reserve space for 2 extra */
5648 if (*p == '<')
5649 extra += 2;
5650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652
5653 if (*p != '"')
5654 {
5655 EMSG2(_("E114: Missing quote: %s"), *arg);
5656 return FAIL;
5657 }
5658
5659 /* If only parsing, set *arg and return here */
5660 if (!evaluate)
5661 {
5662 *arg = p + 1;
5663 return OK;
5664 }
5665
5666 /*
5667 * Copy the string into allocated memory, handling backslashed
5668 * characters.
5669 */
5670 name = alloc((unsigned)(p - *arg + extra));
5671 if (name == NULL)
5672 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 rettv->v_type = VAR_STRING;
5674 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 if (*p == '\\')
5679 {
5680 switch (*++p)
5681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 case 'b': *name++ = BS; ++p; break;
5683 case 'e': *name++ = ESC; ++p; break;
5684 case 'f': *name++ = FF; ++p; break;
5685 case 'n': *name++ = NL; ++p; break;
5686 case 'r': *name++ = CAR; ++p; break;
5687 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689 case 'X': /* hex: "\x1", "\x12" */
5690 case 'x':
5691 case 'u': /* Unicode: "\u0023" */
5692 case 'U':
5693 if (vim_isxdigit(p[1]))
5694 {
5695 int n, nr;
5696 int c = toupper(*p);
5697
5698 if (c == 'X')
5699 n = 2;
5700 else
5701 n = 4;
5702 nr = 0;
5703 while (--n >= 0 && vim_isxdigit(p[1]))
5704 {
5705 ++p;
5706 nr = (nr << 4) + hex2nr(*p);
5707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#ifdef FEAT_MBYTE
5710 /* For "\u" store the number according to
5711 * 'encoding'. */
5712 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 else
5715#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 break;
5719
5720 /* octal: "\1", "\12", "\123" */
5721 case '0':
5722 case '1':
5723 case '2':
5724 case '3':
5725 case '4':
5726 case '5':
5727 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 case '7': *name = *p++ - '0';
5729 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 *name = (*name << 3) + *p++ - '0';
5732 if (*p >= '0' && *p <= '7')
5733 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737
5738 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (extra != 0)
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 break;
5744 }
5745 /* FALLTHROUGH */
5746
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 break;
5749 }
5750 }
5751 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 *arg = p + 1;
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 return OK;
5759}
5760
5761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 * Return OK or FAIL.
5764 */
5765 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005766get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 int evaluate;
5770{
5771 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 int reduce = 0;
5774
5775 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 */
5778 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 break;
5784 ++reduce;
5785 ++p;
5786 }
5787 }
5788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 return FAIL;
5793 }
5794
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 if (!evaluate)
5797 {
5798 *arg = p + 1;
5799 return OK;
5800 }
5801
5802 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 */
5805 str = alloc((unsigned)((p - *arg) - reduce));
5806 if (str == NULL)
5807 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 rettv->v_type = VAR_STRING;
5809 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 break;
5817 ++p;
5818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005822 *arg = p + 1;
5823
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 return OK;
5825}
5826
5827/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 * Allocate a variable for a List and fill it from "*arg".
5829 * Return OK or FAIL.
5830 */
5831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 int evaluate;
5836{
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l = NULL;
5838 typval_T tv;
5839 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840
5841 if (evaluate)
5842 {
5843 l = list_alloc();
5844 if (l == NULL)
5845 return FAIL;
5846 }
5847
5848 *arg = skipwhite(*arg + 1);
5849 while (**arg != ']' && **arg != NUL)
5850 {
5851 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5852 goto failret;
5853 if (evaluate)
5854 {
5855 item = listitem_alloc();
5856 if (item != NULL)
5857 {
5858 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005859 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 list_append(l, item);
5861 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005862 else
5863 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 }
5865
5866 if (**arg == ']')
5867 break;
5868 if (**arg != ',')
5869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 goto failret;
5872 }
5873 *arg = skipwhite(*arg + 1);
5874 }
5875
5876 if (**arg != ']')
5877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879failret:
5880 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005881 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 return FAIL;
5883 }
5884
5885 *arg = skipwhite(*arg + 1);
5886 if (evaluate)
5887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888 rettv->v_type = VAR_LIST;
5889 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 ++l->lv_refcount;
5891 }
5892
5893 return OK;
5894}
5895
5896/*
5897 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005898 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005900 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901list_alloc()
5902{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005903 list_T *l;
5904
5905 l = (list_T *)alloc_clear(sizeof(list_T));
5906 if (l != NULL)
5907 {
5908 /* Prepend the list to the list of lists for garbage collection. */
5909 if (first_list != NULL)
5910 first_list->lv_used_prev = l;
5911 l->lv_used_prev = NULL;
5912 l->lv_used_next = first_list;
5913 first_list = l;
5914 }
5915 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916}
5917
5918/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005919 * Allocate an empty list for a return value.
5920 * Returns OK or FAIL.
5921 */
5922 static int
5923rettv_list_alloc(rettv)
5924 typval_T *rettv;
5925{
5926 list_T *l = list_alloc();
5927
5928 if (l == NULL)
5929 return FAIL;
5930
5931 rettv->vval.v_list = l;
5932 rettv->v_type = VAR_LIST;
5933 ++l->lv_refcount;
5934 return OK;
5935}
5936
5937/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 * Unreference a list: decrement the reference count and free it when it
5939 * becomes zero.
5940 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005941 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005945 if (l != NULL && --l->lv_refcount <= 0)
5946 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947}
5948
5949/*
5950 * Free a list, including all items it points to.
5951 * Ignores the reference count.
5952 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005953 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005954list_free(l, recurse)
5955 list_T *l;
5956 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 /* Remove the list from the list of lists for garbage collection. */
5961 if (l->lv_used_prev == NULL)
5962 first_list = l->lv_used_next;
5963 else
5964 l->lv_used_prev->lv_used_next = l->lv_used_next;
5965 if (l->lv_used_next != NULL)
5966 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5967
Bram Moolenaard9fba312005-06-26 22:34:35 +00005968 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005970 /* Remove the item before deleting it. */
5971 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005972 if (recurse || (item->li_tv.v_type != VAR_LIST
5973 && item->li_tv.v_type != VAR_DICT))
5974 clear_tv(&item->li_tv);
5975 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 }
5977 vim_free(l);
5978}
5979
5980/*
5981 * Allocate a list item.
5982 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005983 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984listitem_alloc()
5985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987}
5988
5989/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005990 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005992 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005996 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 vim_free(item);
5998}
5999
6000/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006001 * Remove a list item from a List and free it. Also clears the value.
6002 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006003 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006004listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 list_T *l;
6006 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006007{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006008 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006009 listitem_free(item);
6010}
6011
6012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 * Get the number of items in a list.
6014 */
6015 static long
6016list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 if (l == NULL)
6020 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006021 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022}
6023
6024/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 * Return TRUE when two lists have exactly the same values.
6026 */
6027 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006028list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 list_T *l1;
6030 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006031 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033{
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006036 if (l1 == NULL || l2 == NULL)
6037 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006038 if (l1 == l2)
6039 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040 if (list_len(l1) != list_len(l2))
6041 return FALSE;
6042
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006043 for (item1 = l1->lv_first, item2 = l2->lv_first;
6044 item1 != NULL && item2 != NULL;
6045 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047 return FALSE;
6048 return item1 == NULL && item2 == NULL;
6049}
6050
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006051#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6052 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006053/*
6054 * Return the dictitem that an entry in a hashtable points to.
6055 */
6056 dictitem_T *
6057dict_lookup(hi)
6058 hashitem_T *hi;
6059{
6060 return HI2DI(hi);
6061}
6062#endif
6063
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006064/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 * Return TRUE when two dictionaries have exactly the same key/values.
6066 */
6067 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006068dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 dict_T *d1;
6070 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006073{
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 hashitem_T *hi;
6075 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006077
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006078 if (d1 == NULL || d2 == NULL)
6079 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006080 if (d1 == d2)
6081 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006082 if (dict_len(d1) != dict_len(d2))
6083 return FALSE;
6084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006085 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088 if (!HASHITEM_EMPTY(hi))
6089 {
6090 item2 = dict_find(d2, hi->hi_key, -1);
6091 if (item2 == NULL)
6092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 return FALSE;
6095 --todo;
6096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 }
6098 return TRUE;
6099}
6100
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101static int tv_equal_recurse_limit;
6102
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006103/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006106 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107 */
6108 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 typval_T *tv1;
6111 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 int ic; /* ignore case */
6113 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114{
6115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006116 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006118 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006120 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006123 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 * recursiveness to a limit. We guess they are equal then.
6125 * A fixed limit has the problem of still taking an awful long time.
6126 * Reduce the limit every time running into it. That should work fine for
6127 * deeply linked structures that are not recursively linked and catch
6128 * recursiveness quickly. */
6129 if (!recursive)
6130 tv_equal_recurse_limit = 1000;
6131 if (recursive_cnt >= tv_equal_recurse_limit)
6132 {
6133 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136
6137 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006139 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 ++recursive_cnt;
6141 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6142 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006143 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006144
6145 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 ++recursive_cnt;
6147 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6148 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006149 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150
6151 case VAR_FUNC:
6152 return (tv1->vval.v_string != NULL
6153 && tv2->vval.v_string != NULL
6154 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6155
6156 case VAR_NUMBER:
6157 return tv1->vval.v_number == tv2->vval.v_number;
6158
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006159#ifdef FEAT_FLOAT
6160 case VAR_FLOAT:
6161 return tv1->vval.v_float == tv2->vval.v_float;
6162#endif
6163
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 case VAR_STRING:
6165 s1 = get_tv_string_buf(tv1, buf1);
6166 s2 = get_tv_string_buf(tv2, buf2);
6167 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169
6170 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 return TRUE;
6172}
6173
6174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 * Locate item with index "n" in list "l" and return it.
6176 * A negative index is counted from the end; -1 is the last item.
6177 * Returns NULL when "n" is out of range.
6178 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006179 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 long n;
6183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 long idx;
6186
6187 if (l == NULL)
6188 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006189
6190 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006192 n = l->lv_len + n;
6193
6194 /* Check for index out of range. */
6195 if (n < 0 || n >= l->lv_len)
6196 return NULL;
6197
6198 /* When there is a cached index may start search from there. */
6199 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201 if (n < l->lv_idx / 2)
6202 {
6203 /* closest to the start of the list */
6204 item = l->lv_first;
6205 idx = 0;
6206 }
6207 else if (n > (l->lv_idx + l->lv_len) / 2)
6208 {
6209 /* closest to the end of the list */
6210 item = l->lv_last;
6211 idx = l->lv_len - 1;
6212 }
6213 else
6214 {
6215 /* closest to the cached index */
6216 item = l->lv_idx_item;
6217 idx = l->lv_idx;
6218 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 }
6220 else
6221 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006222 if (n < l->lv_len / 2)
6223 {
6224 /* closest to the start of the list */
6225 item = l->lv_first;
6226 idx = 0;
6227 }
6228 else
6229 {
6230 /* closest to the end of the list */
6231 item = l->lv_last;
6232 idx = l->lv_len - 1;
6233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006235
6236 while (n > idx)
6237 {
6238 /* search forward */
6239 item = item->li_next;
6240 ++idx;
6241 }
6242 while (n < idx)
6243 {
6244 /* search backward */
6245 item = item->li_prev;
6246 --idx;
6247 }
6248
6249 /* cache the used index */
6250 l->lv_idx = idx;
6251 l->lv_idx_item = item;
6252
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 return item;
6254}
6255
6256/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006257 * Get list item "l[idx]" as a number.
6258 */
6259 static long
6260list_find_nr(l, idx, errorp)
6261 list_T *l;
6262 long idx;
6263 int *errorp; /* set to TRUE when something wrong */
6264{
6265 listitem_T *li;
6266
6267 li = list_find(l, idx);
6268 if (li == NULL)
6269 {
6270 if (errorp != NULL)
6271 *errorp = TRUE;
6272 return -1L;
6273 }
6274 return get_tv_number_chk(&li->li_tv, errorp);
6275}
6276
6277/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006278 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6279 */
6280 char_u *
6281list_find_str(l, idx)
6282 list_T *l;
6283 long idx;
6284{
6285 listitem_T *li;
6286
6287 li = list_find(l, idx - 1);
6288 if (li == NULL)
6289 {
6290 EMSGN(_(e_listidx), idx);
6291 return NULL;
6292 }
6293 return get_tv_string(&li->li_tv);
6294}
6295
6296/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297 * Locate "item" list "l" and return its index.
6298 * Returns -1 when "item" is not in the list.
6299 */
6300 static long
6301list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 list_T *l;
6303 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006304{
6305 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006307
6308 if (l == NULL)
6309 return -1;
6310 idx = 0;
6311 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6312 ++idx;
6313 if (li == NULL)
6314 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006315 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006316}
6317
6318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 * Append item "item" to the end of list "l".
6320 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006321 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 list_T *l;
6324 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325{
6326 if (l->lv_last == NULL)
6327 {
6328 /* empty list */
6329 l->lv_first = item;
6330 l->lv_last = item;
6331 item->li_prev = NULL;
6332 }
6333 else
6334 {
6335 l->lv_last->li_next = item;
6336 item->li_prev = l->lv_last;
6337 l->lv_last = item;
6338 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006339 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 item->li_next = NULL;
6341}
6342
6343/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006345 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006347 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 list_T *l;
6350 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 copy_tv(tv, &li->li_tv);
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Return FAIL when out of memory.
6364 */
6365 int
6366list_append_dict(list, dict)
6367 list_T *list;
6368 dict_T *dict;
6369{
6370 listitem_T *li = listitem_alloc();
6371
6372 if (li == NULL)
6373 return FAIL;
6374 li->li_tv.v_type = VAR_DICT;
6375 li->li_tv.v_lock = 0;
6376 li->li_tv.vval.v_dict = dict;
6377 list_append(list, li);
6378 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 return OK;
6380}
6381
6382/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385 * Returns FAIL when out of memory.
6386 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006387 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006388list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006389 list_T *l;
6390 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006391 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006392{
6393 listitem_T *li = listitem_alloc();
6394
6395 if (li == NULL)
6396 return FAIL;
6397 list_append(l, li);
6398 li->li_tv.v_type = VAR_STRING;
6399 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006400 if (str == NULL)
6401 li->li_tv.vval.v_string = NULL;
6402 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006403 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006404 return FAIL;
6405 return OK;
6406}
6407
6408/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409 * Append "n" to list "l".
6410 * Returns FAIL when out of memory.
6411 */
6412 static int
6413list_append_number(l, n)
6414 list_T *l;
6415 varnumber_T n;
6416{
6417 listitem_T *li;
6418
6419 li = listitem_alloc();
6420 if (li == NULL)
6421 return FAIL;
6422 li->li_tv.v_type = VAR_NUMBER;
6423 li->li_tv.v_lock = 0;
6424 li->li_tv.vval.v_number = n;
6425 list_append(l, li);
6426 return OK;
6427}
6428
6429/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 * If "item" is NULL append at the end.
6432 * Return FAIL when out of memory.
6433 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006434 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 list_T *l;
6437 typval_T *tv;
6438 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441
6442 if (ni == NULL)
6443 return FAIL;
6444 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006445 list_insert(l, ni, item);
6446 return OK;
6447}
6448
6449 void
6450list_insert(l, ni, item)
6451 list_T *l;
6452 listitem_T *ni;
6453 listitem_T *item;
6454{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 if (item == NULL)
6456 /* Append new item at end of list. */
6457 list_append(l, ni);
6458 else
6459 {
6460 /* Insert new item before existing item. */
6461 ni->li_prev = item->li_prev;
6462 ni->li_next = item;
6463 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 ++l->lv_idx;
6467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006469 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006471 l->lv_idx_item = NULL;
6472 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006474 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476}
6477
6478/*
6479 * Extend "l1" with "l2".
6480 * If "bef" is NULL append at the end, otherwise insert before this item.
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
6484list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *l1;
6486 list_T *l2;
6487 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488{
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006490 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006492 /* We also quit the loop when we have inserted the original item count of
6493 * the list, avoid a hang when we extend a list with itself. */
6494 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6496 return FAIL;
6497 return OK;
6498}
6499
6500/*
6501 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6502 * Return FAIL when out of memory.
6503 */
6504 static int
6505list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 list_T *l1;
6507 list_T *l2;
6508 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509{
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006512 if (l1 == NULL || l2 == NULL)
6513 return FAIL;
6514
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 if (l == NULL)
6518 return FAIL;
6519 tv->v_type = VAR_LIST;
6520 tv->vval.v_list = l;
6521
6522 /* append all items from the second list */
6523 return list_extend(l, l2, NULL);
6524}
6525
6526/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 * Returns NULL when out of memory.
6531 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 list_T *copy;
6539 listitem_T *item;
6540 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541
6542 if (orig == NULL)
6543 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544
6545 copy = list_alloc();
6546 if (copy != NULL)
6547 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 if (copyID != 0)
6549 {
6550 /* Do this before adding the items, because one of the items may
6551 * refer back to this list. */
6552 orig->lv_copyID = copyID;
6553 orig->lv_copylist = copy;
6554 }
6555 for (item = orig->lv_first; item != NULL && !got_int;
6556 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 {
6558 ni = listitem_alloc();
6559 if (ni == NULL)
6560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006561 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 {
6563 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6564 {
6565 vim_free(ni);
6566 break;
6567 }
6568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 list_append(copy, ni);
6572 }
6573 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (item != NULL)
6575 {
6576 list_unref(copy);
6577 copy = NULL;
6578 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 }
6580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 return copy;
6582}
6583
6584/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006586 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006587 * This used to be called list_remove, but that conflicts with a Sun header
6588 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006590 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006591vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *l;
6593 listitem_T *item;
6594 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595{
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 /* notify watchers */
6599 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006601 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 list_fix_watch(l, ip);
6603 if (ip == item2)
6604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606
6607 if (item2->li_next == NULL)
6608 l->lv_last = item->li_prev;
6609 else
6610 item2->li_next->li_prev = item->li_prev;
6611 if (item->li_prev == NULL)
6612 l->lv_first = item2->li_next;
6613 else
6614 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006615 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616}
6617
6618/*
6619 * Return an allocated string with the string representation of a list.
6620 * May return NULL.
6621 */
6622 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006625 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626{
6627 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628
6629 if (tv->vval.v_list == NULL)
6630 return NULL;
6631 ga_init2(&ga, (int)sizeof(char), 80);
6632 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006633 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 {
6635 vim_free(ga.ga_data);
6636 return NULL;
6637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 ga_append(&ga, ']');
6639 ga_append(&ga, NUL);
6640 return (char_u *)ga.ga_data;
6641}
6642
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006643typedef struct join_S {
6644 char_u *s;
6645 char_u *tofree;
6646} join_T;
6647
6648 static int
6649list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6650 garray_T *gap; /* to store the result in */
6651 list_T *l;
6652 char_u *sep;
6653 int echo_style;
6654 int copyID;
6655 garray_T *join_gap; /* to keep each list item string */
6656{
6657 int i;
6658 join_T *p;
6659 int len;
6660 int sumlen = 0;
6661 int first = TRUE;
6662 char_u *tofree;
6663 char_u numbuf[NUMBUFLEN];
6664 listitem_T *item;
6665 char_u *s;
6666
6667 /* Stringify each item in the list. */
6668 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6669 {
6670 if (echo_style)
6671 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6672 else
6673 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6674 if (s == NULL)
6675 return FAIL;
6676
6677 len = (int)STRLEN(s);
6678 sumlen += len;
6679
6680 ga_grow(join_gap, 1);
6681 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6682 if (tofree != NULL || s != numbuf)
6683 {
6684 p->s = s;
6685 p->tofree = tofree;
6686 }
6687 else
6688 {
6689 p->s = vim_strnsave(s, len);
6690 p->tofree = p->s;
6691 }
6692
6693 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006694 if (did_echo_string_emsg) /* recursion error, bail out */
6695 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 }
6697
6698 /* Allocate result buffer with its total size, avoid re-allocation and
6699 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6700 if (join_gap->ga_len >= 2)
6701 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6702 if (ga_grow(gap, sumlen + 2) == FAIL)
6703 return FAIL;
6704
6705 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6706 {
6707 if (first)
6708 first = FALSE;
6709 else
6710 ga_concat(gap, sep);
6711 p = ((join_T *)join_gap->ga_data) + i;
6712
6713 if (p->s != NULL)
6714 ga_concat(gap, p->s);
6715 line_breakcheck();
6716 }
6717
6718 return OK;
6719}
6720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006723 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006726 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006727list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006731 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006732 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 garray_T join_ga;
6735 int retval;
6736 join_T *p;
6737 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6740 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6741
6742 /* Dispose each item in join_ga. */
6743 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745 p = (join_T *)join_ga.ga_data;
6746 for (i = 0; i < join_ga.ga_len; ++i)
6747 {
6748 vim_free(p->tofree);
6749 ++p;
6750 }
6751 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006752 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753
6754 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006755}
6756
6757/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 * Garbage collection for lists and dictionaries.
6759 *
6760 * We use reference counts to be able to free most items right away when they
6761 * are no longer used. But for composite items it's possible that it becomes
6762 * unused while the reference count is > 0: When there is a recursive
6763 * reference. Example:
6764 * :let l = [1, 2, 3]
6765 * :let d = {9: l}
6766 * :let l[1] = d
6767 *
6768 * Since this is quite unusual we handle this with garbage collection: every
6769 * once in a while find out which lists and dicts are not referenced from any
6770 * variable.
6771 *
6772 * Here is a good reference text about garbage collection (refers to Python
6773 * but it applies to all reference-counting mechanisms):
6774 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006776
6777/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 * Do garbage collection for lists and dicts.
6779 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006780 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 int
6782garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006783{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006784 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785 buf_T *buf;
6786 win_T *wp;
6787 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006788 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006789 int did_free;
6790 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006791#ifdef FEAT_WINDOWS
6792 tabpage_T *tp;
6793#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006795 /* Only do this once. */
6796 want_garbage_collect = FALSE;
6797 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006798 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006799
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800 /* We advance by two because we add one for items referenced through
6801 * previous_funccal. */
6802 current_copyID += COPYID_INC;
6803 copyID = current_copyID;
6804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 /*
6806 * 1. Go through all accessible variables and mark all lists and dicts
6807 * with copyID.
6808 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006809
6810 /* Don't free variables in the previous_funccal list unless they are only
6811 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006812 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6814 {
6815 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6816 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6817 }
6818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 /* script-local variables */
6820 for (i = 1; i <= ga_scripts.ga_len; ++i)
6821 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6822
6823 /* buffer-local variables */
6824 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006825 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826
6827 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006829 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006830#ifdef FEAT_AUTOCMD
6831 if (aucmd_win != NULL)
6832 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6833#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006835#ifdef FEAT_WINDOWS
6836 /* tabpage-local variables */
6837 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006838 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006839#endif
6840
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 /* global variables */
6842 set_ref_in_ht(&globvarht, copyID);
6843
6844 /* function-local variables */
6845 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6846 {
6847 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6848 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6849 }
6850
Bram Moolenaard812df62008-11-09 12:46:09 +00006851 /* v: vars */
6852 set_ref_in_ht(&vimvarht, copyID);
6853
Bram Moolenaar1dced572012-04-05 16:54:08 +02006854#ifdef FEAT_LUA
6855 set_ref_in_lua(copyID);
6856#endif
6857
Bram Moolenaardb913952012-06-29 12:54:53 +02006858#ifdef FEAT_PYTHON
6859 set_ref_in_python(copyID);
6860#endif
6861
6862#ifdef FEAT_PYTHON3
6863 set_ref_in_python3(copyID);
6864#endif
6865
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006866 /*
6867 * 2. Free lists and dictionaries that are not referenced.
6868 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 did_free = free_unref_items(copyID);
6870
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006871 /*
6872 * 3. Check if any funccal can be freed now.
6873 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 for (pfc = &previous_funccal; *pfc != NULL; )
6875 {
6876 if (can_free_funccal(*pfc, copyID))
6877 {
6878 fc = *pfc;
6879 *pfc = fc->caller;
6880 free_funccal(fc, TRUE);
6881 did_free = TRUE;
6882 did_free_funccal = TRUE;
6883 }
6884 else
6885 pfc = &(*pfc)->caller;
6886 }
6887 if (did_free_funccal)
6888 /* When a funccal was freed some more items might be garbage
6889 * collected, so run again. */
6890 (void)garbage_collect();
6891
6892 return did_free;
6893}
6894
6895/*
6896 * Free lists and dictionaries that are no longer referenced.
6897 */
6898 static int
6899free_unref_items(copyID)
6900 int copyID;
6901{
6902 dict_T *dd;
6903 list_T *ll;
6904 int did_free = FALSE;
6905
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006907 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 */
6909 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the Dictionary and ordinary items it contains, but don't
6913 * recurse into Lists and Dictionaries, they will be in the list
6914 * of dicts or list of lists. */
6915 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
6918 /* restart, next dict may also have been freed */
6919 dd = first_dict;
6920 }
6921 else
6922 dd = dd->dv_used_next;
6923
6924 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 * Go through the list of lists and free items without the copyID.
6926 * But don't free a list that has a watcher (used in a for loop), these
6927 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 */
6929 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6931 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006933 /* Free the List and ordinary items it contains, but don't recurse
6934 * into Lists and Dictionaries, they will be in the list of dicts
6935 * or list of lists. */
6936 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 did_free = TRUE;
6938
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006939 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 ll = first_list;
6941 }
6942 else
6943 ll = ll->lv_used_next;
6944
6945 return did_free;
6946}
6947
6948/*
6949 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6950 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006951 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952set_ref_in_ht(ht, copyID)
6953 hashtab_T *ht;
6954 int copyID;
6955{
6956 int todo;
6957 hashitem_T *hi;
6958
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006959 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 for (hi = ht->ht_array; todo > 0; ++hi)
6961 if (!HASHITEM_EMPTY(hi))
6962 {
6963 --todo;
6964 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6965 }
6966}
6967
6968/*
6969 * Mark all lists and dicts referenced through list "l" with "copyID".
6970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006971 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972set_ref_in_list(l, copyID)
6973 list_T *l;
6974 int copyID;
6975{
6976 listitem_T *li;
6977
6978 for (li = l->lv_first; li != NULL; li = li->li_next)
6979 set_ref_in_item(&li->li_tv, copyID);
6980}
6981
6982/*
6983 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6984 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006985 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986set_ref_in_item(tv, copyID)
6987 typval_T *tv;
6988 int copyID;
6989{
6990 dict_T *dd;
6991 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006992
6993 switch (tv->v_type)
6994 {
6995 case VAR_DICT:
6996 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006997 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006998 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 /* Didn't see this dict yet. */
7000 dd->dv_copyID = copyID;
7001 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007002 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007004
7005 case VAR_LIST:
7006 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007007 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /* Didn't see this list yet. */
7010 ll->lv_copyID = copyID;
7011 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007014 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007016}
7017
7018/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019 * Allocate an empty header for a dictionary.
7020 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007021 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022dict_alloc()
7023{
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007025
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007029 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 if (first_dict != NULL)
7031 first_dict->dv_used_prev = d;
7032 d->dv_used_next = first_dict;
7033 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007034 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007037 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007038 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007039 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007041 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043}
7044
7045/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007046 * Allocate an empty dict for a return value.
7047 * Returns OK or FAIL.
7048 */
7049 static int
7050rettv_dict_alloc(rettv)
7051 typval_T *rettv;
7052{
7053 dict_T *d = dict_alloc();
7054
7055 if (d == NULL)
7056 return FAIL;
7057
7058 rettv->vval.v_dict = d;
7059 rettv->v_type = VAR_DICT;
7060 ++d->dv_refcount;
7061 return OK;
7062}
7063
7064
7065/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 * Unreference a Dictionary: decrement the reference count and free it when it
7067 * becomes zero.
7068 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007069 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073 if (d != NULL && --d->dv_refcount <= 0)
7074 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075}
7076
7077/*
7078 * Free a Dictionary, including all items it contains.
7079 * Ignores the reference count.
7080 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007081 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007082dict_free(d, recurse)
7083 dict_T *d;
7084 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007087 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007088 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 /* Remove the dict from the list of dicts for garbage collection. */
7091 if (d->dv_used_prev == NULL)
7092 first_dict = d->dv_used_next;
7093 else
7094 d->dv_used_prev->dv_used_next = d->dv_used_next;
7095 if (d->dv_used_next != NULL)
7096 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7097
7098 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007099 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007100 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 if (!HASHITEM_EMPTY(hi))
7104 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007105 /* Remove the item before deleting it, just in case there is
7106 * something recursive causing trouble. */
7107 di = HI2DI(hi);
7108 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007109 if (recurse || (di->di_tv.v_type != VAR_LIST
7110 && di->di_tv.v_type != VAR_DICT))
7111 clear_tv(&di->di_tv);
7112 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 --todo;
7114 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 vim_free(d);
7118}
7119
7120/*
7121 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 * The "key" is copied to the new item.
7123 * Note that the value of the item "di_tv" still needs to be initialized!
7124 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007126 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127dictitem_alloc(key)
7128 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129{
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007132 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007136 di->di_flags = 0;
7137 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139}
7140
7141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 * Make a copy of a Dictionary item.
7143 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007150 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7151 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 if (di != NULL)
7153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007155 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156 copy_tv(&org->di_tv, &di->di_tv);
7157 }
7158 return di;
7159}
7160
7161/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 * Remove item "item" from Dictionary "dict" and free it.
7163 */
7164 static void
7165dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 dict_T *dict;
7167 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168{
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 if (HASHITEM_EMPTY(hi))
7173 EMSG2(_(e_intern2), "dictitem_remove()");
7174 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176 dictitem_free(item);
7177}
7178
7179/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 * Free a dict item. Also clears the value.
7181 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007182 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 clear_tv(&item->di_tv);
7187 vim_free(item);
7188}
7189
7190/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7192 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 * Returns NULL when out of memory.
7195 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201{
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 dict_T *copy;
7203 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206
7207 if (orig == NULL)
7208 return NULL;
7209
7210 copy = dict_alloc();
7211 if (copy != NULL)
7212 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 if (copyID != 0)
7214 {
7215 orig->dv_copyID = copyID;
7216 orig->dv_copydict = copy;
7217 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 --todo;
7224
7225 di = dictitem_alloc(hi->hi_key);
7226 if (di == NULL)
7227 break;
7228 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007229 {
7230 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7231 copyID) == FAIL)
7232 {
7233 vim_free(di);
7234 break;
7235 }
7236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 else
7238 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7239 if (dict_add(copy, di) == FAIL)
7240 {
7241 dictitem_free(di);
7242 break;
7243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007245 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 if (todo > 0)
7249 {
7250 dict_unref(copy);
7251 copy = NULL;
7252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 }
7254
7255 return copy;
7256}
7257
7258/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007260 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007262 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 dict_T *d;
7265 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266{
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268}
7269
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007271 * Add a number or string entry to dictionary "d".
7272 * When "str" is NULL use number "nr", otherwise use "str".
7273 * Returns FAIL when out of memory and when key already exists.
7274 */
7275 int
7276dict_add_nr_str(d, key, nr, str)
7277 dict_T *d;
7278 char *key;
7279 long nr;
7280 char_u *str;
7281{
7282 dictitem_T *item;
7283
7284 item = dictitem_alloc((char_u *)key);
7285 if (item == NULL)
7286 return FAIL;
7287 item->di_tv.v_lock = 0;
7288 if (str == NULL)
7289 {
7290 item->di_tv.v_type = VAR_NUMBER;
7291 item->di_tv.vval.v_number = nr;
7292 }
7293 else
7294 {
7295 item->di_tv.v_type = VAR_STRING;
7296 item->di_tv.vval.v_string = vim_strsave(str);
7297 }
7298 if (dict_add(d, item) == FAIL)
7299 {
7300 dictitem_free(item);
7301 return FAIL;
7302 }
7303 return OK;
7304}
7305
7306/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007307 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007308 * Returns FAIL when out of memory and when key already exists.
7309 */
7310 int
7311dict_add_list(d, key, list)
7312 dict_T *d;
7313 char *key;
7314 list_T *list;
7315{
7316 dictitem_T *item;
7317
7318 item = dictitem_alloc((char_u *)key);
7319 if (item == NULL)
7320 return FAIL;
7321 item->di_tv.v_lock = 0;
7322 item->di_tv.v_type = VAR_LIST;
7323 item->di_tv.vval.v_list = list;
7324 if (dict_add(d, item) == FAIL)
7325 {
7326 dictitem_free(item);
7327 return FAIL;
7328 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007329 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007330 return OK;
7331}
7332
7333/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 * Get the number of items in a Dictionary.
7335 */
7336 static long
7337dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 if (d == NULL)
7341 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007342 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343}
7344
7345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 * Find item "key[len]" in Dictionary "d".
7347 * If "len" is negative use strlen(key).
7348 * Returns NULL when not found.
7349 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007350 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 char_u *key;
7354 int len;
7355{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356#define AKEYLEN 200
7357 char_u buf[AKEYLEN];
7358 char_u *akey;
7359 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (len < 0)
7363 akey = key;
7364 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 tofree = akey = vim_strnsave(key, len);
7367 if (akey == NULL)
7368 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007369 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 else
7371 {
7372 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007373 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 akey = buf;
7375 }
7376
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 vim_free(tofree);
7379 if (HASHITEM_EMPTY(hi))
7380 return NULL;
7381 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382}
7383
7384/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 * Get a string item from a dictionary.
7386 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007387 * Returns NULL if the entry doesn't exist or out of memory.
7388 */
7389 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007390get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007391 dict_T *d;
7392 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007393 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007394{
7395 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007396 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007397
7398 di = dict_find(d, key, -1);
7399 if (di == NULL)
7400 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007401 s = get_tv_string(&di->di_tv);
7402 if (save && s != NULL)
7403 s = vim_strsave(s);
7404 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007405}
7406
7407/*
7408 * Get a number item from a dictionary.
7409 * Returns 0 if the entry doesn't exist or out of memory.
7410 */
7411 long
7412get_dict_number(d, key)
7413 dict_T *d;
7414 char_u *key;
7415{
7416 dictitem_T *di;
7417
7418 di = dict_find(d, key, -1);
7419 if (di == NULL)
7420 return 0;
7421 return get_tv_number(&di->di_tv);
7422}
7423
7424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 * Return an allocated string with the string representation of a Dictionary.
7426 * May return NULL.
7427 */
7428 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432{
7433 garray_T ga;
7434 int first = TRUE;
7435 char_u *tofree;
7436 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 return NULL;
7444 ga_init2(&ga, (int)sizeof(char), 80);
7445 ga_append(&ga, '{');
7446
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007447 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007448 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 --todo;
7453
7454 if (first)
7455 first = FALSE;
7456 else
7457 ga_concat(&ga, (char_u *)", ");
7458
7459 tofree = string_quote(hi->hi_key, FALSE);
7460 if (tofree != NULL)
7461 {
7462 ga_concat(&ga, tofree);
7463 vim_free(tofree);
7464 }
7465 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007466 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 if (s != NULL)
7468 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007470 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007471 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007472 line_breakcheck();
7473
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007476 if (todo > 0)
7477 {
7478 vim_free(ga.ga_data);
7479 return NULL;
7480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 ga_append(&ga, '}');
7483 ga_append(&ga, NUL);
7484 return (char_u *)ga.ga_data;
7485}
7486
7487/*
7488 * Allocate a variable for a Dictionary and fill it from "*arg".
7489 * Return OK or FAIL. Returns NOTDONE for {expr}.
7490 */
7491 static int
7492get_dict_tv(arg, rettv, evaluate)
7493 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 int evaluate;
7496{
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 dict_T *d = NULL;
7498 typval_T tvkey;
7499 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007500 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504
7505 /*
7506 * First check if it's not a curly-braces thing: {expr}.
7507 * Must do this without evaluating, otherwise a function may be called
7508 * twice. Unfortunately this means we need to call eval1() twice for the
7509 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (*start != '}')
7513 {
7514 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7515 return FAIL;
7516 if (*start == '}')
7517 return NOTDONE;
7518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519
7520 if (evaluate)
7521 {
7522 d = dict_alloc();
7523 if (d == NULL)
7524 return FAIL;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 tvkey.v_type = VAR_UNKNOWN;
7527 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528
7529 *arg = skipwhite(*arg + 1);
7530 while (**arg != '}' && **arg != NUL)
7531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 goto failret;
7534 if (**arg != ':')
7535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007536 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 goto failret;
7539 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007540 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007542 key = get_tv_string_buf_chk(&tvkey, buf);
7543 if (key == NULL || *key == NUL)
7544 {
7545 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7546 if (key != NULL)
7547 EMSG(_(e_emptykey));
7548 clear_tv(&tvkey);
7549 goto failret;
7550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552
7553 *arg = skipwhite(*arg + 1);
7554 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7555 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007556 if (evaluate)
7557 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 goto failret;
7559 }
7560 if (evaluate)
7561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 if (item != NULL)
7564 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007565 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 clear_tv(&tv);
7568 goto failret;
7569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 item = dictitem_alloc(key);
7571 clear_tv(&tvkey);
7572 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007575 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 if (dict_add(d, item) == FAIL)
7577 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 }
7579 }
7580
7581 if (**arg == '}')
7582 break;
7583 if (**arg != ',')
7584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007585 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 goto failret;
7587 }
7588 *arg = skipwhite(*arg + 1);
7589 }
7590
7591 if (**arg != '}')
7592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007593 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594failret:
7595 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007596 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 return FAIL;
7598 }
7599
7600 *arg = skipwhite(*arg + 1);
7601 if (evaluate)
7602 {
7603 rettv->v_type = VAR_DICT;
7604 rettv->vval.v_dict = d;
7605 ++d->dv_refcount;
7606 }
7607
7608 return OK;
7609}
7610
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 * Return a string with the string representation of a variable.
7613 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007614 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007615 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007617 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 */
7619 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007624 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007625{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 static int recurse = 0;
7627 char_u *r = NULL;
7628
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007631 if (!did_echo_string_emsg)
7632 {
7633 /* Only give this message once for a recursive call to avoid
7634 * flooding the user with errors. And stop iterating over lists
7635 * and dicts. */
7636 did_echo_string_emsg = TRUE;
7637 EMSG(_("E724: variable nested too deep for displaying"));
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007640 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 }
7642 ++recurse;
7643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 switch (tv->v_type)
7645 {
7646 case VAR_FUNC:
7647 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 r = tv->vval.v_string;
7649 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652 if (tv->vval.v_list == NULL)
7653 {
7654 *tofree = NULL;
7655 r = NULL;
7656 }
7657 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7658 {
7659 *tofree = NULL;
7660 r = (char_u *)"[...]";
7661 }
7662 else
7663 {
7664 tv->vval.v_list->lv_copyID = copyID;
7665 *tofree = list2string(tv, copyID);
7666 r = *tofree;
7667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007671 if (tv->vval.v_dict == NULL)
7672 {
7673 *tofree = NULL;
7674 r = NULL;
7675 }
7676 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7677 {
7678 *tofree = NULL;
7679 r = (char_u *)"{...}";
7680 }
7681 else
7682 {
7683 tv->vval.v_dict->dv_copyID = copyID;
7684 *tofree = dict2string(tv, copyID);
7685 r = *tofree;
7686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 case VAR_STRING:
7690 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 *tofree = NULL;
7692 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007695#ifdef FEAT_FLOAT
7696 case VAR_FLOAT:
7697 *tofree = NULL;
7698 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7699 r = numbuf;
7700 break;
7701#endif
7702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007707
Bram Moolenaar8502c702014-06-17 12:51:16 +02007708 if (--recurse == 0)
7709 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711}
7712
7713/*
7714 * Return a string with the string representation of a variable.
7715 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7716 * "numbuf" is used for a number.
7717 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007718 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 */
7720 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007721tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 char_u **tofree;
7724 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726{
7727 switch (tv->v_type)
7728 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 case VAR_FUNC:
7730 *tofree = string_quote(tv->vval.v_string, TRUE);
7731 return *tofree;
7732 case VAR_STRING:
7733 *tofree = string_quote(tv->vval.v_string, FALSE);
7734 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007735#ifdef FEAT_FLOAT
7736 case VAR_FLOAT:
7737 *tofree = NULL;
7738 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7739 return numbuf;
7740#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007745 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007748 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749}
7750
7751/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 * Return string "str" in ' quotes, doubling ' characters.
7753 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755 */
7756 static char_u *
7757string_quote(str, function)
7758 char_u *str;
7759 int function;
7760{
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007762 char_u *p, *r, *s;
7763
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 len = (function ? 13 : 3);
7765 if (str != NULL)
7766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007767 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 for (p = str; *p != NUL; mb_ptr_adv(p))
7769 if (*p == '\'')
7770 ++len;
7771 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007772 s = r = alloc(len);
7773 if (r != NULL)
7774 {
7775 if (function)
7776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007778 r += 10;
7779 }
7780 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 if (str != NULL)
7783 for (p = str; *p != NUL; )
7784 {
7785 if (*p == '\'')
7786 *r++ = '\'';
7787 MB_COPY_CHAR(p, r);
7788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007789 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790 if (function)
7791 *r++ = ')';
7792 *r++ = NUL;
7793 }
7794 return s;
7795}
7796
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007797#ifdef FEAT_FLOAT
7798/*
7799 * Convert the string "text" to a floating point number.
7800 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7801 * this always uses a decimal point.
7802 * Returns the length of the text that was consumed.
7803 */
7804 static int
7805string2float(text, value)
7806 char_u *text;
7807 float_T *value; /* result stored here */
7808{
7809 char *s = (char *)text;
7810 float_T f;
7811
7812 f = strtod(s, &s);
7813 *value = f;
7814 return (int)((char_u *)s - text);
7815}
7816#endif
7817
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 * Get the value of an environment variable.
7820 * "arg" is pointing to the '$'. It is advanced to after the name.
7821 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007822 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 */
7824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 int evaluate;
7829{
7830 char_u *string = NULL;
7831 int len;
7832 int cc;
7833 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007834 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 ++*arg;
7837 name = *arg;
7838 len = get_env_len(arg);
7839 if (evaluate)
7840 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007841 if (len == 0)
7842 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007843
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007844 cc = name[len];
7845 name[len] = NUL;
7846 /* first try vim_getenv(), fast for normal environment vars */
7847 string = vim_getenv(name, &mustfree);
7848 if (string != NULL && *string != NUL)
7849 {
7850 if (!mustfree)
7851 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007853 else
7854 {
7855 if (mustfree)
7856 vim_free(string);
7857
7858 /* next try expanding things like $VIM and ${HOME} */
7859 string = expand_env_save(name - 1);
7860 if (string != NULL && *string == '$')
7861 {
7862 vim_free(string);
7863 string = NULL;
7864 }
7865 }
7866 name[len] = cc;
7867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 rettv->v_type = VAR_STRING;
7869 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871
7872 return OK;
7873}
7874
7875/*
7876 * Array with names and number of arguments of all internal functions
7877 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7878 */
7879static struct fst
7880{
7881 char *f_name; /* function name */
7882 char f_min_argc; /* minimal number of arguments */
7883 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007885 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886} functions[] =
7887{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#ifdef FEAT_FLOAT
7889 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007891#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007892 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007893 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"append", 2, 2, f_append},
7895 {"argc", 0, 0, f_argc},
7896 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007897 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007898 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007900 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007902 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007905 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"bufexists", 1, 1, f_bufexists},
7907 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7908 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7909 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7910 {"buflisted", 1, 1, f_buflisted},
7911 {"bufloaded", 1, 1, f_bufloaded},
7912 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007913 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"bufwinnr", 1, 1, f_bufwinnr},
7915 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007916 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007917 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#ifdef FEAT_FLOAT
7920 {"ceil", 1, 1, f_ceil},
7921#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007922 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007923 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007925 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007927#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007928 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007929 {"complete_add", 1, 1, f_complete_add},
7930 {"complete_check", 0, 0, f_complete_check},
7931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007934#ifdef FEAT_FLOAT
7935 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007936 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007937#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007940 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007941 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"delete", 1, 1, f_delete},
7943 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007944 {"diff_filler", 1, 1, f_diff_filler},
7945 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007946 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007948 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"eventhandler", 0, 0, f_eventhandler},
7950 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007951 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007953#ifdef FEAT_FLOAT
7954 {"exp", 1, 1, f_exp},
7955#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007956 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007957 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007958 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7960 {"filereadable", 1, 1, f_filereadable},
7961 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007963 {"finddir", 1, 3, f_finddir},
7964 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965#ifdef FEAT_FLOAT
7966 {"float2nr", 1, 1, f_float2nr},
7967 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007968 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007970 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"fnamemodify", 2, 2, f_fnamemodify},
7972 {"foldclosed", 1, 1, f_foldclosed},
7973 {"foldclosedend", 1, 1, f_foldclosedend},
7974 {"foldlevel", 1, 1, f_foldlevel},
7975 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007976 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007979 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007980 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007981 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007982 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"getchar", 0, 1, f_getchar},
7984 {"getcharmod", 0, 0, f_getcharmod},
7985 {"getcmdline", 0, 0, f_getcmdline},
7986 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007987 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02007988 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02007989 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007991 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007992 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"getfsize", 1, 1, f_getfsize},
7994 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007995 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007996 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007997 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007998 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007999 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008000 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008001 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008002 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008004 {"gettabvar", 2, 3, f_gettabvar},
8005 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"getwinposx", 0, 0, f_getwinposx},
8007 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008008 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008009 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008010 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008013 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008014 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8016 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8017 {"histadd", 2, 2, f_histadd},
8018 {"histdel", 1, 2, f_histdel},
8019 {"histget", 1, 2, f_histget},
8020 {"histnr", 1, 1, f_histnr},
8021 {"hlID", 1, 1, f_hlID},
8022 {"hlexists", 1, 1, f_hlexists},
8023 {"hostname", 0, 0, f_hostname},
8024 {"iconv", 3, 3, f_iconv},
8025 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008027 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008029 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"inputrestore", 0, 0, f_inputrestore},
8031 {"inputsave", 0, 0, f_inputsave},
8032 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008034 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008036 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008039 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"libcall", 3, 3, f_libcall},
8043 {"libcallnr", 3, 3, f_libcallnr},
8044 {"line", 1, 1, f_line},
8045 {"line2byte", 1, 1, f_line2byte},
8046 {"lispindent", 1, 1, f_lispindent},
8047 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008049 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050 {"log10", 1, 1, f_log10},
8051#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008052#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008053 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008054#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008055 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008056 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008057 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008058 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008059 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008060 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008061 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008062 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008063 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008064 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008065 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008066 {"max", 1, 1, f_max},
8067 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008068#ifdef vim_mkdir
8069 {"mkdir", 1, 3, f_mkdir},
8070#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008072#ifdef FEAT_MZSCHEME
8073 {"mzeval", 1, 1, f_mzeval},
8074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008076 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008077 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008078 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"pow", 2, 2, f_pow},
8081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008083 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008084 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008085#ifdef FEAT_PYTHON3
8086 {"py3eval", 1, 1, f_py3eval},
8087#endif
8088#ifdef FEAT_PYTHON
8089 {"pyeval", 1, 1, f_pyeval},
8090#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008091 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008092 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008093 {"reltime", 0, 2, f_reltime},
8094 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"remote_expr", 2, 3, f_remote_expr},
8096 {"remote_foreground", 1, 1, f_remote_foreground},
8097 {"remote_peek", 1, 2, f_remote_peek},
8098 {"remote_read", 1, 1, f_remote_read},
8099 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008100 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008102 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008104 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"round", 1, 1, f_round},
8107#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008108 {"screenattr", 2, 2, f_screenattr},
8109 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008110 {"screencol", 0, 0, f_screencol},
8111 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008112 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008113 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008114 {"searchpair", 3, 7, f_searchpair},
8115 {"searchpairpos", 3, 7, f_searchpairpos},
8116 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"server2client", 2, 2, f_server2client},
8118 {"serverlist", 0, 0, f_serverlist},
8119 {"setbufvar", 3, 3, f_setbufvar},
8120 {"setcmdpos", 1, 1, f_setcmdpos},
8121 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008122 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008123 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008124 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008125 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008127 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008128 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008130#ifdef FEAT_CRYPT
8131 {"sha256", 1, 1, f_sha256},
8132#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008133 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008134 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#ifdef FEAT_FLOAT
8137 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008140 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008141 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008142 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008143 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008144 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#ifdef FEAT_FLOAT
8146 {"sqrt", 1, 1, f_sqrt},
8147 {"str2float", 1, 1, f_str2float},
8148#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008149 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008150 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008151 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#ifdef HAVE_STRFTIME
8153 {"strftime", 1, 2, f_strftime},
8154#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"strlen", 1, 1, f_strlen},
8158 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008159 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008161 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008162 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"substitute", 4, 4, f_substitute},
8164 {"synID", 3, 3, f_synID},
8165 {"synIDattr", 2, 3, f_synIDattr},
8166 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008167 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008168 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008169 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008170 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008171 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008172 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008173 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008174 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008175 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008176#ifdef FEAT_FLOAT
8177 {"tan", 1, 1, f_tan},
8178 {"tanh", 1, 1, f_tanh},
8179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008181 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"tolower", 1, 1, f_tolower},
8183 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008184 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"trunc", 1, 1, f_trunc},
8187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008189 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008190 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008191 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"virtcol", 1, 1, f_virtcol},
8194 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008195 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"winbufnr", 1, 1, f_winbufnr},
8197 {"wincol", 0, 0, f_wincol},
8198 {"winheight", 1, 1, f_winheight},
8199 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008200 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008202 {"winrestview", 1, 1, f_winrestview},
8203 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008205 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008206 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207};
8208
8209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8210
8211/*
8212 * Function given to ExpandGeneric() to obtain the list of internal
8213 * or user defined function names.
8214 */
8215 char_u *
8216get_function_name(xp, idx)
8217 expand_T *xp;
8218 int idx;
8219{
8220 static int intidx = -1;
8221 char_u *name;
8222
8223 if (idx == 0)
8224 intidx = -1;
8225 if (intidx < 0)
8226 {
8227 name = get_user_func_name(xp, idx);
8228 if (name != NULL)
8229 return name;
8230 }
8231 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8232 {
8233 STRCPY(IObuff, functions[intidx].f_name);
8234 STRCAT(IObuff, "(");
8235 if (functions[intidx].f_max_argc == 0)
8236 STRCAT(IObuff, ")");
8237 return IObuff;
8238 }
8239
8240 return NULL;
8241}
8242
8243/*
8244 * Function given to ExpandGeneric() to obtain the list of internal or
8245 * user defined variable or function names.
8246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 char_u *
8248get_expr_name(xp, idx)
8249 expand_T *xp;
8250 int idx;
8251{
8252 static int intidx = -1;
8253 char_u *name;
8254
8255 if (idx == 0)
8256 intidx = -1;
8257 if (intidx < 0)
8258 {
8259 name = get_function_name(xp, idx);
8260 if (name != NULL)
8261 return name;
8262 }
8263 return get_user_var_name(xp, ++intidx);
8264}
8265
8266#endif /* FEAT_CMDL_COMPL */
8267
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008268#if defined(EBCDIC) || defined(PROTO)
8269/*
8270 * Compare struct fst by function name.
8271 */
8272 static int
8273compare_func_name(s1, s2)
8274 const void *s1;
8275 const void *s2;
8276{
8277 struct fst *p1 = (struct fst *)s1;
8278 struct fst *p2 = (struct fst *)s2;
8279
8280 return STRCMP(p1->f_name, p2->f_name);
8281}
8282
8283/*
8284 * Sort the function table by function name.
8285 * The sorting of the table above is ASCII dependant.
8286 * On machines using EBCDIC we have to sort it.
8287 */
8288 static void
8289sortFunctions()
8290{
8291 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8292
8293 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8294}
8295#endif
8296
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298/*
8299 * Find internal function in table above.
8300 * Return index, or -1 if not found
8301 */
8302 static int
8303find_internal_func(name)
8304 char_u *name; /* name of the function */
8305{
8306 int first = 0;
8307 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8308 int cmp;
8309 int x;
8310
8311 /*
8312 * Find the function name in the table. Binary search.
8313 */
8314 while (first <= last)
8315 {
8316 x = first + ((unsigned)(last - first) >> 1);
8317 cmp = STRCMP(name, functions[x].f_name);
8318 if (cmp < 0)
8319 last = x - 1;
8320 else if (cmp > 0)
8321 first = x + 1;
8322 else
8323 return x;
8324 }
8325 return -1;
8326}
8327
8328/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008329 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8330 * name it contains, otherwise return "name".
8331 */
8332 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008333deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 char_u *name;
8335 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008336 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337{
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 int cc;
8340
8341 cc = name[*lenp];
8342 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008343 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348 {
8349 *lenp = 0;
8350 return (char_u *)""; /* just in case */
8351 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008352 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 }
8355
8356 return name;
8357}
8358
8359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 * Allocate a variable for the result of a function.
8361 * Return OK or FAIL.
8362 */
8363 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008364get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8365 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 char_u *name; /* name of the function */
8367 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 char_u **arg; /* argument, pointing to the '(' */
8370 linenr_T firstline; /* first line of range */
8371 linenr_T lastline; /* last line of range */
8372 int *doesrange; /* return: function handled range */
8373 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 char_u *argp;
8377 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008378 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 int argcount = 0; /* number of arguments found */
8380
8381 /*
8382 * Get the arguments.
8383 */
8384 argp = *arg;
8385 while (argcount < MAX_FUNC_ARGS)
8386 {
8387 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8388 if (*argp == ')' || *argp == ',' || *argp == NUL)
8389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8391 {
8392 ret = FAIL;
8393 break;
8394 }
8395 ++argcount;
8396 if (*argp != ',')
8397 break;
8398 }
8399 if (*argp == ')')
8400 ++argp;
8401 else
8402 ret = FAIL;
8403
8404 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008408 {
8409 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008410 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008412 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008416 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
8418 *arg = skipwhite(argp);
8419 return ret;
8420}
8421
8422
8423/*
8424 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008425 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008426 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 */
8428 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008429call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008431 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008435 typval_T *argvars; /* vars for arguments, must have "argcount"
8436 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 linenr_T firstline; /* first line of range */
8438 linenr_T lastline; /* last line of range */
8439 int *doesrange; /* return: function handled range */
8440 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444#define ERROR_UNKNOWN 0
8445#define ERROR_TOOMANY 1
8446#define ERROR_TOOFEW 2
8447#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448#define ERROR_DICT 4
8449#define ERROR_NONE 5
8450#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 int error = ERROR_NONE;
8452 int i;
8453 int llen;
8454 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455#define FLEN_FIXED 40
8456 char_u fname_buf[FLEN_FIXED + 1];
8457 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008458 char_u *name;
8459
8460 /* Make a copy of the name, if it comes from a funcref variable it could
8461 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008462 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008463 if (name == NULL)
8464 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
8466 /*
8467 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8468 * Change <SNR>123_name() to K_SNR 123_name().
8469 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 llen = eval_fname_script(name);
8472 if (llen > 0)
8473 {
8474 fname_buf[0] = K_SPECIAL;
8475 fname_buf[1] = KS_EXTRA;
8476 fname_buf[2] = (int)KE_SNR;
8477 i = 3;
8478 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8479 {
8480 if (current_SID <= 0)
8481 error = ERROR_SCRIPT;
8482 else
8483 {
8484 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8485 i = (int)STRLEN(fname_buf);
8486 }
8487 }
8488 if (i + STRLEN(name + llen) < FLEN_FIXED)
8489 {
8490 STRCPY(fname_buf + i, name + llen);
8491 fname = fname_buf;
8492 }
8493 else
8494 {
8495 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8496 if (fname == NULL)
8497 error = ERROR_OTHER;
8498 else
8499 {
8500 mch_memmove(fname, fname_buf, (size_t)i);
8501 STRCPY(fname + i, name + llen);
8502 }
8503 }
8504 }
8505 else
8506 fname = name;
8507
8508 *doesrange = FALSE;
8509
8510
8511 /* execute the function if no errors detected and executing */
8512 if (evaluate && error == ERROR_NONE)
8513 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008514 char_u *rfname = fname;
8515
8516 /* Ignore "g:" before a function name. */
8517 if (fname[0] == 'g' && fname[1] == ':')
8518 rfname = fname + 2;
8519
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008520 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8521 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_UNKNOWN;
8523
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008524 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
8526 /*
8527 * User defined function.
8528 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008529 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008532 /* Trigger FuncUndefined event, may load the function. */
8533 if (fp == NULL
8534 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008535 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008536 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008538 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008539 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
8541#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008542 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008543 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008544 {
8545 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008546 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008547 }
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 if (fp != NULL)
8550 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008551 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008553 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008555 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008557 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else
8560 {
8561 /*
8562 * Call the user function.
8563 * Save and restore search patterns, script variables and
8564 * redo buffer.
8565 */
8566 save_search_patterns();
8567 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008568 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008571 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8572 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8573 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008574 /* Function was unreferenced while being used, free it
8575 * now. */
8576 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 restoreRedobuff();
8578 restore_search_patterns();
8579 error = ERROR_NONE;
8580 }
8581 }
8582 }
8583 else
8584 {
8585 /*
8586 * Find the function name in the table, call its implementation.
8587 */
8588 i = find_internal_func(fname);
8589 if (i >= 0)
8590 {
8591 if (argcount < functions[i].f_min_argc)
8592 error = ERROR_TOOFEW;
8593 else if (argcount > functions[i].f_max_argc)
8594 error = ERROR_TOOMANY;
8595 else
8596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 error = ERROR_NONE;
8600 }
8601 }
8602 }
8603 /*
8604 * The function call (or "FuncUndefined" autocommand sequence) might
8605 * have been aborted by an error, an interrupt, or an explicitly thrown
8606 * exception that has not been caught so far. This situation can be
8607 * tested for by calling aborting(). For an error in an internal
8608 * function or for the "E132" error in call_user_func(), however, the
8609 * throw point at which the "force_abort" flag (temporarily reset by
8610 * emsg()) is normally updated has not been reached yet. We need to
8611 * update that flag first to make aborting() reliable.
8612 */
8613 update_force_abort();
8614 }
8615 if (error == ERROR_NONE)
8616 ret = OK;
8617
8618 /*
8619 * Report an error unless the argument evaluation or function call has been
8620 * cancelled due to an aborting error, an interrupt, or an exception.
8621 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008622 if (!aborting())
8623 {
8624 switch (error)
8625 {
8626 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008627 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008628 break;
8629 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008630 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008631 break;
8632 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008633 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008634 name);
8635 break;
8636 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008637 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008638 name);
8639 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008641 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 name);
8643 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008644 }
8645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (fname != name && fname != fname_buf)
8648 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008649 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 return ret;
8652}
8653
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008654/*
8655 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008656 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 */
8658 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008659emsg_funcname(ermsg, name)
8660 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661 char_u *name;
8662{
8663 char_u *p;
8664
8665 if (*name == K_SPECIAL)
8666 p = concat_str((char_u *)"<SNR>", name + 3);
8667 else
8668 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008669 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008670 if (p != name)
8671 vim_free(p);
8672}
8673
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008674/*
8675 * Return TRUE for a non-zero Number and a non-empty String.
8676 */
8677 static int
8678non_zero_arg(argvars)
8679 typval_T *argvars;
8680{
8681 return ((argvars[0].v_type == VAR_NUMBER
8682 && argvars[0].vval.v_number != 0)
8683 || (argvars[0].v_type == VAR_STRING
8684 && argvars[0].vval.v_string != NULL
8685 && *argvars[0].vval.v_string != NUL));
8686}
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688/*********************************************
8689 * Implementation of the built-in functions
8690 */
8691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008693static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8694
8695/*
8696 * Get the float value of "argvars[0]" into "f".
8697 * Returns FAIL when the argument is not a Number or Float.
8698 */
8699 static int
8700get_float_arg(argvars, f)
8701 typval_T *argvars;
8702 float_T *f;
8703{
8704 if (argvars[0].v_type == VAR_FLOAT)
8705 {
8706 *f = argvars[0].vval.v_float;
8707 return OK;
8708 }
8709 if (argvars[0].v_type == VAR_NUMBER)
8710 {
8711 *f = (float_T)argvars[0].vval.v_number;
8712 return OK;
8713 }
8714 EMSG(_("E808: Number or Float required"));
8715 return FAIL;
8716}
8717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008718/*
8719 * "abs(expr)" function
8720 */
8721 static void
8722f_abs(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 if (argvars[0].v_type == VAR_FLOAT)
8727 {
8728 rettv->v_type = VAR_FLOAT;
8729 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8730 }
8731 else
8732 {
8733 varnumber_T n;
8734 int error = FALSE;
8735
8736 n = get_tv_number_chk(&argvars[0], &error);
8737 if (error)
8738 rettv->vval.v_number = -1;
8739 else if (n > 0)
8740 rettv->vval.v_number = n;
8741 else
8742 rettv->vval.v_number = -n;
8743 }
8744}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008745
8746/*
8747 * "acos()" function
8748 */
8749 static void
8750f_acos(argvars, rettv)
8751 typval_T *argvars;
8752 typval_T *rettv;
8753{
8754 float_T f;
8755
8756 rettv->v_type = VAR_FLOAT;
8757 if (get_float_arg(argvars, &f) == OK)
8758 rettv->vval.v_float = acos(f);
8759 else
8760 rettv->vval.v_float = 0.0;
8761}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008762#endif
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 */
8767 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008768f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *argvars;
8770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008775 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008777 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008778 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008779 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008781 }
8782 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008783 EMSG(_(e_listreq));
8784}
8785
8786/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008787 * "and(expr, expr)" function
8788 */
8789 static void
8790f_and(argvars, rettv)
8791 typval_T *argvars;
8792 typval_T *rettv;
8793{
8794 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8795 & get_tv_number_chk(&argvars[1], NULL);
8796}
8797
8798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008799 * "append(lnum, string/list)" function
8800 */
8801 static void
8802f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805{
8806 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 list_T *l = NULL;
8809 listitem_T *li = NULL;
8810 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008811 long added = 0;
8812
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008813 /* When coming here from Insert mode, sync undo, so that this can be
8814 * undone separately from what was previously inserted. */
8815 if (u_sync_once == 2)
8816 {
8817 u_sync_once = 1; /* notify that u_sync() was called */
8818 u_sync(TRUE);
8819 }
8820
Bram Moolenaar0d660222005-01-07 21:51:51 +00008821 lnum = get_tv_lnum(argvars);
8822 if (lnum >= 0
8823 && lnum <= curbuf->b_ml.ml_line_count
8824 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008826 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008828 l = argvars[1].vval.v_list;
8829 if (l == NULL)
8830 return;
8831 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008833 for (;;)
8834 {
8835 if (l == NULL)
8836 tv = &argvars[1]; /* append a string */
8837 else if (li == NULL)
8838 break; /* end of list */
8839 else
8840 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 line = get_tv_string_chk(tv);
8842 if (line == NULL) /* type error */
8843 {
8844 rettv->vval.v_number = 1; /* Failed */
8845 break;
8846 }
8847 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008848 ++added;
8849 if (l == NULL)
8850 break;
8851 li = li->li_next;
8852 }
8853
8854 appended_lines_mark(lnum, added);
8855 if (curwin->w_cursor.lnum > lnum)
8856 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 else
8859 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860}
8861
8862/*
8863 * "argc()" function
8864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871}
8872
8873/*
8874 * "argidx()" function
8875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008878 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
8884/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008885 * "arglistid()" function
8886 */
8887 static void
8888f_arglistid(argvars, rettv)
8889 typval_T *argvars UNUSED;
8890 typval_T *rettv;
8891{
8892 win_T *wp;
8893 tabpage_T *tp = NULL;
8894 long n;
8895
8896 rettv->vval.v_number = -1;
8897 if (argvars[0].v_type != VAR_UNKNOWN)
8898 {
8899 if (argvars[1].v_type != VAR_UNKNOWN)
8900 {
8901 n = get_tv_number(&argvars[1]);
8902 if (n >= 0)
8903 tp = find_tabpage(n);
8904 }
8905 else
8906 tp = curtab;
8907
8908 if (tp != NULL)
8909 {
8910 wp = find_win_by_nr(&argvars[0], tp);
8911 if (wp != NULL)
8912 rettv->vval.v_number = wp->w_alist->id;
8913 }
8914 }
8915 else
8916 rettv->vval.v_number = curwin->w_alist->id;
8917}
8918
8919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * "argv(nr)" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 int idx;
8928
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008929 if (argvars[0].v_type != VAR_UNKNOWN)
8930 {
8931 idx = get_tv_number_chk(&argvars[0], NULL);
8932 if (idx >= 0 && idx < ARGCOUNT)
8933 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8934 else
8935 rettv->vval.v_string = NULL;
8936 rettv->v_type = VAR_STRING;
8937 }
8938 else if (rettv_list_alloc(rettv) == OK)
8939 for (idx = 0; idx < ARGCOUNT; ++idx)
8940 list_append_string(rettv->vval.v_list,
8941 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008945/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008946 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008947 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008948 static void
8949f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008951 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008952{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008953 float_T f;
8954
8955 rettv->v_type = VAR_FLOAT;
8956 if (get_float_arg(argvars, &f) == OK)
8957 rettv->vval.v_float = asin(f);
8958 else
8959 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008960}
8961
8962/*
8963 * "atan()" function
8964 */
8965 static void
8966f_atan(argvars, rettv)
8967 typval_T *argvars;
8968 typval_T *rettv;
8969{
8970 float_T f;
8971
8972 rettv->v_type = VAR_FLOAT;
8973 if (get_float_arg(argvars, &f) == OK)
8974 rettv->vval.v_float = atan(f);
8975 else
8976 rettv->vval.v_float = 0.0;
8977}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008978
8979/*
8980 * "atan2()" function
8981 */
8982 static void
8983f_atan2(argvars, rettv)
8984 typval_T *argvars;
8985 typval_T *rettv;
8986{
8987 float_T fx, fy;
8988
8989 rettv->v_type = VAR_FLOAT;
8990 if (get_float_arg(argvars, &fx) == OK
8991 && get_float_arg(&argvars[1], &fy) == OK)
8992 rettv->vval.v_float = atan2(fx, fy);
8993 else
8994 rettv->vval.v_float = 0.0;
8995}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996#endif
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998/*
8999 * "browse(save, title, initdir, default)" function
9000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009003 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006#ifdef FEAT_BROWSE
9007 int save;
9008 char_u *title;
9009 char_u *initdir;
9010 char_u *defname;
9011 char_u buf[NUMBUFLEN];
9012 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 save = get_tv_number_chk(&argvars[0], &error);
9016 title = get_tv_string_chk(&argvars[1]);
9017 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9018 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 if (error || title == NULL || initdir == NULL || defname == NULL)
9021 rettv->vval.v_string = NULL;
9022 else
9023 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009024 do_browse(save ? BROWSE_SAVE : 0,
9025 title, defname, NULL, initdir, NULL, curbuf);
9026#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009030}
9031
9032/*
9033 * "browsedir(title, initdir)" function
9034 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009039{
9040#ifdef FEAT_BROWSE
9041 char_u *title;
9042 char_u *initdir;
9043 char_u buf[NUMBUFLEN];
9044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 title = get_tv_string_chk(&argvars[0]);
9046 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 if (title == NULL || initdir == NULL)
9049 rettv->vval.v_string = NULL;
9050 else
9051 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009052 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
9058
Bram Moolenaar33570922005-01-25 22:26:29 +00009059static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009060
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061/*
9062 * Find a buffer by number or exact name.
9063 */
9064 static buf_T *
9065find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 if (avar->v_type == VAR_NUMBER)
9071 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009072 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009074 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009075 if (buf == NULL)
9076 {
9077 /* No full path name match, try a match with a URL or a "nofile"
9078 * buffer, these don't use the full path. */
9079 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9080 if (buf->b_fname != NULL
9081 && (path_with_url(buf->b_fname)
9082#ifdef FEAT_QUICKFIX
9083 || bt_nofile(buf)
9084#endif
9085 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009087 break;
9088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090 return buf;
9091}
9092
9093/*
9094 * "bufexists(expr)" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "buflisted(expr)" function
9106 */
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *argvars;
9110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 buf_T *buf;
9113
9114 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "bufloaded(expr)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 buf_T *buf;
9127
9128 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009132static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134/*
9135 * Get buffer by number or pattern.
9136 */
9137 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009138get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009140 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 int save_magic;
9144 char_u *save_cpo;
9145 buf_T *buf;
9146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 if (tv->v_type == VAR_NUMBER)
9148 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009149 if (tv->v_type != VAR_STRING)
9150 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (name == NULL || *name == NUL)
9152 return curbuf;
9153 if (name[0] == '$' && name[1] == NUL)
9154 return lastbuf;
9155
9156 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9157 save_magic = p_magic;
9158 p_magic = TRUE;
9159 save_cpo = p_cpo;
9160 p_cpo = (char_u *)"";
9161
9162 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009163 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 p_magic = save_magic;
9166 p_cpo = save_cpo;
9167
9168 /* If not found, try expanding the name, like done for bufexists(). */
9169 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171
9172 return buf;
9173}
9174
9175/*
9176 * "bufname(expr)" function
9177 */
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 buf_T *buf;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009187 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 --emsg_off;
9194}
9195
9196/*
9197 * "bufnr(expr)" function
9198 */
9199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *argvars;
9202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009205 int error = FALSE;
9206 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009211 --emsg_off;
9212
9213 /* If the buffer isn't found and the second argument is not zero create a
9214 * new buffer. */
9215 if (buf == NULL
9216 && argvars[1].v_type != VAR_UNKNOWN
9217 && get_tv_number_chk(&argvars[1], &error) != 0
9218 && !error
9219 && (name = get_tv_string_chk(&argvars[0])) != NULL
9220 && !error)
9221 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
9229/*
9230 * "bufwinnr(nr)" function
9231 */
9232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009234 typval_T *argvars;
9235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237#ifdef FEAT_WINDOWS
9238 win_T *wp;
9239 int winnr = 0;
9240#endif
9241 buf_T *buf;
9242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009245 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#ifdef FEAT_WINDOWS
9247 for (wp = firstwin; wp; wp = wp->w_next)
9248 {
9249 ++winnr;
9250 if (wp->w_buffer == buf)
9251 break;
9252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256#endif
9257 --emsg_off;
9258}
9259
9260/*
9261 * "byte2line(byte)" function
9262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#else
9271 long boff = 0;
9272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 (linenr_T)0, &boff);
9279#endif
9280}
9281
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009282 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009283byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009286 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009287{
9288#ifdef FEAT_MBYTE
9289 char_u *t;
9290#endif
9291 char_u *str;
9292 long idx;
9293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 str = get_tv_string_chk(&argvars[0]);
9295 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009298 return;
9299
9300#ifdef FEAT_MBYTE
9301 t = str;
9302 for ( ; idx > 0; idx--)
9303 {
9304 if (*t == NUL) /* EOL reached */
9305 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009306 if (enc_utf8 && comp)
9307 t += utf_ptr2len(t);
9308 else
9309 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009310 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009311 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009312#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009315#endif
9316}
9317
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009318/*
9319 * "byteidx()" function
9320 */
9321 static void
9322f_byteidx(argvars, rettv)
9323 typval_T *argvars;
9324 typval_T *rettv;
9325{
9326 byteidx(argvars, rettv, FALSE);
9327}
9328
9329/*
9330 * "byteidxcomp()" function
9331 */
9332 static void
9333f_byteidxcomp(argvars, rettv)
9334 typval_T *argvars;
9335 typval_T *rettv;
9336{
9337 byteidx(argvars, rettv, TRUE);
9338}
9339
Bram Moolenaardb913952012-06-29 12:54:53 +02009340 int
9341func_call(name, args, selfdict, rettv)
9342 char_u *name;
9343 typval_T *args;
9344 dict_T *selfdict;
9345 typval_T *rettv;
9346{
9347 listitem_T *item;
9348 typval_T argv[MAX_FUNC_ARGS + 1];
9349 int argc = 0;
9350 int dummy;
9351 int r = 0;
9352
9353 for (item = args->vval.v_list->lv_first; item != NULL;
9354 item = item->li_next)
9355 {
9356 if (argc == MAX_FUNC_ARGS)
9357 {
9358 EMSG(_("E699: Too many arguments"));
9359 break;
9360 }
9361 /* Make a copy of each argument. This is needed to be able to set
9362 * v_lock to VAR_FIXED in the copy without changing the original list.
9363 */
9364 copy_tv(&item->li_tv, &argv[argc++]);
9365 }
9366
9367 if (item == NULL)
9368 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9369 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9370 &dummy, TRUE, selfdict);
9371
9372 /* Free the arguments. */
9373 while (argc > 0)
9374 clear_tv(&argv[--argc]);
9375
9376 return r;
9377}
9378
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009379/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009380 * "call(func, arglist)" function
9381 */
9382 static void
9383f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009386{
9387 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009389
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009390 if (argvars[1].v_type != VAR_LIST)
9391 {
9392 EMSG(_(e_listreq));
9393 return;
9394 }
9395 if (argvars[1].vval.v_list == NULL)
9396 return;
9397
9398 if (argvars[0].v_type == VAR_FUNC)
9399 func = argvars[0].vval.v_string;
9400 else
9401 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009402 if (*func == NUL)
9403 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009404
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 if (argvars[2].v_type != VAR_UNKNOWN)
9406 {
9407 if (argvars[2].v_type != VAR_DICT)
9408 {
9409 EMSG(_(e_dictreq));
9410 return;
9411 }
9412 selfdict = argvars[2].vval.v_dict;
9413 }
9414
Bram Moolenaardb913952012-06-29 12:54:53 +02009415 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009416}
9417
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009418#ifdef FEAT_FLOAT
9419/*
9420 * "ceil({float})" function
9421 */
9422 static void
9423f_ceil(argvars, rettv)
9424 typval_T *argvars;
9425 typval_T *rettv;
9426{
9427 float_T f;
9428
9429 rettv->v_type = VAR_FLOAT;
9430 if (get_float_arg(argvars, &f) == OK)
9431 rettv->vval.v_float = ceil(f);
9432 else
9433 rettv->vval.v_float = 0.0;
9434}
9435#endif
9436
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009437/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009438 * "changenr()" function
9439 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009440 static void
9441f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009442 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009443 typval_T *rettv;
9444{
9445 rettv->vval.v_number = curbuf->b_u_seq_cur;
9446}
9447
9448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 * "char2nr(string)" function
9450 */
9451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009453 typval_T *argvars;
9454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_MBYTE
9457 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009458 {
9459 int utf8 = 0;
9460
9461 if (argvars[1].v_type != VAR_UNKNOWN)
9462 utf8 = get_tv_number_chk(&argvars[1], NULL);
9463
9464 if (utf8)
9465 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9466 else
9467 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 else
9470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
9474/*
9475 * "cindent(lnum)" function
9476 */
9477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
9482#ifdef FEAT_CINDENT
9483 pos_T pos;
9484 linenr_T lnum;
9485
9486 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9489 {
9490 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 curwin->w_cursor = pos;
9493 }
9494 else
9495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497}
9498
9499/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009500 * "clearmatches()" function
9501 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009502 static void
9503f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009504 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009505 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009506{
9507#ifdef FEAT_SEARCH_EXTRA
9508 clear_matches(curwin);
9509#endif
9510}
9511
9512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 * "col(string)" function
9514 */
9515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 typval_T *argvars;
9518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 colnr_T col = 0;
9521 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009522 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009524 fp = var2fpos(&argvars[0], FALSE, &fnum);
9525 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
9527 if (fp->col == MAXCOL)
9528 {
9529 /* '> can be MAXCOL, get the length of the line then */
9530 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009531 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 else
9533 col = MAXCOL;
9534 }
9535 else
9536 {
9537 col = fp->col + 1;
9538#ifdef FEAT_VIRTUALEDIT
9539 /* col(".") when the cursor is on the NUL at the end of the line
9540 * because of "coladd" can be seen as an extra column. */
9541 if (virtual_active() && fp == &curwin->w_cursor)
9542 {
9543 char_u *p = ml_get_cursor();
9544
9545 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9546 curwin->w_virtcol - curwin->w_cursor.coladd))
9547 {
9548# ifdef FEAT_MBYTE
9549 int l;
9550
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009551 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 col += l;
9553# else
9554 if (*p != NUL && p[1] == NUL)
9555 ++col;
9556# endif
9557 }
9558 }
9559#endif
9560 }
9561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
Bram Moolenaar572cb562005-08-05 21:35:02 +00009565#if defined(FEAT_INS_EXPAND)
9566/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009567 * "complete()" function
9568 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009569 static void
9570f_complete(argvars, rettv)
9571 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009572 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009573{
9574 int startcol;
9575
9576 if ((State & INSERT) == 0)
9577 {
9578 EMSG(_("E785: complete() can only be used in Insert mode"));
9579 return;
9580 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009581
9582 /* Check for undo allowed here, because if something was already inserted
9583 * the line was already saved for undo and this check isn't done. */
9584 if (!undo_allowed())
9585 return;
9586
Bram Moolenaarade00832006-03-10 21:46:58 +00009587 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9588 {
9589 EMSG(_(e_invarg));
9590 return;
9591 }
9592
9593 startcol = get_tv_number_chk(&argvars[0], NULL);
9594 if (startcol <= 0)
9595 return;
9596
9597 set_completion(startcol - 1, argvars[1].vval.v_list);
9598}
9599
9600/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009601 * "complete_add()" function
9602 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009603 static void
9604f_complete_add(argvars, rettv)
9605 typval_T *argvars;
9606 typval_T *rettv;
9607{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009608 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009609}
9610
9611/*
9612 * "complete_check()" function
9613 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009614 static void
9615f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009616 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009617 typval_T *rettv;
9618{
9619 int saved = RedrawingDisabled;
9620
9621 RedrawingDisabled = 0;
9622 ins_compl_check_keys(0);
9623 rettv->vval.v_number = compl_interrupted;
9624 RedrawingDisabled = saved;
9625}
9626#endif
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628/*
9629 * "confirm(message, buttons[, default [, type]])" function
9630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009633 typval_T *argvars UNUSED;
9634 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9637 char_u *message;
9638 char_u *buttons = NULL;
9639 char_u buf[NUMBUFLEN];
9640 char_u buf2[NUMBUFLEN];
9641 int def = 1;
9642 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 char_u *typestr;
9644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 message = get_tv_string_chk(&argvars[0]);
9647 if (message == NULL)
9648 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009649 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9652 if (buttons == NULL)
9653 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009657 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9660 if (typestr == NULL)
9661 error = TRUE;
9662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 switch (TOUPPER_ASC(*typestr))
9665 {
9666 case 'E': type = VIM_ERROR; break;
9667 case 'Q': type = VIM_QUESTION; break;
9668 case 'I': type = VIM_INFO; break;
9669 case 'W': type = VIM_WARNING; break;
9670 case 'G': type = VIM_GENERIC; break;
9671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 }
9673 }
9674 }
9675 }
9676
9677 if (buttons == NULL || *buttons == NUL)
9678 buttons = (char_u *)_("&Ok");
9679
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009680 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009681 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009682 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#endif
9684}
9685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686/*
9687 * "copy()" function
9688 */
9689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009691 typval_T *argvars;
9692 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009694 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009697#ifdef FEAT_FLOAT
9698/*
9699 * "cos()" function
9700 */
9701 static void
9702f_cos(argvars, rettv)
9703 typval_T *argvars;
9704 typval_T *rettv;
9705{
9706 float_T f;
9707
9708 rettv->v_type = VAR_FLOAT;
9709 if (get_float_arg(argvars, &f) == OK)
9710 rettv->vval.v_float = cos(f);
9711 else
9712 rettv->vval.v_float = 0.0;
9713}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009714
9715/*
9716 * "cosh()" function
9717 */
9718 static void
9719f_cosh(argvars, rettv)
9720 typval_T *argvars;
9721 typval_T *rettv;
9722{
9723 float_T f;
9724
9725 rettv->v_type = VAR_FLOAT;
9726 if (get_float_arg(argvars, &f) == OK)
9727 rettv->vval.v_float = cosh(f);
9728 else
9729 rettv->vval.v_float = 0.0;
9730}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009731#endif
9732
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009734 * "count()" function
9735 */
9736 static void
9737f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009740{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009741 long n = 0;
9742 int ic = FALSE;
9743
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009746 listitem_T *li;
9747 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009749
Bram Moolenaare9a41262005-01-15 22:18:47 +00009750 if ((l = argvars[0].vval.v_list) != NULL)
9751 {
9752 li = l->lv_first;
9753 if (argvars[2].v_type != VAR_UNKNOWN)
9754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 int error = FALSE;
9756
9757 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009758 if (argvars[3].v_type != VAR_UNKNOWN)
9759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009760 idx = get_tv_number_chk(&argvars[3], &error);
9761 if (!error)
9762 {
9763 li = list_find(l, idx);
9764 if (li == NULL)
9765 EMSGN(_(e_listidx), idx);
9766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 if (error)
9769 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 }
9771
9772 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009773 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009774 ++n;
9775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 else if (argvars[0].v_type == VAR_DICT)
9778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 int todo;
9780 dict_T *d;
9781 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009782
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009783 if ((d = argvars[0].vval.v_dict) != NULL)
9784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 int error = FALSE;
9786
Bram Moolenaare9a41262005-01-15 22:18:47 +00009787 if (argvars[2].v_type != VAR_UNKNOWN)
9788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009790 if (argvars[3].v_type != VAR_UNKNOWN)
9791 EMSG(_(e_invarg));
9792 }
9793
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009794 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009796 {
9797 if (!HASHITEM_EMPTY(hi))
9798 {
9799 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009800 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009801 ++n;
9802 }
9803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009804 }
9805 }
9806 else
9807 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 rettv->vval.v_number = n;
9809}
9810
9811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9813 *
9814 * Checks the existence of a cscope connection.
9815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821#ifdef FEAT_CSCOPE
9822 int num = 0;
9823 char_u *dbpath = NULL;
9824 char_u *prepend = NULL;
9825 char_u buf[NUMBUFLEN];
9826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009827 if (argvars[0].v_type != VAR_UNKNOWN
9828 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 num = (int)get_tv_number(&argvars[0]);
9831 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837#endif
9838}
9839
9840/*
9841 * "cursor(lnum, col)" function
9842 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009843 * Moves the cursor to the specified line and column.
9844 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009852#ifdef FEAT_VIRTUALEDIT
9853 long coladd = 0;
9854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009856 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009857 if (argvars[1].v_type == VAR_UNKNOWN)
9858 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009859 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009860 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009861
Bram Moolenaar493c1782014-05-28 14:34:46 +02009862 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009863 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009864 line = pos.lnum;
9865 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009866#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009867 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009868#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009869 if (curswant >= 0)
9870 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009871 }
9872 else
9873 {
9874 line = get_tv_lnum(argvars);
9875 col = get_tv_number_chk(&argvars[1], NULL);
9876#ifdef FEAT_VIRTUALEDIT
9877 if (argvars[2].v_type != VAR_UNKNOWN)
9878 coladd = get_tv_number_chk(&argvars[2], NULL);
9879#endif
9880 }
9881 if (line < 0 || col < 0
9882#ifdef FEAT_VIRTUALEDIT
9883 || coladd < 0
9884#endif
9885 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (line > 0)
9888 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (col > 0)
9890 curwin->w_cursor.col = col - 1;
9891#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009892 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893#endif
9894
9895 /* Make sure the cursor is in a valid position. */
9896 check_cursor();
9897#ifdef FEAT_MBYTE
9898 /* Correct cursor for multi-byte character. */
9899 if (has_mbyte)
9900 mb_adjust_cursor();
9901#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009902
9903 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009904 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009908 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 */
9910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 typval_T *argvars;
9913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009915 int noref = 0;
9916
9917 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009919 if (noref < 0 || noref > 1)
9920 EMSG(_(e_invarg));
9921 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009922 {
9923 current_copyID += COPYID_INC;
9924 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926}
9927
9928/*
9929 * "delete()" function
9930 */
9931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 typval_T *argvars;
9934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935{
9936 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940}
9941
9942/*
9943 * "did_filetype()" function
9944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009947 typval_T *argvars UNUSED;
9948 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
9950#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952#endif
9953}
9954
9955/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009956 * "diff_filler()" function
9957 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009960 typval_T *argvars UNUSED;
9961 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009962{
9963#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009965#endif
9966}
9967
9968/*
9969 * "diff_hlID()" function
9970 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009973 typval_T *argvars UNUSED;
9974 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009975{
9976#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009978 static linenr_T prev_lnum = 0;
9979 static int changedtick = 0;
9980 static int fnum = 0;
9981 static int change_start = 0;
9982 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009983 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009984 int filler_lines;
9985 int col;
9986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009987 if (lnum < 0) /* ignore type error in {lnum} arg */
9988 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009989 if (lnum != prev_lnum
9990 || changedtick != curbuf->b_changedtick
9991 || fnum != curbuf->b_fnum)
9992 {
9993 /* New line, buffer, change: need to get the values. */
9994 filler_lines = diff_check(curwin, lnum);
9995 if (filler_lines < 0)
9996 {
9997 if (filler_lines == -1)
9998 {
9999 change_start = MAXCOL;
10000 change_end = -1;
10001 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10002 hlID = HLF_ADD; /* added line */
10003 else
10004 hlID = HLF_CHD; /* changed line */
10005 }
10006 else
10007 hlID = HLF_ADD; /* added line */
10008 }
10009 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010010 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010011 prev_lnum = lnum;
10012 changedtick = curbuf->b_changedtick;
10013 fnum = curbuf->b_fnum;
10014 }
10015
10016 if (hlID == HLF_CHD || hlID == HLF_TXD)
10017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010019 if (col >= change_start && col <= change_end)
10020 hlID = HLF_TXD; /* changed text */
10021 else
10022 hlID = HLF_CHD; /* changed line */
10023 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010024 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010025#endif
10026}
10027
10028/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010029 * "empty({expr})" function
10030 */
10031 static void
10032f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010035{
10036 int n;
10037
10038 switch (argvars[0].v_type)
10039 {
10040 case VAR_STRING:
10041 case VAR_FUNC:
10042 n = argvars[0].vval.v_string == NULL
10043 || *argvars[0].vval.v_string == NUL;
10044 break;
10045 case VAR_NUMBER:
10046 n = argvars[0].vval.v_number == 0;
10047 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010048#ifdef FEAT_FLOAT
10049 case VAR_FLOAT:
10050 n = argvars[0].vval.v_float == 0.0;
10051 break;
10052#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010053 case VAR_LIST:
10054 n = argvars[0].vval.v_list == NULL
10055 || argvars[0].vval.v_list->lv_first == NULL;
10056 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010057 case VAR_DICT:
10058 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010059 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010061 default:
10062 EMSG2(_(e_intern2), "f_empty()");
10063 n = 0;
10064 }
10065
10066 rettv->vval.v_number = n;
10067}
10068
10069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 * "escape({string}, {chars})" function
10071 */
10072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010074 typval_T *argvars;
10075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 char_u buf[NUMBUFLEN];
10078
Bram Moolenaar758711c2005-02-02 23:11:38 +000010079 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10080 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082}
10083
10084/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010085 * "eval()" function
10086 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010087 static void
10088f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010091{
10092 char_u *s;
10093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 s = get_tv_string_chk(&argvars[0]);
10095 if (s != NULL)
10096 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10099 {
10100 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010103 else if (*s != NUL)
10104 EMSG(_(e_trailing));
10105}
10106
10107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 * "eventhandler()" function
10109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
10118/*
10119 * "executable()" function
10120 */
10121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010123 typval_T *argvars;
10124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010126 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10127}
10128
10129/*
10130 * "exepath()" function
10131 */
10132 static void
10133f_exepath(argvars, rettv)
10134 typval_T *argvars;
10135 typval_T *rettv;
10136{
10137 char_u *p = NULL;
10138
10139 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10140 rettv->v_type = VAR_STRING;
10141 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142}
10143
10144/*
10145 * "exists()" function
10146 */
10147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010149 typval_T *argvars;
10150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 char_u *p;
10153 char_u *name;
10154 int n = FALSE;
10155 int len = 0;
10156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (*p == '$') /* environment variable */
10159 {
10160 /* first try "normal" environment variables (fast) */
10161 if (mch_getenv(p + 1) != NULL)
10162 n = TRUE;
10163 else
10164 {
10165 /* try expanding things like $VIM and ${HOME} */
10166 p = expand_env_save(p);
10167 if (p != NULL && *p != '$')
10168 n = TRUE;
10169 vim_free(p);
10170 }
10171 }
10172 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010175 if (*skipwhite(p) != NUL)
10176 n = FALSE; /* trailing garbage */
10177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 else if (*p == '*') /* internal or user defined function */
10179 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010180 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 }
10182 else if (*p == ':')
10183 {
10184 n = cmd_exists(p + 1);
10185 }
10186 else if (*p == '#')
10187 {
10188#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010189 if (p[1] == '#')
10190 n = autocmd_supported(p + 2);
10191 else
10192 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193#endif
10194 }
10195 else /* internal variable */
10196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010197 char_u *tofree;
10198 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010200 /* get_name_len() takes care of expanding curly braces */
10201 name = p;
10202 len = get_name_len(&p, &tofree, TRUE, FALSE);
10203 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010205 if (tofree != NULL)
10206 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010207 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010208 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010210 /* handle d.key, l[idx], f(expr) */
10211 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10212 if (n)
10213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010216 if (*p != NUL)
10217 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010219 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 }
10221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223}
10224
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010225#ifdef FEAT_FLOAT
10226/*
10227 * "exp()" function
10228 */
10229 static void
10230f_exp(argvars, rettv)
10231 typval_T *argvars;
10232 typval_T *rettv;
10233{
10234 float_T f;
10235
10236 rettv->v_type = VAR_FLOAT;
10237 if (get_float_arg(argvars, &f) == OK)
10238 rettv->vval.v_float = exp(f);
10239 else
10240 rettv->vval.v_float = 0.0;
10241}
10242#endif
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244/*
10245 * "expand()" function
10246 */
10247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 typval_T *argvars;
10250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251{
10252 char_u *s;
10253 int len;
10254 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010255 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010258 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010261 if (argvars[1].v_type != VAR_UNKNOWN
10262 && argvars[2].v_type != VAR_UNKNOWN
10263 && get_tv_number_chk(&argvars[2], &error)
10264 && !error)
10265 {
10266 rettv->v_type = VAR_LIST;
10267 rettv->vval.v_list = NULL;
10268 }
10269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 if (*s == '%' || *s == '#' || *s == '<')
10272 {
10273 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010274 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010276 if (rettv->v_type == VAR_LIST)
10277 {
10278 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10279 list_append_string(rettv->vval.v_list, result, -1);
10280 else
10281 vim_free(result);
10282 }
10283 else
10284 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286 else
10287 {
10288 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010289 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 if (argvars[1].v_type != VAR_UNKNOWN
10291 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010292 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 if (!error)
10294 {
10295 ExpandInit(&xpc);
10296 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010297 if (p_wic)
10298 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010299 if (rettv->v_type == VAR_STRING)
10300 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10301 options, WILD_ALL);
10302 else if (rettv_list_alloc(rettv) != FAIL)
10303 {
10304 int i;
10305
10306 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10307 for (i = 0; i < xpc.xp_numfiles; i++)
10308 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10309 ExpandCleanup(&xpc);
10310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 }
10312 else
10313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315}
10316
10317/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010318 * Go over all entries in "d2" and add them to "d1".
10319 * When "action" is "error" then a duplicate key is an error.
10320 * When "action" is "force" then a duplicate key is overwritten.
10321 * Otherwise duplicate keys are ignored ("action" is "keep").
10322 */
10323 void
10324dict_extend(d1, d2, action)
10325 dict_T *d1;
10326 dict_T *d2;
10327 char_u *action;
10328{
10329 dictitem_T *di1;
10330 hashitem_T *hi2;
10331 int todo;
10332
10333 todo = (int)d2->dv_hashtab.ht_used;
10334 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10335 {
10336 if (!HASHITEM_EMPTY(hi2))
10337 {
10338 --todo;
10339 di1 = dict_find(d1, hi2->hi_key, -1);
10340 if (d1->dv_scope != 0)
10341 {
10342 /* Disallow replacing a builtin function in l: and g:.
10343 * Check the key to be valid when adding to any
10344 * scope. */
10345 if (d1->dv_scope == VAR_DEF_SCOPE
10346 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10347 && var_check_func_name(hi2->hi_key,
10348 di1 == NULL))
10349 break;
10350 if (!valid_varname(hi2->hi_key))
10351 break;
10352 }
10353 if (di1 == NULL)
10354 {
10355 di1 = dictitem_copy(HI2DI(hi2));
10356 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10357 dictitem_free(di1);
10358 }
10359 else if (*action == 'e')
10360 {
10361 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10362 break;
10363 }
10364 else if (*action == 'f' && HI2DI(hi2) != di1)
10365 {
10366 clear_tv(&di1->di_tv);
10367 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10368 }
10369 }
10370 }
10371}
10372
10373/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010374 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010375 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010376 */
10377 static void
10378f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010381{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010382 char *arg_errmsg = N_("extend() argument");
10383
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010386 list_T *l1, *l2;
10387 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010389 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010390
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 l1 = argvars[0].vval.v_list;
10392 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010393 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010394 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010395 {
10396 if (argvars[2].v_type != VAR_UNKNOWN)
10397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 before = get_tv_number_chk(&argvars[2], &error);
10399 if (error)
10400 return; /* type error; errmsg already given */
10401
Bram Moolenaar758711c2005-02-02 23:11:38 +000010402 if (before == l1->lv_len)
10403 item = NULL;
10404 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010406 item = list_find(l1, before);
10407 if (item == NULL)
10408 {
10409 EMSGN(_(e_listidx), before);
10410 return;
10411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010412 }
10413 }
10414 else
10415 item = NULL;
10416 list_extend(l1, l2, item);
10417
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 copy_tv(&argvars[0], rettv);
10419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10422 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010423 dict_T *d1, *d2;
10424 char_u *action;
10425 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426
10427 d1 = argvars[0].vval.v_dict;
10428 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010429 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010430 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010431 {
10432 /* Check the third argument. */
10433 if (argvars[2].v_type != VAR_UNKNOWN)
10434 {
10435 static char *(av[]) = {"keep", "force", "error"};
10436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 action = get_tv_string_chk(&argvars[2]);
10438 if (action == NULL)
10439 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010440 for (i = 0; i < 3; ++i)
10441 if (STRCMP(action, av[i]) == 0)
10442 break;
10443 if (i == 3)
10444 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010445 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 return;
10447 }
10448 }
10449 else
10450 action = (char_u *)"force";
10451
Bram Moolenaara9922d62013-05-30 13:01:18 +020010452 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 copy_tv(&argvars[0], rettv);
10455 }
10456 }
10457 else
10458 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010459}
10460
10461/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010462 * "feedkeys()" function
10463 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010464 static void
10465f_feedkeys(argvars, rettv)
10466 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010467 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010468{
10469 int remap = TRUE;
10470 char_u *keys, *flags;
10471 char_u nbuf[NUMBUFLEN];
10472 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010473 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010474
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010475 /* This is not allowed in the sandbox. If the commands would still be
10476 * executed in the sandbox it would be OK, but it probably happens later,
10477 * when "sandbox" is no longer set. */
10478 if (check_secure())
10479 return;
10480
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010481 keys = get_tv_string(&argvars[0]);
10482 if (*keys != NUL)
10483 {
10484 if (argvars[1].v_type != VAR_UNKNOWN)
10485 {
10486 flags = get_tv_string_buf(&argvars[1], nbuf);
10487 for ( ; *flags != NUL; ++flags)
10488 {
10489 switch (*flags)
10490 {
10491 case 'n': remap = FALSE; break;
10492 case 'm': remap = TRUE; break;
10493 case 't': typed = TRUE; break;
10494 }
10495 }
10496 }
10497
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010498 /* Need to escape K_SPECIAL and CSI before putting the string in the
10499 * typeahead buffer. */
10500 keys_esc = vim_strsave_escape_csi(keys);
10501 if (keys_esc != NULL)
10502 {
10503 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010504 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010505 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010506 if (vgetc_busy)
10507 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010508 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010509 }
10510}
10511
10512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 * "filereadable()" function
10514 */
10515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010520 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 char_u *p;
10522 int n;
10523
Bram Moolenaarc236c162008-07-13 17:41:49 +000010524#ifndef O_NONBLOCK
10525# define O_NONBLOCK 0
10526#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010528 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10529 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 {
10531 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010532 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534 else
10535 n = FALSE;
10536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539
10540/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010541 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 * rights to write into.
10543 */
10544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 typval_T *argvars;
10547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010549 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550}
10551
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010552static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010553
10554 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010555findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010558 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010559{
10560#ifdef FEAT_SEARCHPATH
10561 char_u *fname;
10562 char_u *fresult = NULL;
10563 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10564 char_u *p;
10565 char_u pathbuf[NUMBUFLEN];
10566 int count = 1;
10567 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010568 int error = FALSE;
10569#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010570
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010571 rettv->vval.v_string = NULL;
10572 rettv->v_type = VAR_STRING;
10573
10574#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010576
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10580 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010581 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 else
10583 {
10584 if (*p != NUL)
10585 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010588 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010590 }
10591
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010592 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10593 error = TRUE;
10594
10595 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 do
10598 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010599 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010600 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 fresult = find_file_in_path_option(first ? fname : NULL,
10602 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010603 0, first, path,
10604 find_what,
10605 curbuf->b_ffname,
10606 find_what == FINDFILE_DIR
10607 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010609
10610 if (fresult != NULL && rettv->v_type == VAR_LIST)
10611 list_append_string(rettv->vval.v_list, fresult, -1);
10612
10613 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010615
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010616 if (rettv->v_type == VAR_STRING)
10617 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010618#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010619}
10620
Bram Moolenaar33570922005-01-25 22:26:29 +000010621static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10622static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623
10624/*
10625 * Implementation of map() and filter().
10626 */
10627 static void
10628filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010631 int map;
10632{
10633 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 listitem_T *li, *nli;
10636 list_T *l = NULL;
10637 dictitem_T *di;
10638 hashtab_T *ht;
10639 hashitem_T *hi;
10640 dict_T *d = NULL;
10641 typval_T save_val;
10642 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010644 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010645 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10646 char *arg_errmsg = (map ? N_("map() argument")
10647 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010648 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010649 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010653 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010654 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655 return;
10656 }
10657 else if (argvars[0].v_type == VAR_DICT)
10658 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010659 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010660 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010661 return;
10662 }
10663 else
10664 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010665 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 return;
10667 }
10668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 expr = get_tv_string_buf_chk(&argvars[1], buf);
10670 /* On type errors, the preceding call has already displayed an error
10671 * message. Avoid a misleading error message for an empty string that
10672 * was not passed as argument. */
10673 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 prepare_vimvar(VV_VAL, &save_val);
10676 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010677
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010678 /* We reset "did_emsg" to be able to detect whether an error
10679 * occurred during evaluation of the expression. */
10680 save_did_emsg = did_emsg;
10681 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010682
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010683 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010684 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 vimvars[VV_KEY].vv_type = VAR_STRING;
10687
10688 ht = &d->dv_hashtab;
10689 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010690 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 if (!HASHITEM_EMPTY(hi))
10694 {
10695 --todo;
10696 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010697 if (tv_check_lock(di->di_tv.v_lock,
10698 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 break;
10700 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010701 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010702 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 break;
10704 if (!map && rem)
10705 dictitem_remove(d, di);
10706 clear_tv(&vimvars[VV_KEY].vv_tv);
10707 }
10708 }
10709 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 }
10711 else
10712 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010713 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 for (li = l->lv_first; li != NULL; li = nli)
10716 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010717 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010718 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010720 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010721 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010722 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010723 break;
10724 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010726 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010727 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010729
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010730 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010731 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010732
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010733 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735
10736 copy_tv(&argvars[0], rettv);
10737}
10738
10739 static int
10740filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 char_u *expr;
10743 int map;
10744 int *remp;
10745{
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010748 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010751 s = expr;
10752 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010753 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010754 if (*s != NUL) /* check for trailing chars after expr */
10755 {
10756 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010757 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 }
10759 if (map)
10760 {
10761 /* map(): replace the list item value */
10762 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010763 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010764 *tv = rettv;
10765 }
10766 else
10767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010768 int error = FALSE;
10769
Bram Moolenaare9a41262005-01-15 22:18:47 +000010770 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 /* On type error, nothing has been removed; return FAIL to stop the
10774 * loop. The error message was given by get_tv_number_chk(). */
10775 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010776 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010778 retval = OK;
10779theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010781 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010782}
10783
10784/*
10785 * "filter()" function
10786 */
10787 static void
10788f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010791{
10792 filter_map(argvars, rettv, FALSE);
10793}
10794
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010795/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010796 * "finddir({fname}[, {path}[, {count}]])" function
10797 */
10798 static void
10799f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010800 typval_T *argvars;
10801 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010803 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804}
10805
10806/*
10807 * "findfile({fname}[, {path}[, {count}]])" function
10808 */
10809 static void
10810f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010814 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010815}
10816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010817#ifdef FEAT_FLOAT
10818/*
10819 * "float2nr({float})" function
10820 */
10821 static void
10822f_float2nr(argvars, rettv)
10823 typval_T *argvars;
10824 typval_T *rettv;
10825{
10826 float_T f;
10827
10828 if (get_float_arg(argvars, &f) == OK)
10829 {
10830 if (f < -0x7fffffff)
10831 rettv->vval.v_number = -0x7fffffff;
10832 else if (f > 0x7fffffff)
10833 rettv->vval.v_number = 0x7fffffff;
10834 else
10835 rettv->vval.v_number = (varnumber_T)f;
10836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010837}
10838
10839/*
10840 * "floor({float})" function
10841 */
10842 static void
10843f_floor(argvars, rettv)
10844 typval_T *argvars;
10845 typval_T *rettv;
10846{
10847 float_T f;
10848
10849 rettv->v_type = VAR_FLOAT;
10850 if (get_float_arg(argvars, &f) == OK)
10851 rettv->vval.v_float = floor(f);
10852 else
10853 rettv->vval.v_float = 0.0;
10854}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010855
10856/*
10857 * "fmod()" function
10858 */
10859 static void
10860f_fmod(argvars, rettv)
10861 typval_T *argvars;
10862 typval_T *rettv;
10863{
10864 float_T fx, fy;
10865
10866 rettv->v_type = VAR_FLOAT;
10867 if (get_float_arg(argvars, &fx) == OK
10868 && get_float_arg(&argvars[1], &fy) == OK)
10869 rettv->vval.v_float = fmod(fx, fy);
10870 else
10871 rettv->vval.v_float = 0.0;
10872}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010873#endif
10874
Bram Moolenaar0d660222005-01-07 21:51:51 +000010875/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010876 * "fnameescape({string})" function
10877 */
10878 static void
10879f_fnameescape(argvars, rettv)
10880 typval_T *argvars;
10881 typval_T *rettv;
10882{
10883 rettv->vval.v_string = vim_strsave_fnameescape(
10884 get_tv_string(&argvars[0]), FALSE);
10885 rettv->v_type = VAR_STRING;
10886}
10887
10888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 * "fnamemodify({fname}, {mods})" function
10890 */
10891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 char_u *fname;
10897 char_u *mods;
10898 int usedlen = 0;
10899 int len;
10900 char_u *fbuf = NULL;
10901 char_u buf[NUMBUFLEN];
10902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 fname = get_tv_string_chk(&argvars[0]);
10904 mods = get_tv_string_buf_chk(&argvars[1], buf);
10905 if (fname == NULL || mods == NULL)
10906 fname = NULL;
10907 else
10908 {
10909 len = (int)STRLEN(fname);
10910 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 vim_free(fbuf);
10919}
10920
Bram Moolenaar33570922005-01-25 22:26:29 +000010921static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922
10923/*
10924 * "foldclosed()" function
10925 */
10926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010928 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010930 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
10932#ifdef FEAT_FOLDING
10933 linenr_T lnum;
10934 linenr_T first, last;
10935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10938 {
10939 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10940 {
10941 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 return;
10946 }
10947 }
10948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950}
10951
10952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010953 * "foldclosed()" function
10954 */
10955 static void
10956f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959{
10960 foldclosed_both(argvars, rettv, FALSE);
10961}
10962
10963/*
10964 * "foldclosedend()" function
10965 */
10966 static void
10967f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970{
10971 foldclosed_both(argvars, rettv, TRUE);
10972}
10973
10974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 * "foldlevel()" function
10976 */
10977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010979 typval_T *argvars UNUSED;
10980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
10982#ifdef FEAT_FOLDING
10983 linenr_T lnum;
10984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989}
10990
10991/*
10992 * "foldtext()" function
10993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010996 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
10999#ifdef FEAT_FOLDING
11000 linenr_T lnum;
11001 char_u *s;
11002 char_u *r;
11003 int len;
11004 char *txt;
11005#endif
11006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 rettv->v_type = VAR_STRING;
11008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11011 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11012 <= curbuf->b_ml.ml_line_count
11013 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 {
11015 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11017 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 {
11019 if (!linewhite(lnum))
11020 break;
11021 ++lnum;
11022 }
11023
11024 /* Find interesting text in this line. */
11025 s = skipwhite(ml_get(lnum));
11026 /* skip C comment-start */
11027 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011030 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011032 {
11033 s = skipwhite(ml_get(lnum + 1));
11034 if (*s == '*')
11035 s = skipwhite(s + 1);
11036 }
11037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 txt = _("+-%s%3ld lines: ");
11039 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 + 20 /* for %3ld */
11042 + STRLEN(s))); /* concatenated */
11043 if (r != NULL)
11044 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11046 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11047 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 len = (int)STRLEN(r);
11049 STRCAT(r, s);
11050 /* remove 'foldmarker' and 'commentstring' */
11051 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054 }
11055#endif
11056}
11057
11058/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011059 * "foldtextresult(lnum)" function
11060 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011065{
11066#ifdef FEAT_FOLDING
11067 linenr_T lnum;
11068 char_u *text;
11069 char_u buf[51];
11070 foldinfo_T foldinfo;
11071 int fold_count;
11072#endif
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_STRING;
11075 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011076#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 /* treat illegal types and illegal string values for {lnum} the same */
11079 if (lnum < 0)
11080 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011081 fold_count = foldedCount(curwin, lnum, &foldinfo);
11082 if (fold_count > 0)
11083 {
11084 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11085 &foldinfo, buf);
11086 if (text == buf)
11087 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011089 }
11090#endif
11091}
11092
11093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 * "foreground()" function
11095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011098 typval_T *argvars UNUSED;
11099 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101#ifdef FEAT_GUI
11102 if (gui.in_use)
11103 gui_mch_set_foreground();
11104#else
11105# ifdef WIN32
11106 win32_set_foreground();
11107# endif
11108#endif
11109}
11110
11111/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011112 * "function()" function
11113 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011118{
11119 char_u *s;
11120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011124 /* Don't check an autoload name for existence here. */
11125 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011126 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011127 else
11128 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011129 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011130 {
11131 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011132 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011133
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011134 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11135 * also be called from another script. Using trans_function_name()
11136 * would also work, but some plugins depend on the name being
11137 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011138 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011139 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011140 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011141 if (rettv->vval.v_string != NULL)
11142 {
11143 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011144 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011145 }
11146 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011147 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011148 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 }
11151}
11152
11153/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011154 * "garbagecollect()" function
11155 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011156 static void
11157f_garbagecollect(argvars, rettv)
11158 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011159 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011160{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011161 /* This is postponed until we are back at the toplevel, because we may be
11162 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11163 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011164
11165 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11166 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011167}
11168
11169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170 * "get()" function
11171 */
11172 static void
11173f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011174 typval_T *argvars;
11175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176{
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 listitem_T *li;
11178 list_T *l;
11179 dictitem_T *di;
11180 dict_T *d;
11181 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011184 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 int error = FALSE;
11188
11189 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11190 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 else if (argvars[0].v_type == VAR_DICT)
11195 {
11196 if ((d = argvars[0].vval.v_dict) != NULL)
11197 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011198 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011199 if (di != NULL)
11200 tv = &di->di_tv;
11201 }
11202 }
11203 else
11204 EMSG2(_(e_listdictarg), "get()");
11205
11206 if (tv == NULL)
11207 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 copy_tv(&argvars[2], rettv);
11210 }
11211 else
11212 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011213}
11214
Bram Moolenaar342337a2005-07-21 21:11:17 +000011215static 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 +000011216
11217/*
11218 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011219 * Return a range (from start to end) of lines in rettv from the specified
11220 * buffer.
11221 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011222 */
11223 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011224get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011225 buf_T *buf;
11226 linenr_T start;
11227 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011228 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011229 typval_T *rettv;
11230{
11231 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011232
Bram Moolenaar959a1432013-12-14 12:17:38 +010011233 rettv->v_type = VAR_STRING;
11234 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011235 if (retlist && rettv_list_alloc(rettv) == FAIL)
11236 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011237
11238 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11239 return;
11240
11241 if (!retlist)
11242 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011243 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11244 p = ml_get_buf(buf, start, FALSE);
11245 else
11246 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011247 rettv->vval.v_string = vim_strsave(p);
11248 }
11249 else
11250 {
11251 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011252 return;
11253
11254 if (start < 1)
11255 start = 1;
11256 if (end > buf->b_ml.ml_line_count)
11257 end = buf->b_ml.ml_line_count;
11258 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011259 if (list_append_string(rettv->vval.v_list,
11260 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011261 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011262 }
11263}
11264
11265/*
11266 * "getbufline()" function
11267 */
11268 static void
11269f_getbufline(argvars, rettv)
11270 typval_T *argvars;
11271 typval_T *rettv;
11272{
11273 linenr_T lnum;
11274 linenr_T end;
11275 buf_T *buf;
11276
11277 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11278 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011279 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011280 --emsg_off;
11281
Bram Moolenaar661b1822005-07-28 22:36:45 +000011282 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011283 if (argvars[2].v_type == VAR_UNKNOWN)
11284 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011285 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011286 end = get_tv_lnum_buf(&argvars[2], buf);
11287
Bram Moolenaar342337a2005-07-21 21:11:17 +000011288 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011289}
11290
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291/*
11292 * "getbufvar()" function
11293 */
11294 static void
11295f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298{
11299 buf_T *buf;
11300 buf_T *save_curbuf;
11301 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011302 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011303 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11306 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011308 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011310 rettv->v_type = VAR_STRING;
11311 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011312
11313 if (buf != NULL && varname != NULL)
11314 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011315 /* set curbuf to be our buf, temporarily */
11316 save_curbuf = curbuf;
11317 curbuf = buf;
11318
Bram Moolenaar0d660222005-01-07 21:51:51 +000011319 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011320 {
11321 if (get_option_tv(&varname, rettv, TRUE) == OK)
11322 done = TRUE;
11323 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011324 else if (STRCMP(varname, "changedtick") == 0)
11325 {
11326 rettv->v_type = VAR_NUMBER;
11327 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011328 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011329 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330 else
11331 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011332 /* Look up the variable. */
11333 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11334 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11335 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011339 done = TRUE;
11340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011342
11343 /* restore previous notion of curbuf */
11344 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345 }
11346
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011347 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11348 /* use the default value */
11349 copy_tv(&argvars[2], rettv);
11350
Bram Moolenaar0d660222005-01-07 21:51:51 +000011351 --emsg_off;
11352}
11353
11354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 * "getchar()" function
11356 */
11357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *argvars;
11360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011365 /* Position the cursor. Needed after a message that ends in a space. */
11366 windgoto(msg_row, msg_col);
11367
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 ++no_mapping;
11369 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011370 for (;;)
11371 {
11372 if (argvars[0].v_type == VAR_UNKNOWN)
11373 /* getchar(): blocking wait. */
11374 n = safe_vgetc();
11375 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11376 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011377 n = vpeekc_any();
11378 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011379 /* illegal argument or getchar(0) and no char avail: return zero */
11380 n = 0;
11381 else
11382 /* getchar(0) and char avail: return char */
11383 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011384
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011385 if (n == K_IGNORE)
11386 continue;
11387 break;
11388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 --no_mapping;
11390 --allow_keys;
11391
Bram Moolenaar219b8702006-11-01 14:32:36 +000011392 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11393 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11394 vimvars[VV_MOUSE_COL].vv_nr = 0;
11395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 if (IS_SPECIAL(n) || mod_mask != 0)
11398 {
11399 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11400 int i = 0;
11401
11402 /* Turn a special key into three bytes, plus modifier. */
11403 if (mod_mask != 0)
11404 {
11405 temp[i++] = K_SPECIAL;
11406 temp[i++] = KS_MODIFIER;
11407 temp[i++] = mod_mask;
11408 }
11409 if (IS_SPECIAL(n))
11410 {
11411 temp[i++] = K_SPECIAL;
11412 temp[i++] = K_SECOND(n);
11413 temp[i++] = K_THIRD(n);
11414 }
11415#ifdef FEAT_MBYTE
11416 else if (has_mbyte)
11417 i += (*mb_char2bytes)(n, temp + i);
11418#endif
11419 else
11420 temp[i++] = n;
11421 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->v_type = VAR_STRING;
11423 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011424
11425#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011426 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011427 {
11428 int row = mouse_row;
11429 int col = mouse_col;
11430 win_T *win;
11431 linenr_T lnum;
11432# ifdef FEAT_WINDOWS
11433 win_T *wp;
11434# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011435 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011436
11437 if (row >= 0 && col >= 0)
11438 {
11439 /* Find the window at the mouse coordinates and compute the
11440 * text position. */
11441 win = mouse_find_win(&row, &col);
11442 (void)mouse_comp_pos(win, &row, &col, &lnum);
11443# ifdef FEAT_WINDOWS
11444 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011445 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011446# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011447 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011448 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11449 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11450 }
11451 }
11452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 }
11454}
11455
11456/*
11457 * "getcharmod()" function
11458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * "getcmdline()" function
11469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->v_type = VAR_STRING;
11476 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477}
11478
11479/*
11480 * "getcmdpos()" function
11481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011491 * "getcmdtype()" function
11492 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011493 static void
11494f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011495 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011496 typval_T *rettv;
11497{
11498 rettv->v_type = VAR_STRING;
11499 rettv->vval.v_string = alloc(2);
11500 if (rettv->vval.v_string != NULL)
11501 {
11502 rettv->vval.v_string[0] = get_cmdline_type();
11503 rettv->vval.v_string[1] = NUL;
11504 }
11505}
11506
11507/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011508 * "getcmdwintype()" function
11509 */
11510 static void
11511f_getcmdwintype(argvars, rettv)
11512 typval_T *argvars UNUSED;
11513 typval_T *rettv;
11514{
11515 rettv->v_type = VAR_STRING;
11516 rettv->vval.v_string = NULL;
11517#ifdef FEAT_CMDWIN
11518 rettv->vval.v_string = alloc(2);
11519 if (rettv->vval.v_string != NULL)
11520 {
11521 rettv->vval.v_string[0] = cmdwin_type;
11522 rettv->vval.v_string[1] = NUL;
11523 }
11524#endif
11525}
11526
11527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 * "getcwd()" function
11529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011532 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011535 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011538 rettv->vval.v_string = NULL;
11539 cwd = alloc(MAXPATHL);
11540 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011542 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11543 {
11544 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011546 if (rettv->vval.v_string != NULL)
11547 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011549 }
11550 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 }
11552}
11553
11554/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011555 * "getfontname()" function
11556 */
11557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->v_type = VAR_STRING;
11563 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011564#ifdef FEAT_GUI
11565 if (gui.in_use)
11566 {
11567 GuiFont font;
11568 char_u *name = NULL;
11569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011570 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011571 {
11572 /* Get the "Normal" font. Either the name saved by
11573 * hl_set_font_name() or from the font ID. */
11574 font = gui.norm_font;
11575 name = hl_get_font_name();
11576 }
11577 else
11578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011580 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11581 return;
11582 font = gui_mch_get_font(name, FALSE);
11583 if (font == NOFONT)
11584 return; /* Invalid font name, return empty string. */
11585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011588 gui_mch_free_font(font);
11589 }
11590#endif
11591}
11592
11593/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594 * "getfperm({fname})" function
11595 */
11596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *argvars;
11599 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011600{
11601 char_u *fname;
11602 struct stat st;
11603 char_u *perm = NULL;
11604 char_u flags[] = "rwx";
11605 int i;
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011610 if (mch_stat((char *)fname, &st) >= 0)
11611 {
11612 perm = vim_strsave((char_u *)"---------");
11613 if (perm != NULL)
11614 {
11615 for (i = 0; i < 9; i++)
11616 {
11617 if (st.st_mode & (1 << (8 - i)))
11618 perm[i] = flags[i % 3];
11619 }
11620 }
11621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011623}
11624
11625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 * "getfsize({fname})" function
11627 */
11628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 typval_T *argvars;
11631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632{
11633 char_u *fname;
11634 struct stat st;
11635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
11640 if (mch_stat((char *)fname, &st) >= 0)
11641 {
11642 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011647
11648 /* non-perfect check for overflow */
11649 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11650 rettv->vval.v_number = -2;
11651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 }
11653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655}
11656
11657/*
11658 * "getftime({fname})" function
11659 */
11660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 typval_T *argvars;
11663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
11665 char_u *fname;
11666 struct stat st;
11667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669
11670 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674}
11675
11676/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011677 * "getftype({fname})" function
11678 */
11679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011683{
11684 char_u *fname;
11685 struct stat st;
11686 char_u *type = NULL;
11687 char *t;
11688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011692 if (mch_lstat((char *)fname, &st) >= 0)
11693 {
11694#ifdef S_ISREG
11695 if (S_ISREG(st.st_mode))
11696 t = "file";
11697 else if (S_ISDIR(st.st_mode))
11698 t = "dir";
11699# ifdef S_ISLNK
11700 else if (S_ISLNK(st.st_mode))
11701 t = "link";
11702# endif
11703# ifdef S_ISBLK
11704 else if (S_ISBLK(st.st_mode))
11705 t = "bdev";
11706# endif
11707# ifdef S_ISCHR
11708 else if (S_ISCHR(st.st_mode))
11709 t = "cdev";
11710# endif
11711# ifdef S_ISFIFO
11712 else if (S_ISFIFO(st.st_mode))
11713 t = "fifo";
11714# endif
11715# ifdef S_ISSOCK
11716 else if (S_ISSOCK(st.st_mode))
11717 t = "fifo";
11718# endif
11719 else
11720 t = "other";
11721#else
11722# ifdef S_IFMT
11723 switch (st.st_mode & S_IFMT)
11724 {
11725 case S_IFREG: t = "file"; break;
11726 case S_IFDIR: t = "dir"; break;
11727# ifdef S_IFLNK
11728 case S_IFLNK: t = "link"; break;
11729# endif
11730# ifdef S_IFBLK
11731 case S_IFBLK: t = "bdev"; break;
11732# endif
11733# ifdef S_IFCHR
11734 case S_IFCHR: t = "cdev"; break;
11735# endif
11736# ifdef S_IFIFO
11737 case S_IFIFO: t = "fifo"; break;
11738# endif
11739# ifdef S_IFSOCK
11740 case S_IFSOCK: t = "socket"; break;
11741# endif
11742 default: t = "other";
11743 }
11744# else
11745 if (mch_isdir(fname))
11746 t = "dir";
11747 else
11748 t = "file";
11749# endif
11750#endif
11751 type = vim_strsave((char_u *)t);
11752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011754}
11755
11756/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011757 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011758 */
11759 static void
11760f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011763{
11764 linenr_T lnum;
11765 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011766 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011767
11768 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011769 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011770 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011771 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011772 retlist = FALSE;
11773 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011776 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011777 retlist = TRUE;
11778 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011779
Bram Moolenaar342337a2005-07-21 21:11:17 +000011780 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781}
11782
11783/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011784 * "getmatches()" function
11785 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011786 static void
11787f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011788 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011789 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011790{
11791#ifdef FEAT_SEARCH_EXTRA
11792 dict_T *dict;
11793 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011794 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011795
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011796 if (rettv_list_alloc(rettv) == OK)
11797 {
11798 while (cur != NULL)
11799 {
11800 dict = dict_alloc();
11801 if (dict == NULL)
11802 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011803 if (cur->match.regprog == NULL)
11804 {
11805 /* match added with matchaddpos() */
11806 for (i = 0; i < MAXPOSMATCH; ++i)
11807 {
11808 llpos_T *llpos;
11809 char buf[6];
11810 list_T *l;
11811
11812 llpos = &cur->pos.pos[i];
11813 if (llpos->lnum == 0)
11814 break;
11815 l = list_alloc();
11816 if (l == NULL)
11817 break;
11818 list_append_number(l, (varnumber_T)llpos->lnum);
11819 if (llpos->col > 0)
11820 {
11821 list_append_number(l, (varnumber_T)llpos->col);
11822 list_append_number(l, (varnumber_T)llpos->len);
11823 }
11824 sprintf(buf, "pos%d", i + 1);
11825 dict_add_list(dict, buf, l);
11826 }
11827 }
11828 else
11829 {
11830 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11831 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011832 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011833 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11834 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11835 list_append_dict(rettv->vval.v_list, dict);
11836 cur = cur->next;
11837 }
11838 }
11839#endif
11840}
11841
11842/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011843 * "getpid()" function
11844 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011845 static void
11846f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011847 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011848 typval_T *rettv;
11849{
11850 rettv->vval.v_number = mch_get_pid();
11851}
11852
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011853static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11854
11855/*
11856 * "getcurpos()" function
11857 */
11858 static void
11859f_getcurpos(argvars, rettv)
11860 typval_T *argvars;
11861 typval_T *rettv;
11862{
11863 getpos_both(argvars, rettv, TRUE);
11864}
11865
Bram Moolenaar18081e32008-02-20 19:11:07 +000011866/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011867 * "getpos(string)" function
11868 */
11869 static void
11870f_getpos(argvars, rettv)
11871 typval_T *argvars;
11872 typval_T *rettv;
11873{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011874 getpos_both(argvars, rettv, FALSE);
11875}
11876
11877 static void
11878getpos_both(argvars, rettv, getcurpos)
11879 typval_T *argvars;
11880 typval_T *rettv;
11881 int getcurpos;
11882{
Bram Moolenaara5525202006-03-02 22:52:09 +000011883 pos_T *fp;
11884 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011885 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011886
11887 if (rettv_list_alloc(rettv) == OK)
11888 {
11889 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011890 if (getcurpos)
11891 fp = &curwin->w_cursor;
11892 else
11893 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011894 if (fnum != -1)
11895 list_append_number(l, (varnumber_T)fnum);
11896 else
11897 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011898 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11899 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011900 list_append_number(l, (fp != NULL)
11901 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011902 : (varnumber_T)0);
11903 list_append_number(l,
11904#ifdef FEAT_VIRTUALEDIT
11905 (fp != NULL) ? (varnumber_T)fp->coladd :
11906#endif
11907 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011908 if (getcurpos)
Bram Moolenaar493c1782014-05-28 14:34:46 +020011909 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011910 }
11911 else
11912 rettv->vval.v_number = FALSE;
11913}
11914
11915/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011916 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011917 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011918 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011919f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011920 typval_T *argvars UNUSED;
11921 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011922{
11923#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011924 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011925#endif
11926
Bram Moolenaar2641f772005-03-25 21:58:17 +000011927#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011928 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011929 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011930 wp = NULL;
11931 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11932 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011933 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011934 if (wp == NULL)
11935 return;
11936 }
11937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011938 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011939 }
11940#endif
11941}
11942
11943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 * "getreg()" function
11945 */
11946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950{
11951 char_u *strregname;
11952 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011953 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011954 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 strregname = get_tv_string_chk(&argvars[0]);
11960 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011961 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011964 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11965 return_list = get_tv_number_chk(&argvars[2], &error);
11966 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011969 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011970
11971 if (error)
11972 return;
11973
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 regname = (strregname == NULL ? '"' : *strregname);
11975 if (regname == 0)
11976 regname = '"';
11977
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011978 if (return_list)
11979 {
11980 rettv->v_type = VAR_LIST;
11981 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11982 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11983 }
11984 else
11985 {
11986 rettv->v_type = VAR_STRING;
11987 rettv->vval.v_string = get_reg_contents(regname,
11988 arg2 ? GREG_EXPR_SRC : 0);
11989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
11993 * "getregtype()" function
11994 */
11995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 typval_T *argvars;
11998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
12000 char_u *strregname;
12001 int regname;
12002 char_u buf[NUMBUFLEN + 2];
12003 long reglen = 0;
12004
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012005 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 {
12007 strregname = get_tv_string_chk(&argvars[0]);
12008 if (strregname == NULL) /* type error; errmsg already given */
12009 {
12010 rettv->v_type = VAR_STRING;
12011 rettv->vval.v_string = NULL;
12012 return;
12013 }
12014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 else
12016 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
12019 regname = (strregname == NULL ? '"' : *strregname);
12020 if (regname == 0)
12021 regname = '"';
12022
12023 buf[0] = NUL;
12024 buf[1] = NUL;
12025 switch (get_reg_type(regname, &reglen))
12026 {
12027 case MLINE: buf[0] = 'V'; break;
12028 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 case MBLOCK:
12030 buf[0] = Ctrl_V;
12031 sprintf((char *)buf + 1, "%ld", reglen + 1);
12032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034 rettv->v_type = VAR_STRING;
12035 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036}
12037
12038/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012039 * "gettabvar()" function
12040 */
12041 static void
12042f_gettabvar(argvars, rettv)
12043 typval_T *argvars;
12044 typval_T *rettv;
12045{
12046 tabpage_T *tp;
12047 dictitem_T *v;
12048 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012049 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012050
12051 rettv->v_type = VAR_STRING;
12052 rettv->vval.v_string = NULL;
12053
12054 varname = get_tv_string_chk(&argvars[1]);
12055 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12056 if (tp != NULL && varname != NULL)
12057 {
12058 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020012059 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012060 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012061 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012062 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012063 done = TRUE;
12064 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012065 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012066
12067 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012068 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012069}
12070
12071/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012072 * "gettabwinvar()" function
12073 */
12074 static void
12075f_gettabwinvar(argvars, rettv)
12076 typval_T *argvars;
12077 typval_T *rettv;
12078{
12079 getwinvar(argvars, rettv, 1);
12080}
12081
12082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 * "getwinposx()" function
12084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef FEAT_GUI
12092 if (gui.in_use)
12093 {
12094 int x, y;
12095
12096 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 }
12099#endif
12100}
12101
12102/*
12103 * "getwinposy()" function
12104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012107 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111#ifdef FEAT_GUI
12112 if (gui.in_use)
12113 {
12114 int x, y;
12115
12116 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
12119#endif
12120}
12121
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012122/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012123 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012124 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012125 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012126find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012127 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012128 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012129{
12130#ifdef FEAT_WINDOWS
12131 win_T *wp;
12132#endif
12133 int nr;
12134
12135 nr = get_tv_number_chk(vp, NULL);
12136
12137#ifdef FEAT_WINDOWS
12138 if (nr < 0)
12139 return NULL;
12140 if (nr == 0)
12141 return curwin;
12142
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012143 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12144 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012145 if (--nr <= 0)
12146 break;
12147 return wp;
12148#else
12149 if (nr == 0 || nr == 1)
12150 return curwin;
12151 return NULL;
12152#endif
12153}
12154
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155/*
12156 * "getwinvar()" function
12157 */
12158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012163 getwinvar(argvars, rettv, 0);
12164}
12165
12166/*
12167 * getwinvar() and gettabwinvar()
12168 */
12169 static void
12170getwinvar(argvars, rettv, off)
12171 typval_T *argvars;
12172 typval_T *rettv;
12173 int off; /* 1 for gettabwinvar() */
12174{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 win_T *win, *oldcurwin;
12176 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012178 tabpage_T *tp = NULL;
12179 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012180 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012182#ifdef FEAT_WINDOWS
12183 if (off == 1)
12184 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12185 else
12186 tp = curtab;
12187#endif
12188 win = find_win_by_nr(&argvars[off], tp);
12189 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012192 rettv->v_type = VAR_STRING;
12193 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
12195 if (win != NULL && varname != NULL)
12196 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012197 /* Set curwin to be our win, temporarily. Also set the tabpage,
12198 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012199 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012200
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012202 {
12203 if (get_option_tv(&varname, rettv, 1) == OK)
12204 done = TRUE;
12205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 else
12207 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012208 /* Look up the variable. */
12209 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12210 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012213 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012214 done = TRUE;
12215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012217
12218 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012219 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 }
12221
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012222 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12223 /* use the default return value */
12224 copy_tv(&argvars[off + 2], rettv);
12225
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 --emsg_off;
12227}
12228
12229/*
12230 * "glob()" function
12231 */
12232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *argvars;
12235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012237 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012241 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012242 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012244 if (argvars[1].v_type != VAR_UNKNOWN)
12245 {
12246 if (get_tv_number_chk(&argvars[1], &error))
12247 options |= WILD_KEEP_ALL;
12248 if (argvars[2].v_type != VAR_UNKNOWN
12249 && get_tv_number_chk(&argvars[2], &error))
12250 {
12251 rettv->v_type = VAR_LIST;
12252 rettv->vval.v_list = NULL;
12253 }
12254 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012255 if (!error)
12256 {
12257 ExpandInit(&xpc);
12258 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012259 if (p_wic)
12260 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012261 if (rettv->v_type == VAR_STRING)
12262 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012263 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012264 else if (rettv_list_alloc(rettv) != FAIL)
12265 {
12266 int i;
12267
12268 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12269 NULL, options, WILD_ALL_KEEP);
12270 for (i = 0; i < xpc.xp_numfiles; i++)
12271 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12272
12273 ExpandCleanup(&xpc);
12274 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012275 }
12276 else
12277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278}
12279
12280/*
12281 * "globpath()" function
12282 */
12283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012288 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012291 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012292 garray_T ga;
12293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012295 /* When the optional second argument is non-zero, don't remove matches
12296 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012298 if (argvars[2].v_type != VAR_UNKNOWN)
12299 {
12300 if (get_tv_number_chk(&argvars[2], &error))
12301 flags |= WILD_KEEP_ALL;
12302 if (argvars[3].v_type != VAR_UNKNOWN
12303 && get_tv_number_chk(&argvars[3], &error))
12304 {
12305 rettv->v_type = VAR_LIST;
12306 rettv->vval.v_list = NULL;
12307 }
12308 }
12309 if (file != NULL && !error)
12310 {
12311 ga_init2(&ga, (int)sizeof(char_u *), 10);
12312 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12313 if (rettv->v_type == VAR_STRING)
12314 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12315 else if (rettv_list_alloc(rettv) != FAIL)
12316 for (i = 0; i < ga.ga_len; ++i)
12317 list_append_string(rettv->vval.v_list,
12318 ((char_u **)(ga.ga_data))[i], -1);
12319 ga_clear_strings(&ga);
12320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323}
12324
12325/*
12326 * "has()" function
12327 */
12328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *argvars;
12331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332{
12333 int i;
12334 char_u *name;
12335 int n = FALSE;
12336 static char *(has_list[]) =
12337 {
12338#ifdef AMIGA
12339 "amiga",
12340# ifdef FEAT_ARP
12341 "arp",
12342# endif
12343#endif
12344#ifdef __BEOS__
12345 "beos",
12346#endif
12347#ifdef MSDOS
12348# ifdef DJGPP
12349 "dos32",
12350# else
12351 "dos16",
12352# endif
12353#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012354#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 "mac",
12356#endif
12357#if defined(MACOS_X_UNIX)
12358 "macunix",
12359#endif
12360#ifdef OS2
12361 "os2",
12362#endif
12363#ifdef __QNX__
12364 "qnx",
12365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#ifdef UNIX
12367 "unix",
12368#endif
12369#ifdef VMS
12370 "vms",
12371#endif
12372#ifdef WIN16
12373 "win16",
12374#endif
12375#ifdef WIN32
12376 "win32",
12377#endif
12378#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12379 "win32unix",
12380#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012381#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 "win64",
12383#endif
12384#ifdef EBCDIC
12385 "ebcdic",
12386#endif
12387#ifndef CASE_INSENSITIVE_FILENAME
12388 "fname_case",
12389#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012390#ifdef HAVE_ACL
12391 "acl",
12392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393#ifdef FEAT_ARABIC
12394 "arabic",
12395#endif
12396#ifdef FEAT_AUTOCMD
12397 "autocmd",
12398#endif
12399#ifdef FEAT_BEVAL
12400 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012401# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12402 "balloon_multiline",
12403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404#endif
12405#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12406 "builtin_terms",
12407# ifdef ALL_BUILTIN_TCAPS
12408 "all_builtin_terms",
12409# endif
12410#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012411#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12412 || defined(FEAT_GUI_W32) \
12413 || defined(FEAT_GUI_MOTIF))
12414 "browsefilter",
12415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416#ifdef FEAT_BYTEOFF
12417 "byte_offset",
12418#endif
12419#ifdef FEAT_CINDENT
12420 "cindent",
12421#endif
12422#ifdef FEAT_CLIENTSERVER
12423 "clientserver",
12424#endif
12425#ifdef FEAT_CLIPBOARD
12426 "clipboard",
12427#endif
12428#ifdef FEAT_CMDL_COMPL
12429 "cmdline_compl",
12430#endif
12431#ifdef FEAT_CMDHIST
12432 "cmdline_hist",
12433#endif
12434#ifdef FEAT_COMMENTS
12435 "comments",
12436#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012437#ifdef FEAT_CONCEAL
12438 "conceal",
12439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440#ifdef FEAT_CRYPT
12441 "cryptv",
12442#endif
12443#ifdef FEAT_CSCOPE
12444 "cscope",
12445#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012446#ifdef FEAT_CURSORBIND
12447 "cursorbind",
12448#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012449#ifdef CURSOR_SHAPE
12450 "cursorshape",
12451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452#ifdef DEBUG
12453 "debug",
12454#endif
12455#ifdef FEAT_CON_DIALOG
12456 "dialog_con",
12457#endif
12458#ifdef FEAT_GUI_DIALOG
12459 "dialog_gui",
12460#endif
12461#ifdef FEAT_DIFF
12462 "diff",
12463#endif
12464#ifdef FEAT_DIGRAPHS
12465 "digraphs",
12466#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012467#ifdef FEAT_DIRECTX
12468 "directx",
12469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470#ifdef FEAT_DND
12471 "dnd",
12472#endif
12473#ifdef FEAT_EMACS_TAGS
12474 "emacs_tags",
12475#endif
12476 "eval", /* always present, of course! */
12477#ifdef FEAT_EX_EXTRA
12478 "ex_extra",
12479#endif
12480#ifdef FEAT_SEARCH_EXTRA
12481 "extra_search",
12482#endif
12483#ifdef FEAT_FKMAP
12484 "farsi",
12485#endif
12486#ifdef FEAT_SEARCHPATH
12487 "file_in_path",
12488#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012489#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012490 "filterpipe",
12491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492#ifdef FEAT_FIND_ID
12493 "find_in_path",
12494#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012495#ifdef FEAT_FLOAT
12496 "float",
12497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498#ifdef FEAT_FOLDING
12499 "folding",
12500#endif
12501#ifdef FEAT_FOOTER
12502 "footer",
12503#endif
12504#if !defined(USE_SYSTEM) && defined(UNIX)
12505 "fork",
12506#endif
12507#ifdef FEAT_GETTEXT
12508 "gettext",
12509#endif
12510#ifdef FEAT_GUI
12511 "gui",
12512#endif
12513#ifdef FEAT_GUI_ATHENA
12514# ifdef FEAT_GUI_NEXTAW
12515 "gui_neXtaw",
12516# else
12517 "gui_athena",
12518# endif
12519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520#ifdef FEAT_GUI_GTK
12521 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012524#ifdef FEAT_GUI_GNOME
12525 "gui_gnome",
12526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527#ifdef FEAT_GUI_MAC
12528 "gui_mac",
12529#endif
12530#ifdef FEAT_GUI_MOTIF
12531 "gui_motif",
12532#endif
12533#ifdef FEAT_GUI_PHOTON
12534 "gui_photon",
12535#endif
12536#ifdef FEAT_GUI_W16
12537 "gui_win16",
12538#endif
12539#ifdef FEAT_GUI_W32
12540 "gui_win32",
12541#endif
12542#ifdef FEAT_HANGULIN
12543 "hangul_input",
12544#endif
12545#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12546 "iconv",
12547#endif
12548#ifdef FEAT_INS_EXPAND
12549 "insert_expand",
12550#endif
12551#ifdef FEAT_JUMPLIST
12552 "jumplist",
12553#endif
12554#ifdef FEAT_KEYMAP
12555 "keymap",
12556#endif
12557#ifdef FEAT_LANGMAP
12558 "langmap",
12559#endif
12560#ifdef FEAT_LIBCALL
12561 "libcall",
12562#endif
12563#ifdef FEAT_LINEBREAK
12564 "linebreak",
12565#endif
12566#ifdef FEAT_LISP
12567 "lispindent",
12568#endif
12569#ifdef FEAT_LISTCMDS
12570 "listcmds",
12571#endif
12572#ifdef FEAT_LOCALMAP
12573 "localmap",
12574#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012575#ifdef FEAT_LUA
12576# ifndef DYNAMIC_LUA
12577 "lua",
12578# endif
12579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580#ifdef FEAT_MENU
12581 "menu",
12582#endif
12583#ifdef FEAT_SESSION
12584 "mksession",
12585#endif
12586#ifdef FEAT_MODIFY_FNAME
12587 "modify_fname",
12588#endif
12589#ifdef FEAT_MOUSE
12590 "mouse",
12591#endif
12592#ifdef FEAT_MOUSESHAPE
12593 "mouseshape",
12594#endif
12595#if defined(UNIX) || defined(VMS)
12596# ifdef FEAT_MOUSE_DEC
12597 "mouse_dec",
12598# endif
12599# ifdef FEAT_MOUSE_GPM
12600 "mouse_gpm",
12601# endif
12602# ifdef FEAT_MOUSE_JSB
12603 "mouse_jsbterm",
12604# endif
12605# ifdef FEAT_MOUSE_NET
12606 "mouse_netterm",
12607# endif
12608# ifdef FEAT_MOUSE_PTERM
12609 "mouse_pterm",
12610# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012611# ifdef FEAT_MOUSE_SGR
12612 "mouse_sgr",
12613# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012614# ifdef FEAT_SYSMOUSE
12615 "mouse_sysmouse",
12616# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012617# ifdef FEAT_MOUSE_URXVT
12618 "mouse_urxvt",
12619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620# ifdef FEAT_MOUSE_XTERM
12621 "mouse_xterm",
12622# endif
12623#endif
12624#ifdef FEAT_MBYTE
12625 "multi_byte",
12626#endif
12627#ifdef FEAT_MBYTE_IME
12628 "multi_byte_ime",
12629#endif
12630#ifdef FEAT_MULTI_LANG
12631 "multi_lang",
12632#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012633#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012634#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012635 "mzscheme",
12636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638#ifdef FEAT_OLE
12639 "ole",
12640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef FEAT_PATH_EXTRA
12642 "path_extra",
12643#endif
12644#ifdef FEAT_PERL
12645#ifndef DYNAMIC_PERL
12646 "perl",
12647#endif
12648#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012649#ifdef FEAT_PERSISTENT_UNDO
12650 "persistent_undo",
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef FEAT_PYTHON
12653#ifndef DYNAMIC_PYTHON
12654 "python",
12655#endif
12656#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012657#ifdef FEAT_PYTHON3
12658#ifndef DYNAMIC_PYTHON3
12659 "python3",
12660#endif
12661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662#ifdef FEAT_POSTSCRIPT
12663 "postscript",
12664#endif
12665#ifdef FEAT_PRINTER
12666 "printer",
12667#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012668#ifdef FEAT_PROFILE
12669 "profile",
12670#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012671#ifdef FEAT_RELTIME
12672 "reltime",
12673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674#ifdef FEAT_QUICKFIX
12675 "quickfix",
12676#endif
12677#ifdef FEAT_RIGHTLEFT
12678 "rightleft",
12679#endif
12680#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12681 "ruby",
12682#endif
12683#ifdef FEAT_SCROLLBIND
12684 "scrollbind",
12685#endif
12686#ifdef FEAT_CMDL_INFO
12687 "showcmd",
12688 "cmdline_info",
12689#endif
12690#ifdef FEAT_SIGNS
12691 "signs",
12692#endif
12693#ifdef FEAT_SMARTINDENT
12694 "smartindent",
12695#endif
12696#ifdef FEAT_SNIFF
12697 "sniff",
12698#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012699#ifdef STARTUPTIME
12700 "startuptime",
12701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702#ifdef FEAT_STL_OPT
12703 "statusline",
12704#endif
12705#ifdef FEAT_SUN_WORKSHOP
12706 "sun_workshop",
12707#endif
12708#ifdef FEAT_NETBEANS_INTG
12709 "netbeans_intg",
12710#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012711#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012712 "spell",
12713#endif
12714#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 "syntax",
12716#endif
12717#if defined(USE_SYSTEM) || !defined(UNIX)
12718 "system",
12719#endif
12720#ifdef FEAT_TAG_BINS
12721 "tag_binary",
12722#endif
12723#ifdef FEAT_TAG_OLDSTATIC
12724 "tag_old_static",
12725#endif
12726#ifdef FEAT_TAG_ANYWHITE
12727 "tag_any_white",
12728#endif
12729#ifdef FEAT_TCL
12730# ifndef DYNAMIC_TCL
12731 "tcl",
12732# endif
12733#endif
12734#ifdef TERMINFO
12735 "terminfo",
12736#endif
12737#ifdef FEAT_TERMRESPONSE
12738 "termresponse",
12739#endif
12740#ifdef FEAT_TEXTOBJ
12741 "textobjects",
12742#endif
12743#ifdef HAVE_TGETENT
12744 "tgetent",
12745#endif
12746#ifdef FEAT_TITLE
12747 "title",
12748#endif
12749#ifdef FEAT_TOOLBAR
12750 "toolbar",
12751#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012752#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12753 "unnamedplus",
12754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#ifdef FEAT_USR_CMDS
12756 "user-commands", /* was accidentally included in 5.4 */
12757 "user_commands",
12758#endif
12759#ifdef FEAT_VIMINFO
12760 "viminfo",
12761#endif
12762#ifdef FEAT_VERTSPLIT
12763 "vertsplit",
12764#endif
12765#ifdef FEAT_VIRTUALEDIT
12766 "virtualedit",
12767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#ifdef FEAT_VISUALEXTRA
12770 "visualextra",
12771#endif
12772#ifdef FEAT_VREPLACE
12773 "vreplace",
12774#endif
12775#ifdef FEAT_WILDIGN
12776 "wildignore",
12777#endif
12778#ifdef FEAT_WILDMENU
12779 "wildmenu",
12780#endif
12781#ifdef FEAT_WINDOWS
12782 "windows",
12783#endif
12784#ifdef FEAT_WAK
12785 "winaltkeys",
12786#endif
12787#ifdef FEAT_WRITEBACKUP
12788 "writebackup",
12789#endif
12790#ifdef FEAT_XIM
12791 "xim",
12792#endif
12793#ifdef FEAT_XFONTSET
12794 "xfontset",
12795#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012796#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012797 "xpm",
12798 "xpm_w32", /* for backward compatibility */
12799#else
12800# if defined(HAVE_XPM)
12801 "xpm",
12802# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804#ifdef USE_XSMP
12805 "xsmp",
12806#endif
12807#ifdef USE_XSMP_INTERACT
12808 "xsmp_interact",
12809#endif
12810#ifdef FEAT_XCLIPBOARD
12811 "xterm_clipboard",
12812#endif
12813#ifdef FEAT_XTERM_SAVE
12814 "xterm_save",
12815#endif
12816#if defined(UNIX) && defined(FEAT_X11)
12817 "X11",
12818#endif
12819 NULL
12820 };
12821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 for (i = 0; has_list[i] != NULL; ++i)
12824 if (STRICMP(name, has_list[i]) == 0)
12825 {
12826 n = TRUE;
12827 break;
12828 }
12829
12830 if (n == FALSE)
12831 {
12832 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012833 {
12834 if (name[5] == '-'
12835 && STRLEN(name) > 11
12836 && vim_isdigit(name[6])
12837 && vim_isdigit(name[8])
12838 && vim_isdigit(name[10]))
12839 {
12840 int major = atoi((char *)name + 6);
12841 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012842
12843 /* Expect "patch-9.9.01234". */
12844 n = (major < VIM_VERSION_MAJOR
12845 || (major == VIM_VERSION_MAJOR
12846 && (minor < VIM_VERSION_MINOR
12847 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012848 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012849 }
12850 else
12851 n = has_patch(atoi((char *)name + 5));
12852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 else if (STRICMP(name, "vim_starting") == 0)
12854 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012855#ifdef FEAT_MBYTE
12856 else if (STRICMP(name, "multi_byte_encoding") == 0)
12857 n = has_mbyte;
12858#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012859#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12860 else if (STRICMP(name, "balloon_multiline") == 0)
12861 n = multiline_balloon_available();
12862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863#ifdef DYNAMIC_TCL
12864 else if (STRICMP(name, "tcl") == 0)
12865 n = tcl_enabled(FALSE);
12866#endif
12867#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12868 else if (STRICMP(name, "iconv") == 0)
12869 n = iconv_enabled(FALSE);
12870#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012871#ifdef DYNAMIC_LUA
12872 else if (STRICMP(name, "lua") == 0)
12873 n = lua_enabled(FALSE);
12874#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012875#ifdef DYNAMIC_MZSCHEME
12876 else if (STRICMP(name, "mzscheme") == 0)
12877 n = mzscheme_enabled(FALSE);
12878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#ifdef DYNAMIC_RUBY
12880 else if (STRICMP(name, "ruby") == 0)
12881 n = ruby_enabled(FALSE);
12882#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012883#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884#ifdef DYNAMIC_PYTHON
12885 else if (STRICMP(name, "python") == 0)
12886 n = python_enabled(FALSE);
12887#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012888#endif
12889#ifdef FEAT_PYTHON3
12890#ifdef DYNAMIC_PYTHON3
12891 else if (STRICMP(name, "python3") == 0)
12892 n = python3_enabled(FALSE);
12893#endif
12894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895#ifdef DYNAMIC_PERL
12896 else if (STRICMP(name, "perl") == 0)
12897 n = perl_enabled(FALSE);
12898#endif
12899#ifdef FEAT_GUI
12900 else if (STRICMP(name, "gui_running") == 0)
12901 n = (gui.in_use || gui.starting);
12902# ifdef FEAT_GUI_W32
12903 else if (STRICMP(name, "gui_win32s") == 0)
12904 n = gui_is_win32s();
12905# endif
12906# ifdef FEAT_BROWSE
12907 else if (STRICMP(name, "browse") == 0)
12908 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12909# endif
12910#endif
12911#ifdef FEAT_SYN_HL
12912 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012913 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914#endif
12915#if defined(WIN3264)
12916 else if (STRICMP(name, "win95") == 0)
12917 n = mch_windows95();
12918#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012919#ifdef FEAT_NETBEANS_INTG
12920 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012921 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 }
12924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926}
12927
12928/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012929 * "has_key()" function
12930 */
12931 static void
12932f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012933 typval_T *argvars;
12934 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012935{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012936 if (argvars[0].v_type != VAR_DICT)
12937 {
12938 EMSG(_(e_dictreq));
12939 return;
12940 }
12941 if (argvars[0].vval.v_dict == NULL)
12942 return;
12943
12944 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012945 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012946}
12947
12948/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012949 * "haslocaldir()" function
12950 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012951 static void
12952f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012953 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012954 typval_T *rettv;
12955{
12956 rettv->vval.v_number = (curwin->w_localdir != NULL);
12957}
12958
12959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 * "hasmapto()" function
12961 */
12962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012964 typval_T *argvars;
12965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966{
12967 char_u *name;
12968 char_u *mode;
12969 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012970 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012973 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 mode = (char_u *)"nvo";
12975 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012978 if (argvars[2].v_type != VAR_UNKNOWN)
12979 abbr = get_tv_number(&argvars[2]);
12980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981
Bram Moolenaar2c932302006-03-18 21:42:09 +000012982 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986}
12987
12988/*
12989 * "histadd()" function
12990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012993 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996#ifdef FEAT_CMDHIST
12997 int histype;
12998 char_u *str;
12999 char_u buf[NUMBUFLEN];
13000#endif
13001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 if (check_restricted() || check_secure())
13004 return;
13005#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13007 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 if (histype >= 0)
13009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 if (*str != NUL)
13012 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013013 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013015 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 return;
13017 }
13018 }
13019#endif
13020}
13021
13022/*
13023 * "histdel()" function
13024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013027 typval_T *argvars UNUSED;
13028 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029{
13030#ifdef FEAT_CMDHIST
13031 int n;
13032 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13036 if (str == NULL)
13037 n = 0;
13038 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013041 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 else
13046 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 get_tv_string_buf(&argvars[1], buf));
13049 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050#endif
13051}
13052
13053/*
13054 * "histget()" function
13055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060{
13061#ifdef FEAT_CMDHIST
13062 int type;
13063 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13067 if (str == NULL)
13068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 {
13071 type = get_histtype(str);
13072 if (argvars[1].v_type == VAR_UNKNOWN)
13073 idx = get_history_idx(type);
13074 else
13075 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13076 /* -1 on type error */
13077 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083}
13084
13085/*
13086 * "histnr()" function
13087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092{
13093 int i;
13094
13095#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 char_u *history = get_tv_string_chk(&argvars[0]);
13097
13098 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 if (i >= HIST_CMD && i < HIST_COUNT)
13100 i = get_history_idx(i);
13101 else
13102#endif
13103 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105}
13106
13107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 * "highlightID(name)" function
13109 */
13110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013112 typval_T *argvars;
13113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116}
13117
13118/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013119 * "highlight_exists()" function
13120 */
13121 static void
13122f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013123 typval_T *argvars;
13124 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013125{
13126 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13127}
13128
13129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 * "hostname()" function
13131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136{
13137 char_u hostname[256];
13138
13139 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 rettv->v_type = VAR_STRING;
13141 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142}
13143
13144/*
13145 * iconv() function
13146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013149 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151{
13152#ifdef FEAT_MBYTE
13153 char_u buf1[NUMBUFLEN];
13154 char_u buf2[NUMBUFLEN];
13155 char_u *from, *to, *str;
13156 vimconv_T vimconv;
13157#endif
13158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 rettv->v_type = VAR_STRING;
13160 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161
13162#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 str = get_tv_string(&argvars[0]);
13164 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13165 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 vimconv.vc_type = CONV_NONE;
13167 convert_setup(&vimconv, from, to);
13168
13169 /* If the encodings are equal, no conversion needed. */
13170 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174
13175 convert_setup(&vimconv, NULL, NULL);
13176 vim_free(from);
13177 vim_free(to);
13178#endif
13179}
13180
13181/*
13182 * "indent()" function
13183 */
13184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013186 typval_T *argvars;
13187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188{
13189 linenr_T lnum;
13190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013198/*
13199 * "index()" function
13200 */
13201 static void
13202f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013205{
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 list_T *l;
13207 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013208 long idx = 0;
13209 int ic = FALSE;
13210
13211 rettv->vval.v_number = -1;
13212 if (argvars[0].v_type != VAR_LIST)
13213 {
13214 EMSG(_(e_listreq));
13215 return;
13216 }
13217 l = argvars[0].vval.v_list;
13218 if (l != NULL)
13219 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013220 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 int error = FALSE;
13224
Bram Moolenaar758711c2005-02-02 23:11:38 +000013225 /* Start at specified item. Use the cached index that list_find()
13226 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013228 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013229 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 ic = get_tv_number_chk(&argvars[3], &error);
13231 if (error)
13232 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013233 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234
Bram Moolenaar758711c2005-02-02 23:11:38 +000013235 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013236 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013237 {
13238 rettv->vval.v_number = idx;
13239 break;
13240 }
13241 }
13242}
13243
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244static int inputsecret_flag = 0;
13245
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013246static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13247
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013249 * This function is used by f_input() and f_inputdialog() functions. The third
13250 * argument to f_input() specifies the type of completion to use at the
13251 * prompt. The third argument to f_inputdialog() specifies the value to return
13252 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 */
13254 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013255get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *argvars;
13257 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013258 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 char_u *p = NULL;
13262 int c;
13263 char_u buf[NUMBUFLEN];
13264 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013266 int xp_type = EXPAND_NOTHING;
13267 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013270 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271
13272#ifdef NO_CONSOLE_INPUT
13273 /* While starting up, there is no place to enter text. */
13274 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#endif
13277
13278 cmd_silent = FALSE; /* Want to see the prompt. */
13279 if (prompt != NULL)
13280 {
13281 /* Only the part of the message after the last NL is considered as
13282 * prompt for the command line */
13283 p = vim_strrchr(prompt, '\n');
13284 if (p == NULL)
13285 p = prompt;
13286 else
13287 {
13288 ++p;
13289 c = *p;
13290 *p = NUL;
13291 msg_start();
13292 msg_clr_eos();
13293 msg_puts_attr(prompt, echo_attr);
13294 msg_didout = FALSE;
13295 msg_starthere();
13296 *p = c;
13297 }
13298 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013300 if (argvars[1].v_type != VAR_UNKNOWN)
13301 {
13302 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13303 if (defstr != NULL)
13304 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013306 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013307 {
13308 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013309 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013310 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013311
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013312 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013313 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013314
Bram Moolenaar4463f292005-09-25 22:20:24 +000013315 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13316 if (xp_name == NULL)
13317 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013319 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013320
Bram Moolenaar4463f292005-09-25 22:20:24 +000013321 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13322 &xp_arg) == FAIL)
13323 return;
13324 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013325 }
13326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013328 {
13329# ifdef FEAT_EX_EXTRA
13330 int save_ex_normal_busy = ex_normal_busy;
13331 ex_normal_busy = 0;
13332# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013333 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013334 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13335 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013336# ifdef FEAT_EX_EXTRA
13337 ex_normal_busy = save_ex_normal_busy;
13338# endif
13339 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013340 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013341 && argvars[1].v_type != VAR_UNKNOWN
13342 && argvars[2].v_type != VAR_UNKNOWN)
13343 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13344 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013345
13346 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 /* since the user typed this, no need to wait for return */
13349 need_wait_return = FALSE;
13350 msg_didout = FALSE;
13351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 cmd_silent = cmd_silent_save;
13353}
13354
13355/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013356 * "input()" function
13357 * Also handles inputsecret() when inputsecret is set.
13358 */
13359 static void
13360f_input(argvars, rettv)
13361 typval_T *argvars;
13362 typval_T *rettv;
13363{
13364 get_user_input(argvars, rettv, FALSE);
13365}
13366
13367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 * "inputdialog()" function
13369 */
13370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 typval_T *argvars;
13373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374{
13375#if defined(FEAT_GUI_TEXTDIALOG)
13376 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13377 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13378 {
13379 char_u *message;
13380 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 message = get_tv_string_chk(&argvars[0]);
13384 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013385 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013386 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 else
13388 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 if (message != NULL && defstr != NULL
13390 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013391 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 else
13394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013395 if (message != NULL && defstr != NULL
13396 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013397 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->vval.v_string = vim_strsave(
13399 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 }
13405 else
13406#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013407 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408}
13409
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013410/*
13411 * "inputlist()" function
13412 */
13413 static void
13414f_inputlist(argvars, rettv)
13415 typval_T *argvars;
13416 typval_T *rettv;
13417{
13418 listitem_T *li;
13419 int selected;
13420 int mouse_used;
13421
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013422#ifdef NO_CONSOLE_INPUT
13423 /* While starting up, there is no place to enter text. */
13424 if (no_console_input())
13425 return;
13426#endif
13427 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13428 {
13429 EMSG2(_(e_listarg), "inputlist()");
13430 return;
13431 }
13432
13433 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013434 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013435 lines_left = Rows; /* avoid more prompt */
13436 msg_scroll = TRUE;
13437 msg_clr_eos();
13438
13439 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13440 {
13441 msg_puts(get_tv_string(&li->li_tv));
13442 msg_putchar('\n');
13443 }
13444
13445 /* Ask for choice. */
13446 selected = prompt_for_number(&mouse_used);
13447 if (mouse_used)
13448 selected -= lines_left;
13449
13450 rettv->vval.v_number = selected;
13451}
13452
13453
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13455
13456/*
13457 * "inputrestore()" function
13458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463{
13464 if (ga_userinput.ga_len > 0)
13465 {
13466 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13468 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013469 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 }
13471 else if (p_verbose > 1)
13472 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013473 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 }
13476}
13477
13478/*
13479 * "inputsave()" function
13480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013483 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013486 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 if (ga_grow(&ga_userinput, 1) == OK)
13488 {
13489 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13490 + ga_userinput.ga_len);
13491 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013492 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 }
13494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013495 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496}
13497
13498/*
13499 * "inputsecret()" function
13500 */
13501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *argvars;
13504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505{
13506 ++cmdline_star;
13507 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 --cmdline_star;
13510 --inputsecret_flag;
13511}
13512
13513/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 * "insert()" function
13515 */
13516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 typval_T *argvars;
13519 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520{
13521 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013522 listitem_T *item;
13523 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013525
13526 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013527 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013528 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013529 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 {
13531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 before = get_tv_number_chk(&argvars[2], &error);
13533 if (error)
13534 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535
Bram Moolenaar758711c2005-02-02 23:11:38 +000013536 if (before == l->lv_len)
13537 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013538 else
13539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013540 item = list_find(l, before);
13541 if (item == NULL)
13542 {
13543 EMSGN(_(e_listidx), before);
13544 l = NULL;
13545 }
13546 }
13547 if (l != NULL)
13548 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013549 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013550 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551 }
13552 }
13553}
13554
13555/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013556 * "invert(expr)" function
13557 */
13558 static void
13559f_invert(argvars, rettv)
13560 typval_T *argvars;
13561 typval_T *rettv;
13562{
13563 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13564}
13565
13566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 * "isdirectory()" function
13568 */
13569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575}
13576
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013577/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013578 * "islocked()" function
13579 */
13580 static void
13581f_islocked(argvars, rettv)
13582 typval_T *argvars;
13583 typval_T *rettv;
13584{
13585 lval_T lv;
13586 char_u *end;
13587 dictitem_T *di;
13588
13589 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013590 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13591 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013592 if (end != NULL && lv.ll_name != NULL)
13593 {
13594 if (*end != NUL)
13595 EMSG(_(e_trailing));
13596 else
13597 {
13598 if (lv.ll_tv == NULL)
13599 {
13600 if (check_changedtick(lv.ll_name))
13601 rettv->vval.v_number = 1; /* always locked */
13602 else
13603 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013604 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013605 if (di != NULL)
13606 {
13607 /* Consider a variable locked when:
13608 * 1. the variable itself is locked
13609 * 2. the value of the variable is locked.
13610 * 3. the List or Dict value is locked.
13611 */
13612 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13613 || tv_islocked(&di->di_tv));
13614 }
13615 }
13616 }
13617 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013618 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013619 else if (lv.ll_newkey != NULL)
13620 EMSG2(_(e_dictkey), lv.ll_newkey);
13621 else if (lv.ll_list != NULL)
13622 /* List item. */
13623 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13624 else
13625 /* Dictionary item. */
13626 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13627 }
13628 }
13629
13630 clear_lval(&lv);
13631}
13632
Bram Moolenaar33570922005-01-25 22:26:29 +000013633static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013634
13635/*
13636 * Turn a dict into a list:
13637 * "what" == 0: list of keys
13638 * "what" == 1: list of values
13639 * "what" == 2: list of items
13640 */
13641 static void
13642dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013645 int what;
13646{
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 list_T *l2;
13648 dictitem_T *di;
13649 hashitem_T *hi;
13650 listitem_T *li;
13651 listitem_T *li2;
13652 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013653 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013654
Bram Moolenaar8c711452005-01-14 21:53:12 +000013655 if (argvars[0].v_type != VAR_DICT)
13656 {
13657 EMSG(_(e_dictreq));
13658 return;
13659 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013660 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013661 return;
13662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013663 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013664 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013665
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013666 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013671 --todo;
13672 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013673
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013674 li = listitem_alloc();
13675 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013676 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013677 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013678
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013679 if (what == 0)
13680 {
13681 /* keys() */
13682 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013683 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013684 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13685 }
13686 else if (what == 1)
13687 {
13688 /* values() */
13689 copy_tv(&di->di_tv, &li->li_tv);
13690 }
13691 else
13692 {
13693 /* items() */
13694 l2 = list_alloc();
13695 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013696 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013697 li->li_tv.vval.v_list = l2;
13698 if (l2 == NULL)
13699 break;
13700 ++l2->lv_refcount;
13701
13702 li2 = listitem_alloc();
13703 if (li2 == NULL)
13704 break;
13705 list_append(l2, li2);
13706 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013707 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013708 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13709
13710 li2 = listitem_alloc();
13711 if (li2 == NULL)
13712 break;
13713 list_append(l2, li2);
13714 copy_tv(&di->di_tv, &li2->li_tv);
13715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013716 }
13717 }
13718}
13719
13720/*
13721 * "items(dict)" function
13722 */
13723 static void
13724f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *argvars;
13726 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013727{
13728 dict_list(argvars, rettv, 2);
13729}
13730
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013732 * "join()" function
13733 */
13734 static void
13735f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 typval_T *argvars;
13737 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013738{
13739 garray_T ga;
13740 char_u *sep;
13741
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013742 if (argvars[0].v_type != VAR_LIST)
13743 {
13744 EMSG(_(e_listreq));
13745 return;
13746 }
13747 if (argvars[0].vval.v_list == NULL)
13748 return;
13749 if (argvars[1].v_type == VAR_UNKNOWN)
13750 sep = (char_u *)" ";
13751 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013753
13754 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755
13756 if (sep != NULL)
13757 {
13758 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013759 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 ga_append(&ga, NUL);
13761 rettv->vval.v_string = (char_u *)ga.ga_data;
13762 }
13763 else
13764 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013765}
13766
13767/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013768 * "keys()" function
13769 */
13770 static void
13771f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013772 typval_T *argvars;
13773 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013774{
13775 dict_list(argvars, rettv, 0);
13776}
13777
13778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 * "last_buffer_nr()" function.
13780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785{
13786 int n = 0;
13787 buf_T *buf;
13788
13789 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13790 if (n < buf->b_fnum)
13791 n = buf->b_fnum;
13792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013794}
13795
13796/*
13797 * "len()" function
13798 */
13799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013803{
13804 switch (argvars[0].v_type)
13805 {
13806 case VAR_STRING:
13807 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808 rettv->vval.v_number = (varnumber_T)STRLEN(
13809 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013810 break;
13811 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013813 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013814 case VAR_DICT:
13815 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13816 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013817 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013818 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013819 break;
13820 }
13821}
13822
Bram Moolenaar33570922005-01-25 22:26:29 +000013823static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013824
13825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 typval_T *argvars;
13828 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013829 int type;
13830{
13831#ifdef FEAT_LIBCALL
13832 char_u *string_in;
13833 char_u **string_result;
13834 int nr_result;
13835#endif
13836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013838 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013840
13841 if (check_restricted() || check_secure())
13842 return;
13843
13844#ifdef FEAT_LIBCALL
13845 /* The first two args must be strings, otherwise its meaningless */
13846 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13847 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013848 string_in = NULL;
13849 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013850 string_in = argvars[2].vval.v_string;
13851 if (type == VAR_NUMBER)
13852 string_result = NULL;
13853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013855 if (mch_libcall(argvars[0].vval.v_string,
13856 argvars[1].vval.v_string,
13857 string_in,
13858 argvars[2].vval.v_number,
13859 string_result,
13860 &nr_result) == OK
13861 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013863 }
13864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865}
13866
13867/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013868 * "libcall()" function
13869 */
13870 static void
13871f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T *argvars;
13873 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874{
13875 libcall_common(argvars, rettv, VAR_STRING);
13876}
13877
13878/*
13879 * "libcallnr()" function
13880 */
13881 static void
13882f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 typval_T *argvars;
13884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885{
13886 libcall_common(argvars, rettv, VAR_NUMBER);
13887}
13888
13889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 * "line(string)" function
13891 */
13892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013894 typval_T *argvars;
13895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896{
13897 linenr_T lnum = 0;
13898 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013899 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013901 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 if (fp != NULL)
13903 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905}
13906
13907/*
13908 * "line2byte(lnum)" function
13909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013911f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013912 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914{
13915#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917#else
13918 linenr_T lnum;
13919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13925 if (rettv->vval.v_number >= 0)
13926 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927#endif
13928}
13929
13930/*
13931 * "lispindent(lnum)" function
13932 */
13933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
13938#ifdef FEAT_LISP
13939 pos_T pos;
13940 linenr_T lnum;
13941
13942 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13945 {
13946 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 curwin->w_cursor = pos;
13949 }
13950 else
13951#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953}
13954
13955/*
13956 * "localtime()" function
13957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964}
13965
Bram Moolenaar33570922005-01-25 22:26:29 +000013966static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967
13968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013970 typval_T *argvars;
13971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 int exact;
13973{
13974 char_u *keys;
13975 char_u *which;
13976 char_u buf[NUMBUFLEN];
13977 char_u *keys_buf = NULL;
13978 char_u *rhs;
13979 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013980 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013981 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013982 mapblock_T *mp;
13983 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984
13985 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986 rettv->v_type = VAR_STRING;
13987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 if (*keys == NUL)
13991 return;
13992
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013993 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013997 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013998 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013999 if (argvars[3].v_type != VAR_UNKNOWN)
14000 get_dict = get_tv_number(&argvars[3]);
14001 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 else
14004 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 if (which == NULL)
14006 return;
14007
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 mode = get_map_mode(&which, 0);
14009
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014010 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014011 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014013
14014 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014016 /* Return a string. */
14017 if (rhs != NULL)
14018 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019
Bram Moolenaarbd743252010-10-20 21:23:33 +020014020 }
14021 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14022 {
14023 /* Return a dictionary. */
14024 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14025 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14026 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027
Bram Moolenaarbd743252010-10-20 21:23:33 +020014028 dict_add_nr_str(dict, "lhs", 0L, lhs);
14029 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14030 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14031 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14032 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14033 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14034 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014035 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014036 dict_add_nr_str(dict, "mode", 0L, mapmode);
14037
14038 vim_free(lhs);
14039 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 }
14041}
14042
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014043#ifdef FEAT_FLOAT
14044/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014045 * "log()" function
14046 */
14047 static void
14048f_log(argvars, rettv)
14049 typval_T *argvars;
14050 typval_T *rettv;
14051{
14052 float_T f;
14053
14054 rettv->v_type = VAR_FLOAT;
14055 if (get_float_arg(argvars, &f) == OK)
14056 rettv->vval.v_float = log(f);
14057 else
14058 rettv->vval.v_float = 0.0;
14059}
14060
14061/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014062 * "log10()" function
14063 */
14064 static void
14065f_log10(argvars, rettv)
14066 typval_T *argvars;
14067 typval_T *rettv;
14068{
14069 float_T f;
14070
14071 rettv->v_type = VAR_FLOAT;
14072 if (get_float_arg(argvars, &f) == OK)
14073 rettv->vval.v_float = log10(f);
14074 else
14075 rettv->vval.v_float = 0.0;
14076}
14077#endif
14078
Bram Moolenaar1dced572012-04-05 16:54:08 +020014079#ifdef FEAT_LUA
14080/*
14081 * "luaeval()" function
14082 */
14083 static void
14084f_luaeval(argvars, rettv)
14085 typval_T *argvars;
14086 typval_T *rettv;
14087{
14088 char_u *str;
14089 char_u buf[NUMBUFLEN];
14090
14091 str = get_tv_string_buf(&argvars[0], buf);
14092 do_luaeval(str, argvars + 1, rettv);
14093}
14094#endif
14095
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014097 * "map()" function
14098 */
14099 static void
14100f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 typval_T *argvars;
14102 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014103{
14104 filter_map(argvars, rettv, TRUE);
14105}
14106
14107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 */
14110 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116}
14117
14118/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 */
14121 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014122f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014123 typval_T *argvars;
14124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014126 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
Bram Moolenaar33570922005-01-25 22:26:29 +000014129static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130
14131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 int type;
14136{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014137 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014138 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014139 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 char_u *pat;
14141 regmatch_T regmatch;
14142 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014143 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 char_u *save_cpo;
14145 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014146 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014147 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014148 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014149 list_T *l = NULL;
14150 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014151 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014152 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153
14154 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14155 save_cpo = p_cpo;
14156 p_cpo = (char_u *)"";
14157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014158 rettv->vval.v_number = -1;
14159 if (type == 3)
14160 {
14161 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014162 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014163 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014164 }
14165 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167 rettv->v_type = VAR_STRING;
14168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014171 if (argvars[0].v_type == VAR_LIST)
14172 {
14173 if ((l = argvars[0].vval.v_list) == NULL)
14174 goto theend;
14175 li = l->lv_first;
14176 }
14177 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014178 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014179 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014180 len = (long)STRLEN(str);
14181 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14184 if (pat == NULL)
14185 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014186
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 int error = FALSE;
14190
14191 start = get_tv_number_chk(&argvars[2], &error);
14192 if (error)
14193 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014194 if (l != NULL)
14195 {
14196 li = list_find(l, start);
14197 if (li == NULL)
14198 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014199 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014200 }
14201 else
14202 {
14203 if (start < 0)
14204 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014205 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014206 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014207 /* When "count" argument is there ignore matches before "start",
14208 * otherwise skip part of the string. Differs when pattern is "^"
14209 * or "\<". */
14210 if (argvars[3].v_type != VAR_UNKNOWN)
14211 startcol = start;
14212 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014213 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014214 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014215 len -= start;
14216 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014217 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014219 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 nth = get_tv_number_chk(&argvars[3], &error);
14221 if (error)
14222 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 }
14224
14225 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14226 if (regmatch.regprog != NULL)
14227 {
14228 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014229
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014230 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014231 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014232 if (l != NULL)
14233 {
14234 if (li == NULL)
14235 {
14236 match = FALSE;
14237 break;
14238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014239 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014240 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014241 if (str == NULL)
14242 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014243 }
14244
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014245 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014246
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014247 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014248 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014249 if (l == NULL && !match)
14250 break;
14251
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014252 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014253 if (l != NULL)
14254 {
14255 li = li->li_next;
14256 ++idx;
14257 }
14258 else
14259 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014260#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014261 startcol = (colnr_T)(regmatch.startp[0]
14262 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014263#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014264 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014265#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014266 if (startcol > (colnr_T)len
14267 || str + startcol <= regmatch.startp[0])
14268 {
14269 match = FALSE;
14270 break;
14271 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014272 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014273 }
14274
14275 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014277 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014279 int i;
14280
14281 /* return list with matched string and submatches */
14282 for (i = 0; i < NSUBEXP; ++i)
14283 {
14284 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014285 {
14286 if (list_append_string(rettv->vval.v_list,
14287 (char_u *)"", 0) == FAIL)
14288 break;
14289 }
14290 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014291 regmatch.startp[i],
14292 (int)(regmatch.endp[i] - regmatch.startp[i]))
14293 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014294 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014295 }
14296 }
14297 else if (type == 2)
14298 {
14299 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014300 if (l != NULL)
14301 copy_tv(&li->li_tv, rettv);
14302 else
14303 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014305 }
14306 else if (l != NULL)
14307 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 else
14309 {
14310 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 (varnumber_T)(regmatch.startp[0] - str);
14313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014316 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 }
14318 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014319 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 }
14321
14322theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014323 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 p_cpo = save_cpo;
14325}
14326
14327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014328 * "match()" function
14329 */
14330 static void
14331f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 typval_T *argvars;
14333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014334{
14335 find_some_match(argvars, rettv, 1);
14336}
14337
14338/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014339 * "matchadd()" function
14340 */
14341 static void
14342f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014343 typval_T *argvars UNUSED;
14344 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014345{
14346#ifdef FEAT_SEARCH_EXTRA
14347 char_u buf[NUMBUFLEN];
14348 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14349 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14350 int prio = 10; /* default priority */
14351 int id = -1;
14352 int error = FALSE;
14353
14354 rettv->vval.v_number = -1;
14355
14356 if (grp == NULL || pat == NULL)
14357 return;
14358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014359 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014360 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014361 if (argvars[3].v_type != VAR_UNKNOWN)
14362 id = get_tv_number_chk(&argvars[3], &error);
14363 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014364 if (error == TRUE)
14365 return;
14366 if (id >= 1 && id <= 3)
14367 {
14368 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14369 return;
14370 }
14371
Bram Moolenaarb3414592014-06-17 17:48:32 +020014372 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14373#endif
14374}
14375
14376/*
14377 * "matchaddpos()" function
14378 */
14379 static void
14380f_matchaddpos(argvars, rettv)
14381 typval_T *argvars UNUSED;
14382 typval_T *rettv UNUSED;
14383{
14384#ifdef FEAT_SEARCH_EXTRA
14385 char_u buf[NUMBUFLEN];
14386 char_u *group;
14387 int prio = 10;
14388 int id = -1;
14389 int error = FALSE;
14390 list_T *l;
14391
14392 rettv->vval.v_number = -1;
14393
14394 group = get_tv_string_buf_chk(&argvars[0], buf);
14395 if (group == NULL)
14396 return;
14397
14398 if (argvars[1].v_type != VAR_LIST)
14399 {
14400 EMSG2(_(e_listarg), "matchaddpos()");
14401 return;
14402 }
14403 l = argvars[1].vval.v_list;
14404 if (l == NULL)
14405 return;
14406
14407 if (argvars[2].v_type != VAR_UNKNOWN)
14408 {
14409 prio = get_tv_number_chk(&argvars[2], &error);
14410 if (argvars[3].v_type != VAR_UNKNOWN)
14411 id = get_tv_number_chk(&argvars[3], &error);
14412 }
14413 if (error == TRUE)
14414 return;
14415
14416 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14417 if (id == 1 || id == 2)
14418 {
14419 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14420 return;
14421 }
14422
14423 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014424#endif
14425}
14426
14427/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014428 * "matcharg()" function
14429 */
14430 static void
14431f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014432 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014433 typval_T *rettv;
14434{
14435 if (rettv_list_alloc(rettv) == OK)
14436 {
14437#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014438 int id = get_tv_number(&argvars[0]);
14439 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014440
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014441 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014443 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14444 {
14445 list_append_string(rettv->vval.v_list,
14446 syn_id2name(m->hlg_id), -1);
14447 list_append_string(rettv->vval.v_list, m->pattern, -1);
14448 }
14449 else
14450 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014451 list_append_string(rettv->vval.v_list, NULL, -1);
14452 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014453 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014454 }
14455#endif
14456 }
14457}
14458
14459/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014460 * "matchdelete()" function
14461 */
14462 static void
14463f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014464 typval_T *argvars UNUSED;
14465 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014466{
14467#ifdef FEAT_SEARCH_EXTRA
14468 rettv->vval.v_number = match_delete(curwin,
14469 (int)get_tv_number(&argvars[0]), TRUE);
14470#endif
14471}
14472
14473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 * "matchend()" function
14475 */
14476 static void
14477f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *argvars;
14479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480{
14481 find_some_match(argvars, rettv, 0);
14482}
14483
14484/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014485 * "matchlist()" function
14486 */
14487 static void
14488f_matchlist(argvars, rettv)
14489 typval_T *argvars;
14490 typval_T *rettv;
14491{
14492 find_some_match(argvars, rettv, 3);
14493}
14494
14495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 * "matchstr()" function
14497 */
14498 static void
14499f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *argvars;
14501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502{
14503 find_some_match(argvars, rettv, 2);
14504}
14505
Bram Moolenaar33570922005-01-25 22:26:29 +000014506static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014507
14508 static void
14509max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 typval_T *argvars;
14511 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014512 int domax;
14513{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014514 long n = 0;
14515 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014516 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014517
14518 if (argvars[0].v_type == VAR_LIST)
14519 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014520 list_T *l;
14521 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014522
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014523 l = argvars[0].vval.v_list;
14524 if (l != NULL)
14525 {
14526 li = l->lv_first;
14527 if (li != NULL)
14528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014529 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014530 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014531 {
14532 li = li->li_next;
14533 if (li == NULL)
14534 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014536 if (domax ? i > n : i < n)
14537 n = i;
14538 }
14539 }
14540 }
14541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014542 else if (argvars[0].v_type == VAR_DICT)
14543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014545 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014547 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014548
14549 d = argvars[0].vval.v_dict;
14550 if (d != NULL)
14551 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014552 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014553 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014554 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014555 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014557 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014559 if (first)
14560 {
14561 n = i;
14562 first = FALSE;
14563 }
14564 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014565 n = i;
14566 }
14567 }
14568 }
14569 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014570 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014571 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014573}
14574
14575/*
14576 * "max()" function
14577 */
14578 static void
14579f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 typval_T *argvars;
14581 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014582{
14583 max_min(argvars, rettv, TRUE);
14584}
14585
14586/*
14587 * "min()" function
14588 */
14589 static void
14590f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *argvars;
14592 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014593{
14594 max_min(argvars, rettv, FALSE);
14595}
14596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014597static int mkdir_recurse __ARGS((char_u *dir, int prot));
14598
14599/*
14600 * Create the directory in which "dir" is located, and higher levels when
14601 * needed.
14602 */
14603 static int
14604mkdir_recurse(dir, prot)
14605 char_u *dir;
14606 int prot;
14607{
14608 char_u *p;
14609 char_u *updir;
14610 int r = FAIL;
14611
14612 /* Get end of directory name in "dir".
14613 * We're done when it's "/" or "c:/". */
14614 p = gettail_sep(dir);
14615 if (p <= get_past_head(dir))
14616 return OK;
14617
14618 /* If the directory exists we're done. Otherwise: create it.*/
14619 updir = vim_strnsave(dir, (int)(p - dir));
14620 if (updir == NULL)
14621 return FAIL;
14622 if (mch_isdir(updir))
14623 r = OK;
14624 else if (mkdir_recurse(updir, prot) == OK)
14625 r = vim_mkdir_emsg(updir, prot);
14626 vim_free(updir);
14627 return r;
14628}
14629
14630#ifdef vim_mkdir
14631/*
14632 * "mkdir()" function
14633 */
14634 static void
14635f_mkdir(argvars, rettv)
14636 typval_T *argvars;
14637 typval_T *rettv;
14638{
14639 char_u *dir;
14640 char_u buf[NUMBUFLEN];
14641 int prot = 0755;
14642
14643 rettv->vval.v_number = FAIL;
14644 if (check_restricted() || check_secure())
14645 return;
14646
14647 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014648 if (*dir == NUL)
14649 rettv->vval.v_number = FAIL;
14650 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014651 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014652 if (*gettail(dir) == NUL)
14653 /* remove trailing slashes */
14654 *gettail_sep(dir) = NUL;
14655
14656 if (argvars[1].v_type != VAR_UNKNOWN)
14657 {
14658 if (argvars[2].v_type != VAR_UNKNOWN)
14659 prot = get_tv_number_chk(&argvars[2], NULL);
14660 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14661 mkdir_recurse(dir, prot);
14662 }
14663 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014664 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014665}
14666#endif
14667
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 * "mode()" function
14670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 typval_T *argvars;
14674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014676 char_u buf[3];
14677
14678 buf[1] = NUL;
14679 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 if (VIsual_active)
14682 {
14683 if (VIsual_select)
14684 buf[0] = VIsual_mode + 's' - 'v';
14685 else
14686 buf[0] = VIsual_mode;
14687 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014688 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014689 || State == CONFIRM)
14690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014692 if (State == ASKMORE)
14693 buf[1] = 'm';
14694 else if (State == CONFIRM)
14695 buf[1] = '?';
14696 }
14697 else if (State == EXTERNCMD)
14698 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 else if (State & INSERT)
14700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014701#ifdef FEAT_VREPLACE
14702 if (State & VREPLACE_FLAG)
14703 {
14704 buf[0] = 'R';
14705 buf[1] = 'v';
14706 }
14707 else
14708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 if (State & REPLACE_FLAG)
14710 buf[0] = 'R';
14711 else
14712 buf[0] = 'i';
14713 }
14714 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014717 if (exmode_active)
14718 buf[1] = 'v';
14719 }
14720 else if (exmode_active)
14721 {
14722 buf[0] = 'c';
14723 buf[1] = 'e';
14724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014728 if (finish_op)
14729 buf[1] = 'o';
14730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014732 /* Clear out the minor mode when the argument is not a non-zero number or
14733 * non-empty string. */
14734 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014735 buf[1] = NUL;
14736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014737 rettv->vval.v_string = vim_strsave(buf);
14738 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739}
14740
Bram Moolenaar429fa852013-04-15 12:27:36 +020014741#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014742/*
14743 * "mzeval()" function
14744 */
14745 static void
14746f_mzeval(argvars, rettv)
14747 typval_T *argvars;
14748 typval_T *rettv;
14749{
14750 char_u *str;
14751 char_u buf[NUMBUFLEN];
14752
14753 str = get_tv_string_buf(&argvars[0], buf);
14754 do_mzeval(str, rettv);
14755}
Bram Moolenaar75676462013-01-30 14:55:42 +010014756
14757 void
14758mzscheme_call_vim(name, args, rettv)
14759 char_u *name;
14760 typval_T *args;
14761 typval_T *rettv;
14762{
14763 typval_T argvars[3];
14764
14765 argvars[0].v_type = VAR_STRING;
14766 argvars[0].vval.v_string = name;
14767 copy_tv(args, &argvars[1]);
14768 argvars[2].v_type = VAR_UNKNOWN;
14769 f_call(argvars, rettv);
14770 clear_tv(&argvars[1]);
14771}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014772#endif
14773
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014775 * "nextnonblank()" function
14776 */
14777 static void
14778f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014779 typval_T *argvars;
14780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014781{
14782 linenr_T lnum;
14783
14784 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787 {
14788 lnum = 0;
14789 break;
14790 }
14791 if (*skipwhite(ml_get(lnum)) != NUL)
14792 break;
14793 }
14794 rettv->vval.v_number = lnum;
14795}
14796
14797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 * "nr2char()" function
14799 */
14800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014801f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
14805 char_u buf[NUMBUFLEN];
14806
14807#ifdef FEAT_MBYTE
14808 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014809 {
14810 int utf8 = 0;
14811
14812 if (argvars[1].v_type != VAR_UNKNOWN)
14813 utf8 = get_tv_number_chk(&argvars[1], NULL);
14814 if (utf8)
14815 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14816 else
14817 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 else
14820#endif
14821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 buf[1] = NUL;
14824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 rettv->v_type = VAR_STRING;
14826 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827}
14828
14829/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014830 * "or(expr, expr)" function
14831 */
14832 static void
14833f_or(argvars, rettv)
14834 typval_T *argvars;
14835 typval_T *rettv;
14836{
14837 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14838 | get_tv_number_chk(&argvars[1], NULL);
14839}
14840
14841/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014842 * "pathshorten()" function
14843 */
14844 static void
14845f_pathshorten(argvars, rettv)
14846 typval_T *argvars;
14847 typval_T *rettv;
14848{
14849 char_u *p;
14850
14851 rettv->v_type = VAR_STRING;
14852 p = get_tv_string_chk(&argvars[0]);
14853 if (p == NULL)
14854 rettv->vval.v_string = NULL;
14855 else
14856 {
14857 p = vim_strsave(p);
14858 rettv->vval.v_string = p;
14859 if (p != NULL)
14860 shorten_dir(p);
14861 }
14862}
14863
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014864#ifdef FEAT_FLOAT
14865/*
14866 * "pow()" function
14867 */
14868 static void
14869f_pow(argvars, rettv)
14870 typval_T *argvars;
14871 typval_T *rettv;
14872{
14873 float_T fx, fy;
14874
14875 rettv->v_type = VAR_FLOAT;
14876 if (get_float_arg(argvars, &fx) == OK
14877 && get_float_arg(&argvars[1], &fy) == OK)
14878 rettv->vval.v_float = pow(fx, fy);
14879 else
14880 rettv->vval.v_float = 0.0;
14881}
14882#endif
14883
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014885 * "prevnonblank()" function
14886 */
14887 static void
14888f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014891{
14892 linenr_T lnum;
14893
14894 lnum = get_tv_lnum(argvars);
14895 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14896 lnum = 0;
14897 else
14898 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14899 --lnum;
14900 rettv->vval.v_number = lnum;
14901}
14902
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014903#ifdef HAVE_STDARG_H
14904/* This dummy va_list is here because:
14905 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14906 * - locally in the function results in a "used before set" warning
14907 * - using va_start() to initialize it gives "function with fixed args" error */
14908static va_list ap;
14909#endif
14910
Bram Moolenaar8c711452005-01-14 21:53:12 +000014911/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014912 * "printf()" function
14913 */
14914 static void
14915f_printf(argvars, rettv)
14916 typval_T *argvars;
14917 typval_T *rettv;
14918{
14919 rettv->v_type = VAR_STRING;
14920 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014921#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014922 {
14923 char_u buf[NUMBUFLEN];
14924 int len;
14925 char_u *s;
14926 int saved_did_emsg = did_emsg;
14927 char *fmt;
14928
14929 /* Get the required length, allocate the buffer and do it for real. */
14930 did_emsg = FALSE;
14931 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014932 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014933 if (!did_emsg)
14934 {
14935 s = alloc(len + 1);
14936 if (s != NULL)
14937 {
14938 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014939 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014940 }
14941 }
14942 did_emsg |= saved_did_emsg;
14943 }
14944#endif
14945}
14946
14947/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014948 * "pumvisible()" function
14949 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014950 static void
14951f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014952 typval_T *argvars UNUSED;
14953 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014954{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014955#ifdef FEAT_INS_EXPAND
14956 if (pum_visible())
14957 rettv->vval.v_number = 1;
14958#endif
14959}
14960
Bram Moolenaardb913952012-06-29 12:54:53 +020014961#ifdef FEAT_PYTHON3
14962/*
14963 * "py3eval()" function
14964 */
14965 static void
14966f_py3eval(argvars, rettv)
14967 typval_T *argvars;
14968 typval_T *rettv;
14969{
14970 char_u *str;
14971 char_u buf[NUMBUFLEN];
14972
14973 str = get_tv_string_buf(&argvars[0], buf);
14974 do_py3eval(str, rettv);
14975}
14976#endif
14977
14978#ifdef FEAT_PYTHON
14979/*
14980 * "pyeval()" function
14981 */
14982 static void
14983f_pyeval(argvars, rettv)
14984 typval_T *argvars;
14985 typval_T *rettv;
14986{
14987 char_u *str;
14988 char_u buf[NUMBUFLEN];
14989
14990 str = get_tv_string_buf(&argvars[0], buf);
14991 do_pyeval(str, rettv);
14992}
14993#endif
14994
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014995/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014996 * "range()" function
14997 */
14998 static void
14999f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015000 typval_T *argvars;
15001 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015002{
15003 long start;
15004 long end;
15005 long stride = 1;
15006 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015009 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010 if (argvars[1].v_type == VAR_UNKNOWN)
15011 {
15012 end = start - 1;
15013 start = 0;
15014 }
15015 else
15016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 }
15021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 if (error)
15023 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015025 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015026 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015027 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015028 else
15029 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015030 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015031 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015032 if (list_append_number(rettv->vval.v_list,
15033 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015035 }
15036}
15037
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015038/*
15039 * "readfile()" function
15040 */
15041 static void
15042f_readfile(argvars, rettv)
15043 typval_T *argvars;
15044 typval_T *rettv;
15045{
15046 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015047 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015048 char_u *fname;
15049 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015050 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15051 int io_size = sizeof(buf);
15052 int readlen; /* size of last fread() */
15053 char_u *prev = NULL; /* previously read bytes, if any */
15054 long prevlen = 0; /* length of data in prev */
15055 long prevsize = 0; /* size of prev buffer */
15056 long maxline = MAXLNUM;
15057 long cnt = 0;
15058 char_u *p; /* position in buf */
15059 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015061 if (argvars[1].v_type != VAR_UNKNOWN)
15062 {
15063 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15064 binary = TRUE;
15065 if (argvars[2].v_type != VAR_UNKNOWN)
15066 maxline = get_tv_number(&argvars[2]);
15067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015068
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015069 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015070 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015071
15072 /* Always open the file in binary mode, library functions have a mind of
15073 * their own about CR-LF conversion. */
15074 fname = get_tv_string(&argvars[0]);
15075 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15076 {
15077 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15078 return;
15079 }
15080
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015081 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015082 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015083 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015084
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015085 /* This for loop processes what was read, but is also entered at end
15086 * of file so that either:
15087 * - an incomplete line gets written
15088 * - a "binary" file gets an empty line at the end if it ends in a
15089 * newline. */
15090 for (p = buf, start = buf;
15091 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15092 ++p)
15093 {
15094 if (*p == '\n' || readlen <= 0)
15095 {
15096 listitem_T *li;
15097 char_u *s = NULL;
15098 long_u len = p - start;
15099
15100 /* Finished a line. Remove CRs before NL. */
15101 if (readlen > 0 && !binary)
15102 {
15103 while (len > 0 && start[len - 1] == '\r')
15104 --len;
15105 /* removal may cross back to the "prev" string */
15106 if (len == 0)
15107 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15108 --prevlen;
15109 }
15110 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015111 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015112 else
15113 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015114 /* Change "prev" buffer to be the right size. This way
15115 * the bytes are only copied once, and very long lines are
15116 * allocated only once. */
15117 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015118 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015119 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015120 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015121 prev = NULL; /* the list will own the string */
15122 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015123 }
15124 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015125 if (s == NULL)
15126 {
15127 do_outofmem_msg((long_u) prevlen + len + 1);
15128 failed = TRUE;
15129 break;
15130 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015131
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015132 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015133 {
15134 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015135 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015136 break;
15137 }
15138 li->li_tv.v_type = VAR_STRING;
15139 li->li_tv.v_lock = 0;
15140 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015141 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015142
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015143 start = p + 1; /* step over newline */
15144 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015145 break;
15146 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015147 else if (*p == NUL)
15148 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015149#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015150 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15151 * when finding the BF and check the previous two bytes. */
15152 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015153 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015154 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15155 * + 1, these may be in the "prev" string. */
15156 char_u back1 = p >= buf + 1 ? p[-1]
15157 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15158 char_u back2 = p >= buf + 2 ? p[-2]
15159 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15160 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015161
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015162 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015163 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015164 char_u *dest = p - 2;
15165
15166 /* Usually a BOM is at the beginning of a file, and so at
15167 * the beginning of a line; then we can just step over it.
15168 */
15169 if (start == dest)
15170 start = p + 1;
15171 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015172 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015173 /* have to shuffle buf to close gap */
15174 int adjust_prevlen = 0;
15175
15176 if (dest < buf)
15177 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015178 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015179 dest = buf;
15180 }
15181 if (readlen > p - buf + 1)
15182 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15183 readlen -= 3 - adjust_prevlen;
15184 prevlen -= adjust_prevlen;
15185 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015186 }
15187 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015188 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015189#endif
15190 } /* for */
15191
15192 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15193 break;
15194 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015195 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015196 /* There's part of a line in buf, store it in "prev". */
15197 if (p - start + prevlen >= prevsize)
15198 {
15199 /* need bigger "prev" buffer */
15200 char_u *newprev;
15201
15202 /* A common use case is ordinary text files and "prev" gets a
15203 * fragment of a line, so the first allocation is made
15204 * small, to avoid repeatedly 'allocing' large and
15205 * 'reallocing' small. */
15206 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015207 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015208 else
15209 {
15210 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015211 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015212 prevsize = grow50pc > growmin ? grow50pc : growmin;
15213 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015214 newprev = prev == NULL ? alloc(prevsize)
15215 : vim_realloc(prev, prevsize);
15216 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015217 {
15218 do_outofmem_msg((long_u)prevsize);
15219 failed = TRUE;
15220 break;
15221 }
15222 prev = newprev;
15223 }
15224 /* Add the line part to end of "prev". */
15225 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015226 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015227 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015228 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015229
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015230 /*
15231 * For a negative line count use only the lines at the end of the file,
15232 * free the rest.
15233 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015234 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015235 while (cnt > -maxline)
15236 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015237 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015238 --cnt;
15239 }
15240
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015241 if (failed)
15242 {
15243 list_free(rettv->vval.v_list, TRUE);
15244 /* readfile doc says an empty list is returned on error */
15245 rettv->vval.v_list = list_alloc();
15246 }
15247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015248 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015249 fclose(fd);
15250}
15251
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015252#if defined(FEAT_RELTIME)
15253static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15254
15255/*
15256 * Convert a List to proftime_T.
15257 * Return FAIL when there is something wrong.
15258 */
15259 static int
15260list2proftime(arg, tm)
15261 typval_T *arg;
15262 proftime_T *tm;
15263{
15264 long n1, n2;
15265 int error = FALSE;
15266
15267 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15268 || arg->vval.v_list->lv_len != 2)
15269 return FAIL;
15270 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15271 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15272# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015273 tm->HighPart = n1;
15274 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015275# else
15276 tm->tv_sec = n1;
15277 tm->tv_usec = n2;
15278# endif
15279 return error ? FAIL : OK;
15280}
15281#endif /* FEAT_RELTIME */
15282
15283/*
15284 * "reltime()" function
15285 */
15286 static void
15287f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015288 typval_T *argvars UNUSED;
15289 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015290{
15291#ifdef FEAT_RELTIME
15292 proftime_T res;
15293 proftime_T start;
15294
15295 if (argvars[0].v_type == VAR_UNKNOWN)
15296 {
15297 /* No arguments: get current time. */
15298 profile_start(&res);
15299 }
15300 else if (argvars[1].v_type == VAR_UNKNOWN)
15301 {
15302 if (list2proftime(&argvars[0], &res) == FAIL)
15303 return;
15304 profile_end(&res);
15305 }
15306 else
15307 {
15308 /* Two arguments: compute the difference. */
15309 if (list2proftime(&argvars[0], &start) == FAIL
15310 || list2proftime(&argvars[1], &res) == FAIL)
15311 return;
15312 profile_sub(&res, &start);
15313 }
15314
15315 if (rettv_list_alloc(rettv) == OK)
15316 {
15317 long n1, n2;
15318
15319# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015320 n1 = res.HighPart;
15321 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015322# else
15323 n1 = res.tv_sec;
15324 n2 = res.tv_usec;
15325# endif
15326 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15327 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15328 }
15329#endif
15330}
15331
15332/*
15333 * "reltimestr()" function
15334 */
15335 static void
15336f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015337 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015338 typval_T *rettv;
15339{
15340#ifdef FEAT_RELTIME
15341 proftime_T tm;
15342#endif
15343
15344 rettv->v_type = VAR_STRING;
15345 rettv->vval.v_string = NULL;
15346#ifdef FEAT_RELTIME
15347 if (list2proftime(&argvars[0], &tm) == OK)
15348 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15349#endif
15350}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15353static void make_connection __ARGS((void));
15354static int check_connection __ARGS((void));
15355
15356 static void
15357make_connection()
15358{
15359 if (X_DISPLAY == NULL
15360# ifdef FEAT_GUI
15361 && !gui.in_use
15362# endif
15363 )
15364 {
15365 x_force_connect = TRUE;
15366 setup_term_clip();
15367 x_force_connect = FALSE;
15368 }
15369}
15370
15371 static int
15372check_connection()
15373{
15374 make_connection();
15375 if (X_DISPLAY == NULL)
15376 {
15377 EMSG(_("E240: No connection to Vim server"));
15378 return FAIL;
15379 }
15380 return OK;
15381}
15382#endif
15383
15384#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015385static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015386
15387 static void
15388remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 typval_T *argvars;
15390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 int expr;
15392{
15393 char_u *server_name;
15394 char_u *keys;
15395 char_u *r = NULL;
15396 char_u buf[NUMBUFLEN];
15397# ifdef WIN32
15398 HWND w;
15399# else
15400 Window w;
15401# endif
15402
15403 if (check_restricted() || check_secure())
15404 return;
15405
15406# ifdef FEAT_X11
15407 if (check_connection() == FAIL)
15408 return;
15409# endif
15410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015411 server_name = get_tv_string_chk(&argvars[0]);
15412 if (server_name == NULL)
15413 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414 keys = get_tv_string_buf(&argvars[1], buf);
15415# ifdef WIN32
15416 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15417# else
15418 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15419 < 0)
15420# endif
15421 {
15422 if (r != NULL)
15423 EMSG(r); /* sending worked but evaluation failed */
15424 else
15425 EMSG2(_("E241: Unable to send to %s"), server_name);
15426 return;
15427 }
15428
15429 rettv->vval.v_string = r;
15430
15431 if (argvars[2].v_type != VAR_UNKNOWN)
15432 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015433 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015434 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015436
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015437 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 v.di_tv.v_type = VAR_STRING;
15439 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015440 idvar = get_tv_string_chk(&argvars[2]);
15441 if (idvar != NULL)
15442 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015443 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015444 }
15445}
15446#endif
15447
15448/*
15449 * "remote_expr()" function
15450 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015451 static void
15452f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015453 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015455{
15456 rettv->v_type = VAR_STRING;
15457 rettv->vval.v_string = NULL;
15458#ifdef FEAT_CLIENTSERVER
15459 remote_common(argvars, rettv, TRUE);
15460#endif
15461}
15462
15463/*
15464 * "remote_foreground()" function
15465 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015466 static void
15467f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015468 typval_T *argvars UNUSED;
15469 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015470{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471#ifdef FEAT_CLIENTSERVER
15472# ifdef WIN32
15473 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015474 {
15475 char_u *server_name = get_tv_string_chk(&argvars[0]);
15476
15477 if (server_name != NULL)
15478 serverForeground(server_name);
15479 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480# else
15481 /* Send a foreground() expression to the server. */
15482 argvars[1].v_type = VAR_STRING;
15483 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15484 argvars[2].v_type = VAR_UNKNOWN;
15485 remote_common(argvars, rettv, TRUE);
15486 vim_free(argvars[1].vval.v_string);
15487# endif
15488#endif
15489}
15490
Bram Moolenaar0d660222005-01-07 21:51:51 +000015491 static void
15492f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015493 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015495{
15496#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015497 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015498 char_u *s = NULL;
15499# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015500 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015501# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015502 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015503
15504 if (check_restricted() || check_secure())
15505 {
15506 rettv->vval.v_number = -1;
15507 return;
15508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015509 serverid = get_tv_string_chk(&argvars[0]);
15510 if (serverid == NULL)
15511 {
15512 rettv->vval.v_number = -1;
15513 return; /* type error; errmsg already given */
15514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015516 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015517 if (n == 0)
15518 rettv->vval.v_number = -1;
15519 else
15520 {
15521 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15522 rettv->vval.v_number = (s != NULL);
15523 }
15524# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015525 if (check_connection() == FAIL)
15526 return;
15527
15528 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015529 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015530# endif
15531
15532 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 char_u *retvar;
15535
Bram Moolenaar33570922005-01-25 22:26:29 +000015536 v.di_tv.v_type = VAR_STRING;
15537 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015538 retvar = get_tv_string_chk(&argvars[1]);
15539 if (retvar != NULL)
15540 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015541 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015542 }
15543#else
15544 rettv->vval.v_number = -1;
15545#endif
15546}
15547
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 static void
15549f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015550 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552{
15553 char_u *r = NULL;
15554
15555#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015556 char_u *serverid = get_tv_string_chk(&argvars[0]);
15557
15558 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 {
15560# ifdef WIN32
15561 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015562 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015564 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565 if (n != 0)
15566 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15567 if (r == NULL)
15568# else
15569 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015570 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571# endif
15572 EMSG(_("E277: Unable to read a server reply"));
15573 }
15574#endif
15575 rettv->v_type = VAR_STRING;
15576 rettv->vval.v_string = r;
15577}
15578
15579/*
15580 * "remote_send()" function
15581 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 static void
15583f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015584 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586{
15587 rettv->v_type = VAR_STRING;
15588 rettv->vval.v_string = NULL;
15589#ifdef FEAT_CLIENTSERVER
15590 remote_common(argvars, rettv, FALSE);
15591#endif
15592}
15593
15594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015595 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015596 */
15597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015598f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 typval_T *argvars;
15600 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015601{
Bram Moolenaar33570922005-01-25 22:26:29 +000015602 list_T *l;
15603 listitem_T *item, *item2;
15604 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015605 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015606 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015607 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015608 dict_T *d;
15609 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015610 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015611
Bram Moolenaar8c711452005-01-14 21:53:12 +000015612 if (argvars[0].v_type == VAR_DICT)
15613 {
15614 if (argvars[2].v_type != VAR_UNKNOWN)
15615 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015616 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015617 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 key = get_tv_string_chk(&argvars[1]);
15620 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015622 di = dict_find(d, key, -1);
15623 if (di == NULL)
15624 EMSG2(_(e_dictkey), key);
15625 else
15626 {
15627 *rettv = di->di_tv;
15628 init_tv(&di->di_tv);
15629 dictitem_remove(d, di);
15630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015632 }
15633 }
15634 else if (argvars[0].v_type != VAR_LIST)
15635 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015636 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015637 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 int error = FALSE;
15640
15641 idx = get_tv_number_chk(&argvars[1], &error);
15642 if (error)
15643 ; /* type error: do nothing, errmsg already given */
15644 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015645 EMSGN(_(e_listidx), idx);
15646 else
15647 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015648 if (argvars[2].v_type == VAR_UNKNOWN)
15649 {
15650 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015651 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015652 *rettv = item->li_tv;
15653 vim_free(item);
15654 }
15655 else
15656 {
15657 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 end = get_tv_number_chk(&argvars[2], &error);
15659 if (error)
15660 ; /* type error: do nothing */
15661 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015662 EMSGN(_(e_listidx), end);
15663 else
15664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015665 int cnt = 0;
15666
15667 for (li = item; li != NULL; li = li->li_next)
15668 {
15669 ++cnt;
15670 if (li == item2)
15671 break;
15672 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015673 if (li == NULL) /* didn't find "item2" after "item" */
15674 EMSG(_(e_invrange));
15675 else
15676 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015677 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015678 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015679 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015680 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015681 l->lv_first = item;
15682 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015683 item->li_prev = NULL;
15684 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015685 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015686 }
15687 }
15688 }
15689 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015690 }
15691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
15694/*
15695 * "rename({from}, {to})" function
15696 */
15697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015699 typval_T *argvars;
15700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701{
15702 char_u buf[NUMBUFLEN];
15703
15704 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015707 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15708 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709}
15710
15711/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015712 * "repeat()" function
15713 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015714 static void
15715f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015716 typval_T *argvars;
15717 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015718{
15719 char_u *p;
15720 int n;
15721 int slen;
15722 int len;
15723 char_u *r;
15724 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015725
15726 n = get_tv_number(&argvars[1]);
15727 if (argvars[0].v_type == VAR_LIST)
15728 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015730 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015731 if (list_extend(rettv->vval.v_list,
15732 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015733 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015734 }
15735 else
15736 {
15737 p = get_tv_string(&argvars[0]);
15738 rettv->v_type = VAR_STRING;
15739 rettv->vval.v_string = NULL;
15740
15741 slen = (int)STRLEN(p);
15742 len = slen * n;
15743 if (len <= 0)
15744 return;
15745
15746 r = alloc(len + 1);
15747 if (r != NULL)
15748 {
15749 for (i = 0; i < n; i++)
15750 mch_memmove(r + i * slen, p, (size_t)slen);
15751 r[len] = NUL;
15752 }
15753
15754 rettv->vval.v_string = r;
15755 }
15756}
15757
15758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 * "resolve()" function
15760 */
15761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015763 typval_T *argvars;
15764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765{
15766 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015767#ifdef HAVE_READLINK
15768 char_u *buf = NULL;
15769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015771 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772#ifdef FEAT_SHORTCUT
15773 {
15774 char_u *v = NULL;
15775
15776 v = mch_resolve_shortcut(p);
15777 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 }
15782#else
15783# ifdef HAVE_READLINK
15784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 char_u *cpy;
15786 int len;
15787 char_u *remain = NULL;
15788 char_u *q;
15789 int is_relative_to_current = FALSE;
15790 int has_trailing_pathsep = FALSE;
15791 int limit = 100;
15792
15793 p = vim_strsave(p);
15794
15795 if (p[0] == '.' && (vim_ispathsep(p[1])
15796 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15797 is_relative_to_current = TRUE;
15798
15799 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015800 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015803 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805
15806 q = getnextcomp(p);
15807 if (*q != NUL)
15808 {
15809 /* Separate the first path component in "p", and keep the
15810 * remainder (beginning with the path separator). */
15811 remain = vim_strsave(q - 1);
15812 q[-1] = NUL;
15813 }
15814
Bram Moolenaard9462e32011-04-11 21:35:11 +020015815 buf = alloc(MAXPATHL + 1);
15816 if (buf == NULL)
15817 goto fail;
15818
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 for (;;)
15820 {
15821 for (;;)
15822 {
15823 len = readlink((char *)p, (char *)buf, MAXPATHL);
15824 if (len <= 0)
15825 break;
15826 buf[len] = NUL;
15827
15828 if (limit-- == 0)
15829 {
15830 vim_free(p);
15831 vim_free(remain);
15832 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 goto fail;
15835 }
15836
15837 /* Ensure that the result will have a trailing path separator
15838 * if the argument has one. */
15839 if (remain == NULL && has_trailing_pathsep)
15840 add_pathsep(buf);
15841
15842 /* Separate the first path component in the link value and
15843 * concatenate the remainders. */
15844 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15845 if (*q != NUL)
15846 {
15847 if (remain == NULL)
15848 remain = vim_strsave(q - 1);
15849 else
15850 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015851 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 if (cpy != NULL)
15853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 vim_free(remain);
15855 remain = cpy;
15856 }
15857 }
15858 q[-1] = NUL;
15859 }
15860
15861 q = gettail(p);
15862 if (q > p && *q == NUL)
15863 {
15864 /* Ignore trailing path separator. */
15865 q[-1] = NUL;
15866 q = gettail(p);
15867 }
15868 if (q > p && !mch_isFullName(buf))
15869 {
15870 /* symlink is relative to directory of argument */
15871 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15872 if (cpy != NULL)
15873 {
15874 STRCPY(cpy, p);
15875 STRCPY(gettail(cpy), buf);
15876 vim_free(p);
15877 p = cpy;
15878 }
15879 }
15880 else
15881 {
15882 vim_free(p);
15883 p = vim_strsave(buf);
15884 }
15885 }
15886
15887 if (remain == NULL)
15888 break;
15889
15890 /* Append the first path component of "remain" to "p". */
15891 q = getnextcomp(remain + 1);
15892 len = q - remain - (*q != NUL);
15893 cpy = vim_strnsave(p, STRLEN(p) + len);
15894 if (cpy != NULL)
15895 {
15896 STRNCAT(cpy, remain, len);
15897 vim_free(p);
15898 p = cpy;
15899 }
15900 /* Shorten "remain". */
15901 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015902 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 else
15904 {
15905 vim_free(remain);
15906 remain = NULL;
15907 }
15908 }
15909
15910 /* If the result is a relative path name, make it explicitly relative to
15911 * the current directory if and only if the argument had this form. */
15912 if (!vim_ispathsep(*p))
15913 {
15914 if (is_relative_to_current
15915 && *p != NUL
15916 && !(p[0] == '.'
15917 && (p[1] == NUL
15918 || vim_ispathsep(p[1])
15919 || (p[1] == '.'
15920 && (p[2] == NUL
15921 || vim_ispathsep(p[2]))))))
15922 {
15923 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015924 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 if (cpy != NULL)
15926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 vim_free(p);
15928 p = cpy;
15929 }
15930 }
15931 else if (!is_relative_to_current)
15932 {
15933 /* Strip leading "./". */
15934 q = p;
15935 while (q[0] == '.' && vim_ispathsep(q[1]))
15936 q += 2;
15937 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015938 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 }
15940 }
15941
15942 /* Ensure that the result will have no trailing path separator
15943 * if the argument had none. But keep "/" or "//". */
15944 if (!has_trailing_pathsep)
15945 {
15946 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015947 if (after_pathsep(p, q))
15948 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 }
15950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 }
15953# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015954 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955# endif
15956#endif
15957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015958 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959
15960#ifdef HAVE_READLINK
15961fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015962 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965}
15966
15967/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015968 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 */
15970 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 typval_T *argvars;
15973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974{
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 list_T *l;
15976 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978 if (argvars[0].v_type != VAR_LIST)
15979 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015980 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015981 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015982 {
15983 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015984 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015985 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 while (li != NULL)
15987 {
15988 ni = li->li_prev;
15989 list_append(l, li);
15990 li = ni;
15991 }
15992 rettv->vval.v_list = l;
15993 rettv->v_type = VAR_LIST;
15994 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015995 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997}
15998
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015999#define SP_NOMOVE 0x01 /* don't move cursor */
16000#define SP_REPEAT 0x02 /* repeat to find outer pair */
16001#define SP_RETCOUNT 0x04 /* return matchcount */
16002#define SP_SETPCMARK 0x08 /* set previous context mark */
16003#define SP_START 0x10 /* accept match at start position */
16004#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16005#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016006
Bram Moolenaar33570922005-01-25 22:26:29 +000016007static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016008
16009/*
16010 * Get flags for a search function.
16011 * Possibly sets "p_ws".
16012 * Returns BACKWARD, FORWARD or zero (for an error).
16013 */
16014 static int
16015get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016016 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016017 int *flagsp;
16018{
16019 int dir = FORWARD;
16020 char_u *flags;
16021 char_u nbuf[NUMBUFLEN];
16022 int mask;
16023
16024 if (varp->v_type != VAR_UNKNOWN)
16025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 flags = get_tv_string_buf_chk(varp, nbuf);
16027 if (flags == NULL)
16028 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 while (*flags != NUL)
16030 {
16031 switch (*flags)
16032 {
16033 case 'b': dir = BACKWARD; break;
16034 case 'w': p_ws = TRUE; break;
16035 case 'W': p_ws = FALSE; break;
16036 default: mask = 0;
16037 if (flagsp != NULL)
16038 switch (*flags)
16039 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016040 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016041 case 'e': mask = SP_END; break;
16042 case 'm': mask = SP_RETCOUNT; break;
16043 case 'n': mask = SP_NOMOVE; break;
16044 case 'p': mask = SP_SUBPAT; break;
16045 case 'r': mask = SP_REPEAT; break;
16046 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047 }
16048 if (mask == 0)
16049 {
16050 EMSG2(_(e_invarg2), flags);
16051 dir = 0;
16052 }
16053 else
16054 *flagsp |= mask;
16055 }
16056 if (dir == 0)
16057 break;
16058 ++flags;
16059 }
16060 }
16061 return dir;
16062}
16063
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016065 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016067 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016068search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016070 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016071 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016073 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 char_u *pat;
16075 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016076 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 int save_p_ws = p_ws;
16078 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016079 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016080 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016081 proftime_T tm;
16082#ifdef FEAT_RELTIME
16083 long time_limit = 0;
16084#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016085 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016086 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016088 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016089 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016090 if (dir == 0)
16091 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016092 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016093 if (flags & SP_START)
16094 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016095 if (flags & SP_END)
16096 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016097
Bram Moolenaar76929292008-01-06 19:07:36 +000016098 /* Optional arguments: line number to stop searching and timeout. */
16099 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016100 {
16101 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16102 if (lnum_stop < 0)
16103 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016104#ifdef FEAT_RELTIME
16105 if (argvars[3].v_type != VAR_UNKNOWN)
16106 {
16107 time_limit = get_tv_number_chk(&argvars[3], NULL);
16108 if (time_limit < 0)
16109 goto theend;
16110 }
16111#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016112 }
16113
Bram Moolenaar76929292008-01-06 19:07:36 +000016114#ifdef FEAT_RELTIME
16115 /* Set the time limit, if there is one. */
16116 profile_setlimit(time_limit, &tm);
16117#endif
16118
Bram Moolenaar231334e2005-07-25 20:46:57 +000016119 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016120 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016121 * Check to make sure only those flags are set.
16122 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16123 * flags cannot be set. Check for that condition also.
16124 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016125 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016126 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016128 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016129 goto theend;
16130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016132 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016133 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016134 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016135 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016137 if (flags & SP_SUBPAT)
16138 retval = subpatnum;
16139 else
16140 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016141 if (flags & SP_SETPCMARK)
16142 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016144 if (match_pos != NULL)
16145 {
16146 /* Store the match cursor position */
16147 match_pos->lnum = pos.lnum;
16148 match_pos->col = pos.col + 1;
16149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 /* "/$" will put the cursor after the end of the line, may need to
16151 * correct that here */
16152 check_cursor();
16153 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016154
16155 /* If 'n' flag is used: restore cursor position. */
16156 if (flags & SP_NOMOVE)
16157 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016158 else
16159 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016160theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016162
16163 return retval;
16164}
16165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016167
16168/*
16169 * round() is not in C90, use ceil() or floor() instead.
16170 */
16171 float_T
16172vim_round(f)
16173 float_T f;
16174{
16175 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16176}
16177
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016178/*
16179 * "round({float})" function
16180 */
16181 static void
16182f_round(argvars, rettv)
16183 typval_T *argvars;
16184 typval_T *rettv;
16185{
16186 float_T f;
16187
16188 rettv->v_type = VAR_FLOAT;
16189 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016190 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016191 else
16192 rettv->vval.v_float = 0.0;
16193}
16194#endif
16195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016196/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016197 * "screenattr()" function
16198 */
16199 static void
16200f_screenattr(argvars, rettv)
16201 typval_T *argvars UNUSED;
16202 typval_T *rettv;
16203{
16204 int row;
16205 int col;
16206 int c;
16207
16208 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16209 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16210 if (row < 0 || row >= screen_Rows
16211 || col < 0 || col >= screen_Columns)
16212 c = -1;
16213 else
16214 c = ScreenAttrs[LineOffset[row] + col];
16215 rettv->vval.v_number = c;
16216}
16217
16218/*
16219 * "screenchar()" function
16220 */
16221 static void
16222f_screenchar(argvars, rettv)
16223 typval_T *argvars UNUSED;
16224 typval_T *rettv;
16225{
16226 int row;
16227 int col;
16228 int off;
16229 int c;
16230
16231 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16232 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16233 if (row < 0 || row >= screen_Rows
16234 || col < 0 || col >= screen_Columns)
16235 c = -1;
16236 else
16237 {
16238 off = LineOffset[row] + col;
16239#ifdef FEAT_MBYTE
16240 if (enc_utf8 && ScreenLinesUC[off] != 0)
16241 c = ScreenLinesUC[off];
16242 else
16243#endif
16244 c = ScreenLines[off];
16245 }
16246 rettv->vval.v_number = c;
16247}
16248
16249/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016250 * "screencol()" function
16251 *
16252 * First column is 1 to be consistent with virtcol().
16253 */
16254 static void
16255f_screencol(argvars, rettv)
16256 typval_T *argvars UNUSED;
16257 typval_T *rettv;
16258{
16259 rettv->vval.v_number = screen_screencol() + 1;
16260}
16261
16262/*
16263 * "screenrow()" function
16264 */
16265 static void
16266f_screenrow(argvars, rettv)
16267 typval_T *argvars UNUSED;
16268 typval_T *rettv;
16269{
16270 rettv->vval.v_number = screen_screenrow() + 1;
16271}
16272
16273/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016274 * "search()" function
16275 */
16276 static void
16277f_search(argvars, rettv)
16278 typval_T *argvars;
16279 typval_T *rettv;
16280{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016281 int flags = 0;
16282
16283 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284}
16285
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016287 * "searchdecl()" function
16288 */
16289 static void
16290f_searchdecl(argvars, rettv)
16291 typval_T *argvars;
16292 typval_T *rettv;
16293{
16294 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016295 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016296 int error = FALSE;
16297 char_u *name;
16298
16299 rettv->vval.v_number = 1; /* default: FAIL */
16300
16301 name = get_tv_string_chk(&argvars[0]);
16302 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016303 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016304 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016305 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16306 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16307 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016308 if (!error && name != NULL)
16309 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016310 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016311}
16312
16313/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016314 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016316 static int
16317searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016319 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320{
16321 char_u *spat, *mpat, *epat;
16322 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 int dir;
16325 int flags = 0;
16326 char_u nbuf1[NUMBUFLEN];
16327 char_u nbuf2[NUMBUFLEN];
16328 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016329 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016330 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016331 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 spat = get_tv_string_chk(&argvars[0]);
16335 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16336 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16337 if (spat == NULL || mpat == NULL || epat == NULL)
16338 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 /* Handle the optional fourth argument: flags */
16341 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016342 if (dir == 0)
16343 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016344
16345 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016346 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16347 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016348 if ((flags & (SP_END | SP_SUBPAT)) != 0
16349 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016350 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016351 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016352 goto theend;
16353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016355 /* Using 'r' implies 'W', otherwise it doesn't work. */
16356 if (flags & SP_REPEAT)
16357 p_ws = FALSE;
16358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016359 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360 if (argvars[3].v_type == VAR_UNKNOWN
16361 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 skip = (char_u *)"";
16363 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016366 if (argvars[5].v_type != VAR_UNKNOWN)
16367 {
16368 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16369 if (lnum_stop < 0)
16370 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016371#ifdef FEAT_RELTIME
16372 if (argvars[6].v_type != VAR_UNKNOWN)
16373 {
16374 time_limit = get_tv_number_chk(&argvars[6], NULL);
16375 if (time_limit < 0)
16376 goto theend;
16377 }
16378#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016379 }
16380 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381 if (skip == NULL)
16382 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016384 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016385 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016386
16387theend:
16388 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016389
16390 return retval;
16391}
16392
16393/*
16394 * "searchpair()" function
16395 */
16396 static void
16397f_searchpair(argvars, rettv)
16398 typval_T *argvars;
16399 typval_T *rettv;
16400{
16401 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16402}
16403
16404/*
16405 * "searchpairpos()" function
16406 */
16407 static void
16408f_searchpairpos(argvars, rettv)
16409 typval_T *argvars;
16410 typval_T *rettv;
16411{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016412 pos_T match_pos;
16413 int lnum = 0;
16414 int col = 0;
16415
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016416 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016417 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016418
16419 if (searchpair_cmn(argvars, &match_pos) > 0)
16420 {
16421 lnum = match_pos.lnum;
16422 col = match_pos.col;
16423 }
16424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016425 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16426 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016427}
16428
16429/*
16430 * Search for a start/middle/end thing.
16431 * Used by searchpair(), see its documentation for the details.
16432 * Returns 0 or -1 for no match,
16433 */
16434 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016435do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16436 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016437 char_u *spat; /* start pattern */
16438 char_u *mpat; /* middle pattern */
16439 char_u *epat; /* end pattern */
16440 int dir; /* BACKWARD or FORWARD */
16441 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016442 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016443 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016444 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016445 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016446{
16447 char_u *save_cpo;
16448 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16449 long retval = 0;
16450 pos_T pos;
16451 pos_T firstpos;
16452 pos_T foundpos;
16453 pos_T save_cursor;
16454 pos_T save_pos;
16455 int n;
16456 int r;
16457 int nest = 1;
16458 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016459 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016460 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016461
16462 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16463 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016464 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016465
Bram Moolenaar76929292008-01-06 19:07:36 +000016466#ifdef FEAT_RELTIME
16467 /* Set the time limit, if there is one. */
16468 profile_setlimit(time_limit, &tm);
16469#endif
16470
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016471 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16472 * start/middle/end (pat3, for the top pair). */
16473 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16474 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16475 if (pat2 == NULL || pat3 == NULL)
16476 goto theend;
16477 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16478 if (*mpat == NUL)
16479 STRCPY(pat3, pat2);
16480 else
16481 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16482 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016483 if (flags & SP_START)
16484 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016485
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 save_cursor = curwin->w_cursor;
16487 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016488 clearpos(&firstpos);
16489 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 pat = pat3;
16491 for (;;)
16492 {
16493 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016494 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16496 /* didn't find it or found the first match again: FAIL */
16497 break;
16498
16499 if (firstpos.lnum == 0)
16500 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016501 if (equalpos(pos, foundpos))
16502 {
16503 /* Found the same position again. Can happen with a pattern that
16504 * has "\zs" at the end and searching backwards. Advance one
16505 * character and try again. */
16506 if (dir == BACKWARD)
16507 decl(&pos);
16508 else
16509 incl(&pos);
16510 }
16511 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016513 /* clear the start flag to avoid getting stuck here */
16514 options &= ~SEARCH_START;
16515
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 /* If the skip pattern matches, ignore this match. */
16517 if (*skip != NUL)
16518 {
16519 save_pos = curwin->w_cursor;
16520 curwin->w_cursor = pos;
16521 r = eval_to_bool(skip, &err, NULL, FALSE);
16522 curwin->w_cursor = save_pos;
16523 if (err)
16524 {
16525 /* Evaluating {skip} caused an error, break here. */
16526 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016527 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 break;
16529 }
16530 if (r)
16531 continue;
16532 }
16533
16534 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16535 {
16536 /* Found end when searching backwards or start when searching
16537 * forward: nested pair. */
16538 ++nest;
16539 pat = pat2; /* nested, don't search for middle */
16540 }
16541 else
16542 {
16543 /* Found end when searching forward or start when searching
16544 * backward: end of (nested) pair; or found middle in outer pair. */
16545 if (--nest == 1)
16546 pat = pat3; /* outer level, search for middle */
16547 }
16548
16549 if (nest == 0)
16550 {
16551 /* Found the match: return matchcount or line number. */
16552 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016553 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016555 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016556 if (flags & SP_SETPCMARK)
16557 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 curwin->w_cursor = pos;
16559 if (!(flags & SP_REPEAT))
16560 break;
16561 nest = 1; /* search for next unmatched */
16562 }
16563 }
16564
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016565 if (match_pos != NULL)
16566 {
16567 /* Store the match cursor position */
16568 match_pos->lnum = curwin->w_cursor.lnum;
16569 match_pos->col = curwin->w_cursor.col + 1;
16570 }
16571
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016573 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 curwin->w_cursor = save_cursor;
16575
16576theend:
16577 vim_free(pat2);
16578 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016579 if (p_cpo == empty_option)
16580 p_cpo = save_cpo;
16581 else
16582 /* Darn, evaluating the {skip} expression changed the value. */
16583 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016584
16585 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586}
16587
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016588/*
16589 * "searchpos()" function
16590 */
16591 static void
16592f_searchpos(argvars, rettv)
16593 typval_T *argvars;
16594 typval_T *rettv;
16595{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016596 pos_T match_pos;
16597 int lnum = 0;
16598 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016599 int n;
16600 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016602 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016603 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016604
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016605 n = search_cmn(argvars, &match_pos, &flags);
16606 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016607 {
16608 lnum = match_pos.lnum;
16609 col = match_pos.col;
16610 }
16611
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16613 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016614 if (flags & SP_SUBPAT)
16615 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016616}
16617
16618
Bram Moolenaar0d660222005-01-07 21:51:51 +000016619 static void
16620f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624#ifdef FEAT_CLIENTSERVER
16625 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016626 char_u *server = get_tv_string_chk(&argvars[0]);
16627 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 if (server == NULL || reply == NULL)
16631 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016632 if (check_restricted() || check_secure())
16633 return;
16634# ifdef FEAT_X11
16635 if (check_connection() == FAIL)
16636 return;
16637# endif
16638
16639 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641 EMSG(_("E258: Unable to send to client"));
16642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016644 rettv->vval.v_number = 0;
16645#else
16646 rettv->vval.v_number = -1;
16647#endif
16648}
16649
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 static void
16651f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654{
16655 char_u *r = NULL;
16656
16657#ifdef FEAT_CLIENTSERVER
16658# ifdef WIN32
16659 r = serverGetVimNames();
16660# else
16661 make_connection();
16662 if (X_DISPLAY != NULL)
16663 r = serverGetVimNames(X_DISPLAY);
16664# endif
16665#endif
16666 rettv->v_type = VAR_STRING;
16667 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668}
16669
16670/*
16671 * "setbufvar()" function
16672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016675 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016676 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677{
16678 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016681 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 char_u nbuf[NUMBUFLEN];
16683
16684 if (check_restricted() || check_secure())
16685 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16687 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016688 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 varp = &argvars[2];
16690
16691 if (buf != NULL && varname != NULL && varp != NULL)
16692 {
16693 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695
16696 if (*varname == '&')
16697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016698 long numval;
16699 char_u *strval;
16700 int error = FALSE;
16701
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 numval = get_tv_number_chk(varp, &error);
16704 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016705 if (!error && strval != NULL)
16706 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 }
16708 else
16709 {
16710 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16711 if (bufvarname != NULL)
16712 {
16713 STRCPY(bufvarname, "b:");
16714 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016715 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 vim_free(bufvarname);
16717 }
16718 }
16719
16720 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723}
16724
16725/*
16726 * "setcmdpos()" function
16727 */
16728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016729f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 typval_T *argvars;
16731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016733 int pos = (int)get_tv_number(&argvars[0]) - 1;
16734
16735 if (pos >= 0)
16736 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737}
16738
16739/*
16740 * "setline()" function
16741 */
16742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 typval_T *argvars;
16745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746{
16747 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016748 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016749 list_T *l = NULL;
16750 listitem_T *li = NULL;
16751 long added = 0;
16752 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016754 lnum = get_tv_lnum(&argvars[0]);
16755 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016757 l = argvars[1].vval.v_list;
16758 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016760 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016762
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016763 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016764 for (;;)
16765 {
16766 if (l != NULL)
16767 {
16768 /* list argument, get next string */
16769 if (li == NULL)
16770 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016771 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016772 li = li->li_next;
16773 }
16774
16775 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016777 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016778
16779 /* When coming here from Insert mode, sync undo, so that this can be
16780 * undone separately from what was previously inserted. */
16781 if (u_sync_once == 2)
16782 {
16783 u_sync_once = 1; /* notify that u_sync() was called */
16784 u_sync(TRUE);
16785 }
16786
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016787 if (lnum <= curbuf->b_ml.ml_line_count)
16788 {
16789 /* existing line, replace it */
16790 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16791 {
16792 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016793 if (lnum == curwin->w_cursor.lnum)
16794 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016795 rettv->vval.v_number = 0; /* OK */
16796 }
16797 }
16798 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16799 {
16800 /* lnum is one past the last line, append the line */
16801 ++added;
16802 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16803 rettv->vval.v_number = 0; /* OK */
16804 }
16805
16806 if (l == NULL) /* only one string argument */
16807 break;
16808 ++lnum;
16809 }
16810
16811 if (added > 0)
16812 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813}
16814
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016815static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16816
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016818 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016819 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016820 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016821set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016822 win_T *wp UNUSED;
16823 typval_T *list_arg UNUSED;
16824 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016825 typval_T *rettv;
16826{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016827#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016828 char_u *act;
16829 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016830#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016831
Bram Moolenaar2641f772005-03-25 21:58:17 +000016832 rettv->vval.v_number = -1;
16833
16834#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016835 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016836 EMSG(_(e_listreq));
16837 else
16838 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016839 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016840
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016841 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016842 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016843 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016844 if (act == NULL)
16845 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016846 if (*act == 'a' || *act == 'r')
16847 action = *act;
16848 }
16849
Bram Moolenaar81484f42012-12-05 15:16:47 +010016850 if (l != NULL && set_errorlist(wp, l, action,
16851 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016852 rettv->vval.v_number = 0;
16853 }
16854#endif
16855}
16856
16857/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016858 * "setloclist()" function
16859 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016860 static void
16861f_setloclist(argvars, rettv)
16862 typval_T *argvars;
16863 typval_T *rettv;
16864{
16865 win_T *win;
16866
16867 rettv->vval.v_number = -1;
16868
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016869 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016870 if (win != NULL)
16871 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16872}
16873
16874/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016875 * "setmatches()" function
16876 */
16877 static void
16878f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016879 typval_T *argvars UNUSED;
16880 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016881{
16882#ifdef FEAT_SEARCH_EXTRA
16883 list_T *l;
16884 listitem_T *li;
16885 dict_T *d;
16886
16887 rettv->vval.v_number = -1;
16888 if (argvars[0].v_type != VAR_LIST)
16889 {
16890 EMSG(_(e_listreq));
16891 return;
16892 }
16893 if ((l = argvars[0].vval.v_list) != NULL)
16894 {
16895
16896 /* To some extent make sure that we are dealing with a list from
16897 * "getmatches()". */
16898 li = l->lv_first;
16899 while (li != NULL)
16900 {
16901 if (li->li_tv.v_type != VAR_DICT
16902 || (d = li->li_tv.vval.v_dict) == NULL)
16903 {
16904 EMSG(_(e_invarg));
16905 return;
16906 }
16907 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16908 && dict_find(d, (char_u *)"pattern", -1) != NULL
16909 && dict_find(d, (char_u *)"priority", -1) != NULL
16910 && dict_find(d, (char_u *)"id", -1) != NULL))
16911 {
16912 EMSG(_(e_invarg));
16913 return;
16914 }
16915 li = li->li_next;
16916 }
16917
16918 clear_matches(curwin);
16919 li = l->lv_first;
16920 while (li != NULL)
16921 {
16922 d = li->li_tv.vval.v_dict;
16923 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16924 get_dict_string(d, (char_u *)"pattern", FALSE),
16925 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020016926 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016927 li = li->li_next;
16928 }
16929 rettv->vval.v_number = 0;
16930 }
16931#endif
16932}
16933
16934/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016935 * "setpos()" function
16936 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016937 static void
16938f_setpos(argvars, rettv)
16939 typval_T *argvars;
16940 typval_T *rettv;
16941{
16942 pos_T pos;
16943 int fnum;
16944 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016945 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016946
Bram Moolenaar08250432008-02-13 11:42:46 +000016947 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016948 name = get_tv_string_chk(argvars);
16949 if (name != NULL)
16950 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016951 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016952 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016953 if (--pos.col < 0)
16954 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016955 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016956 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016957 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016958 if (fnum == curbuf->b_fnum)
16959 {
16960 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016961 if (curswant >= 0)
16962 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016963 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016964 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016965 }
16966 else
16967 EMSG(_(e_invarg));
16968 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016969 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16970 {
16971 /* set mark */
16972 if (setmark_pos(name[1], &pos, fnum) == OK)
16973 rettv->vval.v_number = 0;
16974 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016975 else
16976 EMSG(_(e_invarg));
16977 }
16978 }
16979}
16980
16981/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016982 * "setqflist()" function
16983 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016984 static void
16985f_setqflist(argvars, rettv)
16986 typval_T *argvars;
16987 typval_T *rettv;
16988{
16989 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16990}
16991
16992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 * "setreg()" function
16994 */
16995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 typval_T *argvars;
16998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999{
17000 int regname;
17001 char_u *strregname;
17002 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 int append;
17005 char_u yank_type;
17006 long block_len;
17007
17008 block_len = -1;
17009 yank_type = MAUTO;
17010 append = FALSE;
17011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 if (strregname == NULL)
17016 return; /* type error; errmsg already given */
17017 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 if (regname == 0 || regname == '@')
17019 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 stropt = get_tv_string_chk(&argvars[2]);
17024 if (stropt == NULL)
17025 return; /* type error */
17026 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 switch (*stropt)
17028 {
17029 case 'a': case 'A': /* append */
17030 append = TRUE;
17031 break;
17032 case 'v': case 'c': /* character-wise selection */
17033 yank_type = MCHAR;
17034 break;
17035 case 'V': case 'l': /* line-wise selection */
17036 yank_type = MLINE;
17037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 case 'b': case Ctrl_V: /* block-wise selection */
17039 yank_type = MBLOCK;
17040 if (VIM_ISDIGIT(stropt[1]))
17041 {
17042 ++stropt;
17043 block_len = getdigits(&stropt) - 1;
17044 --stropt;
17045 }
17046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 }
17048 }
17049
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017050 if (argvars[1].v_type == VAR_LIST)
17051 {
17052 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017053 char_u **allocval;
17054 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017055 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017056 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017057 int len = argvars[1].vval.v_list->lv_len;
17058 listitem_T *li;
17059
Bram Moolenaar7d647822014-04-05 21:28:56 +020017060 /* First half: use for pointers to result lines; second half: use for
17061 * pointers to allocated copies. */
17062 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017063 if (lstval == NULL)
17064 return;
17065 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017066 allocval = lstval + len + 2;
17067 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017068
17069 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17070 li = li->li_next)
17071 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017072 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017073 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017074 goto free_lstval;
17075 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017076 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017077 /* Need to make a copy, next get_tv_string_buf_chk() will
17078 * overwrite the string. */
17079 strval = vim_strsave(buf);
17080 if (strval == NULL)
17081 goto free_lstval;
17082 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017083 }
17084 *curval++ = strval;
17085 }
17086 *curval++ = NULL;
17087
17088 write_reg_contents_lst(regname, lstval, -1,
17089 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017090free_lstval:
17091 while (curallocval > allocval)
17092 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017093 vim_free(lstval);
17094 }
17095 else
17096 {
17097 strval = get_tv_string_chk(&argvars[1]);
17098 if (strval == NULL)
17099 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017100 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017103 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104}
17105
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017106/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017107 * "settabvar()" function
17108 */
17109 static void
17110f_settabvar(argvars, rettv)
17111 typval_T *argvars;
17112 typval_T *rettv;
17113{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017114#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017115 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017116 tabpage_T *tp;
17117#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017118 char_u *varname, *tabvarname;
17119 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017120
17121 rettv->vval.v_number = 0;
17122
17123 if (check_restricted() || check_secure())
17124 return;
17125
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017126#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017127 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017128#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017129 varname = get_tv_string_chk(&argvars[1]);
17130 varp = &argvars[2];
17131
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017132 if (varname != NULL && varp != NULL
17133#ifdef FEAT_WINDOWS
17134 && tp != NULL
17135#endif
17136 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017137 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017138#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017139 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017140 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017141#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017142
17143 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17144 if (tabvarname != NULL)
17145 {
17146 STRCPY(tabvarname, "t:");
17147 STRCPY(tabvarname + 2, varname);
17148 set_var(tabvarname, varp, TRUE);
17149 vim_free(tabvarname);
17150 }
17151
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017152#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017153 /* Restore current tabpage */
17154 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017155 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017156#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017157 }
17158}
17159
17160/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017161 * "settabwinvar()" function
17162 */
17163 static void
17164f_settabwinvar(argvars, rettv)
17165 typval_T *argvars;
17166 typval_T *rettv;
17167{
17168 setwinvar(argvars, rettv, 1);
17169}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170
17171/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017172 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017176 typval_T *argvars;
17177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017179 setwinvar(argvars, rettv, 0);
17180}
17181
17182/*
17183 * "setwinvar()" and "settabwinvar()" functions
17184 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017185
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017186 static void
17187setwinvar(argvars, rettv, off)
17188 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017189 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017190 int off;
17191{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 win_T *win;
17193#ifdef FEAT_WINDOWS
17194 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017195 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196#endif
17197 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017200 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201
17202 if (check_restricted() || check_secure())
17203 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017204
17205#ifdef FEAT_WINDOWS
17206 if (off == 1)
17207 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17208 else
17209 tp = curtab;
17210#endif
17211 win = find_win_by_nr(&argvars[off], tp);
17212 varname = get_tv_string_chk(&argvars[off + 1]);
17213 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214
17215 if (win != NULL && varname != NULL && varp != NULL)
17216 {
17217#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017218 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017219 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220#endif
17221
17222 if (*varname == '&')
17223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017224 long numval;
17225 char_u *strval;
17226 int error = FALSE;
17227
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017229 numval = get_tv_number_chk(varp, &error);
17230 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017231 if (!error && strval != NULL)
17232 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 }
17234 else
17235 {
17236 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17237 if (winvarname != NULL)
17238 {
17239 STRCPY(winvarname, "w:");
17240 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017241 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 vim_free(winvarname);
17243 }
17244 }
17245
17246#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017247 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248#endif
17249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250}
17251
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017252#ifdef FEAT_CRYPT
17253/*
17254 * "sha256({string})" function
17255 */
17256 static void
17257f_sha256(argvars, rettv)
17258 typval_T *argvars;
17259 typval_T *rettv;
17260{
17261 char_u *p;
17262
17263 p = get_tv_string(&argvars[0]);
17264 rettv->vval.v_string = vim_strsave(
17265 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17266 rettv->v_type = VAR_STRING;
17267}
17268#endif /* FEAT_CRYPT */
17269
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017271 * "shellescape({string})" function
17272 */
17273 static void
17274f_shellescape(argvars, rettv)
17275 typval_T *argvars;
17276 typval_T *rettv;
17277{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017278 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017279 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017280 rettv->v_type = VAR_STRING;
17281}
17282
17283/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017284 * shiftwidth() function
17285 */
17286 static void
17287f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017288 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017289 typval_T *rettv;
17290{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017291 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017292}
17293
17294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 */
17297 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017299 typval_T *argvars;
17300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017302 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 p = get_tv_string(&argvars[0]);
17305 rettv->vval.v_string = vim_strsave(p);
17306 simplify_filename(rettv->vval.v_string); /* simplify in place */
17307 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308}
17309
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017310#ifdef FEAT_FLOAT
17311/*
17312 * "sin()" function
17313 */
17314 static void
17315f_sin(argvars, rettv)
17316 typval_T *argvars;
17317 typval_T *rettv;
17318{
17319 float_T f;
17320
17321 rettv->v_type = VAR_FLOAT;
17322 if (get_float_arg(argvars, &f) == OK)
17323 rettv->vval.v_float = sin(f);
17324 else
17325 rettv->vval.v_float = 0.0;
17326}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017327
17328/*
17329 * "sinh()" function
17330 */
17331 static void
17332f_sinh(argvars, rettv)
17333 typval_T *argvars;
17334 typval_T *rettv;
17335{
17336 float_T f;
17337
17338 rettv->v_type = VAR_FLOAT;
17339 if (get_float_arg(argvars, &f) == OK)
17340 rettv->vval.v_float = sinh(f);
17341 else
17342 rettv->vval.v_float = 0.0;
17343}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017344#endif
17345
Bram Moolenaar0d660222005-01-07 21:51:51 +000017346static int
17347#ifdef __BORLANDC__
17348 _RTLENTRYF
17349#endif
17350 item_compare __ARGS((const void *s1, const void *s2));
17351static int
17352#ifdef __BORLANDC__
17353 _RTLENTRYF
17354#endif
17355 item_compare2 __ARGS((const void *s1, const void *s2));
17356
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017357/* struct used in the array that's given to qsort() */
17358typedef struct
17359{
17360 listitem_T *item;
17361 int idx;
17362} sortItem_T;
17363
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017365static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017366static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017367static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017369static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017370static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017371#define ITEM_COMPARE_FAIL 999
17372
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017374 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017376 static int
17377#ifdef __BORLANDC__
17378_RTLENTRYF
17379#endif
17380item_compare(s1, s2)
17381 const void *s1;
17382 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017384 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017385 char_u *p1, *p2;
17386 char_u *tofree1, *tofree2;
17387 int res;
17388 char_u numbuf1[NUMBUFLEN];
17389 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017391 si1 = (sortItem_T *)s1;
17392 si2 = (sortItem_T *)s2;
17393 p1 = tv2string(&si1->item->li_tv, &tofree1, numbuf1, 0);
17394 p2 = tv2string(&si2->item->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017395 if (p1 == NULL)
17396 p1 = (char_u *)"";
17397 if (p2 == NULL)
17398 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017399 if (!item_compare_numeric)
17400 {
17401 if (item_compare_ic)
17402 res = STRICMP(p1, p2);
17403 else
17404 res = STRCMP(p1, p2);
17405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017407 {
17408 double n1, n2;
17409 n1 = strtod((char *)p1, (char **)&p1);
17410 n2 = strtod((char *)p2, (char **)&p2);
17411 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17412 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017413
17414 /* When the result would be zero, compare the pointers themselves. Makes
17415 * the sort stable. */
17416 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017417 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017418
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419 vim_free(tofree1);
17420 vim_free(tofree2);
17421 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422}
17423
17424 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425#ifdef __BORLANDC__
17426_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017428item_compare2(s1, s2)
17429 const void *s1;
17430 const void *s2;
17431{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017432 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017435 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 /* shortcut after failure in previous call; compare all items equal */
17439 if (item_compare_func_err)
17440 return 0;
17441
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017442 si1 = (sortItem_T *)s1;
17443 si2 = (sortItem_T *)s2;
17444
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017445 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017446 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017447 copy_tv(&si1->item->li_tv, &argv[0]);
17448 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017449
17450 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017451 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017452 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17453 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 clear_tv(&argv[0]);
17455 clear_tv(&argv[1]);
17456
17457 if (res == FAIL)
17458 res = ITEM_COMPARE_FAIL;
17459 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17461 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017462 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017464
17465 /* When the result would be zero, compare the pointers themselves. Makes
17466 * the sort stable. */
17467 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017468 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017469
Bram Moolenaar0d660222005-01-07 21:51:51 +000017470 return res;
17471}
17472
17473/*
17474 * "sort({list})" function
17475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017477do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 typval_T *argvars;
17479 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017480 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481{
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 list_T *l;
17483 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017484 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 long len;
17486 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487
Bram Moolenaar0d660222005-01-07 21:51:51 +000017488 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017489 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 else
17491 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017493 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017494 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017495 return;
17496 rettv->vval.v_list = l;
17497 rettv->v_type = VAR_LIST;
17498 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500 len = list_len(l);
17501 if (len <= 1)
17502 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017505 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017507 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508 if (argvars[1].v_type != VAR_UNKNOWN)
17509 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017510 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017511 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017512 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017513 else
17514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017515 int error = FALSE;
17516
17517 i = get_tv_number_chk(&argvars[1], &error);
17518 if (error)
17519 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017520 if (i == 1)
17521 item_compare_ic = TRUE;
17522 else
17523 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017524 if (item_compare_func != NULL)
17525 {
17526 if (STRCMP(item_compare_func, "n") == 0)
17527 {
17528 item_compare_func = NULL;
17529 item_compare_numeric = TRUE;
17530 }
17531 else if (STRCMP(item_compare_func, "i") == 0)
17532 {
17533 item_compare_func = NULL;
17534 item_compare_ic = TRUE;
17535 }
17536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017537 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017538
17539 if (argvars[2].v_type != VAR_UNKNOWN)
17540 {
17541 /* optional third argument: {dict} */
17542 if (argvars[2].v_type != VAR_DICT)
17543 {
17544 EMSG(_(e_dictreq));
17545 return;
17546 }
17547 item_compare_selfdict = argvars[2].vval.v_dict;
17548 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017552 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553 if (ptrs == NULL)
17554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555
Bram Moolenaar327aa022014-03-25 18:24:23 +010017556 i = 0;
17557 if (sort)
17558 {
17559 /* sort(): ptrs will be the list to sort */
17560 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017561 {
17562 ptrs[i].item = li;
17563 ptrs[i].idx = i;
17564 ++i;
17565 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017566
17567 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017568 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017569 /* test the compare function */
17570 if (item_compare_func != NULL
17571 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017573 EMSG(_("E702: Sort compare function failed"));
17574 else
17575 {
17576 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017577 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017578 item_compare_func == NULL ? item_compare : item_compare2);
17579
17580 if (!item_compare_func_err)
17581 {
17582 /* Clear the List and append the items in sorted order. */
17583 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17584 l->lv_len = 0;
17585 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017586 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017587 }
17588 }
17589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017591 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017592 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17593
17594 /* f_uniq(): ptrs will be a stack of items to remove */
17595 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017596 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017597 item_compare_func_ptr = item_compare_func
17598 ? item_compare2 : item_compare;
17599
17600 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17601 li = li->li_next)
17602 {
17603 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17604 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017605 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017606 if (item_compare_func_err)
17607 {
17608 EMSG(_("E882: Uniq compare function failed"));
17609 break;
17610 }
17611 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017613 if (!item_compare_func_err)
17614 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017615 while (--i >= 0)
17616 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017617 li = ptrs[i].item->li_next;
17618 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017619 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017620 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017621 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017622 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017623 list_fix_watch(l, li);
17624 listitem_free(li);
17625 l->lv_len--;
17626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628 }
17629
17630 vim_free(ptrs);
17631 }
17632}
17633
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017634/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017635 * "sort({list})" function
17636 */
17637 static void
17638f_sort(argvars, rettv)
17639 typval_T *argvars;
17640 typval_T *rettv;
17641{
17642 do_sort_uniq(argvars, rettv, TRUE);
17643}
17644
17645/*
17646 * "uniq({list})" function
17647 */
17648 static void
17649f_uniq(argvars, rettv)
17650 typval_T *argvars;
17651 typval_T *rettv;
17652{
17653 do_sort_uniq(argvars, rettv, FALSE);
17654}
17655
17656/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017657 * "soundfold({word})" function
17658 */
17659 static void
17660f_soundfold(argvars, rettv)
17661 typval_T *argvars;
17662 typval_T *rettv;
17663{
17664 char_u *s;
17665
17666 rettv->v_type = VAR_STRING;
17667 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017668#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017669 rettv->vval.v_string = eval_soundfold(s);
17670#else
17671 rettv->vval.v_string = vim_strsave(s);
17672#endif
17673}
17674
17675/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017676 * "spellbadword()" function
17677 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017678 static void
17679f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017680 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017681 typval_T *rettv;
17682{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017683 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017684 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017685 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017687 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017688 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017689
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017690#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017691 if (argvars[0].v_type == VAR_UNKNOWN)
17692 {
17693 /* Find the start and length of the badly spelled word. */
17694 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17695 if (len != 0)
17696 word = ml_get_cursor();
17697 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017698 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017699 {
17700 char_u *str = get_tv_string_chk(&argvars[0]);
17701 int capcol = -1;
17702
17703 if (str != NULL)
17704 {
17705 /* Check the argument for spelling. */
17706 while (*str != NUL)
17707 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017708 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017709 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017710 {
17711 word = str;
17712 break;
17713 }
17714 str += len;
17715 }
17716 }
17717 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017718#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017719
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017720 list_append_string(rettv->vval.v_list, word, len);
17721 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017722 attr == HLF_SPB ? "bad" :
17723 attr == HLF_SPR ? "rare" :
17724 attr == HLF_SPL ? "local" :
17725 attr == HLF_SPC ? "caps" :
17726 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017727}
17728
17729/*
17730 * "spellsuggest()" function
17731 */
17732 static void
17733f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017734 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017735 typval_T *rettv;
17736{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017737#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017738 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017739 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017740 int maxcount;
17741 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017742 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017743 listitem_T *li;
17744 int need_capital = FALSE;
17745#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017746
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017747 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017748 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017749
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017750#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017751 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017752 {
17753 str = get_tv_string(&argvars[0]);
17754 if (argvars[1].v_type != VAR_UNKNOWN)
17755 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017756 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017757 if (maxcount <= 0)
17758 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017759 if (argvars[2].v_type != VAR_UNKNOWN)
17760 {
17761 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17762 if (typeerr)
17763 return;
17764 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017765 }
17766 else
17767 maxcount = 25;
17768
Bram Moolenaar4770d092006-01-12 23:22:24 +000017769 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017770
17771 for (i = 0; i < ga.ga_len; ++i)
17772 {
17773 str = ((char_u **)ga.ga_data)[i];
17774
17775 li = listitem_alloc();
17776 if (li == NULL)
17777 vim_free(str);
17778 else
17779 {
17780 li->li_tv.v_type = VAR_STRING;
17781 li->li_tv.v_lock = 0;
17782 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017783 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017784 }
17785 }
17786 ga_clear(&ga);
17787 }
17788#endif
17789}
17790
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017792f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017793 typval_T *argvars;
17794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017795{
17796 char_u *str;
17797 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017798 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017799 regmatch_T regmatch;
17800 char_u patbuf[NUMBUFLEN];
17801 char_u *save_cpo;
17802 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017803 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017804 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017805 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806
17807 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17808 save_cpo = p_cpo;
17809 p_cpo = (char_u *)"";
17810
17811 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017812 if (argvars[1].v_type != VAR_UNKNOWN)
17813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17815 if (pat == NULL)
17816 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017817 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017818 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017819 }
17820 if (pat == NULL || *pat == NUL)
17821 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017825 if (typeerr)
17826 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17829 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017831 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017832 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017833 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017834 if (*str == NUL)
17835 match = FALSE; /* empty item at the end */
17836 else
17837 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017838 if (match)
17839 end = regmatch.startp[0];
17840 else
17841 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017842 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17843 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017845 if (list_append_string(rettv->vval.v_list, str,
17846 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017847 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017848 }
17849 if (!match)
17850 break;
17851 /* Advance to just after the match. */
17852 if (regmatch.endp[0] > str)
17853 col = 0;
17854 else
17855 {
17856 /* Don't get stuck at the same match. */
17857#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017858 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017859#else
17860 col = 1;
17861#endif
17862 }
17863 str = regmatch.endp[0];
17864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865
Bram Moolenaar473de612013-06-08 18:19:48 +020017866 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870}
17871
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017872#ifdef FEAT_FLOAT
17873/*
17874 * "sqrt()" function
17875 */
17876 static void
17877f_sqrt(argvars, rettv)
17878 typval_T *argvars;
17879 typval_T *rettv;
17880{
17881 float_T f;
17882
17883 rettv->v_type = VAR_FLOAT;
17884 if (get_float_arg(argvars, &f) == OK)
17885 rettv->vval.v_float = sqrt(f);
17886 else
17887 rettv->vval.v_float = 0.0;
17888}
17889
17890/*
17891 * "str2float()" function
17892 */
17893 static void
17894f_str2float(argvars, rettv)
17895 typval_T *argvars;
17896 typval_T *rettv;
17897{
17898 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17899
17900 if (*p == '+')
17901 p = skipwhite(p + 1);
17902 (void)string2float(p, &rettv->vval.v_float);
17903 rettv->v_type = VAR_FLOAT;
17904}
17905#endif
17906
Bram Moolenaar2c932302006-03-18 21:42:09 +000017907/*
17908 * "str2nr()" function
17909 */
17910 static void
17911f_str2nr(argvars, rettv)
17912 typval_T *argvars;
17913 typval_T *rettv;
17914{
17915 int base = 10;
17916 char_u *p;
17917 long n;
17918
17919 if (argvars[1].v_type != VAR_UNKNOWN)
17920 {
17921 base = get_tv_number(&argvars[1]);
17922 if (base != 8 && base != 10 && base != 16)
17923 {
17924 EMSG(_(e_invarg));
17925 return;
17926 }
17927 }
17928
17929 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017930 if (*p == '+')
17931 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017932 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17933 rettv->vval.v_number = n;
17934}
17935
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936#ifdef HAVE_STRFTIME
17937/*
17938 * "strftime({format}[, {time}])" function
17939 */
17940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017941f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 typval_T *argvars;
17943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944{
17945 char_u result_buf[256];
17946 struct tm *curtime;
17947 time_t seconds;
17948 char_u *p;
17949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017953 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 seconds = time(NULL);
17955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017956 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 curtime = localtime(&seconds);
17958 /* MSVC returns NULL for an invalid value of seconds. */
17959 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 else
17962 {
17963# ifdef FEAT_MBYTE
17964 vimconv_T conv;
17965 char_u *enc;
17966
17967 conv.vc_type = CONV_NONE;
17968 enc = enc_locale();
17969 convert_setup(&conv, p_enc, enc);
17970 if (conv.vc_type != CONV_NONE)
17971 p = string_convert(&conv, p, NULL);
17972# endif
17973 if (p != NULL)
17974 (void)strftime((char *)result_buf, sizeof(result_buf),
17975 (char *)p, curtime);
17976 else
17977 result_buf[0] = NUL;
17978
17979# ifdef FEAT_MBYTE
17980 if (conv.vc_type != CONV_NONE)
17981 vim_free(p);
17982 convert_setup(&conv, enc, p_enc);
17983 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 else
17986# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988
17989# ifdef FEAT_MBYTE
17990 /* Release conversion descriptors */
17991 convert_setup(&conv, NULL, NULL);
17992 vim_free(enc);
17993# endif
17994 }
17995}
17996#endif
17997
17998/*
17999 * "stridx()" function
18000 */
18001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018002f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018003 typval_T *argvars;
18004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005{
18006 char_u buf[NUMBUFLEN];
18007 char_u *needle;
18008 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018009 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018013 needle = get_tv_string_chk(&argvars[1]);
18014 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018015 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018016 if (needle == NULL || haystack == NULL)
18017 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 if (argvars[2].v_type != VAR_UNKNOWN)
18020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018021 int error = FALSE;
18022
18023 start_idx = get_tv_number_chk(&argvars[2], &error);
18024 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018026 if (start_idx >= 0)
18027 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 }
18029
18030 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18031 if (pos != NULL)
18032 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033}
18034
18035/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018036 * "string()" function
18037 */
18038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 typval_T *argvars;
18041 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018042{
18043 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018046 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018047 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018048 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018049 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018050 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
18053/*
18054 * "strlen()" function
18055 */
18056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018057f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018058 typval_T *argvars;
18059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061 rettv->vval.v_number = (varnumber_T)(STRLEN(
18062 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063}
18064
18065/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018066 * "strchars()" function
18067 */
18068 static void
18069f_strchars(argvars, rettv)
18070 typval_T *argvars;
18071 typval_T *rettv;
18072{
18073 char_u *s = get_tv_string(&argvars[0]);
18074#ifdef FEAT_MBYTE
18075 varnumber_T len = 0;
18076
18077 while (*s != NUL)
18078 {
18079 mb_cptr2char_adv(&s);
18080 ++len;
18081 }
18082 rettv->vval.v_number = len;
18083#else
18084 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18085#endif
18086}
18087
18088/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018089 * "strdisplaywidth()" function
18090 */
18091 static void
18092f_strdisplaywidth(argvars, rettv)
18093 typval_T *argvars;
18094 typval_T *rettv;
18095{
18096 char_u *s = get_tv_string(&argvars[0]);
18097 int col = 0;
18098
18099 if (argvars[1].v_type != VAR_UNKNOWN)
18100 col = get_tv_number(&argvars[1]);
18101
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018102 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018103}
18104
18105/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018106 * "strwidth()" function
18107 */
18108 static void
18109f_strwidth(argvars, rettv)
18110 typval_T *argvars;
18111 typval_T *rettv;
18112{
18113 char_u *s = get_tv_string(&argvars[0]);
18114
18115 rettv->vval.v_number = (varnumber_T)(
18116#ifdef FEAT_MBYTE
18117 mb_string2cells(s, -1)
18118#else
18119 STRLEN(s)
18120#endif
18121 );
18122}
18123
18124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 * "strpart()" function
18126 */
18127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018129 typval_T *argvars;
18130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131{
18132 char_u *p;
18133 int n;
18134 int len;
18135 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018136 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 slen = (int)STRLEN(p);
18140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018141 n = get_tv_number_chk(&argvars[1], &error);
18142 if (error)
18143 len = 0;
18144 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 else
18147 len = slen - n; /* default len: all bytes that are available. */
18148
18149 /*
18150 * Only return the overlap between the specified part and the actual
18151 * string.
18152 */
18153 if (n < 0)
18154 {
18155 len += n;
18156 n = 0;
18157 }
18158 else if (n > slen)
18159 n = slen;
18160 if (len < 0)
18161 len = 0;
18162 else if (n + len > slen)
18163 len = slen - n;
18164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165 rettv->v_type = VAR_STRING;
18166 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167}
18168
18169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 * "strridx()" function
18171 */
18172 static void
18173f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018174 typval_T *argvars;
18175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176{
18177 char_u buf[NUMBUFLEN];
18178 char_u *needle;
18179 char_u *haystack;
18180 char_u *rest;
18181 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018182 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018184 needle = get_tv_string_chk(&argvars[1]);
18185 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018186
18187 rettv->vval.v_number = -1;
18188 if (needle == NULL || haystack == NULL)
18189 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018190
18191 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018192 if (argvars[2].v_type != VAR_UNKNOWN)
18193 {
18194 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018195 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018196 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018198 }
18199 else
18200 end_idx = haystack_len;
18201
Bram Moolenaar0d660222005-01-07 21:51:51 +000018202 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018203 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018204 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018205 lastmatch = haystack + end_idx;
18206 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018207 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018208 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 for (rest = haystack; *rest != '\0'; ++rest)
18210 {
18211 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018212 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213 break;
18214 lastmatch = rest;
18215 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217
18218 if (lastmatch == NULL)
18219 rettv->vval.v_number = -1;
18220 else
18221 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18222}
18223
18224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 * "strtrans()" function
18226 */
18227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018228f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018229 typval_T *argvars;
18230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232 rettv->v_type = VAR_STRING;
18233 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234}
18235
18236/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 * "submatch()" function
18238 */
18239 static void
18240f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 typval_T *argvars;
18242 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243{
Bram Moolenaar41571762014-04-02 19:00:58 +020018244 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018245 int no;
18246 int retList = 0;
18247
18248 no = (int)get_tv_number_chk(&argvars[0], &error);
18249 if (error)
18250 return;
18251 error = FALSE;
18252 if (argvars[1].v_type != VAR_UNKNOWN)
18253 retList = get_tv_number_chk(&argvars[1], &error);
18254 if (error)
18255 return;
18256
18257 if (retList == 0)
18258 {
18259 rettv->v_type = VAR_STRING;
18260 rettv->vval.v_string = reg_submatch(no);
18261 }
18262 else
18263 {
18264 rettv->v_type = VAR_LIST;
18265 rettv->vval.v_list = reg_submatch_list(no);
18266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267}
18268
18269/*
18270 * "substitute()" function
18271 */
18272 static void
18273f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018274 typval_T *argvars;
18275 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276{
18277 char_u patbuf[NUMBUFLEN];
18278 char_u subbuf[NUMBUFLEN];
18279 char_u flagsbuf[NUMBUFLEN];
18280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018281 char_u *str = get_tv_string_chk(&argvars[0]);
18282 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18283 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18284 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18285
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018287 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18288 rettv->vval.v_string = NULL;
18289 else
18290 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291}
18292
18293/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018294 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018298 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300{
18301 int id = 0;
18302#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018303 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 long col;
18305 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018306 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018308 lnum = get_tv_lnum(argvars); /* -1 on type error */
18309 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18310 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018312 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018313 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018314 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315#endif
18316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018317 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318}
18319
18320/*
18321 * "synIDattr(id, what [, mode])" function
18322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018324f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018325 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
18328 char_u *p = NULL;
18329#ifdef FEAT_SYN_HL
18330 int id;
18331 char_u *what;
18332 char_u *mode;
18333 char_u modebuf[NUMBUFLEN];
18334 int modec;
18335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018336 id = get_tv_number(&argvars[0]);
18337 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018342 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 modec = 0; /* replace invalid with current */
18344 }
18345 else
18346 {
18347#ifdef FEAT_GUI
18348 if (gui.in_use)
18349 modec = 'g';
18350 else
18351#endif
18352 if (t_colors > 1)
18353 modec = 'c';
18354 else
18355 modec = 't';
18356 }
18357
18358
18359 switch (TOLOWER_ASC(what[0]))
18360 {
18361 case 'b':
18362 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18363 p = highlight_color(id, what, modec);
18364 else /* bold */
18365 p = highlight_has_attr(id, HL_BOLD, modec);
18366 break;
18367
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018368 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 p = highlight_color(id, what, modec);
18370 break;
18371
18372 case 'i':
18373 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18374 p = highlight_has_attr(id, HL_INVERSE, modec);
18375 else /* italic */
18376 p = highlight_has_attr(id, HL_ITALIC, modec);
18377 break;
18378
18379 case 'n': /* name */
18380 p = get_highlight_name(NULL, id - 1);
18381 break;
18382
18383 case 'r': /* reverse */
18384 p = highlight_has_attr(id, HL_INVERSE, modec);
18385 break;
18386
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018387 case 's':
18388 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18389 p = highlight_color(id, what, modec);
18390 else /* standout */
18391 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 break;
18393
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018394 case 'u':
18395 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18396 /* underline */
18397 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18398 else
18399 /* undercurl */
18400 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 break;
18402 }
18403
18404 if (p != NULL)
18405 p = vim_strsave(p);
18406#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018407 rettv->v_type = VAR_STRING;
18408 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409}
18410
18411/*
18412 * "synIDtrans(id)" function
18413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018416 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418{
18419 int id;
18420
18421#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018422 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423
18424 if (id > 0)
18425 id = syn_get_final_id(id);
18426 else
18427#endif
18428 id = 0;
18429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018430 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431}
18432
18433/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018434 * "synconcealed(lnum, col)" function
18435 */
18436 static void
18437f_synconcealed(argvars, rettv)
18438 typval_T *argvars UNUSED;
18439 typval_T *rettv;
18440{
18441#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18442 long lnum;
18443 long col;
18444 int syntax_flags = 0;
18445 int cchar;
18446 int matchid = 0;
18447 char_u str[NUMBUFLEN];
18448#endif
18449
18450 rettv->v_type = VAR_LIST;
18451 rettv->vval.v_list = NULL;
18452
18453#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18454 lnum = get_tv_lnum(argvars); /* -1 on type error */
18455 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18456
18457 vim_memset(str, NUL, sizeof(str));
18458
18459 if (rettv_list_alloc(rettv) != FAIL)
18460 {
18461 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18462 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18463 && curwin->w_p_cole > 0)
18464 {
18465 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18466 syntax_flags = get_syntax_info(&matchid);
18467
18468 /* get the conceal character */
18469 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18470 {
18471 cchar = syn_get_sub_char();
18472 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18473 cchar = lcs_conceal;
18474 if (cchar != NUL)
18475 {
18476# ifdef FEAT_MBYTE
18477 if (has_mbyte)
18478 (*mb_char2bytes)(cchar, str);
18479 else
18480# endif
18481 str[0] = cchar;
18482 }
18483 }
18484 }
18485
18486 list_append_number(rettv->vval.v_list,
18487 (syntax_flags & HL_CONCEAL) != 0);
18488 /* -1 to auto-determine strlen */
18489 list_append_string(rettv->vval.v_list, str, -1);
18490 list_append_number(rettv->vval.v_list, matchid);
18491 }
18492#endif
18493}
18494
18495/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018496 * "synstack(lnum, col)" function
18497 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018498 static void
18499f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018500 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018501 typval_T *rettv;
18502{
18503#ifdef FEAT_SYN_HL
18504 long lnum;
18505 long col;
18506 int i;
18507 int id;
18508#endif
18509
18510 rettv->v_type = VAR_LIST;
18511 rettv->vval.v_list = NULL;
18512
18513#ifdef FEAT_SYN_HL
18514 lnum = get_tv_lnum(argvars); /* -1 on type error */
18515 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18516
18517 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018518 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018519 && rettv_list_alloc(rettv) != FAIL)
18520 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018521 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018522 for (i = 0; ; ++i)
18523 {
18524 id = syn_get_stack_item(i);
18525 if (id < 0)
18526 break;
18527 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18528 break;
18529 }
18530 }
18531#endif
18532}
18533
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018535get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018536 typval_T *argvars;
18537 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018538 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018540 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018542 char_u *infile = NULL;
18543 char_u buf[NUMBUFLEN];
18544 int err = FALSE;
18545 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018546 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018548 rettv->v_type = VAR_STRING;
18549 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018550 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018551 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018552
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018553 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018554 {
18555 /*
18556 * Write the string to a temp file, to be used for input of the shell
18557 * command.
18558 */
18559 if ((infile = vim_tempname('i')) == NULL)
18560 {
18561 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018562 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018563 }
18564
18565 fd = mch_fopen((char *)infile, WRITEBIN);
18566 if (fd == NULL)
18567 {
18568 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018569 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018570 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018571 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018572 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018573 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18574 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018575 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018576 else
18577 {
18578 p = get_tv_string_buf_chk(&argvars[1], buf);
18579 if (p == NULL)
18580 {
18581 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018582 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018583 }
18584 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18585 err = TRUE;
18586 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018587 if (fclose(fd) != 0)
18588 err = TRUE;
18589 if (err)
18590 {
18591 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018592 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018593 }
18594 }
18595
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018596 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018598 int len;
18599 listitem_T *li;
18600 char_u *s = NULL;
18601 char_u *start;
18602 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018603 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018605 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18606 SHELL_SILENT | SHELL_COOKED, &len);
18607 if (res == NULL)
18608 goto errret;
18609
18610 list = list_alloc();
18611 if (list == NULL)
18612 goto errret;
18613
18614 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018616 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018617 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018618 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018619 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018620
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018621 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018622 if (s == NULL)
18623 goto errret;
18624
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018625 for (p = s; start < end; ++p, ++start)
18626 *p = *start == NUL ? NL : *start;
18627 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018628
18629 li = listitem_alloc();
18630 if (li == NULL)
18631 {
18632 vim_free(s);
18633 goto errret;
18634 }
18635 li->li_tv.v_type = VAR_STRING;
18636 li->li_tv.vval.v_string = s;
18637 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018639
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018640 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018641 rettv->v_type = VAR_LIST;
18642 rettv->vval.v_list = list;
18643 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018645 else
18646 {
18647 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18648 SHELL_SILENT | SHELL_COOKED, NULL);
18649#ifdef USE_CR
18650 /* translate <CR> into <NL> */
18651 if (res != NULL)
18652 {
18653 char_u *s;
18654
18655 for (s = res; *s; ++s)
18656 {
18657 if (*s == CAR)
18658 *s = NL;
18659 }
18660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661#else
18662# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018663 /* translate <CR><NL> into <NL> */
18664 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018666 char_u *s, *d;
18667
18668 d = res;
18669 for (s = res; *s; ++s)
18670 {
18671 if (s[0] == CAR && s[1] == NL)
18672 ++s;
18673 *d++ = *s;
18674 }
18675 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677# endif
18678#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018679 rettv->vval.v_string = res;
18680 res = NULL;
18681 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018682
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018683errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018684 if (infile != NULL)
18685 {
18686 mch_remove(infile);
18687 vim_free(infile);
18688 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018689 if (res != NULL)
18690 vim_free(res);
18691 if (list != NULL)
18692 list_free(list, TRUE);
18693}
18694
18695/*
18696 * "system()" function
18697 */
18698 static void
18699f_system(argvars, rettv)
18700 typval_T *argvars;
18701 typval_T *rettv;
18702{
18703 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18704}
18705
18706/*
18707 * "systemlist()" function
18708 */
18709 static void
18710f_systemlist(argvars, rettv)
18711 typval_T *argvars;
18712 typval_T *rettv;
18713{
18714 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715}
18716
18717/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018718 * "tabpagebuflist()" function
18719 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018720 static void
18721f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018722 typval_T *argvars UNUSED;
18723 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018724{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018725#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018726 tabpage_T *tp;
18727 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018728
18729 if (argvars[0].v_type == VAR_UNKNOWN)
18730 wp = firstwin;
18731 else
18732 {
18733 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18734 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018735 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018736 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018737 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018738 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018739 for (; wp != NULL; wp = wp->w_next)
18740 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018741 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018742 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018743 }
18744#endif
18745}
18746
18747
18748/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018749 * "tabpagenr()" function
18750 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018751 static void
18752f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018753 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018754 typval_T *rettv;
18755{
18756 int nr = 1;
18757#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018758 char_u *arg;
18759
18760 if (argvars[0].v_type != VAR_UNKNOWN)
18761 {
18762 arg = get_tv_string_chk(&argvars[0]);
18763 nr = 0;
18764 if (arg != NULL)
18765 {
18766 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018767 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018768 else
18769 EMSG2(_(e_invexpr2), arg);
18770 }
18771 }
18772 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018773 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018774#endif
18775 rettv->vval.v_number = nr;
18776}
18777
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018778
18779#ifdef FEAT_WINDOWS
18780static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18781
18782/*
18783 * Common code for tabpagewinnr() and winnr().
18784 */
18785 static int
18786get_winnr(tp, argvar)
18787 tabpage_T *tp;
18788 typval_T *argvar;
18789{
18790 win_T *twin;
18791 int nr = 1;
18792 win_T *wp;
18793 char_u *arg;
18794
18795 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18796 if (argvar->v_type != VAR_UNKNOWN)
18797 {
18798 arg = get_tv_string_chk(argvar);
18799 if (arg == NULL)
18800 nr = 0; /* type error; errmsg already given */
18801 else if (STRCMP(arg, "$") == 0)
18802 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18803 else if (STRCMP(arg, "#") == 0)
18804 {
18805 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18806 if (twin == NULL)
18807 nr = 0;
18808 }
18809 else
18810 {
18811 EMSG2(_(e_invexpr2), arg);
18812 nr = 0;
18813 }
18814 }
18815
18816 if (nr > 0)
18817 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18818 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018819 {
18820 if (wp == NULL)
18821 {
18822 /* didn't find it in this tabpage */
18823 nr = 0;
18824 break;
18825 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018826 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018827 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018828 return nr;
18829}
18830#endif
18831
18832/*
18833 * "tabpagewinnr()" function
18834 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018835 static void
18836f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018837 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018838 typval_T *rettv;
18839{
18840 int nr = 1;
18841#ifdef FEAT_WINDOWS
18842 tabpage_T *tp;
18843
18844 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18845 if (tp == NULL)
18846 nr = 0;
18847 else
18848 nr = get_winnr(tp, &argvars[1]);
18849#endif
18850 rettv->vval.v_number = nr;
18851}
18852
18853
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018854/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018855 * "tagfiles()" function
18856 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018857 static void
18858f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018859 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018860 typval_T *rettv;
18861{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018862 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018863 tagname_T tn;
18864 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018865
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018866 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018867 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018868 fname = alloc(MAXPATHL);
18869 if (fname == NULL)
18870 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018871
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018872 for (first = TRUE; ; first = FALSE)
18873 if (get_tagfname(&tn, first, fname) == FAIL
18874 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018875 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018876 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018877 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018878}
18879
18880/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018881 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018882 */
18883 static void
18884f_taglist(argvars, rettv)
18885 typval_T *argvars;
18886 typval_T *rettv;
18887{
18888 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018889
18890 tag_pattern = get_tv_string(&argvars[0]);
18891
18892 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018893 if (*tag_pattern == NUL)
18894 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018895
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018896 if (rettv_list_alloc(rettv) == OK)
18897 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018898}
18899
18900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 * "tempname()" function
18902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907{
18908 static int x = 'A';
18909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018910 rettv->v_type = VAR_STRING;
18911 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912
18913 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18914 * names. Skip 'I' and 'O', they are used for shell redirection. */
18915 do
18916 {
18917 if (x == 'Z')
18918 x = '0';
18919 else if (x == '9')
18920 x = 'A';
18921 else
18922 {
18923#ifdef EBCDIC
18924 if (x == 'I')
18925 x = 'J';
18926 else if (x == 'R')
18927 x = 'S';
18928 else
18929#endif
18930 ++x;
18931 }
18932 } while (x == 'I' || x == 'O');
18933}
18934
18935/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018936 * "test(list)" function: Just checking the walls...
18937 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018938 static void
18939f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018940 typval_T *argvars UNUSED;
18941 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018942{
18943 /* Used for unit testing. Change the code below to your liking. */
18944#if 0
18945 listitem_T *li;
18946 list_T *l;
18947 char_u *bad, *good;
18948
18949 if (argvars[0].v_type != VAR_LIST)
18950 return;
18951 l = argvars[0].vval.v_list;
18952 if (l == NULL)
18953 return;
18954 li = l->lv_first;
18955 if (li == NULL)
18956 return;
18957 bad = get_tv_string(&li->li_tv);
18958 li = li->li_next;
18959 if (li == NULL)
18960 return;
18961 good = get_tv_string(&li->li_tv);
18962 rettv->vval.v_number = test_edit_score(bad, good);
18963#endif
18964}
18965
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018966#ifdef FEAT_FLOAT
18967/*
18968 * "tan()" function
18969 */
18970 static void
18971f_tan(argvars, rettv)
18972 typval_T *argvars;
18973 typval_T *rettv;
18974{
18975 float_T f;
18976
18977 rettv->v_type = VAR_FLOAT;
18978 if (get_float_arg(argvars, &f) == OK)
18979 rettv->vval.v_float = tan(f);
18980 else
18981 rettv->vval.v_float = 0.0;
18982}
18983
18984/*
18985 * "tanh()" function
18986 */
18987 static void
18988f_tanh(argvars, rettv)
18989 typval_T *argvars;
18990 typval_T *rettv;
18991{
18992 float_T f;
18993
18994 rettv->v_type = VAR_FLOAT;
18995 if (get_float_arg(argvars, &f) == OK)
18996 rettv->vval.v_float = tanh(f);
18997 else
18998 rettv->vval.v_float = 0.0;
18999}
19000#endif
19001
Bram Moolenaard52d9742005-08-21 22:20:28 +000019002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 * "tolower(string)" function
19004 */
19005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 typval_T *argvars;
19008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009{
19010 char_u *p;
19011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019012 p = vim_strsave(get_tv_string(&argvars[0]));
19013 rettv->v_type = VAR_STRING;
19014 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015
19016 if (p != NULL)
19017 while (*p != NUL)
19018 {
19019#ifdef FEAT_MBYTE
19020 int l;
19021
19022 if (enc_utf8)
19023 {
19024 int c, lc;
19025
19026 c = utf_ptr2char(p);
19027 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019028 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 /* TODO: reallocate string when byte count changes. */
19030 if (utf_char2len(lc) == l)
19031 utf_char2bytes(lc, p);
19032 p += l;
19033 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019034 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 p += l; /* skip multi-byte character */
19036 else
19037#endif
19038 {
19039 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19040 ++p;
19041 }
19042 }
19043}
19044
19045/*
19046 * "toupper(string)" function
19047 */
19048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 typval_T *argvars;
19051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019054 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055}
19056
19057/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019058 * "tr(string, fromstr, tostr)" function
19059 */
19060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019061f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019062 typval_T *argvars;
19063 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019064{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019065 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019066 char_u *fromstr;
19067 char_u *tostr;
19068 char_u *p;
19069#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019070 int inlen;
19071 int fromlen;
19072 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019073 int idx;
19074 char_u *cpstr;
19075 int cplen;
19076 int first = TRUE;
19077#endif
19078 char_u buf[NUMBUFLEN];
19079 char_u buf2[NUMBUFLEN];
19080 garray_T ga;
19081
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019082 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019083 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19084 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019085
19086 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087 rettv->v_type = VAR_STRING;
19088 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019089 if (fromstr == NULL || tostr == NULL)
19090 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019091 ga_init2(&ga, (int)sizeof(char), 80);
19092
19093#ifdef FEAT_MBYTE
19094 if (!has_mbyte)
19095#endif
19096 /* not multi-byte: fromstr and tostr must be the same length */
19097 if (STRLEN(fromstr) != STRLEN(tostr))
19098 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019099#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019100error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019101#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019102 EMSG2(_(e_invarg2), fromstr);
19103 ga_clear(&ga);
19104 return;
19105 }
19106
19107 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019108 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019109 {
19110#ifdef FEAT_MBYTE
19111 if (has_mbyte)
19112 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019113 inlen = (*mb_ptr2len)(in_str);
19114 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019115 cplen = inlen;
19116 idx = 0;
19117 for (p = fromstr; *p != NUL; p += fromlen)
19118 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019119 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019120 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019121 {
19122 for (p = tostr; *p != NUL; p += tolen)
19123 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019124 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019125 if (idx-- == 0)
19126 {
19127 cplen = tolen;
19128 cpstr = p;
19129 break;
19130 }
19131 }
19132 if (*p == NUL) /* tostr is shorter than fromstr */
19133 goto error;
19134 break;
19135 }
19136 ++idx;
19137 }
19138
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019139 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019140 {
19141 /* Check that fromstr and tostr have the same number of
19142 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019143 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019144 first = FALSE;
19145 for (p = tostr; *p != NUL; p += tolen)
19146 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019147 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019148 --idx;
19149 }
19150 if (idx != 0)
19151 goto error;
19152 }
19153
19154 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019155 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019156 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019157
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019158 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019159 }
19160 else
19161#endif
19162 {
19163 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019164 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019165 if (p != NULL)
19166 ga_append(&ga, tostr[p - fromstr]);
19167 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019168 ga_append(&ga, *in_str);
19169 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019170 }
19171 }
19172
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019173 /* add a terminating NUL */
19174 ga_grow(&ga, 1);
19175 ga_append(&ga, NUL);
19176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019178}
19179
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019180#ifdef FEAT_FLOAT
19181/*
19182 * "trunc({float})" function
19183 */
19184 static void
19185f_trunc(argvars, rettv)
19186 typval_T *argvars;
19187 typval_T *rettv;
19188{
19189 float_T f;
19190
19191 rettv->v_type = VAR_FLOAT;
19192 if (get_float_arg(argvars, &f) == OK)
19193 /* trunc() is not in C90, use floor() or ceil() instead. */
19194 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19195 else
19196 rettv->vval.v_float = 0.0;
19197}
19198#endif
19199
Bram Moolenaar8299df92004-07-10 09:47:34 +000019200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 * "type(expr)" function
19202 */
19203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019205 typval_T *argvars;
19206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019208 int n;
19209
19210 switch (argvars[0].v_type)
19211 {
19212 case VAR_NUMBER: n = 0; break;
19213 case VAR_STRING: n = 1; break;
19214 case VAR_FUNC: n = 2; break;
19215 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019216 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019217#ifdef FEAT_FLOAT
19218 case VAR_FLOAT: n = 5; break;
19219#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019220 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19221 }
19222 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223}
19224
19225/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019226 * "undofile(name)" function
19227 */
19228 static void
19229f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019230 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019231 typval_T *rettv;
19232{
19233 rettv->v_type = VAR_STRING;
19234#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019235 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019236 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019237
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019238 if (*fname == NUL)
19239 {
19240 /* If there is no file name there will be no undo file. */
19241 rettv->vval.v_string = NULL;
19242 }
19243 else
19244 {
19245 char_u *ffname = FullName_save(fname, FALSE);
19246
19247 if (ffname != NULL)
19248 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19249 vim_free(ffname);
19250 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019251 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019252#else
19253 rettv->vval.v_string = NULL;
19254#endif
19255}
19256
19257/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019258 * "undotree()" function
19259 */
19260 static void
19261f_undotree(argvars, rettv)
19262 typval_T *argvars UNUSED;
19263 typval_T *rettv;
19264{
19265 if (rettv_dict_alloc(rettv) == OK)
19266 {
19267 dict_T *dict = rettv->vval.v_dict;
19268 list_T *list;
19269
Bram Moolenaar730cde92010-06-27 05:18:54 +020019270 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019271 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019272 dict_add_nr_str(dict, "save_last",
19273 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019274 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19275 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019276 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019277
19278 list = list_alloc();
19279 if (list != NULL)
19280 {
19281 u_eval_tree(curbuf->b_u_oldhead, list);
19282 dict_add_list(dict, "entries", list);
19283 }
19284 }
19285}
19286
19287/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019288 * "values(dict)" function
19289 */
19290 static void
19291f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019292 typval_T *argvars;
19293 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019294{
19295 dict_list(argvars, rettv, 1);
19296}
19297
19298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 * "virtcol(string)" function
19300 */
19301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019302f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019303 typval_T *argvars;
19304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305{
19306 colnr_T vcol = 0;
19307 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019308 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019310 fp = var2fpos(&argvars[0], FALSE, &fnum);
19311 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19312 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 {
19314 getvvcol(curwin, fp, NULL, NULL, &vcol);
19315 ++vcol;
19316 }
19317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321/*
19322 * "visualmode()" function
19323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019326 typval_T *argvars UNUSED;
19327 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 char_u str[2];
19330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 str[0] = curbuf->b_visual_mode_eval;
19333 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335
19336 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019337 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339}
19340
19341/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019342 * "wildmenumode()" function
19343 */
19344 static void
19345f_wildmenumode(argvars, rettv)
19346 typval_T *argvars UNUSED;
19347 typval_T *rettv UNUSED;
19348{
19349#ifdef FEAT_WILDMENU
19350 if (wild_menu_showing)
19351 rettv->vval.v_number = 1;
19352#endif
19353}
19354
19355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 * "winbufnr(nr)" function
19357 */
19358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019360 typval_T *argvars;
19361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362{
19363 win_T *wp;
19364
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019365 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019369 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370}
19371
19372/*
19373 * "wincol()" function
19374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379{
19380 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382}
19383
19384/*
19385 * "winheight(nr)" function
19386 */
19387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 typval_T *argvars;
19390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391{
19392 win_T *wp;
19393
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019394 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399}
19400
19401/*
19402 * "winline()" function
19403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408{
19409 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411}
19412
19413/*
19414 * "winnr()" function
19415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019417f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019418 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420{
19421 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019422
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019424 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427}
19428
19429/*
19430 * "winrestcmd()" function
19431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019433f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436{
19437#ifdef FEAT_WINDOWS
19438 win_T *wp;
19439 int winnr = 1;
19440 garray_T ga;
19441 char_u buf[50];
19442
19443 ga_init2(&ga, (int)sizeof(char), 70);
19444 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19445 {
19446 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19447 ga_concat(&ga, buf);
19448# ifdef FEAT_VERTSPLIT
19449 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19450 ga_concat(&ga, buf);
19451# endif
19452 ++winnr;
19453 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019454 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019456 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461}
19462
19463/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019464 * "winrestview()" function
19465 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019466 static void
19467f_winrestview(argvars, rettv)
19468 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019469 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019470{
19471 dict_T *dict;
19472
19473 if (argvars[0].v_type != VAR_DICT
19474 || (dict = argvars[0].vval.v_dict) == NULL)
19475 EMSG(_(e_invarg));
19476 else
19477 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019478 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19479 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19480 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19481 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019482#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019483 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19484 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019485#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019486 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19487 {
19488 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19489 curwin->w_set_curswant = FALSE;
19490 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019491
Bram Moolenaar82c25852014-05-28 16:47:16 +020019492 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19493 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019494#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019495 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19496 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019497#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019498 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19499 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19500 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19501 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019502
19503 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019504 win_new_height(curwin, curwin->w_height);
19505# ifdef FEAT_VERTSPLIT
19506 win_new_width(curwin, W_WIDTH(curwin));
19507# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019508 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019509
19510 if (curwin->w_topline == 0)
19511 curwin->w_topline = 1;
19512 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19513 curwin->w_topline = curbuf->b_ml.ml_line_count;
19514#ifdef FEAT_DIFF
19515 check_topfill(curwin, TRUE);
19516#endif
19517 }
19518}
19519
19520/*
19521 * "winsaveview()" function
19522 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019523 static void
19524f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019525 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019526 typval_T *rettv;
19527{
19528 dict_T *dict;
19529
Bram Moolenaara800b422010-06-27 01:15:55 +020019530 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019531 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019532 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019533
19534 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19535 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19536#ifdef FEAT_VIRTUALEDIT
19537 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19538#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019539 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019540 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19541
19542 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19543#ifdef FEAT_DIFF
19544 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19545#endif
19546 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19547 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19548}
19549
19550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 * "winwidth(nr)" function
19552 */
19553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019554f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 typval_T *argvars;
19556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557{
19558 win_T *wp;
19559
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019560 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 else
19564#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568#endif
19569}
19570
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019572 * Write list of strings to file
19573 */
19574 static int
19575write_list(fd, list, binary)
19576 FILE *fd;
19577 list_T *list;
19578 int binary;
19579{
19580 listitem_T *li;
19581 int c;
19582 int ret = OK;
19583 char_u *s;
19584
19585 for (li = list->lv_first; li != NULL; li = li->li_next)
19586 {
19587 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19588 {
19589 if (*s == '\n')
19590 c = putc(NUL, fd);
19591 else
19592 c = putc(*s, fd);
19593 if (c == EOF)
19594 {
19595 ret = FAIL;
19596 break;
19597 }
19598 }
19599 if (!binary || li->li_next != NULL)
19600 if (putc('\n', fd) == EOF)
19601 {
19602 ret = FAIL;
19603 break;
19604 }
19605 if (ret == FAIL)
19606 {
19607 EMSG(_(e_write));
19608 break;
19609 }
19610 }
19611 return ret;
19612}
19613
19614/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019615 * "writefile()" function
19616 */
19617 static void
19618f_writefile(argvars, rettv)
19619 typval_T *argvars;
19620 typval_T *rettv;
19621{
19622 int binary = FALSE;
19623 char_u *fname;
19624 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019625 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019626
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019627 if (check_restricted() || check_secure())
19628 return;
19629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019630 if (argvars[0].v_type != VAR_LIST)
19631 {
19632 EMSG2(_(e_listarg), "writefile()");
19633 return;
19634 }
19635 if (argvars[0].vval.v_list == NULL)
19636 return;
19637
19638 if (argvars[2].v_type != VAR_UNKNOWN
19639 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19640 binary = TRUE;
19641
19642 /* Always open the file in binary mode, library functions have a mind of
19643 * their own about CR-LF conversion. */
19644 fname = get_tv_string(&argvars[1]);
19645 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19646 {
19647 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19648 ret = -1;
19649 }
19650 else
19651 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019652 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19653 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019654 fclose(fd);
19655 }
19656
19657 rettv->vval.v_number = ret;
19658}
19659
19660/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019661 * "xor(expr, expr)" function
19662 */
19663 static void
19664f_xor(argvars, rettv)
19665 typval_T *argvars;
19666 typval_T *rettv;
19667{
19668 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19669 ^ get_tv_number_chk(&argvars[1], NULL);
19670}
19671
19672
19673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019675 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 */
19677 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019678var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019680 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019681 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019683 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019685 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
Bram Moolenaara5525202006-03-02 22:52:09 +000019687 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019688 if (varp->v_type == VAR_LIST)
19689 {
19690 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019691 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019692 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019693 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019694
19695 l = varp->vval.v_list;
19696 if (l == NULL)
19697 return NULL;
19698
19699 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019700 pos.lnum = list_find_nr(l, 0L, &error);
19701 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019702 return NULL; /* invalid line number */
19703
19704 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019705 pos.col = list_find_nr(l, 1L, &error);
19706 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019707 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019708 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019709
19710 /* We accept "$" for the column number: last column. */
19711 li = list_find(l, 1L);
19712 if (li != NULL && li->li_tv.v_type == VAR_STRING
19713 && li->li_tv.vval.v_string != NULL
19714 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19715 pos.col = len + 1;
19716
Bram Moolenaara5525202006-03-02 22:52:09 +000019717 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019718 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019719 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019720 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019721
Bram Moolenaara5525202006-03-02 22:52:09 +000019722#ifdef FEAT_VIRTUALEDIT
19723 /* Get the virtual offset. Defaults to zero. */
19724 pos.coladd = list_find_nr(l, 2L, &error);
19725 if (error)
19726 pos.coladd = 0;
19727#endif
19728
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019729 return &pos;
19730 }
19731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019732 name = get_tv_string_chk(varp);
19733 if (name == NULL)
19734 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019735 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019737 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19738 {
19739 if (VIsual_active)
19740 return &VIsual;
19741 return &curwin->w_cursor;
19742 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019743 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019745 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19747 return NULL;
19748 return pp;
19749 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019750
19751#ifdef FEAT_VIRTUALEDIT
19752 pos.coladd = 0;
19753#endif
19754
Bram Moolenaar477933c2007-07-17 14:32:23 +000019755 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019756 {
19757 pos.col = 0;
19758 if (name[1] == '0') /* "w0": first visible line */
19759 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019760 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019761 pos.lnum = curwin->w_topline;
19762 return &pos;
19763 }
19764 else if (name[1] == '$') /* "w$": last visible line */
19765 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019766 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019767 pos.lnum = curwin->w_botline - 1;
19768 return &pos;
19769 }
19770 }
19771 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019773 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 {
19775 pos.lnum = curbuf->b_ml.ml_line_count;
19776 pos.col = 0;
19777 }
19778 else
19779 {
19780 pos.lnum = curwin->w_cursor.lnum;
19781 pos.col = (colnr_T)STRLEN(ml_get_curline());
19782 }
19783 return &pos;
19784 }
19785 return NULL;
19786}
19787
19788/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019789 * Convert list in "arg" into a position and optional file number.
19790 * When "fnump" is NULL there is no file number, only 3 items.
19791 * Note that the column is passed on as-is, the caller may want to decrement
19792 * it to use 1 for the first column.
19793 * Return FAIL when conversion is not possible, doesn't check the position for
19794 * validity.
19795 */
19796 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019797list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019798 typval_T *arg;
19799 pos_T *posp;
19800 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019801 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019802{
19803 list_T *l = arg->vval.v_list;
19804 long i = 0;
19805 long n;
19806
Bram Moolenaar493c1782014-05-28 14:34:46 +020019807 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19808 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019809 if (arg->v_type != VAR_LIST
19810 || l == NULL
19811 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019812 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019813 return FAIL;
19814
19815 if (fnump != NULL)
19816 {
19817 n = list_find_nr(l, i++, NULL); /* fnum */
19818 if (n < 0)
19819 return FAIL;
19820 if (n == 0)
19821 n = curbuf->b_fnum; /* current buffer */
19822 *fnump = n;
19823 }
19824
19825 n = list_find_nr(l, i++, NULL); /* lnum */
19826 if (n < 0)
19827 return FAIL;
19828 posp->lnum = n;
19829
19830 n = list_find_nr(l, i++, NULL); /* col */
19831 if (n < 0)
19832 return FAIL;
19833 posp->col = n;
19834
19835#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019836 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019837 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019838 posp->coladd = 0;
19839 else
19840 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019841#endif
19842
Bram Moolenaar493c1782014-05-28 14:34:46 +020019843 if (curswantp != NULL)
19844 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19845
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019846 return OK;
19847}
19848
19849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 * Get the length of an environment variable name.
19851 * Advance "arg" to the first character after the name.
19852 * Return 0 for error.
19853 */
19854 static int
19855get_env_len(arg)
19856 char_u **arg;
19857{
19858 char_u *p;
19859 int len;
19860
19861 for (p = *arg; vim_isIDc(*p); ++p)
19862 ;
19863 if (p == *arg) /* no name found */
19864 return 0;
19865
19866 len = (int)(p - *arg);
19867 *arg = p;
19868 return len;
19869}
19870
19871/*
19872 * Get the length of the name of a function or internal variable.
19873 * "arg" is advanced to the first non-white character after the name.
19874 * Return 0 if something is wrong.
19875 */
19876 static int
19877get_id_len(arg)
19878 char_u **arg;
19879{
19880 char_u *p;
19881 int len;
19882
19883 /* Find the end of the name. */
19884 for (p = *arg; eval_isnamec(*p); ++p)
19885 ;
19886 if (p == *arg) /* no name found */
19887 return 0;
19888
19889 len = (int)(p - *arg);
19890 *arg = skipwhite(p);
19891
19892 return len;
19893}
19894
19895/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019896 * Get the length of the name of a variable or function.
19897 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019899 * Return -1 if curly braces expansion failed.
19900 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 * If the name contains 'magic' {}'s, expand them and return the
19902 * expanded name in an allocated string via 'alias' - caller must free.
19903 */
19904 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019905get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 char_u **arg;
19907 char_u **alias;
19908 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019909 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910{
19911 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 char_u *p;
19913 char_u *expr_start;
19914 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915
19916 *alias = NULL; /* default to no alias */
19917
19918 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19919 && (*arg)[2] == (int)KE_SNR)
19920 {
19921 /* hard coded <SNR>, already translated */
19922 *arg += 3;
19923 return get_id_len(arg) + 3;
19924 }
19925 len = eval_fname_script(*arg);
19926 if (len > 0)
19927 {
19928 /* literal "<SID>", "s:" or "<SNR>" */
19929 *arg += len;
19930 }
19931
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019935 p = find_name_end(*arg, &expr_start, &expr_end,
19936 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 if (expr_start != NULL)
19938 {
19939 char_u *temp_string;
19940
19941 if (!evaluate)
19942 {
19943 len += (int)(p - *arg);
19944 *arg = skipwhite(p);
19945 return len;
19946 }
19947
19948 /*
19949 * Include any <SID> etc in the expanded string:
19950 * Thus the -len here.
19951 */
19952 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19953 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019954 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 *alias = temp_string;
19956 *arg = skipwhite(p);
19957 return (int)STRLEN(temp_string);
19958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959
19960 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019961 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 EMSG2(_(e_invexpr2), *arg);
19963
19964 return len;
19965}
19966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967/*
19968 * Find the end of a variable or function name, taking care of magic braces.
19969 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19970 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019971 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 * Return a pointer to just after the name. Equal to "arg" if there is no
19973 * valid name.
19974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019976find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 char_u *arg;
19978 char_u **expr_start;
19979 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019980 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 int mb_nest = 0;
19983 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 char_u *p;
19985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019988 *expr_start = NULL;
19989 *expr_end = NULL;
19990 }
19991
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019992 /* Quick check for valid starting character. */
19993 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19994 return arg;
19995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 for (p = arg; *p != NUL
19997 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019998 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019999 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020001 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020003 if (*p == '\'')
20004 {
20005 /* skip over 'string' to avoid counting [ and ] inside it. */
20006 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20007 ;
20008 if (*p == NUL)
20009 break;
20010 }
20011 else if (*p == '"')
20012 {
20013 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20014 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20015 if (*p == '\\' && p[1] != NUL)
20016 ++p;
20017 if (*p == NUL)
20018 break;
20019 }
20020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020021 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 if (*p == '[')
20024 ++br_nest;
20025 else if (*p == ']')
20026 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 if (*p == '{')
20032 {
20033 mb_nest++;
20034 if (expr_start != NULL && *expr_start == NULL)
20035 *expr_start = p;
20036 }
20037 else if (*p == '}')
20038 {
20039 mb_nest--;
20040 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20041 *expr_end = p;
20042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 }
20045
20046 return p;
20047}
20048
20049/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020050 * Expands out the 'magic' {}'s in a variable/function name.
20051 * Note that this can call itself recursively, to deal with
20052 * constructs like foo{bar}{baz}{bam}
20053 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20054 * "in_start" ^
20055 * "expr_start" ^
20056 * "expr_end" ^
20057 * "in_end" ^
20058 *
20059 * Returns a new allocated string, which the caller must free.
20060 * Returns NULL for failure.
20061 */
20062 static char_u *
20063make_expanded_name(in_start, expr_start, expr_end, in_end)
20064 char_u *in_start;
20065 char_u *expr_start;
20066 char_u *expr_end;
20067 char_u *in_end;
20068{
20069 char_u c1;
20070 char_u *retval = NULL;
20071 char_u *temp_result;
20072 char_u *nextcmd = NULL;
20073
20074 if (expr_end == NULL || in_end == NULL)
20075 return NULL;
20076 *expr_start = NUL;
20077 *expr_end = NUL;
20078 c1 = *in_end;
20079 *in_end = NUL;
20080
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020081 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020082 if (temp_result != NULL && nextcmd == NULL)
20083 {
20084 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20085 + (in_end - expr_end) + 1));
20086 if (retval != NULL)
20087 {
20088 STRCPY(retval, in_start);
20089 STRCAT(retval, temp_result);
20090 STRCAT(retval, expr_end + 1);
20091 }
20092 }
20093 vim_free(temp_result);
20094
20095 *in_end = c1; /* put char back for error messages */
20096 *expr_start = '{';
20097 *expr_end = '}';
20098
20099 if (retval != NULL)
20100 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020101 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020102 if (expr_start != NULL)
20103 {
20104 /* Further expansion! */
20105 temp_result = make_expanded_name(retval, expr_start,
20106 expr_end, temp_result);
20107 vim_free(retval);
20108 retval = temp_result;
20109 }
20110 }
20111
20112 return retval;
20113}
20114
20115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020117 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 */
20119 static int
20120eval_isnamec(c)
20121 int c;
20122{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020123 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20124}
20125
20126/*
20127 * Return TRUE if character "c" can be used as the first character in a
20128 * variable or function name (excluding '{' and '}').
20129 */
20130 static int
20131eval_isnamec1(c)
20132 int c;
20133{
20134 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135}
20136
20137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 * Set number v: variable to "val".
20139 */
20140 void
20141set_vim_var_nr(idx, val)
20142 int idx;
20143 long val;
20144{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020145 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146}
20147
20148/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020149 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 */
20151 long
20152get_vim_var_nr(idx)
20153 int idx;
20154{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020155 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020158/*
20159 * Get string v: variable value. Uses a static buffer, can only be used once.
20160 */
20161 char_u *
20162get_vim_var_str(idx)
20163 int idx;
20164{
20165 return get_tv_string(&vimvars[idx].vv_tv);
20166}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020167
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020169 * Get List v: variable value. Caller must take care of reference count when
20170 * needed.
20171 */
20172 list_T *
20173get_vim_var_list(idx)
20174 int idx;
20175{
20176 return vimvars[idx].vv_list;
20177}
20178
20179/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020180 * Set v:char to character "c".
20181 */
20182 void
20183set_vim_var_char(c)
20184 int c;
20185{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020186 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020187
20188#ifdef FEAT_MBYTE
20189 if (has_mbyte)
20190 buf[(*mb_char2bytes)(c, buf)] = NUL;
20191 else
20192#endif
20193 {
20194 buf[0] = c;
20195 buf[1] = NUL;
20196 }
20197 set_vim_var_string(VV_CHAR, buf, -1);
20198}
20199
20200/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020201 * Set v:count to "count" and v:count1 to "count1".
20202 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 */
20204 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020205set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 long count;
20207 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020208 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020210 if (set_prevcount)
20211 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020212 vimvars[VV_COUNT].vv_nr = count;
20213 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214}
20215
20216/*
20217 * Set string v: variable to a copy of "val".
20218 */
20219 void
20220set_vim_var_string(idx, val, len)
20221 int idx;
20222 char_u *val;
20223 int len; /* length of "val" to use or -1 (whole string) */
20224{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020225 /* Need to do this (at least) once, since we can't initialize a union.
20226 * Will always be invoked when "v:progname" is set. */
20227 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20228
Bram Moolenaare9a41262005-01-15 22:18:47 +000020229 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020231 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020233 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020235 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
20238/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020239 * Set List v: variable to "val".
20240 */
20241 void
20242set_vim_var_list(idx, val)
20243 int idx;
20244 list_T *val;
20245{
20246 list_unref(vimvars[idx].vv_list);
20247 vimvars[idx].vv_list = val;
20248 if (val != NULL)
20249 ++val->lv_refcount;
20250}
20251
20252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 * Set v:register if needed.
20254 */
20255 void
20256set_reg_var(c)
20257 int c;
20258{
20259 char_u regname;
20260
20261 if (c == 0 || c == ' ')
20262 regname = '"';
20263 else
20264 regname = c;
20265 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020266 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 set_vim_var_string(VV_REG, &regname, 1);
20268}
20269
20270/*
20271 * Get or set v:exception. If "oldval" == NULL, return the current value.
20272 * Otherwise, restore the value to "oldval" and return NULL.
20273 * Must always be called in pairs to save and restore v:exception! Does not
20274 * take care of memory allocations.
20275 */
20276 char_u *
20277v_exception(oldval)
20278 char_u *oldval;
20279{
20280 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020281 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282
Bram Moolenaare9a41262005-01-15 22:18:47 +000020283 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 return NULL;
20285}
20286
20287/*
20288 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20289 * Otherwise, restore the value to "oldval" and return NULL.
20290 * Must always be called in pairs to save and restore v:throwpoint! Does not
20291 * take care of memory allocations.
20292 */
20293 char_u *
20294v_throwpoint(oldval)
20295 char_u *oldval;
20296{
20297 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020298 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299
Bram Moolenaare9a41262005-01-15 22:18:47 +000020300 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 return NULL;
20302}
20303
20304#if defined(FEAT_AUTOCMD) || defined(PROTO)
20305/*
20306 * Set v:cmdarg.
20307 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20308 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20309 * Must always be called in pairs!
20310 */
20311 char_u *
20312set_cmdarg(eap, oldarg)
20313 exarg_T *eap;
20314 char_u *oldarg;
20315{
20316 char_u *oldval;
20317 char_u *newval;
20318 unsigned len;
20319
Bram Moolenaare9a41262005-01-15 22:18:47 +000020320 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020321 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020323 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020324 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020325 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 }
20327
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020328 if (eap->force_bin == FORCE_BIN)
20329 len = 6;
20330 else if (eap->force_bin == FORCE_NOBIN)
20331 len = 8;
20332 else
20333 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020334
20335 if (eap->read_edit)
20336 len += 7;
20337
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020338 if (eap->force_ff != 0)
20339 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20340# ifdef FEAT_MBYTE
20341 if (eap->force_enc != 0)
20342 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020343 if (eap->bad_char != 0)
20344 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020345# endif
20346
20347 newval = alloc(len + 1);
20348 if (newval == NULL)
20349 return NULL;
20350
20351 if (eap->force_bin == FORCE_BIN)
20352 sprintf((char *)newval, " ++bin");
20353 else if (eap->force_bin == FORCE_NOBIN)
20354 sprintf((char *)newval, " ++nobin");
20355 else
20356 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020357
20358 if (eap->read_edit)
20359 STRCAT(newval, " ++edit");
20360
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020361 if (eap->force_ff != 0)
20362 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20363 eap->cmd + eap->force_ff);
20364# ifdef FEAT_MBYTE
20365 if (eap->force_enc != 0)
20366 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20367 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020368 if (eap->bad_char == BAD_KEEP)
20369 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20370 else if (eap->bad_char == BAD_DROP)
20371 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20372 else if (eap->bad_char != 0)
20373 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020374# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020375 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020376 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377}
20378#endif
20379
20380/*
20381 * Get the value of internal variable "name".
20382 * Return OK or FAIL.
20383 */
20384 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020385get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 char_u *name;
20387 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020388 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020389 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020390 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391{
20392 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 typval_T *tv = NULL;
20394 typval_T atv;
20395 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397
20398 /* truncate the name, so that we can use strcmp() */
20399 cc = name[len];
20400 name[len] = NUL;
20401
20402 /*
20403 * Check for "b:changedtick".
20404 */
20405 if (STRCMP(name, "b:changedtick") == 0)
20406 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020407 atv.v_type = VAR_NUMBER;
20408 atv.vval.v_number = curbuf->b_changedtick;
20409 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 }
20411
20412 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 * Check for user-defined variables.
20414 */
20415 else
20416 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020417 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020419 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 }
20421
Bram Moolenaare9a41262005-01-15 22:18:47 +000020422 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020424 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 ret = FAIL;
20427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020428 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020429 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430
20431 name[len] = cc;
20432
20433 return ret;
20434}
20435
20436/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020437 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20438 * Also handle function call with Funcref variable: func(expr)
20439 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20440 */
20441 static int
20442handle_subscript(arg, rettv, evaluate, verbose)
20443 char_u **arg;
20444 typval_T *rettv;
20445 int evaluate; /* do more than finding the end */
20446 int verbose; /* give error messages */
20447{
20448 int ret = OK;
20449 dict_T *selfdict = NULL;
20450 char_u *s;
20451 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020452 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020453
20454 while (ret == OK
20455 && (**arg == '['
20456 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020457 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020458 && !vim_iswhite(*(*arg - 1)))
20459 {
20460 if (**arg == '(')
20461 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020462 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020463 if (evaluate)
20464 {
20465 functv = *rettv;
20466 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020467
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020468 /* Invoke the function. Recursive! */
20469 s = functv.vval.v_string;
20470 }
20471 else
20472 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020473 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020474 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20475 &len, evaluate, selfdict);
20476
20477 /* Clear the funcref afterwards, so that deleting it while
20478 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020479 if (evaluate)
20480 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020481
20482 /* Stop the expression evaluation when immediately aborting on
20483 * error, or when an interrupt occurred or an exception was thrown
20484 * but not caught. */
20485 if (aborting())
20486 {
20487 if (ret == OK)
20488 clear_tv(rettv);
20489 ret = FAIL;
20490 }
20491 dict_unref(selfdict);
20492 selfdict = NULL;
20493 }
20494 else /* **arg == '[' || **arg == '.' */
20495 {
20496 dict_unref(selfdict);
20497 if (rettv->v_type == VAR_DICT)
20498 {
20499 selfdict = rettv->vval.v_dict;
20500 if (selfdict != NULL)
20501 ++selfdict->dv_refcount;
20502 }
20503 else
20504 selfdict = NULL;
20505 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20506 {
20507 clear_tv(rettv);
20508 ret = FAIL;
20509 }
20510 }
20511 }
20512 dict_unref(selfdict);
20513 return ret;
20514}
20515
20516/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020517 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020518 * value).
20519 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020520 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020522{
Bram Moolenaar33570922005-01-25 22:26:29 +000020523 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020524}
20525
20526/*
20527 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 * The string "s" must have been allocated, it is consumed.
20529 * Return NULL for out of memory, the variable otherwise.
20530 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020531 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020532alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 char_u *s;
20534{
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020537 rettv = alloc_tv();
20538 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020540 rettv->v_type = VAR_STRING;
20541 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 }
20543 else
20544 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020545 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546}
20547
20548/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020549 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020551 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020552free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554{
20555 if (varp != NULL)
20556 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020557 switch (varp->v_type)
20558 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020559 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 func_unref(varp->vval.v_string);
20561 /*FALLTHROUGH*/
20562 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020563 vim_free(varp->vval.v_string);
20564 break;
20565 case VAR_LIST:
20566 list_unref(varp->vval.v_list);
20567 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020568 case VAR_DICT:
20569 dict_unref(varp->vval.v_dict);
20570 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020571 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020572#ifdef FEAT_FLOAT
20573 case VAR_FLOAT:
20574#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020575 case VAR_UNKNOWN:
20576 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020577 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020578 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020579 break;
20580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 vim_free(varp);
20582 }
20583}
20584
20585/*
20586 * Free the memory for a variable value and set the value to NULL or 0.
20587 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020588 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591{
20592 if (varp != NULL)
20593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020594 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020596 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020597 func_unref(varp->vval.v_string);
20598 /*FALLTHROUGH*/
20599 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020600 vim_free(varp->vval.v_string);
20601 varp->vval.v_string = NULL;
20602 break;
20603 case VAR_LIST:
20604 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020605 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020606 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020607 case VAR_DICT:
20608 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020609 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020610 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020612 varp->vval.v_number = 0;
20613 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020614#ifdef FEAT_FLOAT
20615 case VAR_FLOAT:
20616 varp->vval.v_float = 0.0;
20617 break;
20618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020619 case VAR_UNKNOWN:
20620 break;
20621 default:
20622 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020624 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 }
20626}
20627
20628/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 * Set the value of a variable to NULL without freeing items.
20630 */
20631 static void
20632init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020633 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634{
20635 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020636 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637}
20638
20639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 * Get the number value of a variable.
20641 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020642 * For incompatible types, return 0.
20643 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20644 * caller of incompatible types: it sets *denote to TRUE if "denote"
20645 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 */
20647 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020648get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020651 int error = FALSE;
20652
20653 return get_tv_number_chk(varp, &error); /* return 0L on error */
20654}
20655
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020656 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020657get_tv_number_chk(varp, denote)
20658 typval_T *varp;
20659 int *denote;
20660{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020661 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020663 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020665 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020666 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020667#ifdef FEAT_FLOAT
20668 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020669 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020670 break;
20671#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020672 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020673 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020674 break;
20675 case VAR_STRING:
20676 if (varp->vval.v_string != NULL)
20677 vim_str2nr(varp->vval.v_string, NULL, NULL,
20678 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020679 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020680 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020681 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020682 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020683 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020684 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020685 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020686 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020687 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020690 if (denote == NULL) /* useful for values that must be unsigned */
20691 n = -1;
20692 else
20693 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020694 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695}
20696
20697/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020698 * Get the lnum from the first argument.
20699 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020700 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 */
20702 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020703get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020704 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705{
Bram Moolenaar33570922005-01-25 22:26:29 +000020706 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 linenr_T lnum;
20708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020709 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 if (lnum == 0) /* no valid number, try using line() */
20711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020712 rettv.v_type = VAR_NUMBER;
20713 f_line(argvars, &rettv);
20714 lnum = rettv.vval.v_number;
20715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 }
20717 return lnum;
20718}
20719
20720/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020721 * Get the lnum from the first argument.
20722 * Also accepts "$", then "buf" is used.
20723 * Returns 0 on error.
20724 */
20725 static linenr_T
20726get_tv_lnum_buf(argvars, buf)
20727 typval_T *argvars;
20728 buf_T *buf;
20729{
20730 if (argvars[0].v_type == VAR_STRING
20731 && argvars[0].vval.v_string != NULL
20732 && argvars[0].vval.v_string[0] == '$'
20733 && buf != NULL)
20734 return buf->b_ml.ml_line_count;
20735 return get_tv_number_chk(&argvars[0], NULL);
20736}
20737
20738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 * Get the string value of a variable.
20740 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020741 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20742 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 * If the String variable has never been set, return an empty string.
20744 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020745 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20746 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 */
20748 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020749get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751{
20752 static char_u mybuf[NUMBUFLEN];
20753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755}
20756
20757 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020758get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020759 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020760 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020762 char_u *res = get_tv_string_buf_chk(varp, buf);
20763
20764 return res != NULL ? res : (char_u *)"";
20765}
20766
Bram Moolenaar7d647822014-04-05 21:28:56 +020020767/*
20768 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20769 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020770 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020771get_tv_string_chk(varp)
20772 typval_T *varp;
20773{
20774 static char_u mybuf[NUMBUFLEN];
20775
20776 return get_tv_string_buf_chk(varp, mybuf);
20777}
20778
20779 static char_u *
20780get_tv_string_buf_chk(varp, buf)
20781 typval_T *varp;
20782 char_u *buf;
20783{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020784 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020786 case VAR_NUMBER:
20787 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20788 return buf;
20789 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020790 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020791 break;
20792 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020793 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020794 break;
20795 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020796 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020797 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020798#ifdef FEAT_FLOAT
20799 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020800 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020801 break;
20802#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020803 case VAR_STRING:
20804 if (varp->vval.v_string != NULL)
20805 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020806 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020807 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020811 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812}
20813
20814/*
20815 * Find variable "name" in the list of variables.
20816 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020817 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020818 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020819 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020821 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020822find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020824 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020825 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829
Bram Moolenaara7043832005-01-21 11:56:39 +000020830 ht = find_var_ht(name, &varname);
20831 if (htp != NULL)
20832 *htp = ht;
20833 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020835 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836}
20837
20838/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020839 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020840 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020842 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020843find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020845 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020846 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020847 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020848{
Bram Moolenaar33570922005-01-25 22:26:29 +000020849 hashitem_T *hi;
20850
20851 if (*varname == NUL)
20852 {
20853 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020854 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020855 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020856 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020857 case 'g': return &globvars_var;
20858 case 'v': return &vimvars_var;
20859 case 'b': return &curbuf->b_bufvar;
20860 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020861#ifdef FEAT_WINDOWS
20862 case 't': return &curtab->tp_winvar;
20863#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020864 case 'l': return current_funccal == NULL
20865 ? NULL : &current_funccal->l_vars_var;
20866 case 'a': return current_funccal == NULL
20867 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 }
20869 return NULL;
20870 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020871
20872 hi = hash_find(ht, varname);
20873 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020874 {
20875 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020876 * worked find the variable again. Don't auto-load a script if it was
20877 * loaded already, otherwise it would be loaded every time when
20878 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020879 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020880 {
20881 /* Note: script_autoload() may make "hi" invalid. It must either
20882 * be obtained again or not used. */
20883 if (!script_autoload(varname, FALSE) || aborting())
20884 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020885 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020886 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020887 if (HASHITEM_EMPTY(hi))
20888 return NULL;
20889 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020890 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020891}
20892
20893/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020894 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020895 * Set "varname" to the start of name without ':'.
20896 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020897 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020898find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 char_u *name;
20900 char_u **varname;
20901{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020902 hashitem_T *hi;
20903
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 if (name[1] != ':')
20905 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020906 /* The name must not start with a colon or #. */
20907 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 return NULL;
20909 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020910
20911 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020912 hi = hash_find(&compat_hashtab, name);
20913 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020914 return &compat_hashtab;
20915
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020917 return &globvarht; /* global variable */
20918 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 }
20920 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020921 if (*name == 'g') /* global variable */
20922 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020923 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20924 */
20925 if (vim_strchr(name + 2, ':') != NULL
20926 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020927 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020929 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020931 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020932#ifdef FEAT_WINDOWS
20933 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020934 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020935#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020936 if (*name == 'v') /* v: variable */
20937 return &vimvarht;
20938 if (*name == 'a' && current_funccal != NULL) /* function argument */
20939 return &current_funccal->l_avars.dv_hashtab;
20940 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20941 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 if (*name == 's' /* script variable */
20943 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20944 return &SCRIPT_VARS(current_SID);
20945 return NULL;
20946}
20947
20948/*
20949 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020950 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 * Returns NULL when it doesn't exist.
20952 */
20953 char_u *
20954get_var_value(name)
20955 char_u *name;
20956{
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020959 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 if (v == NULL)
20961 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020962 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963}
20964
20965/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020966 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 * sourcing this script and when executing functions defined in the script.
20968 */
20969 void
20970new_script_vars(id)
20971 scid_T id;
20972{
Bram Moolenaara7043832005-01-21 11:56:39 +000020973 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020974 hashtab_T *ht;
20975 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020976
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20978 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020979 /* Re-allocating ga_data means that an ht_array pointing to
20980 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020981 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020982 for (i = 1; i <= ga_scripts.ga_len; ++i)
20983 {
20984 ht = &SCRIPT_VARS(i);
20985 if (ht->ht_mask == HT_INIT_SIZE - 1)
20986 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020987 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020988 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020989 }
20990
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 while (ga_scripts.ga_len < id)
20992 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020993 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020994 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020995 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 }
20998 }
20999}
21000
21001/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21003 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 */
21005 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021006init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021007 dict_T *dict;
21008 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021009 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
Bram Moolenaar33570922005-01-25 22:26:29 +000021011 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021012 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021013 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021014 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021015 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 dict_var->di_tv.vval.v_dict = dict;
21017 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021018 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021019 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21020 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021}
21022
21023/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021024 * Unreference a dictionary initialized by init_var_dict().
21025 */
21026 void
21027unref_var_dict(dict)
21028 dict_T *dict;
21029{
21030 /* Now the dict needs to be freed if no one else is using it, go back to
21031 * normal reference counting. */
21032 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21033 dict_unref(dict);
21034}
21035
21036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021038 * Frees all allocated variables and the value they contain.
21039 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 */
21041 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021042vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021043 hashtab_T *ht;
21044{
21045 vars_clear_ext(ht, TRUE);
21046}
21047
21048/*
21049 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21050 */
21051 static void
21052vars_clear_ext(ht, free_val)
21053 hashtab_T *ht;
21054 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055{
Bram Moolenaara7043832005-01-21 11:56:39 +000021056 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021057 hashitem_T *hi;
21058 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059
Bram Moolenaar33570922005-01-25 22:26:29 +000021060 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021061 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021062 for (hi = ht->ht_array; todo > 0; ++hi)
21063 {
21064 if (!HASHITEM_EMPTY(hi))
21065 {
21066 --todo;
21067
Bram Moolenaar33570922005-01-25 22:26:29 +000021068 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021069 * ht_array might change then. hash_clear() takes care of it
21070 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021071 v = HI2DI(hi);
21072 if (free_val)
21073 clear_tv(&v->di_tv);
21074 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21075 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021076 }
21077 }
21078 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021079 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080}
21081
Bram Moolenaara7043832005-01-21 11:56:39 +000021082/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021083 * Delete a variable from hashtab "ht" at item "hi".
21084 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021087delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021088 hashtab_T *ht;
21089 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090{
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021092
21093 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021094 clear_tv(&di->di_tv);
21095 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096}
21097
21098/*
21099 * List the value of one internal variable.
21100 */
21101 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021102list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021103 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021105 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021107 char_u *tofree;
21108 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021109 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021110
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021111 current_copyID += COPYID_INC;
21112 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021114 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021115 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116}
21117
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021119list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 char_u *prefix;
21121 char_u *name;
21122 int type;
21123 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021124 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125{
Bram Moolenaar31859182007-08-14 20:41:13 +000021126 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21127 msg_start();
21128 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 if (name != NULL) /* "a:" vars don't have a name stored */
21130 msg_puts(name);
21131 msg_putchar(' ');
21132 msg_advance(22);
21133 if (type == VAR_NUMBER)
21134 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021135 else if (type == VAR_FUNC)
21136 msg_putchar('*');
21137 else if (type == VAR_LIST)
21138 {
21139 msg_putchar('[');
21140 if (*string == '[')
21141 ++string;
21142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021143 else if (type == VAR_DICT)
21144 {
21145 msg_putchar('{');
21146 if (*string == '{')
21147 ++string;
21148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 else
21150 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021153
21154 if (type == VAR_FUNC)
21155 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021156 if (*first)
21157 {
21158 msg_clr_eos();
21159 *first = FALSE;
21160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161}
21162
21163/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021164 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 * If the variable already exists, the value is updated.
21166 * Otherwise the variable is created.
21167 */
21168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173{
Bram Moolenaar33570922005-01-25 22:26:29 +000021174 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021176 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021178 ht = find_var_ht(name, &varname);
21179 if (ht == NULL || *varname == NUL)
21180 {
21181 EMSG2(_(e_illvar), name);
21182 return;
21183 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021184 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021185
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021186 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21187 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021188
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021191 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021192 if (var_check_ro(v->di_flags, name)
21193 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021194 return;
21195 if (v->di_tv.v_type != tv->v_type
21196 && !((v->di_tv.v_type == VAR_STRING
21197 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021198 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021199 || tv->v_type == VAR_NUMBER))
21200#ifdef FEAT_FLOAT
21201 && !((v->di_tv.v_type == VAR_NUMBER
21202 || v->di_tv.v_type == VAR_FLOAT)
21203 && (tv->v_type == VAR_NUMBER
21204 || tv->v_type == VAR_FLOAT))
21205#endif
21206 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021208 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021209 return;
21210 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021211
21212 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021213 * Handle setting internal v: variables separately: we don't change
21214 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021215 */
21216 if (ht == &vimvarht)
21217 {
21218 if (v->di_tv.v_type == VAR_STRING)
21219 {
21220 vim_free(v->di_tv.vval.v_string);
21221 if (copy || tv->v_type != VAR_STRING)
21222 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21223 else
21224 {
21225 /* Take over the string to avoid an extra alloc/free. */
21226 v->di_tv.vval.v_string = tv->vval.v_string;
21227 tv->vval.v_string = NULL;
21228 }
21229 }
21230 else if (v->di_tv.v_type != VAR_NUMBER)
21231 EMSG2(_(e_intern2), "set_var()");
21232 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021233 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021234 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021235 if (STRCMP(varname, "searchforward") == 0)
21236 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021237#ifdef FEAT_SEARCH_EXTRA
21238 else if (STRCMP(varname, "hlsearch") == 0)
21239 {
21240 no_hlsearch = !v->di_tv.vval.v_number;
21241 redraw_all_later(SOME_VALID);
21242 }
21243#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021244 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021245 return;
21246 }
21247
21248 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 }
21250 else /* add a new variable */
21251 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021252 /* Can't add "v:" variable. */
21253 if (ht == &vimvarht)
21254 {
21255 EMSG2(_(e_illvar), name);
21256 return;
21257 }
21258
Bram Moolenaar92124a32005-06-17 22:03:40 +000021259 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021260 if (!valid_varname(varname))
21261 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021262
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021263 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21264 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021265 if (v == NULL)
21266 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021267 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021268 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021270 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 return;
21272 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021273 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021275
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021276 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021277 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021278 else
21279 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021280 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021281 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021282 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284}
21285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021286/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021287 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021288 * Also give an error message.
21289 */
21290 static int
21291var_check_ro(flags, name)
21292 int flags;
21293 char_u *name;
21294{
21295 if (flags & DI_FLAGS_RO)
21296 {
21297 EMSG2(_(e_readonlyvar), name);
21298 return TRUE;
21299 }
21300 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21301 {
21302 EMSG2(_(e_readonlysbx), name);
21303 return TRUE;
21304 }
21305 return FALSE;
21306}
21307
21308/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021309 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21310 * Also give an error message.
21311 */
21312 static int
21313var_check_fixed(flags, name)
21314 int flags;
21315 char_u *name;
21316{
21317 if (flags & DI_FLAGS_FIX)
21318 {
21319 EMSG2(_("E795: Cannot delete variable %s"), name);
21320 return TRUE;
21321 }
21322 return FALSE;
21323}
21324
21325/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021326 * Check if a funcref is assigned to a valid variable name.
21327 * Return TRUE and give an error if not.
21328 */
21329 static int
21330var_check_func_name(name, new_var)
21331 char_u *name; /* points to start of variable name */
21332 int new_var; /* TRUE when creating the variable */
21333{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021334 /* Allow for w: b: s: and t:. */
21335 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021336 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21337 ? name[2] : name[0]))
21338 {
21339 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21340 name);
21341 return TRUE;
21342 }
21343 /* Don't allow hiding a function. When "v" is not NULL we might be
21344 * assigning another function to the same var, the type is checked
21345 * below. */
21346 if (new_var && function_exists(name))
21347 {
21348 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21349 name);
21350 return TRUE;
21351 }
21352 return FALSE;
21353}
21354
21355/*
21356 * Check if a variable name is valid.
21357 * Return FALSE and give an error if not.
21358 */
21359 static int
21360valid_varname(varname)
21361 char_u *varname;
21362{
21363 char_u *p;
21364
21365 for (p = varname; *p != NUL; ++p)
21366 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21367 && *p != AUTOLOAD_CHAR)
21368 {
21369 EMSG2(_(e_illvar), varname);
21370 return FALSE;
21371 }
21372 return TRUE;
21373}
21374
21375/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021376 * Return TRUE if typeval "tv" is set to be locked (immutable).
21377 * Also give an error message, using "name".
21378 */
21379 static int
21380tv_check_lock(lock, name)
21381 int lock;
21382 char_u *name;
21383{
21384 if (lock & VAR_LOCKED)
21385 {
21386 EMSG2(_("E741: Value is locked: %s"),
21387 name == NULL ? (char_u *)_("Unknown") : name);
21388 return TRUE;
21389 }
21390 if (lock & VAR_FIXED)
21391 {
21392 EMSG2(_("E742: Cannot change value of %s"),
21393 name == NULL ? (char_u *)_("Unknown") : name);
21394 return TRUE;
21395 }
21396 return FALSE;
21397}
21398
21399/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021400 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021401 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021402 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021403 * It is OK for "from" and "to" to point to the same item. This is used to
21404 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021406 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021408 typval_T *from;
21409 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021411 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021412 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413 switch (from->v_type)
21414 {
21415 case VAR_NUMBER:
21416 to->vval.v_number = from->vval.v_number;
21417 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021418#ifdef FEAT_FLOAT
21419 case VAR_FLOAT:
21420 to->vval.v_float = from->vval.v_float;
21421 break;
21422#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021423 case VAR_STRING:
21424 case VAR_FUNC:
21425 if (from->vval.v_string == NULL)
21426 to->vval.v_string = NULL;
21427 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021430 if (from->v_type == VAR_FUNC)
21431 func_ref(to->vval.v_string);
21432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021433 break;
21434 case VAR_LIST:
21435 if (from->vval.v_list == NULL)
21436 to->vval.v_list = NULL;
21437 else
21438 {
21439 to->vval.v_list = from->vval.v_list;
21440 ++to->vval.v_list->lv_refcount;
21441 }
21442 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021443 case VAR_DICT:
21444 if (from->vval.v_dict == NULL)
21445 to->vval.v_dict = NULL;
21446 else
21447 {
21448 to->vval.v_dict = from->vval.v_dict;
21449 ++to->vval.v_dict->dv_refcount;
21450 }
21451 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021453 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021454 break;
21455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456}
21457
21458/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021459 * Make a copy of an item.
21460 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021461 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21462 * reference to an already copied list/dict can be used.
21463 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021464 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021465 static int
21466item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 typval_T *from;
21468 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021469 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021470 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021471{
21472 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021473 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021474
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021476 {
21477 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021478 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021479 }
21480 ++recurse;
21481
21482 switch (from->v_type)
21483 {
21484 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021485#ifdef FEAT_FLOAT
21486 case VAR_FLOAT:
21487#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021488 case VAR_STRING:
21489 case VAR_FUNC:
21490 copy_tv(from, to);
21491 break;
21492 case VAR_LIST:
21493 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021494 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021495 if (from->vval.v_list == NULL)
21496 to->vval.v_list = NULL;
21497 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21498 {
21499 /* use the copy made earlier */
21500 to->vval.v_list = from->vval.v_list->lv_copylist;
21501 ++to->vval.v_list->lv_refcount;
21502 }
21503 else
21504 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21505 if (to->vval.v_list == NULL)
21506 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021507 break;
21508 case VAR_DICT:
21509 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021510 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021511 if (from->vval.v_dict == NULL)
21512 to->vval.v_dict = NULL;
21513 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21514 {
21515 /* use the copy made earlier */
21516 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21517 ++to->vval.v_dict->dv_refcount;
21518 }
21519 else
21520 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21521 if (to->vval.v_dict == NULL)
21522 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021523 break;
21524 default:
21525 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021526 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021527 }
21528 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021529 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021530}
21531
21532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 * ":echo expr1 ..." print each argument separated with a space, add a
21534 * newline at the end.
21535 * ":echon expr1 ..." print each argument plain.
21536 */
21537 void
21538ex_echo(eap)
21539 exarg_T *eap;
21540{
21541 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021543 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 char_u *p;
21545 int needclr = TRUE;
21546 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021547 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548
21549 if (eap->skip)
21550 ++emsg_skip;
21551 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21552 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021553 /* If eval1() causes an error message the text from the command may
21554 * still need to be cleared. E.g., "echo 22,44". */
21555 need_clr_eos = needclr;
21556
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021558 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
21560 /*
21561 * Report the invalid expression unless the expression evaluation
21562 * has been cancelled due to an aborting error, an interrupt, or an
21563 * exception.
21564 */
21565 if (!aborting())
21566 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021567 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 break;
21569 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021570 need_clr_eos = FALSE;
21571
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 if (!eap->skip)
21573 {
21574 if (atstart)
21575 {
21576 atstart = FALSE;
21577 /* Call msg_start() after eval1(), evaluating the expression
21578 * may cause a message to appear. */
21579 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021580 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021581 /* Mark the saved text as finishing the line, so that what
21582 * follows is displayed on a new line when scrolling back
21583 * at the more prompt. */
21584 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 }
21588 else if (eap->cmdidx == CMD_echo)
21589 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021590 current_copyID += COPYID_INC;
21591 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021592 if (p != NULL)
21593 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021595 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021597 if (*p != TAB && needclr)
21598 {
21599 /* remove any text still there from the command */
21600 msg_clr_eos();
21601 needclr = FALSE;
21602 }
21603 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 }
21605 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021606 {
21607#ifdef FEAT_MBYTE
21608 if (has_mbyte)
21609 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021610 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021611
21612 (void)msg_outtrans_len_attr(p, i, echo_attr);
21613 p += i - 1;
21614 }
21615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021617 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 arg = skipwhite(arg);
21624 }
21625 eap->nextcmd = check_nextcmd(arg);
21626
21627 if (eap->skip)
21628 --emsg_skip;
21629 else
21630 {
21631 /* remove text that may still be there from the command */
21632 if (needclr)
21633 msg_clr_eos();
21634 if (eap->cmdidx == CMD_echo)
21635 msg_end();
21636 }
21637}
21638
21639/*
21640 * ":echohl {name}".
21641 */
21642 void
21643ex_echohl(eap)
21644 exarg_T *eap;
21645{
21646 int id;
21647
21648 id = syn_name2id(eap->arg);
21649 if (id == 0)
21650 echo_attr = 0;
21651 else
21652 echo_attr = syn_id2attr(id);
21653}
21654
21655/*
21656 * ":execute expr1 ..." execute the result of an expression.
21657 * ":echomsg expr1 ..." Print a message
21658 * ":echoerr expr1 ..." Print an error
21659 * Each gets spaces around each argument and a newline at the end for
21660 * echo commands
21661 */
21662 void
21663ex_execute(eap)
21664 exarg_T *eap;
21665{
21666 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 int ret = OK;
21669 char_u *p;
21670 garray_T ga;
21671 int len;
21672 int save_did_emsg;
21673
21674 ga_init2(&ga, 1, 80);
21675
21676 if (eap->skip)
21677 ++emsg_skip;
21678 while (*arg != NUL && *arg != '|' && *arg != '\n')
21679 {
21680 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021681 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 {
21683 /*
21684 * Report the invalid expression unless the expression evaluation
21685 * has been cancelled due to an aborting error, an interrupt, or an
21686 * exception.
21687 */
21688 if (!aborting())
21689 EMSG2(_(e_invexpr2), p);
21690 ret = FAIL;
21691 break;
21692 }
21693
21694 if (!eap->skip)
21695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021696 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 len = (int)STRLEN(p);
21698 if (ga_grow(&ga, len + 2) == FAIL)
21699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021700 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 ret = FAIL;
21702 break;
21703 }
21704 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 ga.ga_len += len;
21708 }
21709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 arg = skipwhite(arg);
21712 }
21713
21714 if (ret != FAIL && ga.ga_data != NULL)
21715 {
21716 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021719 out_flush();
21720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 else if (eap->cmdidx == CMD_echoerr)
21722 {
21723 /* We don't want to abort following commands, restore did_emsg. */
21724 save_did_emsg = did_emsg;
21725 EMSG((char_u *)ga.ga_data);
21726 if (!force_abort)
21727 did_emsg = save_did_emsg;
21728 }
21729 else if (eap->cmdidx == CMD_execute)
21730 do_cmdline((char_u *)ga.ga_data,
21731 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21732 }
21733
21734 ga_clear(&ga);
21735
21736 if (eap->skip)
21737 --emsg_skip;
21738
21739 eap->nextcmd = check_nextcmd(arg);
21740}
21741
21742/*
21743 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21744 * "arg" points to the "&" or '+' when called, to "option" when returning.
21745 * Returns NULL when no option name found. Otherwise pointer to the char
21746 * after the option name.
21747 */
21748 static char_u *
21749find_option_end(arg, opt_flags)
21750 char_u **arg;
21751 int *opt_flags;
21752{
21753 char_u *p = *arg;
21754
21755 ++p;
21756 if (*p == 'g' && p[1] == ':')
21757 {
21758 *opt_flags = OPT_GLOBAL;
21759 p += 2;
21760 }
21761 else if (*p == 'l' && p[1] == ':')
21762 {
21763 *opt_flags = OPT_LOCAL;
21764 p += 2;
21765 }
21766 else
21767 *opt_flags = 0;
21768
21769 if (!ASCII_ISALPHA(*p))
21770 return NULL;
21771 *arg = p;
21772
21773 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21774 p += 4; /* termcap option */
21775 else
21776 while (ASCII_ISALPHA(*p))
21777 ++p;
21778 return p;
21779}
21780
21781/*
21782 * ":function"
21783 */
21784 void
21785ex_function(eap)
21786 exarg_T *eap;
21787{
21788 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021789 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 int j;
21791 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021793 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 char_u *name = NULL;
21795 char_u *p;
21796 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021797 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 garray_T newargs;
21799 garray_T newlines;
21800 int varargs = FALSE;
21801 int mustend = FALSE;
21802 int flags = 0;
21803 ufunc_T *fp;
21804 int indent;
21805 int nesting;
21806 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 dictitem_T *v;
21808 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 static int func_nr = 0; /* number for nameless function */
21810 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021811 hashtab_T *ht;
21812 int todo;
21813 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021814 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815
21816 /*
21817 * ":function" without argument: list functions.
21818 */
21819 if (ends_excmd(*eap->arg))
21820 {
21821 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021823 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021824 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021825 {
21826 if (!HASHITEM_EMPTY(hi))
21827 {
21828 --todo;
21829 fp = HI2UF(hi);
21830 if (!isdigit(*fp->uf_name))
21831 list_func_head(fp, FALSE);
21832 }
21833 }
21834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 eap->nextcmd = check_nextcmd(eap->arg);
21836 return;
21837 }
21838
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021839 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021840 * ":function /pat": list functions matching pattern.
21841 */
21842 if (*eap->arg == '/')
21843 {
21844 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21845 if (!eap->skip)
21846 {
21847 regmatch_T regmatch;
21848
21849 c = *p;
21850 *p = NUL;
21851 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21852 *p = c;
21853 if (regmatch.regprog != NULL)
21854 {
21855 regmatch.rm_ic = p_ic;
21856
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021857 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021858 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21859 {
21860 if (!HASHITEM_EMPTY(hi))
21861 {
21862 --todo;
21863 fp = HI2UF(hi);
21864 if (!isdigit(*fp->uf_name)
21865 && vim_regexec(&regmatch, fp->uf_name, 0))
21866 list_func_head(fp, FALSE);
21867 }
21868 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021869 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021870 }
21871 }
21872 if (*p == '/')
21873 ++p;
21874 eap->nextcmd = check_nextcmd(p);
21875 return;
21876 }
21877
21878 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021879 * Get the function name. There are these situations:
21880 * func normal function name
21881 * "name" == func, "fudi.fd_dict" == NULL
21882 * dict.func new dictionary entry
21883 * "name" == NULL, "fudi.fd_dict" set,
21884 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21885 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021886 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021887 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21888 * dict.func existing dict entry that's not a Funcref
21889 * "name" == NULL, "fudi.fd_dict" set,
21890 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021891 * s:func script-local function name
21892 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021895 name = trans_function_name(&p, eap->skip, 0, &fudi);
21896 paren = (vim_strchr(p, '(') != NULL);
21897 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
21899 /*
21900 * Return on an invalid expression in braces, unless the expression
21901 * evaluation has been cancelled due to an aborting error, an
21902 * interrupt, or an exception.
21903 */
21904 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021906 if (!eap->skip && fudi.fd_newkey != NULL)
21907 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021908 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 else
21912 eap->skip = TRUE;
21913 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021914
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 /* An error in a function call during evaluation of an expression in magic
21916 * braces should not cause the function not to be defined. */
21917 saved_did_emsg = did_emsg;
21918 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919
21920 /*
21921 * ":function func" with only function name: list function.
21922 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021923 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 {
21925 if (!ends_excmd(*skipwhite(p)))
21926 {
21927 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021928 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 }
21930 eap->nextcmd = check_nextcmd(p);
21931 if (eap->nextcmd != NULL)
21932 *p = NUL;
21933 if (!eap->skip && !got_int)
21934 {
21935 fp = find_func(name);
21936 if (fp != NULL)
21937 {
21938 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021939 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021941 if (FUNCLINE(fp, j) == NULL)
21942 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 msg_putchar('\n');
21944 msg_outnum((long)(j + 1));
21945 if (j < 9)
21946 msg_putchar(' ');
21947 if (j < 99)
21948 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021949 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 out_flush(); /* show a line at a time */
21951 ui_breakcheck();
21952 }
21953 if (!got_int)
21954 {
21955 msg_putchar('\n');
21956 msg_puts((char_u *)" endfunction");
21957 }
21958 }
21959 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021960 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021962 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 }
21964
21965 /*
21966 * ":function name(arg1, arg2)" Define function.
21967 */
21968 p = skipwhite(p);
21969 if (*p != '(')
21970 {
21971 if (!eap->skip)
21972 {
21973 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021974 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 }
21976 /* attempt to continue by skipping some text */
21977 if (vim_strchr(p, '(') != NULL)
21978 p = vim_strchr(p, '(');
21979 }
21980 p = skipwhite(p + 1);
21981
21982 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21983 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21984
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021985 if (!eap->skip)
21986 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021987 /* Check the name of the function. Unless it's a dictionary function
21988 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021989 if (name != NULL)
21990 arg = name;
21991 else
21992 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021993 if (arg != NULL && (fudi.fd_di == NULL
21994 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021995 {
21996 if (*arg == K_SPECIAL)
21997 j = 3;
21998 else
21999 j = 0;
22000 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22001 : eval_isnamec(arg[j])))
22002 ++j;
22003 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022004 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022005 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022006 /* Disallow using the g: dict. */
22007 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22008 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022009 }
22010
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 /*
22012 * Isolate the arguments: "arg1, arg2, ...)"
22013 */
22014 while (*p != ')')
22015 {
22016 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22017 {
22018 varargs = TRUE;
22019 p += 3;
22020 mustend = TRUE;
22021 }
22022 else
22023 {
22024 arg = p;
22025 while (ASCII_ISALNUM(*p) || *p == '_')
22026 ++p;
22027 if (arg == p || isdigit(*arg)
22028 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22029 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22030 {
22031 if (!eap->skip)
22032 EMSG2(_("E125: Illegal argument: %s"), arg);
22033 break;
22034 }
22035 if (ga_grow(&newargs, 1) == FAIL)
22036 goto erret;
22037 c = *p;
22038 *p = NUL;
22039 arg = vim_strsave(arg);
22040 if (arg == NULL)
22041 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022042
22043 /* Check for duplicate argument name. */
22044 for (i = 0; i < newargs.ga_len; ++i)
22045 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22046 {
22047 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022048 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022049 goto erret;
22050 }
22051
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22053 *p = c;
22054 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 if (*p == ',')
22056 ++p;
22057 else
22058 mustend = TRUE;
22059 }
22060 p = skipwhite(p);
22061 if (mustend && *p != ')')
22062 {
22063 if (!eap->skip)
22064 EMSG2(_(e_invarg2), eap->arg);
22065 break;
22066 }
22067 }
22068 ++p; /* skip the ')' */
22069
Bram Moolenaare9a41262005-01-15 22:18:47 +000022070 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 for (;;)
22072 {
22073 p = skipwhite(p);
22074 if (STRNCMP(p, "range", 5) == 0)
22075 {
22076 flags |= FC_RANGE;
22077 p += 5;
22078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022079 else if (STRNCMP(p, "dict", 4) == 0)
22080 {
22081 flags |= FC_DICT;
22082 p += 4;
22083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 else if (STRNCMP(p, "abort", 5) == 0)
22085 {
22086 flags |= FC_ABORT;
22087 p += 5;
22088 }
22089 else
22090 break;
22091 }
22092
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022093 /* When there is a line break use what follows for the function body.
22094 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22095 if (*p == '\n')
22096 line_arg = p + 1;
22097 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 EMSG(_(e_trailing));
22099
22100 /*
22101 * Read the body of the function, until ":endfunction" is found.
22102 */
22103 if (KeyTyped)
22104 {
22105 /* Check if the function already exists, don't let the user type the
22106 * whole function before telling him it doesn't work! For a script we
22107 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022108 if (!eap->skip && !eap->forceit)
22109 {
22110 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22111 EMSG(_(e_funcdict));
22112 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022113 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022116 if (!eap->skip && did_emsg)
22117 goto erret;
22118
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 msg_putchar('\n'); /* don't overwrite the function name */
22120 cmdline_row = msg_row;
22121 }
22122
22123 indent = 2;
22124 nesting = 0;
22125 for (;;)
22126 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022127 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022128 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022129 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022130 saved_wait_return = FALSE;
22131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022133 sourcing_lnum_off = sourcing_lnum;
22134
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022135 if (line_arg != NULL)
22136 {
22137 /* Use eap->arg, split up in parts by line breaks. */
22138 theline = line_arg;
22139 p = vim_strchr(theline, '\n');
22140 if (p == NULL)
22141 line_arg += STRLEN(line_arg);
22142 else
22143 {
22144 *p = NUL;
22145 line_arg = p + 1;
22146 }
22147 }
22148 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 theline = getcmdline(':', 0L, indent);
22150 else
22151 theline = eap->getline(':', eap->cookie, indent);
22152 if (KeyTyped)
22153 lines_left = Rows - 1;
22154 if (theline == NULL)
22155 {
22156 EMSG(_("E126: Missing :endfunction"));
22157 goto erret;
22158 }
22159
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022160 /* Detect line continuation: sourcing_lnum increased more than one. */
22161 if (sourcing_lnum > sourcing_lnum_off + 1)
22162 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22163 else
22164 sourcing_lnum_off = 0;
22165
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 if (skip_until != NULL)
22167 {
22168 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22169 * don't check for ":endfunc". */
22170 if (STRCMP(theline, skip_until) == 0)
22171 {
22172 vim_free(skip_until);
22173 skip_until = NULL;
22174 }
22175 }
22176 else
22177 {
22178 /* skip ':' and blanks*/
22179 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22180 ;
22181
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022182 /* Check for "endfunction". */
22183 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022185 if (line_arg == NULL)
22186 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 break;
22188 }
22189
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022190 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 * at "end". */
22192 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22193 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022194 else if (STRNCMP(p, "if", 2) == 0
22195 || STRNCMP(p, "wh", 2) == 0
22196 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 || STRNCMP(p, "try", 3) == 0)
22198 indent += 2;
22199
22200 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022201 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022203 if (*p == '!')
22204 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 p += eval_fname_script(p);
22206 if (ASCII_ISALPHA(*p))
22207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022208 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 if (*skipwhite(p) == '(')
22210 {
22211 ++nesting;
22212 indent += 2;
22213 }
22214 }
22215 }
22216
22217 /* Check for ":append" or ":insert". */
22218 p = skip_range(p, NULL);
22219 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22220 || (p[0] == 'i'
22221 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22222 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22223 skip_until = vim_strsave((char_u *)".");
22224
22225 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22226 arg = skipwhite(skiptowhite(p));
22227 if (arg[0] == '<' && arg[1] =='<'
22228 && ((p[0] == 'p' && p[1] == 'y'
22229 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22230 || (p[0] == 'p' && p[1] == 'e'
22231 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22232 || (p[0] == 't' && p[1] == 'c'
22233 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022234 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22235 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22237 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022238 || (p[0] == 'm' && p[1] == 'z'
22239 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 ))
22241 {
22242 /* ":python <<" continues until a dot, like ":append" */
22243 p = skipwhite(arg + 2);
22244 if (*p == NUL)
22245 skip_until = vim_strsave((char_u *)".");
22246 else
22247 skip_until = vim_strsave(p);
22248 }
22249 }
22250
22251 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022252 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022253 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022254 if (line_arg == NULL)
22255 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022257 }
22258
22259 /* Copy the line to newly allocated memory. get_one_sourceline()
22260 * allocates 250 bytes per line, this saves 80% on average. The cost
22261 * is an extra alloc/free. */
22262 p = vim_strsave(theline);
22263 if (p != NULL)
22264 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022265 if (line_arg == NULL)
22266 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022267 theline = p;
22268 }
22269
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022270 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22271
22272 /* Add NULL lines for continuation lines, so that the line count is
22273 * equal to the index in the growarray. */
22274 while (sourcing_lnum_off-- > 0)
22275 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022276
22277 /* Check for end of eap->arg. */
22278 if (line_arg != NULL && *line_arg == NUL)
22279 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 }
22281
22282 /* Don't define the function when skipping commands or when an error was
22283 * detected. */
22284 if (eap->skip || did_emsg)
22285 goto erret;
22286
22287 /*
22288 * If there are no errors, add the function
22289 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022290 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022291 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022292 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022293 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022294 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022295 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022296 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022297 goto erret;
22298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022300 fp = find_func(name);
22301 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022303 if (!eap->forceit)
22304 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022305 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 goto erret;
22307 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022308 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022310 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022311 name);
22312 goto erret;
22313 }
22314 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022315 ga_clear_strings(&(fp->uf_args));
22316 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317 vim_free(name);
22318 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 }
22321 else
22322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022323 char numbuf[20];
22324
22325 fp = NULL;
22326 if (fudi.fd_newkey == NULL && !eap->forceit)
22327 {
22328 EMSG(_(e_funcdict));
22329 goto erret;
22330 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022331 if (fudi.fd_di == NULL)
22332 {
22333 /* Can't add a function to a locked dictionary */
22334 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22335 goto erret;
22336 }
22337 /* Can't change an existing function if it is locked */
22338 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22339 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340
22341 /* Give the function a sequential number. Can only be used with a
22342 * Funcref! */
22343 vim_free(name);
22344 sprintf(numbuf, "%d", ++func_nr);
22345 name = vim_strsave((char_u *)numbuf);
22346 if (name == NULL)
22347 goto erret;
22348 }
22349
22350 if (fp == NULL)
22351 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022352 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022353 {
22354 int slen, plen;
22355 char_u *scriptname;
22356
22357 /* Check that the autoload name matches the script name. */
22358 j = FAIL;
22359 if (sourcing_name != NULL)
22360 {
22361 scriptname = autoload_name(name);
22362 if (scriptname != NULL)
22363 {
22364 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022365 plen = (int)STRLEN(p);
22366 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022367 if (slen > plen && fnamecmp(p,
22368 sourcing_name + slen - plen) == 0)
22369 j = OK;
22370 vim_free(scriptname);
22371 }
22372 }
22373 if (j == FAIL)
22374 {
22375 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22376 goto erret;
22377 }
22378 }
22379
22380 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 if (fp == NULL)
22382 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383
22384 if (fudi.fd_dict != NULL)
22385 {
22386 if (fudi.fd_di == NULL)
22387 {
22388 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022389 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022390 if (fudi.fd_di == NULL)
22391 {
22392 vim_free(fp);
22393 goto erret;
22394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022395 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22396 {
22397 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022398 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022399 goto erret;
22400 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022401 }
22402 else
22403 /* overwrite existing dict entry */
22404 clear_tv(&fudi.fd_di->di_tv);
22405 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022406 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022407 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022408 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022409
22410 /* behave like "dict" was used */
22411 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022412 }
22413
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022415 STRCPY(fp->uf_name, name);
22416 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022418 fp->uf_args = newargs;
22419 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022420#ifdef FEAT_PROFILE
22421 fp->uf_tml_count = NULL;
22422 fp->uf_tml_total = NULL;
22423 fp->uf_tml_self = NULL;
22424 fp->uf_profiling = FALSE;
22425 if (prof_def_func())
22426 func_do_profile(fp);
22427#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022428 fp->uf_varargs = varargs;
22429 fp->uf_flags = flags;
22430 fp->uf_calls = 0;
22431 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022432 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433
22434erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 ga_clear_strings(&newargs);
22436 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022437ret_free:
22438 vim_free(skip_until);
22439 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022442 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443}
22444
22445/*
22446 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022447 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022449 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022450 * TFN_INT: internal function name OK
22451 * TFN_QUIET: be quiet
22452 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 * Advances "pp" to just after the function name (if no error).
22454 */
22455 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022456trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 char_u **pp;
22458 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022459 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022462 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 char_u *start;
22464 char_u *end;
22465 int lead;
22466 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022469
22470 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022473
22474 /* Check for hard coded <SNR>: already translated function ID (from a user
22475 * command). */
22476 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22477 && (*pp)[2] == (int)KE_SNR)
22478 {
22479 *pp += 3;
22480 len = get_id_len(pp) + 3;
22481 return vim_strnsave(start, len);
22482 }
22483
22484 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22485 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022487 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022489
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022490 /* Note that TFN_ flags use the same values as GLV_ flags. */
22491 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022492 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022493 if (end == start)
22494 {
22495 if (!skip)
22496 EMSG(_("E129: Function name required"));
22497 goto theend;
22498 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022499 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022500 {
22501 /*
22502 * Report an invalid expression in braces, unless the expression
22503 * evaluation has been cancelled due to an aborting error, an
22504 * interrupt, or an exception.
22505 */
22506 if (!aborting())
22507 {
22508 if (end != NULL)
22509 EMSG2(_(e_invarg2), start);
22510 }
22511 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022512 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022513 goto theend;
22514 }
22515
22516 if (lv.ll_tv != NULL)
22517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 if (fdp != NULL)
22519 {
22520 fdp->fd_dict = lv.ll_dict;
22521 fdp->fd_newkey = lv.ll_newkey;
22522 lv.ll_newkey = NULL;
22523 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022524 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022525 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22526 {
22527 name = vim_strsave(lv.ll_tv->vval.v_string);
22528 *pp = end;
22529 }
22530 else
22531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022532 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22533 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022534 EMSG(_(e_funcref));
22535 else
22536 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022537 name = NULL;
22538 }
22539 goto theend;
22540 }
22541
22542 if (lv.ll_name == NULL)
22543 {
22544 /* Error found, but continue after the function name. */
22545 *pp = end;
22546 goto theend;
22547 }
22548
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022549 /* Check if the name is a Funcref. If so, use the value. */
22550 if (lv.ll_exp_name != NULL)
22551 {
22552 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022553 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022554 if (name == lv.ll_exp_name)
22555 name = NULL;
22556 }
22557 else
22558 {
22559 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022560 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022561 if (name == *pp)
22562 name = NULL;
22563 }
22564 if (name != NULL)
22565 {
22566 name = vim_strsave(name);
22567 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022568 if (STRNCMP(name, "<SNR>", 5) == 0)
22569 {
22570 /* Change "<SNR>" to the byte sequence. */
22571 name[0] = K_SPECIAL;
22572 name[1] = KS_EXTRA;
22573 name[2] = (int)KE_SNR;
22574 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22575 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022576 goto theend;
22577 }
22578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022579 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022580 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022581 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022582 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22583 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22584 {
22585 /* When there was "s:" already or the name expanded to get a
22586 * leading "s:" then remove it. */
22587 lv.ll_name += 2;
22588 len -= 2;
22589 lead = 2;
22590 }
22591 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022592 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022593 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022594 /* skip over "s:" and "g:" */
22595 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022596 lv.ll_name += 2;
22597 len = (int)(end - lv.ll_name);
22598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022599
22600 /*
22601 * Copy the function name to allocated memory.
22602 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22603 * Accept <SNR>123_name() outside a script.
22604 */
22605 if (skip)
22606 lead = 0; /* do nothing */
22607 else if (lead > 0)
22608 {
22609 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022610 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22611 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022612 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022613 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022614 if (current_SID <= 0)
22615 {
22616 EMSG(_(e_usingsid));
22617 goto theend;
22618 }
22619 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22620 lead += (int)STRLEN(sid_buf);
22621 }
22622 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022623 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022624 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022625 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022626 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022627 goto theend;
22628 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022629 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022630 {
22631 char_u *cp = vim_strchr(lv.ll_name, ':');
22632
22633 if (cp != NULL && cp < end)
22634 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022635 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022636 goto theend;
22637 }
22638 }
22639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022640 name = alloc((unsigned)(len + lead + 1));
22641 if (name != NULL)
22642 {
22643 if (lead > 0)
22644 {
22645 name[0] = K_SPECIAL;
22646 name[1] = KS_EXTRA;
22647 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022648 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022649 STRCPY(name + 3, sid_buf);
22650 }
22651 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022652 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022653 }
22654 *pp = end;
22655
22656theend:
22657 clear_lval(&lv);
22658 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659}
22660
22661/*
22662 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22663 * Return 2 if "p" starts with "s:".
22664 * Return 0 otherwise.
22665 */
22666 static int
22667eval_fname_script(p)
22668 char_u *p;
22669{
22670 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22671 || STRNICMP(p + 1, "SNR>", 4) == 0))
22672 return 5;
22673 if (p[0] == 's' && p[1] == ':')
22674 return 2;
22675 return 0;
22676}
22677
22678/*
22679 * Return TRUE if "p" starts with "<SID>" or "s:".
22680 * Only works if eval_fname_script() returned non-zero for "p"!
22681 */
22682 static int
22683eval_fname_sid(p)
22684 char_u *p;
22685{
22686 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22687}
22688
22689/*
22690 * List the head of the function: "name(arg1, arg2)".
22691 */
22692 static void
22693list_func_head(fp, indent)
22694 ufunc_T *fp;
22695 int indent;
22696{
22697 int j;
22698
22699 msg_start();
22700 if (indent)
22701 MSG_PUTS(" ");
22702 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022703 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
22705 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022706 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 }
22708 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022709 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022711 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 {
22713 if (j)
22714 MSG_PUTS(", ");
22715 msg_puts(FUNCARG(fp, j));
22716 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022717 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 {
22719 if (j)
22720 MSG_PUTS(", ");
22721 MSG_PUTS("...");
22722 }
22723 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022724 if (fp->uf_flags & FC_ABORT)
22725 MSG_PUTS(" abort");
22726 if (fp->uf_flags & FC_RANGE)
22727 MSG_PUTS(" range");
22728 if (fp->uf_flags & FC_DICT)
22729 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022730 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022731 if (p_verbose > 0)
22732 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733}
22734
22735/*
22736 * Find a function by name, return pointer to it in ufuncs.
22737 * Return NULL for unknown function.
22738 */
22739 static ufunc_T *
22740find_func(name)
22741 char_u *name;
22742{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022743 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022745 hi = hash_find(&func_hashtab, name);
22746 if (!HASHITEM_EMPTY(hi))
22747 return HI2UF(hi);
22748 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749}
22750
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022751#if defined(EXITFREE) || defined(PROTO)
22752 void
22753free_all_functions()
22754{
22755 hashitem_T *hi;
22756
22757 /* Need to start all over every time, because func_free() may change the
22758 * hash table. */
22759 while (func_hashtab.ht_used > 0)
22760 for (hi = func_hashtab.ht_array; ; ++hi)
22761 if (!HASHITEM_EMPTY(hi))
22762 {
22763 func_free(HI2UF(hi));
22764 break;
22765 }
22766}
22767#endif
22768
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022769 int
22770translated_function_exists(name)
22771 char_u *name;
22772{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022773 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022774 return find_internal_func(name) >= 0;
22775 return find_func(name) != NULL;
22776}
22777
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022778/*
22779 * Return TRUE if a function "name" exists.
22780 */
22781 static int
22782function_exists(name)
22783 char_u *name;
22784{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022785 char_u *nm = name;
22786 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787 int n = FALSE;
22788
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022789 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22790 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022791 nm = skipwhite(nm);
22792
22793 /* Only accept "funcname", "funcname ", "funcname (..." and
22794 * "funcname(...", not "funcname!...". */
22795 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022796 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022797 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022798 return n;
22799}
22800
Bram Moolenaara1544c02013-05-30 12:35:52 +020022801 char_u *
22802get_expanded_name(name, check)
22803 char_u *name;
22804 int check;
22805{
22806 char_u *nm = name;
22807 char_u *p;
22808
22809 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22810
22811 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022812 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022813 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022814
Bram Moolenaara1544c02013-05-30 12:35:52 +020022815 vim_free(p);
22816 return NULL;
22817}
22818
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022819/*
22820 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022821 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22822 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022823 */
22824 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022825builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022826 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022827 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022828{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022829 char_u *p;
22830
22831 if (!ASCII_ISLOWER(name[0]))
22832 return FALSE;
22833 p = vim_strchr(name, AUTOLOAD_CHAR);
22834 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022835}
22836
Bram Moolenaar05159a02005-02-26 23:04:13 +000022837#if defined(FEAT_PROFILE) || defined(PROTO)
22838/*
22839 * Start profiling function "fp".
22840 */
22841 static void
22842func_do_profile(fp)
22843 ufunc_T *fp;
22844{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022845 int len = fp->uf_lines.ga_len;
22846
22847 if (len == 0)
22848 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022849 fp->uf_tm_count = 0;
22850 profile_zero(&fp->uf_tm_self);
22851 profile_zero(&fp->uf_tm_total);
22852 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022853 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022854 if (fp->uf_tml_total == NULL)
22855 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022856 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022857 if (fp->uf_tml_self == NULL)
22858 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022859 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022860 fp->uf_tml_idx = -1;
22861 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22862 || fp->uf_tml_self == NULL)
22863 return; /* out of memory */
22864
22865 fp->uf_profiling = TRUE;
22866}
22867
22868/*
22869 * Dump the profiling results for all functions in file "fd".
22870 */
22871 void
22872func_dump_profile(fd)
22873 FILE *fd;
22874{
22875 hashitem_T *hi;
22876 int todo;
22877 ufunc_T *fp;
22878 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022879 ufunc_T **sorttab;
22880 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022881
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022882 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022883 if (todo == 0)
22884 return; /* nothing to dump */
22885
Bram Moolenaar73830342005-02-28 22:48:19 +000022886 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22887
Bram Moolenaar05159a02005-02-26 23:04:13 +000022888 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22889 {
22890 if (!HASHITEM_EMPTY(hi))
22891 {
22892 --todo;
22893 fp = HI2UF(hi);
22894 if (fp->uf_profiling)
22895 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022896 if (sorttab != NULL)
22897 sorttab[st_len++] = fp;
22898
Bram Moolenaar05159a02005-02-26 23:04:13 +000022899 if (fp->uf_name[0] == K_SPECIAL)
22900 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22901 else
22902 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22903 if (fp->uf_tm_count == 1)
22904 fprintf(fd, "Called 1 time\n");
22905 else
22906 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22907 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22908 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22909 fprintf(fd, "\n");
22910 fprintf(fd, "count total (s) self (s)\n");
22911
22912 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22913 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022914 if (FUNCLINE(fp, i) == NULL)
22915 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022916 prof_func_line(fd, fp->uf_tml_count[i],
22917 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022918 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22919 }
22920 fprintf(fd, "\n");
22921 }
22922 }
22923 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022924
22925 if (sorttab != NULL && st_len > 0)
22926 {
22927 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22928 prof_total_cmp);
22929 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22930 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22931 prof_self_cmp);
22932 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22933 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022934
22935 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022936}
Bram Moolenaar73830342005-02-28 22:48:19 +000022937
22938 static void
22939prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22940 FILE *fd;
22941 ufunc_T **sorttab;
22942 int st_len;
22943 char *title;
22944 int prefer_self; /* when equal print only self time */
22945{
22946 int i;
22947 ufunc_T *fp;
22948
22949 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22950 fprintf(fd, "count total (s) self (s) function\n");
22951 for (i = 0; i < 20 && i < st_len; ++i)
22952 {
22953 fp = sorttab[i];
22954 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22955 prefer_self);
22956 if (fp->uf_name[0] == K_SPECIAL)
22957 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22958 else
22959 fprintf(fd, " %s()\n", fp->uf_name);
22960 }
22961 fprintf(fd, "\n");
22962}
22963
22964/*
22965 * Print the count and times for one function or function line.
22966 */
22967 static void
22968prof_func_line(fd, count, total, self, prefer_self)
22969 FILE *fd;
22970 int count;
22971 proftime_T *total;
22972 proftime_T *self;
22973 int prefer_self; /* when equal print only self time */
22974{
22975 if (count > 0)
22976 {
22977 fprintf(fd, "%5d ", count);
22978 if (prefer_self && profile_equal(total, self))
22979 fprintf(fd, " ");
22980 else
22981 fprintf(fd, "%s ", profile_msg(total));
22982 if (!prefer_self && profile_equal(total, self))
22983 fprintf(fd, " ");
22984 else
22985 fprintf(fd, "%s ", profile_msg(self));
22986 }
22987 else
22988 fprintf(fd, " ");
22989}
22990
22991/*
22992 * Compare function for total time sorting.
22993 */
22994 static int
22995#ifdef __BORLANDC__
22996_RTLENTRYF
22997#endif
22998prof_total_cmp(s1, s2)
22999 const void *s1;
23000 const void *s2;
23001{
23002 ufunc_T *p1, *p2;
23003
23004 p1 = *(ufunc_T **)s1;
23005 p2 = *(ufunc_T **)s2;
23006 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23007}
23008
23009/*
23010 * Compare function for self time sorting.
23011 */
23012 static int
23013#ifdef __BORLANDC__
23014_RTLENTRYF
23015#endif
23016prof_self_cmp(s1, s2)
23017 const void *s1;
23018 const void *s2;
23019{
23020 ufunc_T *p1, *p2;
23021
23022 p1 = *(ufunc_T **)s1;
23023 p2 = *(ufunc_T **)s2;
23024 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23025}
23026
Bram Moolenaar05159a02005-02-26 23:04:13 +000023027#endif
23028
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023029/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023030 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023031 * Return TRUE if a package was loaded.
23032 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023033 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023034script_autoload(name, reload)
23035 char_u *name;
23036 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023037{
23038 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023039 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023040 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023041 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023042
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023043 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023044 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023045 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023046 return FALSE;
23047
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023048 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023049
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023050 /* Find the name in the list of previously loaded package names. Skip
23051 * "autoload/", it's always the same. */
23052 for (i = 0; i < ga_loaded.ga_len; ++i)
23053 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23054 break;
23055 if (!reload && i < ga_loaded.ga_len)
23056 ret = FALSE; /* was loaded already */
23057 else
23058 {
23059 /* Remember the name if it wasn't loaded already. */
23060 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23061 {
23062 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23063 tofree = NULL;
23064 }
23065
23066 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023067 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023068 ret = TRUE;
23069 }
23070
23071 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 return ret;
23073}
23074
23075/*
23076 * Return the autoload script name for a function or variable name.
23077 * Returns NULL when out of memory.
23078 */
23079 static char_u *
23080autoload_name(name)
23081 char_u *name;
23082{
23083 char_u *p;
23084 char_u *scriptname;
23085
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023086 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023087 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23088 if (scriptname == NULL)
23089 return FALSE;
23090 STRCPY(scriptname, "autoload/");
23091 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023092 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023093 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023094 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023095 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023096 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023097}
23098
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23100
23101/*
23102 * Function given to ExpandGeneric() to obtain the list of user defined
23103 * function names.
23104 */
23105 char_u *
23106get_user_func_name(xp, idx)
23107 expand_T *xp;
23108 int idx;
23109{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023110 static long_u done;
23111 static hashitem_T *hi;
23112 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113
23114 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023116 done = 0;
23117 hi = func_hashtab.ht_array;
23118 }
23119 if (done < func_hashtab.ht_used)
23120 {
23121 if (done++ > 0)
23122 ++hi;
23123 while (HASHITEM_EMPTY(hi))
23124 ++hi;
23125 fp = HI2UF(hi);
23126
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023127 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023128 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023130 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23131 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132
23133 cat_func_name(IObuff, fp);
23134 if (xp->xp_context != EXPAND_USER_FUNC)
23135 {
23136 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023137 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 STRCAT(IObuff, ")");
23139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 return IObuff;
23141 }
23142 return NULL;
23143}
23144
23145#endif /* FEAT_CMDL_COMPL */
23146
23147/*
23148 * Copy the function name of "fp" to buffer "buf".
23149 * "buf" must be able to hold the function name plus three bytes.
23150 * Takes care of script-local function names.
23151 */
23152 static void
23153cat_func_name(buf, fp)
23154 char_u *buf;
23155 ufunc_T *fp;
23156{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023157 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 {
23159 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023160 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 }
23162 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023163 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164}
23165
23166/*
23167 * ":delfunction {name}"
23168 */
23169 void
23170ex_delfunction(eap)
23171 exarg_T *eap;
23172{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023173 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 char_u *p;
23175 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023176 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177
23178 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023179 name = trans_function_name(&p, eap->skip, 0, &fudi);
23180 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023182 {
23183 if (fudi.fd_dict != NULL && !eap->skip)
23184 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 if (!ends_excmd(*skipwhite(p)))
23188 {
23189 vim_free(name);
23190 EMSG(_(e_trailing));
23191 return;
23192 }
23193 eap->nextcmd = check_nextcmd(p);
23194 if (eap->nextcmd != NULL)
23195 *p = NUL;
23196
23197 if (!eap->skip)
23198 fp = find_func(name);
23199 vim_free(name);
23200
23201 if (!eap->skip)
23202 {
23203 if (fp == NULL)
23204 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023205 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 return;
23207 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023208 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 {
23210 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23211 return;
23212 }
23213
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023214 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023216 /* Delete the dict item that refers to the function, it will
23217 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023218 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023220 else
23221 func_free(fp);
23222 }
23223}
23224
23225/*
23226 * Free a function and remove it from the list of functions.
23227 */
23228 static void
23229func_free(fp)
23230 ufunc_T *fp;
23231{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023232 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023233
23234 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023235 ga_clear_strings(&(fp->uf_args));
23236 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023237#ifdef FEAT_PROFILE
23238 vim_free(fp->uf_tml_count);
23239 vim_free(fp->uf_tml_total);
23240 vim_free(fp->uf_tml_self);
23241#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023243 /* remove the function from the function hashtable */
23244 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23245 if (HASHITEM_EMPTY(hi))
23246 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023248 hash_remove(&func_hashtab, hi);
23249
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023250 vim_free(fp);
23251}
23252
23253/*
23254 * Unreference a Function: decrement the reference count and free it when it
23255 * becomes zero. Only for numbered functions.
23256 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023257 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023258func_unref(name)
23259 char_u *name;
23260{
23261 ufunc_T *fp;
23262
23263 if (name != NULL && isdigit(*name))
23264 {
23265 fp = find_func(name);
23266 if (fp == NULL)
23267 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023268 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023269 {
23270 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023271 * when "uf_calls" becomes zero. */
23272 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023273 func_free(fp);
23274 }
23275 }
23276}
23277
23278/*
23279 * Count a reference to a Function.
23280 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023281 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023282func_ref(name)
23283 char_u *name;
23284{
23285 ufunc_T *fp;
23286
23287 if (name != NULL && isdigit(*name))
23288 {
23289 fp = find_func(name);
23290 if (fp == NULL)
23291 EMSG2(_(e_intern2), "func_ref()");
23292 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023293 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 }
23295}
23296
23297/*
23298 * Call a user function.
23299 */
23300 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023301call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 ufunc_T *fp; /* pointer to function */
23303 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023304 typval_T *argvars; /* arguments */
23305 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 linenr_T firstline; /* first line of range */
23307 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309{
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 char_u *save_sourcing_name;
23311 linenr_T save_sourcing_lnum;
23312 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023313 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 int save_did_emsg;
23315 static int depth = 0;
23316 dictitem_T *v;
23317 int fixvar_idx = 0; /* index in fixvar[] */
23318 int i;
23319 int ai;
23320 char_u numbuf[NUMBUFLEN];
23321 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023322#ifdef FEAT_PROFILE
23323 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023324 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326
23327 /* If depth of calling is getting too high, don't execute the function */
23328 if (depth >= p_mfd)
23329 {
23330 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023331 rettv->v_type = VAR_NUMBER;
23332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 return;
23334 }
23335 ++depth;
23336
23337 line_breakcheck(); /* check for CTRL-C hit */
23338
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023339 fc = (funccall_T *)alloc(sizeof(funccall_T));
23340 fc->caller = current_funccal;
23341 current_funccal = fc;
23342 fc->func = fp;
23343 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023344 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023345 fc->linenr = 0;
23346 fc->returned = FALSE;
23347 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023349 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23350 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351
Bram Moolenaar33570922005-01-25 22:26:29 +000023352 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023353 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23355 * each argument variable and saves a lot of time.
23356 */
23357 /*
23358 * Init l: variables.
23359 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023360 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023361 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023362 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023363 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23364 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023365 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023366 name = v->di_key;
23367 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023368 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023369 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023370 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023371 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 v->di_tv.vval.v_dict = selfdict;
23373 ++selfdict->dv_refcount;
23374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023375
Bram Moolenaar33570922005-01-25 22:26:29 +000023376 /*
23377 * Init a: variables.
23378 * Set a:0 to "argcount".
23379 * Set a:000 to a list with room for the "..." arguments.
23380 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023381 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023382 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023383 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023384 /* Use "name" to avoid a warning from some compiler that checks the
23385 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023386 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023387 name = v->di_key;
23388 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023389 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023390 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023392 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023393 v->di_tv.vval.v_list = &fc->l_varlist;
23394 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23395 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23396 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023397
23398 /*
23399 * Set a:firstline to "firstline" and a:lastline to "lastline".
23400 * Set a:name to named arguments.
23401 * Set a:N to the "..." arguments.
23402 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023403 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023404 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023405 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 (varnumber_T)lastline);
23407 for (i = 0; i < argcount; ++i)
23408 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023409 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 if (ai < 0)
23411 /* named argument a:name */
23412 name = FUNCARG(fp, i);
23413 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023414 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023415 /* "..." argument a:1, a:2, etc. */
23416 sprintf((char *)numbuf, "%d", ai + 1);
23417 name = numbuf;
23418 }
23419 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23420 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023421 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023422 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23423 }
23424 else
23425 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023426 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23427 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023428 if (v == NULL)
23429 break;
23430 v->di_flags = DI_FLAGS_RO;
23431 }
23432 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023433 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023434
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023435 /* Note: the values are copied directly to avoid alloc/free.
23436 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023437 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023438 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023439
23440 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23441 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023442 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23443 fc->l_listitems[ai].li_tv = argvars[i];
23444 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023445 }
23446 }
23447
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 /* Don't redraw while executing the function. */
23449 ++RedrawingDisabled;
23450 save_sourcing_name = sourcing_name;
23451 save_sourcing_lnum = sourcing_lnum;
23452 sourcing_lnum = 1;
23453 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023454 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 if (sourcing_name != NULL)
23456 {
23457 if (save_sourcing_name != NULL
23458 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23459 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23460 else
23461 STRCPY(sourcing_name, "function ");
23462 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23463
23464 if (p_verbose >= 12)
23465 {
23466 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023467 verbose_enter_scroll();
23468
Bram Moolenaar555b2802005-05-19 21:08:39 +000023469 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 if (p_verbose >= 14)
23471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023473 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023474 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023475 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476
23477 msg_puts((char_u *)"(");
23478 for (i = 0; i < argcount; ++i)
23479 {
23480 if (i > 0)
23481 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023482 if (argvars[i].v_type == VAR_NUMBER)
23483 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 else
23485 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023486 /* Do not want errors such as E724 here. */
23487 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023488 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023489 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023490 if (s != NULL)
23491 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023492 if (vim_strsize(s) > MSG_BUF_CLEN)
23493 {
23494 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23495 s = buf;
23496 }
23497 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023498 vim_free(tofree);
23499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
23501 }
23502 msg_puts((char_u *)")");
23503 }
23504 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023505
23506 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 --no_wait_return;
23508 }
23509 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023510#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023511 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023512 {
23513 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23514 func_do_profile(fp);
23515 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023516 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517 {
23518 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023519 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023520 profile_zero(&fp->uf_tm_children);
23521 }
23522 script_prof_save(&wait_start);
23523 }
23524#endif
23525
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023527 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 save_did_emsg = did_emsg;
23529 did_emsg = FALSE;
23530
23531 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023532 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23534
23535 --RedrawingDisabled;
23536
23537 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023540 clear_tv(rettv);
23541 rettv->v_type = VAR_NUMBER;
23542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 }
23544
Bram Moolenaar05159a02005-02-26 23:04:13 +000023545#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023546 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023547 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023548 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023549 profile_end(&call_start);
23550 profile_sub_wait(&wait_start, &call_start);
23551 profile_add(&fp->uf_tm_total, &call_start);
23552 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023553 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023554 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023555 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23556 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023557 }
23558 }
23559#endif
23560
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 /* when being verbose, mention the return value */
23562 if (p_verbose >= 12)
23563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023565 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023568 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023569 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023570 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023571 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023574 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023575 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023576 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023577 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023578
Bram Moolenaar555b2802005-05-19 21:08:39 +000023579 /* The value may be very long. Skip the middle part, so that we
23580 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023581 * truncate it at the end. Don't want errors such as E724 here. */
23582 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023583 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023584 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023585 if (s != NULL)
23586 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023587 if (vim_strsize(s) > MSG_BUF_CLEN)
23588 {
23589 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23590 s = buf;
23591 }
23592 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023593 vim_free(tofree);
23594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 }
23596 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023597
23598 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 --no_wait_return;
23600 }
23601
23602 vim_free(sourcing_name);
23603 sourcing_name = save_sourcing_name;
23604 sourcing_lnum = save_sourcing_lnum;
23605 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023607 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023608 script_prof_restore(&wait_start);
23609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610
23611 if (p_verbose >= 12 && sourcing_name != NULL)
23612 {
23613 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023614 verbose_enter_scroll();
23615
Bram Moolenaar555b2802005-05-19 21:08:39 +000023616 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023618
23619 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 --no_wait_return;
23621 }
23622
23623 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023624 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023626
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023627 /* If the a:000 list and the l: and a: dicts are not referenced we can
23628 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023629 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23630 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23631 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23632 {
23633 free_funccal(fc, FALSE);
23634 }
23635 else
23636 {
23637 hashitem_T *hi;
23638 listitem_T *li;
23639 int todo;
23640
23641 /* "fc" is still in use. This can happen when returning "a:000" or
23642 * assigning "l:" to a global variable.
23643 * Link "fc" in the list for garbage collection later. */
23644 fc->caller = previous_funccal;
23645 previous_funccal = fc;
23646
23647 /* Make a copy of the a: variables, since we didn't do that above. */
23648 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23649 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23650 {
23651 if (!HASHITEM_EMPTY(hi))
23652 {
23653 --todo;
23654 v = HI2DI(hi);
23655 copy_tv(&v->di_tv, &v->di_tv);
23656 }
23657 }
23658
23659 /* Make a copy of the a:000 items, since we didn't do that above. */
23660 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23661 copy_tv(&li->li_tv, &li->li_tv);
23662 }
23663}
23664
23665/*
23666 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023667 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023668 */
23669 static int
23670can_free_funccal(fc, copyID)
23671 funccall_T *fc;
23672 int copyID;
23673{
23674 return (fc->l_varlist.lv_copyID != copyID
23675 && fc->l_vars.dv_copyID != copyID
23676 && fc->l_avars.dv_copyID != copyID);
23677}
23678
23679/*
23680 * Free "fc" and what it contains.
23681 */
23682 static void
23683free_funccal(fc, free_val)
23684 funccall_T *fc;
23685 int free_val; /* a: vars were allocated */
23686{
23687 listitem_T *li;
23688
23689 /* The a: variables typevals may not have been allocated, only free the
23690 * allocated variables. */
23691 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23692
23693 /* free all l: variables */
23694 vars_clear(&fc->l_vars.dv_hashtab);
23695
23696 /* Free the a:000 variables if they were allocated. */
23697 if (free_val)
23698 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23699 clear_tv(&li->li_tv);
23700
23701 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702}
23703
23704/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023705 * Add a number variable "name" to dict "dp" with value "nr".
23706 */
23707 static void
23708add_nr_var(dp, v, name, nr)
23709 dict_T *dp;
23710 dictitem_T *v;
23711 char *name;
23712 varnumber_T nr;
23713{
23714 STRCPY(v->di_key, name);
23715 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23716 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23717 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023718 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023719 v->di_tv.vval.v_number = nr;
23720}
23721
23722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 * ":return [expr]"
23724 */
23725 void
23726ex_return(eap)
23727 exarg_T *eap;
23728{
23729 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023730 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 int returning = FALSE;
23732
23733 if (current_funccal == NULL)
23734 {
23735 EMSG(_("E133: :return not inside a function"));
23736 return;
23737 }
23738
23739 if (eap->skip)
23740 ++emsg_skip;
23741
23742 eap->nextcmd = NULL;
23743 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023744 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 {
23746 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023747 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 }
23751 /* It's safer to return also on error. */
23752 else if (!eap->skip)
23753 {
23754 /*
23755 * Return unless the expression evaluation has been cancelled due to an
23756 * aborting error, an interrupt, or an exception.
23757 */
23758 if (!aborting())
23759 returning = do_return(eap, FALSE, TRUE, NULL);
23760 }
23761
23762 /* When skipping or the return gets pending, advance to the next command
23763 * in this line (!returning). Otherwise, ignore the rest of the line.
23764 * Following lines will be ignored by get_func_line(). */
23765 if (returning)
23766 eap->nextcmd = NULL;
23767 else if (eap->nextcmd == NULL) /* no argument */
23768 eap->nextcmd = check_nextcmd(arg);
23769
23770 if (eap->skip)
23771 --emsg_skip;
23772}
23773
23774/*
23775 * Return from a function. Possibly makes the return pending. Also called
23776 * for a pending return at the ":endtry" or after returning from an extra
23777 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023778 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023779 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 * FALSE when the return gets pending.
23781 */
23782 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023783do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 exarg_T *eap;
23785 int reanimate;
23786 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023787 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788{
23789 int idx;
23790 struct condstack *cstack = eap->cstack;
23791
23792 if (reanimate)
23793 /* Undo the return. */
23794 current_funccal->returned = FALSE;
23795
23796 /*
23797 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23798 * not in its finally clause (which then is to be executed next) is found.
23799 * In this case, make the ":return" pending for execution at the ":endtry".
23800 * Otherwise, return normally.
23801 */
23802 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23803 if (idx >= 0)
23804 {
23805 cstack->cs_pending[idx] = CSTP_RETURN;
23806
23807 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023808 /* A pending return again gets pending. "rettv" points to an
23809 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023811 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 else
23813 {
23814 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023815 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023817 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023819 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 {
23821 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023822 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023823 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 else
23825 EMSG(_(e_outofmem));
23826 }
23827 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023828 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829
23830 if (reanimate)
23831 {
23832 /* The pending return value could be overwritten by a ":return"
23833 * without argument in a finally clause; reset the default
23834 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023835 current_funccal->rettv->v_type = VAR_NUMBER;
23836 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 }
23838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023839 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 }
23841 else
23842 {
23843 current_funccal->returned = TRUE;
23844
23845 /* If the return is carried out now, store the return value. For
23846 * a return immediately after reanimation, the value is already
23847 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023848 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023850 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023851 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023853 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 }
23855 }
23856
23857 return idx < 0;
23858}
23859
23860/*
23861 * Free the variable with a pending return value.
23862 */
23863 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023864discard_pending_return(rettv)
23865 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866{
Bram Moolenaar33570922005-01-25 22:26:29 +000023867 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868}
23869
23870/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023871 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 * is an allocated string. Used by report_pending() for verbose messages.
23873 */
23874 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023875get_return_cmd(rettv)
23876 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023878 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023879 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023880 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023882 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023883 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023884 if (s == NULL)
23885 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023886
23887 STRCPY(IObuff, ":return ");
23888 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23889 if (STRLEN(s) + 8 >= IOSIZE)
23890 STRCPY(IObuff + IOSIZE - 4, "...");
23891 vim_free(tofree);
23892 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893}
23894
23895/*
23896 * Get next function line.
23897 * Called by do_cmdline() to get the next line.
23898 * Returns allocated string, or NULL for end of function.
23899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 char_u *
23901get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023902 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023904 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905{
Bram Moolenaar33570922005-01-25 22:26:29 +000023906 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023907 ufunc_T *fp = fcp->func;
23908 char_u *retval;
23909 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910
23911 /* If breakpoints have been added/deleted need to check for it. */
23912 if (fcp->dbg_tick != debug_tick)
23913 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023914 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 sourcing_lnum);
23916 fcp->dbg_tick = debug_tick;
23917 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023918#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023919 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023920 func_line_end(cookie);
23921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922
Bram Moolenaar05159a02005-02-26 23:04:13 +000023923 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023924 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23925 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 retval = NULL;
23927 else
23928 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023929 /* Skip NULL lines (continuation lines). */
23930 while (fcp->linenr < gap->ga_len
23931 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23932 ++fcp->linenr;
23933 if (fcp->linenr >= gap->ga_len)
23934 retval = NULL;
23935 else
23936 {
23937 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23938 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023940 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023941 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023942#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 }
23945
23946 /* Did we encounter a breakpoint? */
23947 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23948 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023949 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023951 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 sourcing_lnum);
23953 fcp->dbg_tick = debug_tick;
23954 }
23955
23956 return retval;
23957}
23958
Bram Moolenaar05159a02005-02-26 23:04:13 +000023959#if defined(FEAT_PROFILE) || defined(PROTO)
23960/*
23961 * Called when starting to read a function line.
23962 * "sourcing_lnum" must be correct!
23963 * When skipping lines it may not actually be executed, but we won't find out
23964 * until later and we need to store the time now.
23965 */
23966 void
23967func_line_start(cookie)
23968 void *cookie;
23969{
23970 funccall_T *fcp = (funccall_T *)cookie;
23971 ufunc_T *fp = fcp->func;
23972
23973 if (fp->uf_profiling && sourcing_lnum >= 1
23974 && sourcing_lnum <= fp->uf_lines.ga_len)
23975 {
23976 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023977 /* Skip continuation lines. */
23978 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23979 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023980 fp->uf_tml_execed = FALSE;
23981 profile_start(&fp->uf_tml_start);
23982 profile_zero(&fp->uf_tml_children);
23983 profile_get_wait(&fp->uf_tml_wait);
23984 }
23985}
23986
23987/*
23988 * Called when actually executing a function line.
23989 */
23990 void
23991func_line_exec(cookie)
23992 void *cookie;
23993{
23994 funccall_T *fcp = (funccall_T *)cookie;
23995 ufunc_T *fp = fcp->func;
23996
23997 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23998 fp->uf_tml_execed = TRUE;
23999}
24000
24001/*
24002 * Called when done with a function line.
24003 */
24004 void
24005func_line_end(cookie)
24006 void *cookie;
24007{
24008 funccall_T *fcp = (funccall_T *)cookie;
24009 ufunc_T *fp = fcp->func;
24010
24011 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24012 {
24013 if (fp->uf_tml_execed)
24014 {
24015 ++fp->uf_tml_count[fp->uf_tml_idx];
24016 profile_end(&fp->uf_tml_start);
24017 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024018 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024019 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24020 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024021 }
24022 fp->uf_tml_idx = -1;
24023 }
24024}
24025#endif
24026
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027/*
24028 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024029 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 */
24031 int
24032func_has_ended(cookie)
24033 void *cookie;
24034{
Bram Moolenaar33570922005-01-25 22:26:29 +000024035 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036
24037 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24038 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024039 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 || fcp->returned);
24041}
24042
24043/*
24044 * return TRUE if cookie indicates a function which "abort"s on errors.
24045 */
24046 int
24047func_has_abort(cookie)
24048 void *cookie;
24049{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024050 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051}
24052
24053#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24054typedef enum
24055{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024056 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24057 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24058 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059} var_flavour_T;
24060
24061static var_flavour_T var_flavour __ARGS((char_u *varname));
24062
24063 static var_flavour_T
24064var_flavour(varname)
24065 char_u *varname;
24066{
24067 char_u *p = varname;
24068
24069 if (ASCII_ISUPPER(*p))
24070 {
24071 while (*(++p))
24072 if (ASCII_ISLOWER(*p))
24073 return VAR_FLAVOUR_SESSION;
24074 return VAR_FLAVOUR_VIMINFO;
24075 }
24076 else
24077 return VAR_FLAVOUR_DEFAULT;
24078}
24079#endif
24080
24081#if defined(FEAT_VIMINFO) || defined(PROTO)
24082/*
24083 * Restore global vars that start with a capital from the viminfo file
24084 */
24085 int
24086read_viminfo_varlist(virp, writing)
24087 vir_T *virp;
24088 int writing;
24089{
24090 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024091 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093
24094 if (!writing && (find_viminfo_parameter('!') != NULL))
24095 {
24096 tab = vim_strchr(virp->vir_line + 1, '\t');
24097 if (tab != NULL)
24098 {
24099 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024100 switch (*tab)
24101 {
24102 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024103#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024104 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024105#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024106 case 'D': type = VAR_DICT; break;
24107 case 'L': type = VAR_LIST; break;
24108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109
24110 tab = vim_strchr(tab, '\t');
24111 if (tab != NULL)
24112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024113 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024114 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024115 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024117#ifdef FEAT_FLOAT
24118 else if (type == VAR_FLOAT)
24119 (void)string2float(tab + 1, &tv.vval.v_float);
24120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024122 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024123 if (type == VAR_DICT || type == VAR_LIST)
24124 {
24125 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24126
24127 if (etv == NULL)
24128 /* Failed to parse back the dict or list, use it as a
24129 * string. */
24130 tv.v_type = VAR_STRING;
24131 else
24132 {
24133 vim_free(tv.vval.v_string);
24134 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024135 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024136 }
24137 }
24138
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024139 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024140
24141 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024142 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024143 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24144 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 }
24146 }
24147 }
24148
24149 return viminfo_readline(virp);
24150}
24151
24152/*
24153 * Write global vars that start with a capital to the viminfo file
24154 */
24155 void
24156write_viminfo_varlist(fp)
24157 FILE *fp;
24158{
Bram Moolenaar33570922005-01-25 22:26:29 +000024159 hashitem_T *hi;
24160 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024161 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024162 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024163 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024164 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024165 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166
24167 if (find_viminfo_parameter('!') == NULL)
24168 return;
24169
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024170 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024171
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024172 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024173 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024175 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024177 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 this_var = HI2DI(hi);
24179 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024181 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024182 {
24183 case VAR_STRING: s = "STR"; break;
24184 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024185#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024186 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024187#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024188 case VAR_DICT: s = "DIC"; break;
24189 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024190 default: continue;
24191 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024193 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024194 if (p != NULL)
24195 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024196 vim_free(tofree);
24197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 }
24199 }
24200}
24201#endif
24202
24203#if defined(FEAT_SESSION) || defined(PROTO)
24204 int
24205store_session_globals(fd)
24206 FILE *fd;
24207{
Bram Moolenaar33570922005-01-25 22:26:29 +000024208 hashitem_T *hi;
24209 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024210 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 char_u *p, *t;
24212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024213 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024214 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024218 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 this_var = HI2DI(hi);
24220 if ((this_var->di_tv.v_type == VAR_NUMBER
24221 || this_var->di_tv.v_type == VAR_STRING)
24222 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024223 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024224 /* Escape special characters with a backslash. Turn a LF and
24225 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024226 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024227 (char_u *)"\\\"\n\r");
24228 if (p == NULL) /* out of memory */
24229 break;
24230 for (t = p; *t != NUL; ++t)
24231 if (*t == '\n')
24232 *t = 'n';
24233 else if (*t == '\r')
24234 *t = 'r';
24235 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024236 this_var->di_key,
24237 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24238 : ' ',
24239 p,
24240 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24241 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024242 || put_eol(fd) == FAIL)
24243 {
24244 vim_free(p);
24245 return FAIL;
24246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 vim_free(p);
24248 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024249#ifdef FEAT_FLOAT
24250 else if (this_var->di_tv.v_type == VAR_FLOAT
24251 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24252 {
24253 float_T f = this_var->di_tv.vval.v_float;
24254 int sign = ' ';
24255
24256 if (f < 0)
24257 {
24258 f = -f;
24259 sign = '-';
24260 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024261 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024262 this_var->di_key, sign, f) < 0)
24263 || put_eol(fd) == FAIL)
24264 return FAIL;
24265 }
24266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 }
24268 }
24269 return OK;
24270}
24271#endif
24272
Bram Moolenaar661b1822005-07-28 22:36:45 +000024273/*
24274 * Display script name where an item was last set.
24275 * Should only be invoked when 'verbose' is non-zero.
24276 */
24277 void
24278last_set_msg(scriptID)
24279 scid_T scriptID;
24280{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024281 char_u *p;
24282
Bram Moolenaar661b1822005-07-28 22:36:45 +000024283 if (scriptID != 0)
24284 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024285 p = home_replace_save(NULL, get_scriptname(scriptID));
24286 if (p != NULL)
24287 {
24288 verbose_enter();
24289 MSG_PUTS(_("\n\tLast set from "));
24290 MSG_PUTS(p);
24291 vim_free(p);
24292 verbose_leave();
24293 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024294 }
24295}
24296
Bram Moolenaard812df62008-11-09 12:46:09 +000024297/*
24298 * List v:oldfiles in a nice way.
24299 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024300 void
24301ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024302 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024303{
24304 list_T *l = vimvars[VV_OLDFILES].vv_list;
24305 listitem_T *li;
24306 int nr = 0;
24307
24308 if (l == NULL)
24309 msg((char_u *)_("No old files"));
24310 else
24311 {
24312 msg_start();
24313 msg_scroll = TRUE;
24314 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24315 {
24316 msg_outnum((long)++nr);
24317 MSG_PUTS(": ");
24318 msg_outtrans(get_tv_string(&li->li_tv));
24319 msg_putchar('\n');
24320 out_flush(); /* output one line at a time */
24321 ui_breakcheck();
24322 }
24323 /* Assume "got_int" was set to truncate the listing. */
24324 got_int = FALSE;
24325
24326#ifdef FEAT_BROWSE_CMD
24327 if (cmdmod.browse)
24328 {
24329 quit_more = FALSE;
24330 nr = prompt_for_number(FALSE);
24331 msg_starthere();
24332 if (nr > 0)
24333 {
24334 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24335 (long)nr);
24336
24337 if (p != NULL)
24338 {
24339 p = expand_env_save(p);
24340 eap->arg = p;
24341 eap->cmdidx = CMD_edit;
24342 cmdmod.browse = FALSE;
24343 do_exedit(eap, NULL);
24344 vim_free(p);
24345 }
24346 }
24347 }
24348#endif
24349 }
24350}
24351
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352#endif /* FEAT_EVAL */
24353
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024355#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356
24357#ifdef WIN3264
24358/*
24359 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24360 */
24361static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24362static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24363static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24364
24365/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024366 * Get the short path (8.3) for the filename in "fnamep".
24367 * Only works for a valid file name.
24368 * When the path gets longer "fnamep" is changed and the allocated buffer
24369 * is put in "bufp".
24370 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24371 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 */
24373 static int
24374get_short_pathname(fnamep, bufp, fnamelen)
24375 char_u **fnamep;
24376 char_u **bufp;
24377 int *fnamelen;
24378{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024379 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 char_u *newbuf;
24381
24382 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 l = GetShortPathName(*fnamep, *fnamep, len);
24384 if (l > len - 1)
24385 {
24386 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024387 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 newbuf = vim_strnsave(*fnamep, l);
24389 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391
24392 vim_free(*bufp);
24393 *fnamep = *bufp = newbuf;
24394
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024395 /* Really should always succeed, as the buffer is big enough. */
24396 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 }
24398
24399 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024400 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401}
24402
24403/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024404 * Get the short path (8.3) for the filename in "fname". The converted
24405 * path is returned in "bufp".
24406 *
24407 * Some of the directories specified in "fname" may not exist. This function
24408 * will shorten the existing directories at the beginning of the path and then
24409 * append the remaining non-existing path.
24410 *
24411 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024412 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024413 * bufp - Pointer to an allocated buffer for the filename.
24414 * fnamelen - Length of the filename pointed to by fname
24415 *
24416 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 */
24418 static int
24419shortpath_for_invalid_fname(fname, bufp, fnamelen)
24420 char_u **fname;
24421 char_u **bufp;
24422 int *fnamelen;
24423{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024424 char_u *short_fname, *save_fname, *pbuf_unused;
24425 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024427 int old_len, len;
24428 int new_len, sfx_len;
24429 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430
24431 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024432 old_len = *fnamelen;
24433 save_fname = vim_strnsave(*fname, old_len);
24434 pbuf_unused = NULL;
24435 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024437 endp = save_fname + old_len - 1; /* Find the end of the copy */
24438 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024440 /*
24441 * Try shortening the supplied path till it succeeds by removing one
24442 * directory at a time from the tail of the path.
24443 */
24444 len = 0;
24445 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024447 /* go back one path-separator */
24448 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24449 --endp;
24450 if (endp <= save_fname)
24451 break; /* processed the complete path */
24452
24453 /*
24454 * Replace the path separator with a NUL and try to shorten the
24455 * resulting path.
24456 */
24457 ch = *endp;
24458 *endp = 0;
24459 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024460 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024461 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24462 {
24463 retval = FAIL;
24464 goto theend;
24465 }
24466 *endp = ch; /* preserve the string */
24467
24468 if (len > 0)
24469 break; /* successfully shortened the path */
24470
24471 /* failed to shorten the path. Skip the path separator */
24472 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 }
24474
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024475 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024477 /*
24478 * Succeeded in shortening the path. Now concatenate the shortened
24479 * path with the remaining path at the tail.
24480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024482 /* Compute the length of the new path. */
24483 sfx_len = (int)(save_endp - endp) + 1;
24484 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024486 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024488 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024490 /* There is not enough space in the currently allocated string,
24491 * copy it to a buffer big enough. */
24492 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024494 {
24495 retval = FAIL;
24496 goto theend;
24497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 }
24499 else
24500 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024501 /* Transfer short_fname to the main buffer (it's big enough),
24502 * unless get_short_pathname() did its work in-place. */
24503 *fname = *bufp = save_fname;
24504 if (short_fname != save_fname)
24505 vim_strncpy(save_fname, short_fname, len);
24506 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024508
24509 /* concat the not-shortened part of the path */
24510 vim_strncpy(*fname + len, endp, sfx_len);
24511 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024513
24514theend:
24515 vim_free(pbuf_unused);
24516 vim_free(save_fname);
24517
24518 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519}
24520
24521/*
24522 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024523 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 */
24525 static int
24526shortpath_for_partial(fnamep, bufp, fnamelen)
24527 char_u **fnamep;
24528 char_u **bufp;
24529 int *fnamelen;
24530{
24531 int sepcount, len, tflen;
24532 char_u *p;
24533 char_u *pbuf, *tfname;
24534 int hasTilde;
24535
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024536 /* Count up the path separators from the RHS.. so we know which part
24537 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024539 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 if (vim_ispathsep(*p))
24541 ++sepcount;
24542
24543 /* Need full path first (use expand_env() to remove a "~/") */
24544 hasTilde = (**fnamep == '~');
24545 if (hasTilde)
24546 pbuf = tfname = expand_env_save(*fnamep);
24547 else
24548 pbuf = tfname = FullName_save(*fnamep, FALSE);
24549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024550 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024552 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554
24555 if (len == 0)
24556 {
24557 /* Don't have a valid filename, so shorten the rest of the
24558 * path if we can. This CAN give us invalid 8.3 filenames, but
24559 * there's not a lot of point in guessing what it might be.
24560 */
24561 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024562 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 }
24565
24566 /* Count the paths backward to find the beginning of the desired string. */
24567 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024568 {
24569#ifdef FEAT_MBYTE
24570 if (has_mbyte)
24571 p -= mb_head_off(tfname, p);
24572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 if (vim_ispathsep(*p))
24574 {
24575 if (sepcount == 0 || (hasTilde && sepcount == 1))
24576 break;
24577 else
24578 sepcount --;
24579 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 if (hasTilde)
24582 {
24583 --p;
24584 if (p >= tfname)
24585 *p = '~';
24586 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024587 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 }
24589 else
24590 ++p;
24591
24592 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24593 vim_free(*bufp);
24594 *fnamelen = (int)STRLEN(p);
24595 *bufp = pbuf;
24596 *fnamep = p;
24597
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024598 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599}
24600#endif /* WIN3264 */
24601
24602/*
24603 * Adjust a filename, according to a string of modifiers.
24604 * *fnamep must be NUL terminated when called. When returning, the length is
24605 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024606 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 * When there is an error, *fnamep is set to NULL.
24608 */
24609 int
24610modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24611 char_u *src; /* string with modifiers */
24612 int *usedlen; /* characters after src that are used */
24613 char_u **fnamep; /* file name so far */
24614 char_u **bufp; /* buffer for allocated file name or NULL */
24615 int *fnamelen; /* length of fnamep */
24616{
24617 int valid = 0;
24618 char_u *tail;
24619 char_u *s, *p, *pbuf;
24620 char_u dirname[MAXPATHL];
24621 int c;
24622 int has_fullname = 0;
24623#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024624 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 int has_shortname = 0;
24626#endif
24627
24628repeat:
24629 /* ":p" - full path/file_name */
24630 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24631 {
24632 has_fullname = 1;
24633
24634 valid |= VALID_PATH;
24635 *usedlen += 2;
24636
24637 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24638 if ((*fnamep)[0] == '~'
24639#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24640 && ((*fnamep)[1] == '/'
24641# ifdef BACKSLASH_IN_FILENAME
24642 || (*fnamep)[1] == '\\'
24643# endif
24644 || (*fnamep)[1] == NUL)
24645
24646#endif
24647 )
24648 {
24649 *fnamep = expand_env_save(*fnamep);
24650 vim_free(*bufp); /* free any allocated file name */
24651 *bufp = *fnamep;
24652 if (*fnamep == NULL)
24653 return -1;
24654 }
24655
24656 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024657 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 {
24659 if (vim_ispathsep(*p)
24660 && p[1] == '.'
24661 && (p[2] == NUL
24662 || vim_ispathsep(p[2])
24663 || (p[2] == '.'
24664 && (p[3] == NUL || vim_ispathsep(p[3])))))
24665 break;
24666 }
24667
24668 /* FullName_save() is slow, don't use it when not needed. */
24669 if (*p != NUL || !vim_isAbsName(*fnamep))
24670 {
24671 *fnamep = FullName_save(*fnamep, *p != NUL);
24672 vim_free(*bufp); /* free any allocated file name */
24673 *bufp = *fnamep;
24674 if (*fnamep == NULL)
24675 return -1;
24676 }
24677
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024678#ifdef WIN3264
24679# if _WIN32_WINNT >= 0x0500
24680 if (vim_strchr(*fnamep, '~') != NULL)
24681 {
24682 /* Expand 8.3 filename to full path. Needed to make sure the same
24683 * file does not have two different names.
24684 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24685 p = alloc(_MAX_PATH + 1);
24686 if (p != NULL)
24687 {
24688 if (GetLongPathName(*fnamep, p, MAXPATHL))
24689 {
24690 vim_free(*bufp);
24691 *bufp = *fnamep = p;
24692 }
24693 else
24694 vim_free(p);
24695 }
24696 }
24697# endif
24698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 /* Append a path separator to a directory. */
24700 if (mch_isdir(*fnamep))
24701 {
24702 /* Make room for one or two extra characters. */
24703 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24704 vim_free(*bufp); /* free any allocated file name */
24705 *bufp = *fnamep;
24706 if (*fnamep == NULL)
24707 return -1;
24708 add_pathsep(*fnamep);
24709 }
24710 }
24711
24712 /* ":." - path relative to the current directory */
24713 /* ":~" - path relative to the home directory */
24714 /* ":8" - shortname path - postponed till after */
24715 while (src[*usedlen] == ':'
24716 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24717 {
24718 *usedlen += 2;
24719 if (c == '8')
24720 {
24721#ifdef WIN3264
24722 has_shortname = 1; /* Postpone this. */
24723#endif
24724 continue;
24725 }
24726 pbuf = NULL;
24727 /* Need full path first (use expand_env() to remove a "~/") */
24728 if (!has_fullname)
24729 {
24730 if (c == '.' && **fnamep == '~')
24731 p = pbuf = expand_env_save(*fnamep);
24732 else
24733 p = pbuf = FullName_save(*fnamep, FALSE);
24734 }
24735 else
24736 p = *fnamep;
24737
24738 has_fullname = 0;
24739
24740 if (p != NULL)
24741 {
24742 if (c == '.')
24743 {
24744 mch_dirname(dirname, MAXPATHL);
24745 s = shorten_fname(p, dirname);
24746 if (s != NULL)
24747 {
24748 *fnamep = s;
24749 if (pbuf != NULL)
24750 {
24751 vim_free(*bufp); /* free any allocated file name */
24752 *bufp = pbuf;
24753 pbuf = NULL;
24754 }
24755 }
24756 }
24757 else
24758 {
24759 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24760 /* Only replace it when it starts with '~' */
24761 if (*dirname == '~')
24762 {
24763 s = vim_strsave(dirname);
24764 if (s != NULL)
24765 {
24766 *fnamep = s;
24767 vim_free(*bufp);
24768 *bufp = s;
24769 }
24770 }
24771 }
24772 vim_free(pbuf);
24773 }
24774 }
24775
24776 tail = gettail(*fnamep);
24777 *fnamelen = (int)STRLEN(*fnamep);
24778
24779 /* ":h" - head, remove "/file_name", can be repeated */
24780 /* Don't remove the first "/" or "c:\" */
24781 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24782 {
24783 valid |= VALID_HEAD;
24784 *usedlen += 2;
24785 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024786 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024787 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 *fnamelen = (int)(tail - *fnamep);
24789#ifdef VMS
24790 if (*fnamelen > 0)
24791 *fnamelen += 1; /* the path separator is part of the path */
24792#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024793 if (*fnamelen == 0)
24794 {
24795 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24796 p = vim_strsave((char_u *)".");
24797 if (p == NULL)
24798 return -1;
24799 vim_free(*bufp);
24800 *bufp = *fnamep = tail = p;
24801 *fnamelen = 1;
24802 }
24803 else
24804 {
24805 while (tail > s && !after_pathsep(s, tail))
24806 mb_ptr_back(*fnamep, tail);
24807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808 }
24809
24810 /* ":8" - shortname */
24811 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24812 {
24813 *usedlen += 2;
24814#ifdef WIN3264
24815 has_shortname = 1;
24816#endif
24817 }
24818
24819#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024820 /*
24821 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822 */
24823 if (has_shortname)
24824 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024825 /* Copy the string if it is shortened by :h and when it wasn't copied
24826 * yet, because we are going to change it in place. Avoids changing
24827 * the buffer name for "%:8". */
24828 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829 {
24830 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024831 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832 return -1;
24833 vim_free(*bufp);
24834 *bufp = *fnamep = p;
24835 }
24836
24837 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024838 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 if (!has_fullname && !vim_isAbsName(*fnamep))
24840 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024841 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 return -1;
24843 }
24844 else
24845 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024846 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847
Bram Moolenaardc935552011-08-17 15:23:23 +020024848 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024850 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 return -1;
24852
24853 if (l == 0)
24854 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024855 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024857 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858 return -1;
24859 }
24860 *fnamelen = l;
24861 }
24862 }
24863#endif /* WIN3264 */
24864
24865 /* ":t" - tail, just the basename */
24866 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24867 {
24868 *usedlen += 2;
24869 *fnamelen -= (int)(tail - *fnamep);
24870 *fnamep = tail;
24871 }
24872
24873 /* ":e" - extension, can be repeated */
24874 /* ":r" - root, without extension, can be repeated */
24875 while (src[*usedlen] == ':'
24876 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24877 {
24878 /* find a '.' in the tail:
24879 * - for second :e: before the current fname
24880 * - otherwise: The last '.'
24881 */
24882 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24883 s = *fnamep - 2;
24884 else
24885 s = *fnamep + *fnamelen - 1;
24886 for ( ; s > tail; --s)
24887 if (s[0] == '.')
24888 break;
24889 if (src[*usedlen + 1] == 'e') /* :e */
24890 {
24891 if (s > tail)
24892 {
24893 *fnamelen += (int)(*fnamep - (s + 1));
24894 *fnamep = s + 1;
24895#ifdef VMS
24896 /* cut version from the extension */
24897 s = *fnamep + *fnamelen - 1;
24898 for ( ; s > *fnamep; --s)
24899 if (s[0] == ';')
24900 break;
24901 if (s > *fnamep)
24902 *fnamelen = s - *fnamep;
24903#endif
24904 }
24905 else if (*fnamep <= tail)
24906 *fnamelen = 0;
24907 }
24908 else /* :r */
24909 {
24910 if (s > tail) /* remove one extension */
24911 *fnamelen = (int)(s - *fnamep);
24912 }
24913 *usedlen += 2;
24914 }
24915
24916 /* ":s?pat?foo?" - substitute */
24917 /* ":gs?pat?foo?" - global substitute */
24918 if (src[*usedlen] == ':'
24919 && (src[*usedlen + 1] == 's'
24920 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24921 {
24922 char_u *str;
24923 char_u *pat;
24924 char_u *sub;
24925 int sep;
24926 char_u *flags;
24927 int didit = FALSE;
24928
24929 flags = (char_u *)"";
24930 s = src + *usedlen + 2;
24931 if (src[*usedlen + 1] == 'g')
24932 {
24933 flags = (char_u *)"g";
24934 ++s;
24935 }
24936
24937 sep = *s++;
24938 if (sep)
24939 {
24940 /* find end of pattern */
24941 p = vim_strchr(s, sep);
24942 if (p != NULL)
24943 {
24944 pat = vim_strnsave(s, (int)(p - s));
24945 if (pat != NULL)
24946 {
24947 s = p + 1;
24948 /* find end of substitution */
24949 p = vim_strchr(s, sep);
24950 if (p != NULL)
24951 {
24952 sub = vim_strnsave(s, (int)(p - s));
24953 str = vim_strnsave(*fnamep, *fnamelen);
24954 if (sub != NULL && str != NULL)
24955 {
24956 *usedlen = (int)(p + 1 - src);
24957 s = do_string_sub(str, pat, sub, flags);
24958 if (s != NULL)
24959 {
24960 *fnamep = s;
24961 *fnamelen = (int)STRLEN(s);
24962 vim_free(*bufp);
24963 *bufp = s;
24964 didit = TRUE;
24965 }
24966 }
24967 vim_free(sub);
24968 vim_free(str);
24969 }
24970 vim_free(pat);
24971 }
24972 }
24973 /* after using ":s", repeat all the modifiers */
24974 if (didit)
24975 goto repeat;
24976 }
24977 }
24978
Bram Moolenaar26df0922014-02-23 23:39:13 +010024979 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24980 {
24981 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24982 if (p == NULL)
24983 return -1;
24984 vim_free(*bufp);
24985 *bufp = *fnamep = p;
24986 *fnamelen = (int)STRLEN(p);
24987 *usedlen += 2;
24988 }
24989
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 return valid;
24991}
24992
24993/*
24994 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24995 * "flags" can be "g" to do a global substitute.
24996 * Returns an allocated string, NULL for error.
24997 */
24998 char_u *
24999do_string_sub(str, pat, sub, flags)
25000 char_u *str;
25001 char_u *pat;
25002 char_u *sub;
25003 char_u *flags;
25004{
25005 int sublen;
25006 regmatch_T regmatch;
25007 int i;
25008 int do_all;
25009 char_u *tail;
25010 garray_T ga;
25011 char_u *ret;
25012 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025013 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014
25015 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25016 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025017 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018
25019 ga_init2(&ga, 1, 200);
25020
25021 do_all = (flags[0] == 'g');
25022
25023 regmatch.rm_ic = p_ic;
25024 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25025 if (regmatch.regprog != NULL)
25026 {
25027 tail = str;
25028 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25029 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025030 /* Skip empty match except for first match. */
25031 if (regmatch.startp[0] == regmatch.endp[0])
25032 {
25033 if (zero_width == regmatch.startp[0])
25034 {
25035 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025036 i = MB_PTR2LEN(tail);
25037 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25038 (size_t)i);
25039 ga.ga_len += i;
25040 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025041 continue;
25042 }
25043 zero_width = regmatch.startp[0];
25044 }
25045
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 /*
25047 * Get some space for a temporary buffer to do the substitution
25048 * into. It will contain:
25049 * - The text up to where the match is.
25050 * - The substituted text.
25051 * - The text after the match.
25052 */
25053 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
25054 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
25055 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25056 {
25057 ga_clear(&ga);
25058 break;
25059 }
25060
25061 /* copy the text up to where the match is */
25062 i = (int)(regmatch.startp[0] - tail);
25063 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25064 /* add the substituted text */
25065 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25066 + ga.ga_len + i, TRUE, TRUE, FALSE);
25067 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025068 tail = regmatch.endp[0];
25069 if (*tail == NUL)
25070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 if (!do_all)
25072 break;
25073 }
25074
25075 if (ga.ga_data != NULL)
25076 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25077
Bram Moolenaar473de612013-06-08 18:19:48 +020025078 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 }
25080
25081 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25082 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025083 if (p_cpo == empty_option)
25084 p_cpo = save_cpo;
25085 else
25086 /* Darn, evaluating {sub} expression changed the value. */
25087 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088
25089 return ret;
25090}
25091
25092#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */