blob: 654e4187d58aa852a8177672beb62d23cadbc2dd [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 Moolenaar1b338d22014-08-22 13:13:27 +020017385 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017386 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017387 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388 int res;
17389 char_u numbuf1[NUMBUFLEN];
17390 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017392 si1 = (sortItem_T *)s1;
17393 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017394 tv1 = &si1->item->li_tv;
17395 tv2 = &si2->item->li_tv;
17396 /* tv2string() puts quotes around a string and allocates memory. Don't do
17397 * that for string variables. Use a single quote when comparing with a
17398 * non-string to do what the docs promise. */
17399 if (tv1->v_type == VAR_STRING)
17400 {
17401 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17402 p1 = (char_u *)"'";
17403 else
17404 p1 = tv1->vval.v_string;
17405 }
17406 else
17407 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17408 if (tv2->v_type == VAR_STRING)
17409 {
17410 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17411 p2 = (char_u *)"'";
17412 else
17413 p2 = tv2->vval.v_string;
17414 }
17415 else
17416 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017417 if (p1 == NULL)
17418 p1 = (char_u *)"";
17419 if (p2 == NULL)
17420 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017421 if (!item_compare_numeric)
17422 {
17423 if (item_compare_ic)
17424 res = STRICMP(p1, p2);
17425 else
17426 res = STRCMP(p1, p2);
17427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017429 {
17430 double n1, n2;
17431 n1 = strtod((char *)p1, (char **)&p1);
17432 n2 = strtod((char *)p2, (char **)&p2);
17433 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17434 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017435
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017436 /* When the result would be zero, compare the item indexes. Makes the
17437 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017438 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017439 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017440
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 vim_free(tofree1);
17442 vim_free(tofree2);
17443 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444}
17445
17446 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017447#ifdef __BORLANDC__
17448_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017450item_compare2(s1, s2)
17451 const void *s1;
17452 const void *s2;
17453{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017454 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017455 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017456 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017457 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017458 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 /* shortcut after failure in previous call; compare all items equal */
17461 if (item_compare_func_err)
17462 return 0;
17463
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017464 si1 = (sortItem_T *)s1;
17465 si2 = (sortItem_T *)s2;
17466
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017467 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017468 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017469 copy_tv(&si1->item->li_tv, &argv[0]);
17470 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017471
17472 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017473 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017474 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17475 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017476 clear_tv(&argv[0]);
17477 clear_tv(&argv[1]);
17478
17479 if (res == FAIL)
17480 res = ITEM_COMPARE_FAIL;
17481 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17483 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017484 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017486
17487 /* When the result would be zero, compare the pointers themselves. Makes
17488 * the sort stable. */
17489 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017490 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017491
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 return res;
17493}
17494
17495/*
17496 * "sort({list})" function
17497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017499do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 typval_T *argvars;
17501 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017502 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503{
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 list_T *l;
17505 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017506 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017507 long len;
17508 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017511 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 else
17513 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017514 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017515 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017516 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017517 return;
17518 rettv->vval.v_list = l;
17519 rettv->v_type = VAR_LIST;
17520 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521
Bram Moolenaar0d660222005-01-07 21:51:51 +000017522 len = list_len(l);
17523 if (len <= 1)
17524 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017527 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017528 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017529 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530 if (argvars[1].v_type != VAR_UNKNOWN)
17531 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017532 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017534 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535 else
17536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 int error = FALSE;
17538
17539 i = get_tv_number_chk(&argvars[1], &error);
17540 if (error)
17541 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542 if (i == 1)
17543 item_compare_ic = TRUE;
17544 else
17545 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017546 if (item_compare_func != NULL)
17547 {
17548 if (STRCMP(item_compare_func, "n") == 0)
17549 {
17550 item_compare_func = NULL;
17551 item_compare_numeric = TRUE;
17552 }
17553 else if (STRCMP(item_compare_func, "i") == 0)
17554 {
17555 item_compare_func = NULL;
17556 item_compare_ic = TRUE;
17557 }
17558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017560
17561 if (argvars[2].v_type != VAR_UNKNOWN)
17562 {
17563 /* optional third argument: {dict} */
17564 if (argvars[2].v_type != VAR_DICT)
17565 {
17566 EMSG(_(e_dictreq));
17567 return;
17568 }
17569 item_compare_selfdict = argvars[2].vval.v_dict;
17570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017574 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 if (ptrs == NULL)
17576 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577
Bram Moolenaar327aa022014-03-25 18:24:23 +010017578 i = 0;
17579 if (sort)
17580 {
17581 /* sort(): ptrs will be the list to sort */
17582 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017583 {
17584 ptrs[i].item = li;
17585 ptrs[i].idx = i;
17586 ++i;
17587 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017588
17589 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017590 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017591 /* test the compare function */
17592 if (item_compare_func != NULL
17593 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017595 EMSG(_("E702: Sort compare function failed"));
17596 else
17597 {
17598 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017599 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017600 item_compare_func == NULL ? item_compare : item_compare2);
17601
17602 if (!item_compare_func_err)
17603 {
17604 /* Clear the List and append the items in sorted order. */
17605 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17606 l->lv_len = 0;
17607 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017608 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017609 }
17610 }
17611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017613 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017614 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17615
17616 /* f_uniq(): ptrs will be a stack of items to remove */
17617 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017618 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017619 item_compare_func_ptr = item_compare_func
17620 ? item_compare2 : item_compare;
17621
17622 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17623 li = li->li_next)
17624 {
17625 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17626 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017627 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017628 if (item_compare_func_err)
17629 {
17630 EMSG(_("E882: Uniq compare function failed"));
17631 break;
17632 }
17633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017635 if (!item_compare_func_err)
17636 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017637 while (--i >= 0)
17638 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017639 li = ptrs[i].item->li_next;
17640 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017641 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017642 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017643 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017644 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017645 list_fix_watch(l, li);
17646 listitem_free(li);
17647 l->lv_len--;
17648 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017650 }
17651
17652 vim_free(ptrs);
17653 }
17654}
17655
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017656/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017657 * "sort({list})" function
17658 */
17659 static void
17660f_sort(argvars, rettv)
17661 typval_T *argvars;
17662 typval_T *rettv;
17663{
17664 do_sort_uniq(argvars, rettv, TRUE);
17665}
17666
17667/*
17668 * "uniq({list})" function
17669 */
17670 static void
17671f_uniq(argvars, rettv)
17672 typval_T *argvars;
17673 typval_T *rettv;
17674{
17675 do_sort_uniq(argvars, rettv, FALSE);
17676}
17677
17678/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017679 * "soundfold({word})" function
17680 */
17681 static void
17682f_soundfold(argvars, rettv)
17683 typval_T *argvars;
17684 typval_T *rettv;
17685{
17686 char_u *s;
17687
17688 rettv->v_type = VAR_STRING;
17689 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017690#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017691 rettv->vval.v_string = eval_soundfold(s);
17692#else
17693 rettv->vval.v_string = vim_strsave(s);
17694#endif
17695}
17696
17697/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017698 * "spellbadword()" function
17699 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017700 static void
17701f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017702 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017703 typval_T *rettv;
17704{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017705 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017706 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017707 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017708
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017709 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017710 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017711
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017712#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017713 if (argvars[0].v_type == VAR_UNKNOWN)
17714 {
17715 /* Find the start and length of the badly spelled word. */
17716 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17717 if (len != 0)
17718 word = ml_get_cursor();
17719 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017720 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017721 {
17722 char_u *str = get_tv_string_chk(&argvars[0]);
17723 int capcol = -1;
17724
17725 if (str != NULL)
17726 {
17727 /* Check the argument for spelling. */
17728 while (*str != NUL)
17729 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017730 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017731 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017732 {
17733 word = str;
17734 break;
17735 }
17736 str += len;
17737 }
17738 }
17739 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017740#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017742 list_append_string(rettv->vval.v_list, word, len);
17743 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017744 attr == HLF_SPB ? "bad" :
17745 attr == HLF_SPR ? "rare" :
17746 attr == HLF_SPL ? "local" :
17747 attr == HLF_SPC ? "caps" :
17748 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017749}
17750
17751/*
17752 * "spellsuggest()" function
17753 */
17754 static void
17755f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017756 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017757 typval_T *rettv;
17758{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017759#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017760 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017761 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017762 int maxcount;
17763 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017764 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017765 listitem_T *li;
17766 int need_capital = FALSE;
17767#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017768
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017769 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017770 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017771
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017772#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017773 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017774 {
17775 str = get_tv_string(&argvars[0]);
17776 if (argvars[1].v_type != VAR_UNKNOWN)
17777 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017778 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017779 if (maxcount <= 0)
17780 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017781 if (argvars[2].v_type != VAR_UNKNOWN)
17782 {
17783 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17784 if (typeerr)
17785 return;
17786 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017787 }
17788 else
17789 maxcount = 25;
17790
Bram Moolenaar4770d092006-01-12 23:22:24 +000017791 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017792
17793 for (i = 0; i < ga.ga_len; ++i)
17794 {
17795 str = ((char_u **)ga.ga_data)[i];
17796
17797 li = listitem_alloc();
17798 if (li == NULL)
17799 vim_free(str);
17800 else
17801 {
17802 li->li_tv.v_type = VAR_STRING;
17803 li->li_tv.v_lock = 0;
17804 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017805 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017806 }
17807 }
17808 ga_clear(&ga);
17809 }
17810#endif
17811}
17812
Bram Moolenaar0d660222005-01-07 21:51:51 +000017813 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017814f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 typval_T *argvars;
17816 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817{
17818 char_u *str;
17819 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017820 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017821 regmatch_T regmatch;
17822 char_u patbuf[NUMBUFLEN];
17823 char_u *save_cpo;
17824 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017826 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017827 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828
17829 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17830 save_cpo = p_cpo;
17831 p_cpo = (char_u *)"";
17832
17833 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017834 if (argvars[1].v_type != VAR_UNKNOWN)
17835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017836 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17837 if (pat == NULL)
17838 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017840 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017841 }
17842 if (pat == NULL || *pat == NUL)
17843 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017845 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017847 if (typeerr)
17848 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17851 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017853 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017854 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017856 if (*str == NUL)
17857 match = FALSE; /* empty item at the end */
17858 else
17859 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860 if (match)
17861 end = regmatch.startp[0];
17862 else
17863 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017864 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17865 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017867 if (list_append_string(rettv->vval.v_list, str,
17868 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017870 }
17871 if (!match)
17872 break;
17873 /* Advance to just after the match. */
17874 if (regmatch.endp[0] > str)
17875 col = 0;
17876 else
17877 {
17878 /* Don't get stuck at the same match. */
17879#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017880 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881#else
17882 col = 1;
17883#endif
17884 }
17885 str = regmatch.endp[0];
17886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887
Bram Moolenaar473de612013-06-08 18:19:48 +020017888 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
Bram Moolenaar0d660222005-01-07 21:51:51 +000017891 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892}
17893
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017894#ifdef FEAT_FLOAT
17895/*
17896 * "sqrt()" function
17897 */
17898 static void
17899f_sqrt(argvars, rettv)
17900 typval_T *argvars;
17901 typval_T *rettv;
17902{
17903 float_T f;
17904
17905 rettv->v_type = VAR_FLOAT;
17906 if (get_float_arg(argvars, &f) == OK)
17907 rettv->vval.v_float = sqrt(f);
17908 else
17909 rettv->vval.v_float = 0.0;
17910}
17911
17912/*
17913 * "str2float()" function
17914 */
17915 static void
17916f_str2float(argvars, rettv)
17917 typval_T *argvars;
17918 typval_T *rettv;
17919{
17920 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17921
17922 if (*p == '+')
17923 p = skipwhite(p + 1);
17924 (void)string2float(p, &rettv->vval.v_float);
17925 rettv->v_type = VAR_FLOAT;
17926}
17927#endif
17928
Bram Moolenaar2c932302006-03-18 21:42:09 +000017929/*
17930 * "str2nr()" function
17931 */
17932 static void
17933f_str2nr(argvars, rettv)
17934 typval_T *argvars;
17935 typval_T *rettv;
17936{
17937 int base = 10;
17938 char_u *p;
17939 long n;
17940
17941 if (argvars[1].v_type != VAR_UNKNOWN)
17942 {
17943 base = get_tv_number(&argvars[1]);
17944 if (base != 8 && base != 10 && base != 16)
17945 {
17946 EMSG(_(e_invarg));
17947 return;
17948 }
17949 }
17950
17951 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017952 if (*p == '+')
17953 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017954 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17955 rettv->vval.v_number = n;
17956}
17957
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958#ifdef HAVE_STRFTIME
17959/*
17960 * "strftime({format}[, {time}])" function
17961 */
17962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017964 typval_T *argvars;
17965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966{
17967 char_u result_buf[256];
17968 struct tm *curtime;
17969 time_t seconds;
17970 char_u *p;
17971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017972 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017974 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017975 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 seconds = time(NULL);
17977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017978 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 curtime = localtime(&seconds);
17980 /* MSVC returns NULL for an invalid value of seconds. */
17981 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017982 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 else
17984 {
17985# ifdef FEAT_MBYTE
17986 vimconv_T conv;
17987 char_u *enc;
17988
17989 conv.vc_type = CONV_NONE;
17990 enc = enc_locale();
17991 convert_setup(&conv, p_enc, enc);
17992 if (conv.vc_type != CONV_NONE)
17993 p = string_convert(&conv, p, NULL);
17994# endif
17995 if (p != NULL)
17996 (void)strftime((char *)result_buf, sizeof(result_buf),
17997 (char *)p, curtime);
17998 else
17999 result_buf[0] = NUL;
18000
18001# ifdef FEAT_MBYTE
18002 if (conv.vc_type != CONV_NONE)
18003 vim_free(p);
18004 convert_setup(&conv, enc, p_enc);
18005 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018006 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 else
18008# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018009 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010
18011# ifdef FEAT_MBYTE
18012 /* Release conversion descriptors */
18013 convert_setup(&conv, NULL, NULL);
18014 vim_free(enc);
18015# endif
18016 }
18017}
18018#endif
18019
18020/*
18021 * "stridx()" function
18022 */
18023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018024f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 typval_T *argvars;
18026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027{
18028 char_u buf[NUMBUFLEN];
18029 char_u *needle;
18030 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018031 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018033 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018035 needle = get_tv_string_chk(&argvars[1]);
18036 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018038 if (needle == NULL || haystack == NULL)
18039 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar33570922005-01-25 22:26:29 +000018041 if (argvars[2].v_type != VAR_UNKNOWN)
18042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018043 int error = FALSE;
18044
18045 start_idx = get_tv_number_chk(&argvars[2], &error);
18046 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018047 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018048 if (start_idx >= 0)
18049 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018050 }
18051
18052 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18053 if (pos != NULL)
18054 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055}
18056
18057/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018058 * "string()" function
18059 */
18060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 typval_T *argvars;
18063 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018064{
18065 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018066 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018068 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018069 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018070 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018071 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018072 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073}
18074
18075/*
18076 * "strlen()" function
18077 */
18078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018079f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018080 typval_T *argvars;
18081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018083 rettv->vval.v_number = (varnumber_T)(STRLEN(
18084 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085}
18086
18087/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018088 * "strchars()" function
18089 */
18090 static void
18091f_strchars(argvars, rettv)
18092 typval_T *argvars;
18093 typval_T *rettv;
18094{
18095 char_u *s = get_tv_string(&argvars[0]);
18096#ifdef FEAT_MBYTE
18097 varnumber_T len = 0;
18098
18099 while (*s != NUL)
18100 {
18101 mb_cptr2char_adv(&s);
18102 ++len;
18103 }
18104 rettv->vval.v_number = len;
18105#else
18106 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18107#endif
18108}
18109
18110/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018111 * "strdisplaywidth()" function
18112 */
18113 static void
18114f_strdisplaywidth(argvars, rettv)
18115 typval_T *argvars;
18116 typval_T *rettv;
18117{
18118 char_u *s = get_tv_string(&argvars[0]);
18119 int col = 0;
18120
18121 if (argvars[1].v_type != VAR_UNKNOWN)
18122 col = get_tv_number(&argvars[1]);
18123
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018124 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018125}
18126
18127/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018128 * "strwidth()" function
18129 */
18130 static void
18131f_strwidth(argvars, rettv)
18132 typval_T *argvars;
18133 typval_T *rettv;
18134{
18135 char_u *s = get_tv_string(&argvars[0]);
18136
18137 rettv->vval.v_number = (varnumber_T)(
18138#ifdef FEAT_MBYTE
18139 mb_string2cells(s, -1)
18140#else
18141 STRLEN(s)
18142#endif
18143 );
18144}
18145
18146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 * "strpart()" function
18148 */
18149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018150f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018151 typval_T *argvars;
18152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
18154 char_u *p;
18155 int n;
18156 int len;
18157 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018158 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018160 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 slen = (int)STRLEN(p);
18162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018163 n = get_tv_number_chk(&argvars[1], &error);
18164 if (error)
18165 len = 0;
18166 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 else
18169 len = slen - n; /* default len: all bytes that are available. */
18170
18171 /*
18172 * Only return the overlap between the specified part and the actual
18173 * string.
18174 */
18175 if (n < 0)
18176 {
18177 len += n;
18178 n = 0;
18179 }
18180 else if (n > slen)
18181 n = slen;
18182 if (len < 0)
18183 len = 0;
18184 else if (n + len > slen)
18185 len = slen - n;
18186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018187 rettv->v_type = VAR_STRING;
18188 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189}
18190
18191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 * "strridx()" function
18193 */
18194 static void
18195f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 typval_T *argvars;
18197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018198{
18199 char_u buf[NUMBUFLEN];
18200 char_u *needle;
18201 char_u *haystack;
18202 char_u *rest;
18203 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018204 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018206 needle = get_tv_string_chk(&argvars[1]);
18207 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018208
18209 rettv->vval.v_number = -1;
18210 if (needle == NULL || haystack == NULL)
18211 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018212
18213 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018214 if (argvars[2].v_type != VAR_UNKNOWN)
18215 {
18216 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018217 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018218 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018219 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018220 }
18221 else
18222 end_idx = haystack_len;
18223
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018225 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018226 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018227 lastmatch = haystack + end_idx;
18228 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018230 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018231 for (rest = haystack; *rest != '\0'; ++rest)
18232 {
18233 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018234 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235 break;
18236 lastmatch = rest;
18237 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018238 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018239
18240 if (lastmatch == NULL)
18241 rettv->vval.v_number = -1;
18242 else
18243 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18244}
18245
18246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 * "strtrans()" function
18248 */
18249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018250f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 typval_T *argvars;
18252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254 rettv->v_type = VAR_STRING;
18255 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256}
18257
18258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 * "submatch()" function
18260 */
18261 static void
18262f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018263 typval_T *argvars;
18264 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265{
Bram Moolenaar41571762014-04-02 19:00:58 +020018266 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018267 int no;
18268 int retList = 0;
18269
18270 no = (int)get_tv_number_chk(&argvars[0], &error);
18271 if (error)
18272 return;
18273 error = FALSE;
18274 if (argvars[1].v_type != VAR_UNKNOWN)
18275 retList = get_tv_number_chk(&argvars[1], &error);
18276 if (error)
18277 return;
18278
18279 if (retList == 0)
18280 {
18281 rettv->v_type = VAR_STRING;
18282 rettv->vval.v_string = reg_submatch(no);
18283 }
18284 else
18285 {
18286 rettv->v_type = VAR_LIST;
18287 rettv->vval.v_list = reg_submatch_list(no);
18288 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289}
18290
18291/*
18292 * "substitute()" function
18293 */
18294 static void
18295f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 typval_T *argvars;
18297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298{
18299 char_u patbuf[NUMBUFLEN];
18300 char_u subbuf[NUMBUFLEN];
18301 char_u flagsbuf[NUMBUFLEN];
18302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018303 char_u *str = get_tv_string_chk(&argvars[0]);
18304 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18305 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18306 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18307
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018309 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18310 rettv->vval.v_string = NULL;
18311 else
18312 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313}
18314
18315/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018316 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018319f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
18323 int id = 0;
18324#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018325 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 long col;
18327 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018328 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018330 lnum = get_tv_lnum(argvars); /* -1 on type error */
18331 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18332 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018334 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018335 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018336 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337#endif
18338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018339 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340}
18341
18342/*
18343 * "synIDattr(id, what [, mode])" function
18344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018347 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349{
18350 char_u *p = NULL;
18351#ifdef FEAT_SYN_HL
18352 int id;
18353 char_u *what;
18354 char_u *mode;
18355 char_u modebuf[NUMBUFLEN];
18356 int modec;
18357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358 id = get_tv_number(&argvars[0]);
18359 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018360 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018364 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 modec = 0; /* replace invalid with current */
18366 }
18367 else
18368 {
18369#ifdef FEAT_GUI
18370 if (gui.in_use)
18371 modec = 'g';
18372 else
18373#endif
18374 if (t_colors > 1)
18375 modec = 'c';
18376 else
18377 modec = 't';
18378 }
18379
18380
18381 switch (TOLOWER_ASC(what[0]))
18382 {
18383 case 'b':
18384 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18385 p = highlight_color(id, what, modec);
18386 else /* bold */
18387 p = highlight_has_attr(id, HL_BOLD, modec);
18388 break;
18389
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018390 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 p = highlight_color(id, what, modec);
18392 break;
18393
18394 case 'i':
18395 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18396 p = highlight_has_attr(id, HL_INVERSE, modec);
18397 else /* italic */
18398 p = highlight_has_attr(id, HL_ITALIC, modec);
18399 break;
18400
18401 case 'n': /* name */
18402 p = get_highlight_name(NULL, id - 1);
18403 break;
18404
18405 case 'r': /* reverse */
18406 p = highlight_has_attr(id, HL_INVERSE, modec);
18407 break;
18408
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018409 case 's':
18410 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18411 p = highlight_color(id, what, modec);
18412 else /* standout */
18413 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 break;
18415
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018416 case 'u':
18417 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18418 /* underline */
18419 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18420 else
18421 /* undercurl */
18422 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 break;
18424 }
18425
18426 if (p != NULL)
18427 p = vim_strsave(p);
18428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->v_type = VAR_STRING;
18430 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431}
18432
18433/*
18434 * "synIDtrans(id)" function
18435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018437f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440{
18441 int id;
18442
18443#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018444 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445
18446 if (id > 0)
18447 id = syn_get_final_id(id);
18448 else
18449#endif
18450 id = 0;
18451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018452 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453}
18454
18455/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018456 * "synconcealed(lnum, col)" function
18457 */
18458 static void
18459f_synconcealed(argvars, rettv)
18460 typval_T *argvars UNUSED;
18461 typval_T *rettv;
18462{
18463#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18464 long lnum;
18465 long col;
18466 int syntax_flags = 0;
18467 int cchar;
18468 int matchid = 0;
18469 char_u str[NUMBUFLEN];
18470#endif
18471
18472 rettv->v_type = VAR_LIST;
18473 rettv->vval.v_list = NULL;
18474
18475#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18476 lnum = get_tv_lnum(argvars); /* -1 on type error */
18477 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18478
18479 vim_memset(str, NUL, sizeof(str));
18480
18481 if (rettv_list_alloc(rettv) != FAIL)
18482 {
18483 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18484 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18485 && curwin->w_p_cole > 0)
18486 {
18487 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18488 syntax_flags = get_syntax_info(&matchid);
18489
18490 /* get the conceal character */
18491 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18492 {
18493 cchar = syn_get_sub_char();
18494 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18495 cchar = lcs_conceal;
18496 if (cchar != NUL)
18497 {
18498# ifdef FEAT_MBYTE
18499 if (has_mbyte)
18500 (*mb_char2bytes)(cchar, str);
18501 else
18502# endif
18503 str[0] = cchar;
18504 }
18505 }
18506 }
18507
18508 list_append_number(rettv->vval.v_list,
18509 (syntax_flags & HL_CONCEAL) != 0);
18510 /* -1 to auto-determine strlen */
18511 list_append_string(rettv->vval.v_list, str, -1);
18512 list_append_number(rettv->vval.v_list, matchid);
18513 }
18514#endif
18515}
18516
18517/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018518 * "synstack(lnum, col)" function
18519 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018520 static void
18521f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018522 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018523 typval_T *rettv;
18524{
18525#ifdef FEAT_SYN_HL
18526 long lnum;
18527 long col;
18528 int i;
18529 int id;
18530#endif
18531
18532 rettv->v_type = VAR_LIST;
18533 rettv->vval.v_list = NULL;
18534
18535#ifdef FEAT_SYN_HL
18536 lnum = get_tv_lnum(argvars); /* -1 on type error */
18537 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18538
18539 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018540 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018541 && rettv_list_alloc(rettv) != FAIL)
18542 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018543 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018544 for (i = 0; ; ++i)
18545 {
18546 id = syn_get_stack_item(i);
18547 if (id < 0)
18548 break;
18549 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18550 break;
18551 }
18552 }
18553#endif
18554}
18555
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018557get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018558 typval_T *argvars;
18559 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018560 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018562 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018564 char_u *infile = NULL;
18565 char_u buf[NUMBUFLEN];
18566 int err = FALSE;
18567 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018568 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018570 rettv->v_type = VAR_STRING;
18571 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018572 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018573 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018574
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018576 {
18577 /*
18578 * Write the string to a temp file, to be used for input of the shell
18579 * command.
18580 */
18581 if ((infile = vim_tempname('i')) == NULL)
18582 {
18583 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018584 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018585 }
18586
18587 fd = mch_fopen((char *)infile, WRITEBIN);
18588 if (fd == NULL)
18589 {
18590 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018591 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018592 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018593 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018594 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018595 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18596 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018597 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018598 else
18599 {
18600 p = get_tv_string_buf_chk(&argvars[1], buf);
18601 if (p == NULL)
18602 {
18603 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018604 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018605 }
18606 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18607 err = TRUE;
18608 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018609 if (fclose(fd) != 0)
18610 err = TRUE;
18611 if (err)
18612 {
18613 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018614 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018615 }
18616 }
18617
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018618 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018620 int len;
18621 listitem_T *li;
18622 char_u *s = NULL;
18623 char_u *start;
18624 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018625 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018627 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18628 SHELL_SILENT | SHELL_COOKED, &len);
18629 if (res == NULL)
18630 goto errret;
18631
18632 list = list_alloc();
18633 if (list == NULL)
18634 goto errret;
18635
18636 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018638 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018639 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018640 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018641 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018642
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018643 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018644 if (s == NULL)
18645 goto errret;
18646
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018647 for (p = s; start < end; ++p, ++start)
18648 *p = *start == NUL ? NL : *start;
18649 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018650
18651 li = listitem_alloc();
18652 if (li == NULL)
18653 {
18654 vim_free(s);
18655 goto errret;
18656 }
18657 li->li_tv.v_type = VAR_STRING;
18658 li->li_tv.vval.v_string = s;
18659 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018661
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018662 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018663 rettv->v_type = VAR_LIST;
18664 rettv->vval.v_list = list;
18665 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018667 else
18668 {
18669 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18670 SHELL_SILENT | SHELL_COOKED, NULL);
18671#ifdef USE_CR
18672 /* translate <CR> into <NL> */
18673 if (res != NULL)
18674 {
18675 char_u *s;
18676
18677 for (s = res; *s; ++s)
18678 {
18679 if (*s == CAR)
18680 *s = NL;
18681 }
18682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683#else
18684# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018685 /* translate <CR><NL> into <NL> */
18686 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018688 char_u *s, *d;
18689
18690 d = res;
18691 for (s = res; *s; ++s)
18692 {
18693 if (s[0] == CAR && s[1] == NL)
18694 ++s;
18695 *d++ = *s;
18696 }
18697 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699# endif
18700#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018701 rettv->vval.v_string = res;
18702 res = NULL;
18703 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018704
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018705errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018706 if (infile != NULL)
18707 {
18708 mch_remove(infile);
18709 vim_free(infile);
18710 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018711 if (res != NULL)
18712 vim_free(res);
18713 if (list != NULL)
18714 list_free(list, TRUE);
18715}
18716
18717/*
18718 * "system()" function
18719 */
18720 static void
18721f_system(argvars, rettv)
18722 typval_T *argvars;
18723 typval_T *rettv;
18724{
18725 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18726}
18727
18728/*
18729 * "systemlist()" function
18730 */
18731 static void
18732f_systemlist(argvars, rettv)
18733 typval_T *argvars;
18734 typval_T *rettv;
18735{
18736 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737}
18738
18739/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018740 * "tabpagebuflist()" function
18741 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018742 static void
18743f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018744 typval_T *argvars UNUSED;
18745 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018746{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018747#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018748 tabpage_T *tp;
18749 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018750
18751 if (argvars[0].v_type == VAR_UNKNOWN)
18752 wp = firstwin;
18753 else
18754 {
18755 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18756 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018757 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018758 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018759 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018760 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018761 for (; wp != NULL; wp = wp->w_next)
18762 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018763 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018764 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018765 }
18766#endif
18767}
18768
18769
18770/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018771 * "tabpagenr()" function
18772 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018773 static void
18774f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018775 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018776 typval_T *rettv;
18777{
18778 int nr = 1;
18779#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018780 char_u *arg;
18781
18782 if (argvars[0].v_type != VAR_UNKNOWN)
18783 {
18784 arg = get_tv_string_chk(&argvars[0]);
18785 nr = 0;
18786 if (arg != NULL)
18787 {
18788 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018789 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018790 else
18791 EMSG2(_(e_invexpr2), arg);
18792 }
18793 }
18794 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018795 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018796#endif
18797 rettv->vval.v_number = nr;
18798}
18799
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018800
18801#ifdef FEAT_WINDOWS
18802static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18803
18804/*
18805 * Common code for tabpagewinnr() and winnr().
18806 */
18807 static int
18808get_winnr(tp, argvar)
18809 tabpage_T *tp;
18810 typval_T *argvar;
18811{
18812 win_T *twin;
18813 int nr = 1;
18814 win_T *wp;
18815 char_u *arg;
18816
18817 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18818 if (argvar->v_type != VAR_UNKNOWN)
18819 {
18820 arg = get_tv_string_chk(argvar);
18821 if (arg == NULL)
18822 nr = 0; /* type error; errmsg already given */
18823 else if (STRCMP(arg, "$") == 0)
18824 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18825 else if (STRCMP(arg, "#") == 0)
18826 {
18827 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18828 if (twin == NULL)
18829 nr = 0;
18830 }
18831 else
18832 {
18833 EMSG2(_(e_invexpr2), arg);
18834 nr = 0;
18835 }
18836 }
18837
18838 if (nr > 0)
18839 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18840 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018841 {
18842 if (wp == NULL)
18843 {
18844 /* didn't find it in this tabpage */
18845 nr = 0;
18846 break;
18847 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018848 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018849 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018850 return nr;
18851}
18852#endif
18853
18854/*
18855 * "tabpagewinnr()" function
18856 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018857 static void
18858f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018859 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018860 typval_T *rettv;
18861{
18862 int nr = 1;
18863#ifdef FEAT_WINDOWS
18864 tabpage_T *tp;
18865
18866 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18867 if (tp == NULL)
18868 nr = 0;
18869 else
18870 nr = get_winnr(tp, &argvars[1]);
18871#endif
18872 rettv->vval.v_number = nr;
18873}
18874
18875
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018876/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018877 * "tagfiles()" function
18878 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018879 static void
18880f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018881 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018882 typval_T *rettv;
18883{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018884 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018885 tagname_T tn;
18886 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018889 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018890 fname = alloc(MAXPATHL);
18891 if (fname == NULL)
18892 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018894 for (first = TRUE; ; first = FALSE)
18895 if (get_tagfname(&tn, first, fname) == FAIL
18896 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018897 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018898 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018899 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018900}
18901
18902/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018903 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018904 */
18905 static void
18906f_taglist(argvars, rettv)
18907 typval_T *argvars;
18908 typval_T *rettv;
18909{
18910 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018911
18912 tag_pattern = get_tv_string(&argvars[0]);
18913
18914 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018915 if (*tag_pattern == NUL)
18916 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018917
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018918 if (rettv_list_alloc(rettv) == OK)
18919 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018920}
18921
18922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 * "tempname()" function
18924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018926f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929{
18930 static int x = 'A';
18931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018932 rettv->v_type = VAR_STRING;
18933 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934
18935 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18936 * names. Skip 'I' and 'O', they are used for shell redirection. */
18937 do
18938 {
18939 if (x == 'Z')
18940 x = '0';
18941 else if (x == '9')
18942 x = 'A';
18943 else
18944 {
18945#ifdef EBCDIC
18946 if (x == 'I')
18947 x = 'J';
18948 else if (x == 'R')
18949 x = 'S';
18950 else
18951#endif
18952 ++x;
18953 }
18954 } while (x == 'I' || x == 'O');
18955}
18956
18957/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018958 * "test(list)" function: Just checking the walls...
18959 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018960 static void
18961f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018962 typval_T *argvars UNUSED;
18963 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018964{
18965 /* Used for unit testing. Change the code below to your liking. */
18966#if 0
18967 listitem_T *li;
18968 list_T *l;
18969 char_u *bad, *good;
18970
18971 if (argvars[0].v_type != VAR_LIST)
18972 return;
18973 l = argvars[0].vval.v_list;
18974 if (l == NULL)
18975 return;
18976 li = l->lv_first;
18977 if (li == NULL)
18978 return;
18979 bad = get_tv_string(&li->li_tv);
18980 li = li->li_next;
18981 if (li == NULL)
18982 return;
18983 good = get_tv_string(&li->li_tv);
18984 rettv->vval.v_number = test_edit_score(bad, good);
18985#endif
18986}
18987
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018988#ifdef FEAT_FLOAT
18989/*
18990 * "tan()" function
18991 */
18992 static void
18993f_tan(argvars, rettv)
18994 typval_T *argvars;
18995 typval_T *rettv;
18996{
18997 float_T f;
18998
18999 rettv->v_type = VAR_FLOAT;
19000 if (get_float_arg(argvars, &f) == OK)
19001 rettv->vval.v_float = tan(f);
19002 else
19003 rettv->vval.v_float = 0.0;
19004}
19005
19006/*
19007 * "tanh()" function
19008 */
19009 static void
19010f_tanh(argvars, rettv)
19011 typval_T *argvars;
19012 typval_T *rettv;
19013{
19014 float_T f;
19015
19016 rettv->v_type = VAR_FLOAT;
19017 if (get_float_arg(argvars, &f) == OK)
19018 rettv->vval.v_float = tanh(f);
19019 else
19020 rettv->vval.v_float = 0.0;
19021}
19022#endif
19023
Bram Moolenaard52d9742005-08-21 22:20:28 +000019024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 * "tolower(string)" function
19026 */
19027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019029 typval_T *argvars;
19030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031{
19032 char_u *p;
19033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019034 p = vim_strsave(get_tv_string(&argvars[0]));
19035 rettv->v_type = VAR_STRING;
19036 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037
19038 if (p != NULL)
19039 while (*p != NUL)
19040 {
19041#ifdef FEAT_MBYTE
19042 int l;
19043
19044 if (enc_utf8)
19045 {
19046 int c, lc;
19047
19048 c = utf_ptr2char(p);
19049 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019050 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 /* TODO: reallocate string when byte count changes. */
19052 if (utf_char2len(lc) == l)
19053 utf_char2bytes(lc, p);
19054 p += l;
19055 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019056 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 p += l; /* skip multi-byte character */
19058 else
19059#endif
19060 {
19061 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19062 ++p;
19063 }
19064 }
19065}
19066
19067/*
19068 * "toupper(string)" function
19069 */
19070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 typval_T *argvars;
19073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019075 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077}
19078
19079/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019080 * "tr(string, fromstr, tostr)" function
19081 */
19082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 typval_T *argvars;
19085 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019086{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019087 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019088 char_u *fromstr;
19089 char_u *tostr;
19090 char_u *p;
19091#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019092 int inlen;
19093 int fromlen;
19094 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019095 int idx;
19096 char_u *cpstr;
19097 int cplen;
19098 int first = TRUE;
19099#endif
19100 char_u buf[NUMBUFLEN];
19101 char_u buf2[NUMBUFLEN];
19102 garray_T ga;
19103
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019104 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19106 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019107
19108 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109 rettv->v_type = VAR_STRING;
19110 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019111 if (fromstr == NULL || tostr == NULL)
19112 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019113 ga_init2(&ga, (int)sizeof(char), 80);
19114
19115#ifdef FEAT_MBYTE
19116 if (!has_mbyte)
19117#endif
19118 /* not multi-byte: fromstr and tostr must be the same length */
19119 if (STRLEN(fromstr) != STRLEN(tostr))
19120 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019121#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019122error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019123#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019124 EMSG2(_(e_invarg2), fromstr);
19125 ga_clear(&ga);
19126 return;
19127 }
19128
19129 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019130 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019131 {
19132#ifdef FEAT_MBYTE
19133 if (has_mbyte)
19134 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019135 inlen = (*mb_ptr2len)(in_str);
19136 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019137 cplen = inlen;
19138 idx = 0;
19139 for (p = fromstr; *p != NUL; p += fromlen)
19140 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019141 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019142 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019143 {
19144 for (p = tostr; *p != NUL; p += tolen)
19145 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019146 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019147 if (idx-- == 0)
19148 {
19149 cplen = tolen;
19150 cpstr = p;
19151 break;
19152 }
19153 }
19154 if (*p == NUL) /* tostr is shorter than fromstr */
19155 goto error;
19156 break;
19157 }
19158 ++idx;
19159 }
19160
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019161 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019162 {
19163 /* Check that fromstr and tostr have the same number of
19164 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019165 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019166 first = FALSE;
19167 for (p = tostr; *p != NUL; p += tolen)
19168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019169 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019170 --idx;
19171 }
19172 if (idx != 0)
19173 goto error;
19174 }
19175
19176 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019177 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019178 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019179
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019180 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019181 }
19182 else
19183#endif
19184 {
19185 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019186 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019187 if (p != NULL)
19188 ga_append(&ga, tostr[p - fromstr]);
19189 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019190 ga_append(&ga, *in_str);
19191 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019192 }
19193 }
19194
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019195 /* add a terminating NUL */
19196 ga_grow(&ga, 1);
19197 ga_append(&ga, NUL);
19198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019200}
19201
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019202#ifdef FEAT_FLOAT
19203/*
19204 * "trunc({float})" function
19205 */
19206 static void
19207f_trunc(argvars, rettv)
19208 typval_T *argvars;
19209 typval_T *rettv;
19210{
19211 float_T f;
19212
19213 rettv->v_type = VAR_FLOAT;
19214 if (get_float_arg(argvars, &f) == OK)
19215 /* trunc() is not in C90, use floor() or ceil() instead. */
19216 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19217 else
19218 rettv->vval.v_float = 0.0;
19219}
19220#endif
19221
Bram Moolenaar8299df92004-07-10 09:47:34 +000019222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 * "type(expr)" function
19224 */
19225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 typval_T *argvars;
19228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019230 int n;
19231
19232 switch (argvars[0].v_type)
19233 {
19234 case VAR_NUMBER: n = 0; break;
19235 case VAR_STRING: n = 1; break;
19236 case VAR_FUNC: n = 2; break;
19237 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019238 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019239#ifdef FEAT_FLOAT
19240 case VAR_FLOAT: n = 5; break;
19241#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019242 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19243 }
19244 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245}
19246
19247/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019248 * "undofile(name)" function
19249 */
19250 static void
19251f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019252 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019253 typval_T *rettv;
19254{
19255 rettv->v_type = VAR_STRING;
19256#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019257 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019258 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019259
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019260 if (*fname == NUL)
19261 {
19262 /* If there is no file name there will be no undo file. */
19263 rettv->vval.v_string = NULL;
19264 }
19265 else
19266 {
19267 char_u *ffname = FullName_save(fname, FALSE);
19268
19269 if (ffname != NULL)
19270 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19271 vim_free(ffname);
19272 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019273 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019274#else
19275 rettv->vval.v_string = NULL;
19276#endif
19277}
19278
19279/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019280 * "undotree()" function
19281 */
19282 static void
19283f_undotree(argvars, rettv)
19284 typval_T *argvars UNUSED;
19285 typval_T *rettv;
19286{
19287 if (rettv_dict_alloc(rettv) == OK)
19288 {
19289 dict_T *dict = rettv->vval.v_dict;
19290 list_T *list;
19291
Bram Moolenaar730cde92010-06-27 05:18:54 +020019292 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019293 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019294 dict_add_nr_str(dict, "save_last",
19295 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019296 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19297 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019298 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019299
19300 list = list_alloc();
19301 if (list != NULL)
19302 {
19303 u_eval_tree(curbuf->b_u_oldhead, list);
19304 dict_add_list(dict, "entries", list);
19305 }
19306 }
19307}
19308
19309/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019310 * "values(dict)" function
19311 */
19312 static void
19313f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 typval_T *argvars;
19315 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019316{
19317 dict_list(argvars, rettv, 1);
19318}
19319
19320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 * "virtcol(string)" function
19322 */
19323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019325 typval_T *argvars;
19326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327{
19328 colnr_T vcol = 0;
19329 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019330 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019332 fp = var2fpos(&argvars[0], FALSE, &fnum);
19333 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19334 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 {
19336 getvvcol(curwin, fp, NULL, NULL, &vcol);
19337 ++vcol;
19338 }
19339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341}
19342
19343/*
19344 * "visualmode()" function
19345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019348 typval_T *argvars UNUSED;
19349 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 char_u str[2];
19352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 str[0] = curbuf->b_visual_mode_eval;
19355 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357
19358 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019359 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361}
19362
19363/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019364 * "wildmenumode()" function
19365 */
19366 static void
19367f_wildmenumode(argvars, rettv)
19368 typval_T *argvars UNUSED;
19369 typval_T *rettv UNUSED;
19370{
19371#ifdef FEAT_WILDMENU
19372 if (wild_menu_showing)
19373 rettv->vval.v_number = 1;
19374#endif
19375}
19376
19377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 * "winbufnr(nr)" function
19379 */
19380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 typval_T *argvars;
19383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384{
19385 win_T *wp;
19386
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019387 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019391 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392}
19393
19394/*
19395 * "wincol()" function
19396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401{
19402 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404}
19405
19406/*
19407 * "winheight(nr)" function
19408 */
19409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019411 typval_T *argvars;
19412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413{
19414 win_T *wp;
19415
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019416 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019420 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421}
19422
19423/*
19424 * "winline()" function
19425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430{
19431 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019432 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
19435/*
19436 * "winnr()" function
19437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019439f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442{
19443 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019444
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019446 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
19452 * "winrestcmd()" function
19453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458{
19459#ifdef FEAT_WINDOWS
19460 win_T *wp;
19461 int winnr = 1;
19462 garray_T ga;
19463 char_u buf[50];
19464
19465 ga_init2(&ga, (int)sizeof(char), 70);
19466 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19467 {
19468 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19469 ga_concat(&ga, buf);
19470# ifdef FEAT_VERTSPLIT
19471 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19472 ga_concat(&ga, buf);
19473# endif
19474 ++winnr;
19475 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019476 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019478 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483}
19484
19485/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019486 * "winrestview()" function
19487 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019488 static void
19489f_winrestview(argvars, rettv)
19490 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019491 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019492{
19493 dict_T *dict;
19494
19495 if (argvars[0].v_type != VAR_DICT
19496 || (dict = argvars[0].vval.v_dict) == NULL)
19497 EMSG(_(e_invarg));
19498 else
19499 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019500 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19501 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19502 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19503 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019504#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019505 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19506 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019507#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019508 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19509 {
19510 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19511 curwin->w_set_curswant = FALSE;
19512 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019513
Bram Moolenaar82c25852014-05-28 16:47:16 +020019514 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19515 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019516#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019517 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19518 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019519#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019520 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19521 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19522 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19523 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019524
19525 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019526 win_new_height(curwin, curwin->w_height);
19527# ifdef FEAT_VERTSPLIT
19528 win_new_width(curwin, W_WIDTH(curwin));
19529# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019530 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019531
19532 if (curwin->w_topline == 0)
19533 curwin->w_topline = 1;
19534 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19535 curwin->w_topline = curbuf->b_ml.ml_line_count;
19536#ifdef FEAT_DIFF
19537 check_topfill(curwin, TRUE);
19538#endif
19539 }
19540}
19541
19542/*
19543 * "winsaveview()" function
19544 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019545 static void
19546f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019547 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019548 typval_T *rettv;
19549{
19550 dict_T *dict;
19551
Bram Moolenaara800b422010-06-27 01:15:55 +020019552 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019553 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019554 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019555
19556 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19557 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19558#ifdef FEAT_VIRTUALEDIT
19559 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19560#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019561 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019562 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19563
19564 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19565#ifdef FEAT_DIFF
19566 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19567#endif
19568 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19569 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19570}
19571
19572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 * "winwidth(nr)" function
19574 */
19575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 typval_T *argvars;
19578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579{
19580 win_T *wp;
19581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019582 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 else
19586#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019587 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590#endif
19591}
19592
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019594 * Write list of strings to file
19595 */
19596 static int
19597write_list(fd, list, binary)
19598 FILE *fd;
19599 list_T *list;
19600 int binary;
19601{
19602 listitem_T *li;
19603 int c;
19604 int ret = OK;
19605 char_u *s;
19606
19607 for (li = list->lv_first; li != NULL; li = li->li_next)
19608 {
19609 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19610 {
19611 if (*s == '\n')
19612 c = putc(NUL, fd);
19613 else
19614 c = putc(*s, fd);
19615 if (c == EOF)
19616 {
19617 ret = FAIL;
19618 break;
19619 }
19620 }
19621 if (!binary || li->li_next != NULL)
19622 if (putc('\n', fd) == EOF)
19623 {
19624 ret = FAIL;
19625 break;
19626 }
19627 if (ret == FAIL)
19628 {
19629 EMSG(_(e_write));
19630 break;
19631 }
19632 }
19633 return ret;
19634}
19635
19636/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019637 * "writefile()" function
19638 */
19639 static void
19640f_writefile(argvars, rettv)
19641 typval_T *argvars;
19642 typval_T *rettv;
19643{
19644 int binary = FALSE;
19645 char_u *fname;
19646 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019647 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019648
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019649 if (check_restricted() || check_secure())
19650 return;
19651
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019652 if (argvars[0].v_type != VAR_LIST)
19653 {
19654 EMSG2(_(e_listarg), "writefile()");
19655 return;
19656 }
19657 if (argvars[0].vval.v_list == NULL)
19658 return;
19659
19660 if (argvars[2].v_type != VAR_UNKNOWN
19661 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19662 binary = TRUE;
19663
19664 /* Always open the file in binary mode, library functions have a mind of
19665 * their own about CR-LF conversion. */
19666 fname = get_tv_string(&argvars[1]);
19667 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19668 {
19669 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19670 ret = -1;
19671 }
19672 else
19673 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019674 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19675 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019676 fclose(fd);
19677 }
19678
19679 rettv->vval.v_number = ret;
19680}
19681
19682/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019683 * "xor(expr, expr)" function
19684 */
19685 static void
19686f_xor(argvars, rettv)
19687 typval_T *argvars;
19688 typval_T *rettv;
19689{
19690 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19691 ^ get_tv_number_chk(&argvars[1], NULL);
19692}
19693
19694
19695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019697 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 */
19699 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019700var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019702 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019703 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019705 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019707 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708
Bram Moolenaara5525202006-03-02 22:52:09 +000019709 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019710 if (varp->v_type == VAR_LIST)
19711 {
19712 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019713 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019714 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019715 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019716
19717 l = varp->vval.v_list;
19718 if (l == NULL)
19719 return NULL;
19720
19721 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019722 pos.lnum = list_find_nr(l, 0L, &error);
19723 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019724 return NULL; /* invalid line number */
19725
19726 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019727 pos.col = list_find_nr(l, 1L, &error);
19728 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019729 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019730 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019731
19732 /* We accept "$" for the column number: last column. */
19733 li = list_find(l, 1L);
19734 if (li != NULL && li->li_tv.v_type == VAR_STRING
19735 && li->li_tv.vval.v_string != NULL
19736 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19737 pos.col = len + 1;
19738
Bram Moolenaara5525202006-03-02 22:52:09 +000019739 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019740 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019741 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019742 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019743
Bram Moolenaara5525202006-03-02 22:52:09 +000019744#ifdef FEAT_VIRTUALEDIT
19745 /* Get the virtual offset. Defaults to zero. */
19746 pos.coladd = list_find_nr(l, 2L, &error);
19747 if (error)
19748 pos.coladd = 0;
19749#endif
19750
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019751 return &pos;
19752 }
19753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019754 name = get_tv_string_chk(varp);
19755 if (name == NULL)
19756 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019757 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019759 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19760 {
19761 if (VIsual_active)
19762 return &VIsual;
19763 return &curwin->w_cursor;
19764 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019765 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019767 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19769 return NULL;
19770 return pp;
19771 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019772
19773#ifdef FEAT_VIRTUALEDIT
19774 pos.coladd = 0;
19775#endif
19776
Bram Moolenaar477933c2007-07-17 14:32:23 +000019777 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019778 {
19779 pos.col = 0;
19780 if (name[1] == '0') /* "w0": first visible line */
19781 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019782 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019783 pos.lnum = curwin->w_topline;
19784 return &pos;
19785 }
19786 else if (name[1] == '$') /* "w$": last visible line */
19787 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019788 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019789 pos.lnum = curwin->w_botline - 1;
19790 return &pos;
19791 }
19792 }
19793 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019795 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 {
19797 pos.lnum = curbuf->b_ml.ml_line_count;
19798 pos.col = 0;
19799 }
19800 else
19801 {
19802 pos.lnum = curwin->w_cursor.lnum;
19803 pos.col = (colnr_T)STRLEN(ml_get_curline());
19804 }
19805 return &pos;
19806 }
19807 return NULL;
19808}
19809
19810/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019811 * Convert list in "arg" into a position and optional file number.
19812 * When "fnump" is NULL there is no file number, only 3 items.
19813 * Note that the column is passed on as-is, the caller may want to decrement
19814 * it to use 1 for the first column.
19815 * Return FAIL when conversion is not possible, doesn't check the position for
19816 * validity.
19817 */
19818 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019819list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019820 typval_T *arg;
19821 pos_T *posp;
19822 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019823 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019824{
19825 list_T *l = arg->vval.v_list;
19826 long i = 0;
19827 long n;
19828
Bram Moolenaar493c1782014-05-28 14:34:46 +020019829 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19830 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019831 if (arg->v_type != VAR_LIST
19832 || l == NULL
19833 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019834 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019835 return FAIL;
19836
19837 if (fnump != NULL)
19838 {
19839 n = list_find_nr(l, i++, NULL); /* fnum */
19840 if (n < 0)
19841 return FAIL;
19842 if (n == 0)
19843 n = curbuf->b_fnum; /* current buffer */
19844 *fnump = n;
19845 }
19846
19847 n = list_find_nr(l, i++, NULL); /* lnum */
19848 if (n < 0)
19849 return FAIL;
19850 posp->lnum = n;
19851
19852 n = list_find_nr(l, i++, NULL); /* col */
19853 if (n < 0)
19854 return FAIL;
19855 posp->col = n;
19856
19857#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019858 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019859 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019860 posp->coladd = 0;
19861 else
19862 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019863#endif
19864
Bram Moolenaar493c1782014-05-28 14:34:46 +020019865 if (curswantp != NULL)
19866 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19867
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019868 return OK;
19869}
19870
19871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 * Get the length of an environment variable name.
19873 * Advance "arg" to the first character after the name.
19874 * Return 0 for error.
19875 */
19876 static int
19877get_env_len(arg)
19878 char_u **arg;
19879{
19880 char_u *p;
19881 int len;
19882
19883 for (p = *arg; vim_isIDc(*p); ++p)
19884 ;
19885 if (p == *arg) /* no name found */
19886 return 0;
19887
19888 len = (int)(p - *arg);
19889 *arg = p;
19890 return len;
19891}
19892
19893/*
19894 * Get the length of the name of a function or internal variable.
19895 * "arg" is advanced to the first non-white character after the name.
19896 * Return 0 if something is wrong.
19897 */
19898 static int
19899get_id_len(arg)
19900 char_u **arg;
19901{
19902 char_u *p;
19903 int len;
19904
19905 /* Find the end of the name. */
19906 for (p = *arg; eval_isnamec(*p); ++p)
19907 ;
19908 if (p == *arg) /* no name found */
19909 return 0;
19910
19911 len = (int)(p - *arg);
19912 *arg = skipwhite(p);
19913
19914 return len;
19915}
19916
19917/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019918 * Get the length of the name of a variable or function.
19919 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019921 * Return -1 if curly braces expansion failed.
19922 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 * If the name contains 'magic' {}'s, expand them and return the
19924 * expanded name in an allocated string via 'alias' - caller must free.
19925 */
19926 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019927get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 char_u **arg;
19929 char_u **alias;
19930 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019931 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
19933 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 char_u *p;
19935 char_u *expr_start;
19936 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937
19938 *alias = NULL; /* default to no alias */
19939
19940 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19941 && (*arg)[2] == (int)KE_SNR)
19942 {
19943 /* hard coded <SNR>, already translated */
19944 *arg += 3;
19945 return get_id_len(arg) + 3;
19946 }
19947 len = eval_fname_script(*arg);
19948 if (len > 0)
19949 {
19950 /* literal "<SID>", "s:" or "<SNR>" */
19951 *arg += len;
19952 }
19953
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019957 p = find_name_end(*arg, &expr_start, &expr_end,
19958 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 if (expr_start != NULL)
19960 {
19961 char_u *temp_string;
19962
19963 if (!evaluate)
19964 {
19965 len += (int)(p - *arg);
19966 *arg = skipwhite(p);
19967 return len;
19968 }
19969
19970 /*
19971 * Include any <SID> etc in the expanded string:
19972 * Thus the -len here.
19973 */
19974 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19975 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019976 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 *alias = temp_string;
19978 *arg = skipwhite(p);
19979 return (int)STRLEN(temp_string);
19980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981
19982 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019983 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 EMSG2(_(e_invexpr2), *arg);
19985
19986 return len;
19987}
19988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019989/*
19990 * Find the end of a variable or function name, taking care of magic braces.
19991 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19992 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019993 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 * Return a pointer to just after the name. Equal to "arg" if there is no
19995 * valid name.
19996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019998find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 char_u *arg;
20000 char_u **expr_start;
20001 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020002 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 int mb_nest = 0;
20005 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 char_u *p;
20007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010 *expr_start = NULL;
20011 *expr_end = NULL;
20012 }
20013
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020014 /* Quick check for valid starting character. */
20015 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20016 return arg;
20017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 for (p = arg; *p != NUL
20019 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020020 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020021 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020023 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020025 if (*p == '\'')
20026 {
20027 /* skip over 'string' to avoid counting [ and ] inside it. */
20028 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20029 ;
20030 if (*p == NUL)
20031 break;
20032 }
20033 else if (*p == '"')
20034 {
20035 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20036 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20037 if (*p == '\\' && p[1] != NUL)
20038 ++p;
20039 if (*p == NUL)
20040 break;
20041 }
20042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045 if (*p == '[')
20046 ++br_nest;
20047 else if (*p == ']')
20048 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053 if (*p == '{')
20054 {
20055 mb_nest++;
20056 if (expr_start != NULL && *expr_start == NULL)
20057 *expr_start = p;
20058 }
20059 else if (*p == '}')
20060 {
20061 mb_nest--;
20062 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20063 *expr_end = p;
20064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 }
20067
20068 return p;
20069}
20070
20071/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020072 * Expands out the 'magic' {}'s in a variable/function name.
20073 * Note that this can call itself recursively, to deal with
20074 * constructs like foo{bar}{baz}{bam}
20075 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20076 * "in_start" ^
20077 * "expr_start" ^
20078 * "expr_end" ^
20079 * "in_end" ^
20080 *
20081 * Returns a new allocated string, which the caller must free.
20082 * Returns NULL for failure.
20083 */
20084 static char_u *
20085make_expanded_name(in_start, expr_start, expr_end, in_end)
20086 char_u *in_start;
20087 char_u *expr_start;
20088 char_u *expr_end;
20089 char_u *in_end;
20090{
20091 char_u c1;
20092 char_u *retval = NULL;
20093 char_u *temp_result;
20094 char_u *nextcmd = NULL;
20095
20096 if (expr_end == NULL || in_end == NULL)
20097 return NULL;
20098 *expr_start = NUL;
20099 *expr_end = NUL;
20100 c1 = *in_end;
20101 *in_end = NUL;
20102
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020103 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020104 if (temp_result != NULL && nextcmd == NULL)
20105 {
20106 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20107 + (in_end - expr_end) + 1));
20108 if (retval != NULL)
20109 {
20110 STRCPY(retval, in_start);
20111 STRCAT(retval, temp_result);
20112 STRCAT(retval, expr_end + 1);
20113 }
20114 }
20115 vim_free(temp_result);
20116
20117 *in_end = c1; /* put char back for error messages */
20118 *expr_start = '{';
20119 *expr_end = '}';
20120
20121 if (retval != NULL)
20122 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020123 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020124 if (expr_start != NULL)
20125 {
20126 /* Further expansion! */
20127 temp_result = make_expanded_name(retval, expr_start,
20128 expr_end, temp_result);
20129 vim_free(retval);
20130 retval = temp_result;
20131 }
20132 }
20133
20134 return retval;
20135}
20136
20137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020139 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 */
20141 static int
20142eval_isnamec(c)
20143 int c;
20144{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020145 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20146}
20147
20148/*
20149 * Return TRUE if character "c" can be used as the first character in a
20150 * variable or function name (excluding '{' and '}').
20151 */
20152 static int
20153eval_isnamec1(c)
20154 int c;
20155{
20156 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157}
20158
20159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 * Set number v: variable to "val".
20161 */
20162 void
20163set_vim_var_nr(idx, val)
20164 int idx;
20165 long val;
20166{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020167 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168}
20169
20170/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020171 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 */
20173 long
20174get_vim_var_nr(idx)
20175 int idx;
20176{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020177 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178}
20179
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020180/*
20181 * Get string v: variable value. Uses a static buffer, can only be used once.
20182 */
20183 char_u *
20184get_vim_var_str(idx)
20185 int idx;
20186{
20187 return get_tv_string(&vimvars[idx].vv_tv);
20188}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020189
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020191 * Get List v: variable value. Caller must take care of reference count when
20192 * needed.
20193 */
20194 list_T *
20195get_vim_var_list(idx)
20196 int idx;
20197{
20198 return vimvars[idx].vv_list;
20199}
20200
20201/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020202 * Set v:char to character "c".
20203 */
20204 void
20205set_vim_var_char(c)
20206 int c;
20207{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020208 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020209
20210#ifdef FEAT_MBYTE
20211 if (has_mbyte)
20212 buf[(*mb_char2bytes)(c, buf)] = NUL;
20213 else
20214#endif
20215 {
20216 buf[0] = c;
20217 buf[1] = NUL;
20218 }
20219 set_vim_var_string(VV_CHAR, buf, -1);
20220}
20221
20222/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020223 * Set v:count to "count" and v:count1 to "count1".
20224 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 */
20226 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020227set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 long count;
20229 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020230 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020232 if (set_prevcount)
20233 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020234 vimvars[VV_COUNT].vv_nr = count;
20235 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
20238/*
20239 * Set string v: variable to a copy of "val".
20240 */
20241 void
20242set_vim_var_string(idx, val, len)
20243 int idx;
20244 char_u *val;
20245 int len; /* length of "val" to use or -1 (whole string) */
20246{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020247 /* Need to do this (at least) once, since we can't initialize a union.
20248 * Will always be invoked when "v:progname" is set. */
20249 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20250
Bram Moolenaare9a41262005-01-15 22:18:47 +000020251 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020253 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020255 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020257 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258}
20259
20260/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020261 * Set List v: variable to "val".
20262 */
20263 void
20264set_vim_var_list(idx, val)
20265 int idx;
20266 list_T *val;
20267{
20268 list_unref(vimvars[idx].vv_list);
20269 vimvars[idx].vv_list = val;
20270 if (val != NULL)
20271 ++val->lv_refcount;
20272}
20273
20274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 * Set v:register if needed.
20276 */
20277 void
20278set_reg_var(c)
20279 int c;
20280{
20281 char_u regname;
20282
20283 if (c == 0 || c == ' ')
20284 regname = '"';
20285 else
20286 regname = c;
20287 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020288 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 set_vim_var_string(VV_REG, &regname, 1);
20290}
20291
20292/*
20293 * Get or set v:exception. If "oldval" == NULL, return the current value.
20294 * Otherwise, restore the value to "oldval" and return NULL.
20295 * Must always be called in pairs to save and restore v:exception! Does not
20296 * take care of memory allocations.
20297 */
20298 char_u *
20299v_exception(oldval)
20300 char_u *oldval;
20301{
20302 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020303 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304
Bram Moolenaare9a41262005-01-15 22:18:47 +000020305 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 return NULL;
20307}
20308
20309/*
20310 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20311 * Otherwise, restore the value to "oldval" and return NULL.
20312 * Must always be called in pairs to save and restore v:throwpoint! Does not
20313 * take care of memory allocations.
20314 */
20315 char_u *
20316v_throwpoint(oldval)
20317 char_u *oldval;
20318{
20319 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020320 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321
Bram Moolenaare9a41262005-01-15 22:18:47 +000020322 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 return NULL;
20324}
20325
20326#if defined(FEAT_AUTOCMD) || defined(PROTO)
20327/*
20328 * Set v:cmdarg.
20329 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20330 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20331 * Must always be called in pairs!
20332 */
20333 char_u *
20334set_cmdarg(eap, oldarg)
20335 exarg_T *eap;
20336 char_u *oldarg;
20337{
20338 char_u *oldval;
20339 char_u *newval;
20340 unsigned len;
20341
Bram Moolenaare9a41262005-01-15 22:18:47 +000020342 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020343 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020345 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020346 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020347 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 }
20349
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020350 if (eap->force_bin == FORCE_BIN)
20351 len = 6;
20352 else if (eap->force_bin == FORCE_NOBIN)
20353 len = 8;
20354 else
20355 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020356
20357 if (eap->read_edit)
20358 len += 7;
20359
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020360 if (eap->force_ff != 0)
20361 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20362# ifdef FEAT_MBYTE
20363 if (eap->force_enc != 0)
20364 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020365 if (eap->bad_char != 0)
20366 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020367# endif
20368
20369 newval = alloc(len + 1);
20370 if (newval == NULL)
20371 return NULL;
20372
20373 if (eap->force_bin == FORCE_BIN)
20374 sprintf((char *)newval, " ++bin");
20375 else if (eap->force_bin == FORCE_NOBIN)
20376 sprintf((char *)newval, " ++nobin");
20377 else
20378 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020379
20380 if (eap->read_edit)
20381 STRCAT(newval, " ++edit");
20382
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020383 if (eap->force_ff != 0)
20384 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20385 eap->cmd + eap->force_ff);
20386# ifdef FEAT_MBYTE
20387 if (eap->force_enc != 0)
20388 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20389 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020390 if (eap->bad_char == BAD_KEEP)
20391 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20392 else if (eap->bad_char == BAD_DROP)
20393 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20394 else if (eap->bad_char != 0)
20395 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020396# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020397 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020398 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399}
20400#endif
20401
20402/*
20403 * Get the value of internal variable "name".
20404 * Return OK or FAIL.
20405 */
20406 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020407get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 char_u *name;
20409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020411 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020412 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413{
20414 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 typval_T *tv = NULL;
20416 typval_T atv;
20417 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419
20420 /* truncate the name, so that we can use strcmp() */
20421 cc = name[len];
20422 name[len] = NUL;
20423
20424 /*
20425 * Check for "b:changedtick".
20426 */
20427 if (STRCMP(name, "b:changedtick") == 0)
20428 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020429 atv.v_type = VAR_NUMBER;
20430 atv.vval.v_number = curbuf->b_changedtick;
20431 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433
20434 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 * Check for user-defined variables.
20436 */
20437 else
20438 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020439 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020441 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 }
20443
Bram Moolenaare9a41262005-01-15 22:18:47 +000020444 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020446 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 ret = FAIL;
20449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020451 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452
20453 name[len] = cc;
20454
20455 return ret;
20456}
20457
20458/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020459 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20460 * Also handle function call with Funcref variable: func(expr)
20461 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20462 */
20463 static int
20464handle_subscript(arg, rettv, evaluate, verbose)
20465 char_u **arg;
20466 typval_T *rettv;
20467 int evaluate; /* do more than finding the end */
20468 int verbose; /* give error messages */
20469{
20470 int ret = OK;
20471 dict_T *selfdict = NULL;
20472 char_u *s;
20473 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020474 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020475
20476 while (ret == OK
20477 && (**arg == '['
20478 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020479 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020480 && !vim_iswhite(*(*arg - 1)))
20481 {
20482 if (**arg == '(')
20483 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020484 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020485 if (evaluate)
20486 {
20487 functv = *rettv;
20488 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020489
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020490 /* Invoke the function. Recursive! */
20491 s = functv.vval.v_string;
20492 }
20493 else
20494 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020495 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020496 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20497 &len, evaluate, selfdict);
20498
20499 /* Clear the funcref afterwards, so that deleting it while
20500 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020501 if (evaluate)
20502 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020503
20504 /* Stop the expression evaluation when immediately aborting on
20505 * error, or when an interrupt occurred or an exception was thrown
20506 * but not caught. */
20507 if (aborting())
20508 {
20509 if (ret == OK)
20510 clear_tv(rettv);
20511 ret = FAIL;
20512 }
20513 dict_unref(selfdict);
20514 selfdict = NULL;
20515 }
20516 else /* **arg == '[' || **arg == '.' */
20517 {
20518 dict_unref(selfdict);
20519 if (rettv->v_type == VAR_DICT)
20520 {
20521 selfdict = rettv->vval.v_dict;
20522 if (selfdict != NULL)
20523 ++selfdict->dv_refcount;
20524 }
20525 else
20526 selfdict = NULL;
20527 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20528 {
20529 clear_tv(rettv);
20530 ret = FAIL;
20531 }
20532 }
20533 }
20534 dict_unref(selfdict);
20535 return ret;
20536}
20537
20538/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020539 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020540 * value).
20541 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020543alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020544{
Bram Moolenaar33570922005-01-25 22:26:29 +000020545 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020546}
20547
20548/*
20549 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 * The string "s" must have been allocated, it is consumed.
20551 * Return NULL for out of memory, the variable otherwise.
20552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 char_u *s;
20556{
Bram Moolenaar33570922005-01-25 22:26:29 +000020557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 rettv = alloc_tv();
20560 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020562 rettv->v_type = VAR_STRING;
20563 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 }
20565 else
20566 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020567 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568}
20569
20570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020571 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020573 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020574free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020575 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576{
20577 if (varp != NULL)
20578 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020579 switch (varp->v_type)
20580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020581 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020582 func_unref(varp->vval.v_string);
20583 /*FALLTHROUGH*/
20584 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020585 vim_free(varp->vval.v_string);
20586 break;
20587 case VAR_LIST:
20588 list_unref(varp->vval.v_list);
20589 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020590 case VAR_DICT:
20591 dict_unref(varp->vval.v_dict);
20592 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020593 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020594#ifdef FEAT_FLOAT
20595 case VAR_FLOAT:
20596#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020597 case VAR_UNKNOWN:
20598 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020599 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020600 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020601 break;
20602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 vim_free(varp);
20604 }
20605}
20606
20607/*
20608 * Free the memory for a variable value and set the value to NULL or 0.
20609 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020610 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613{
20614 if (varp != NULL)
20615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020616 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020618 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020619 func_unref(varp->vval.v_string);
20620 /*FALLTHROUGH*/
20621 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020622 vim_free(varp->vval.v_string);
20623 varp->vval.v_string = NULL;
20624 break;
20625 case VAR_LIST:
20626 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020627 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020628 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020629 case VAR_DICT:
20630 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020631 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020632 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020633 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020634 varp->vval.v_number = 0;
20635 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020636#ifdef FEAT_FLOAT
20637 case VAR_FLOAT:
20638 varp->vval.v_float = 0.0;
20639 break;
20640#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020641 case VAR_UNKNOWN:
20642 break;
20643 default:
20644 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020646 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 }
20648}
20649
20650/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020651 * Set the value of a variable to NULL without freeing items.
20652 */
20653 static void
20654init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020655 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020656{
20657 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020658 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020659}
20660
20661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 * Get the number value of a variable.
20663 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020664 * For incompatible types, return 0.
20665 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20666 * caller of incompatible types: it sets *denote to TRUE if "denote"
20667 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 */
20669 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020670get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020671 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020673 int error = FALSE;
20674
20675 return get_tv_number_chk(varp, &error); /* return 0L on error */
20676}
20677
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020678 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020679get_tv_number_chk(varp, denote)
20680 typval_T *varp;
20681 int *denote;
20682{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020683 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020687 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020688 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020689#ifdef FEAT_FLOAT
20690 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020691 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020692 break;
20693#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020694 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020695 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020696 break;
20697 case VAR_STRING:
20698 if (varp->vval.v_string != NULL)
20699 vim_str2nr(varp->vval.v_string, NULL, NULL,
20700 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020701 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020702 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020703 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020704 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020705 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020706 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020708 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020709 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020712 if (denote == NULL) /* useful for values that must be unsigned */
20713 n = -1;
20714 else
20715 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020716 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717}
20718
20719/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020720 * Get the lnum from the first argument.
20721 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020722 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 */
20724 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020725get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727{
Bram Moolenaar33570922005-01-25 22:26:29 +000020728 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 linenr_T lnum;
20730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020731 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 if (lnum == 0) /* no valid number, try using line() */
20733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020734 rettv.v_type = VAR_NUMBER;
20735 f_line(argvars, &rettv);
20736 lnum = rettv.vval.v_number;
20737 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 }
20739 return lnum;
20740}
20741
20742/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020743 * Get the lnum from the first argument.
20744 * Also accepts "$", then "buf" is used.
20745 * Returns 0 on error.
20746 */
20747 static linenr_T
20748get_tv_lnum_buf(argvars, buf)
20749 typval_T *argvars;
20750 buf_T *buf;
20751{
20752 if (argvars[0].v_type == VAR_STRING
20753 && argvars[0].vval.v_string != NULL
20754 && argvars[0].vval.v_string[0] == '$'
20755 && buf != NULL)
20756 return buf->b_ml.ml_line_count;
20757 return get_tv_number_chk(&argvars[0], NULL);
20758}
20759
20760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 * Get the string value of a variable.
20762 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020763 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20764 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 * If the String variable has never been set, return an empty string.
20766 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020767 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20768 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 */
20770 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773{
20774 static char_u mybuf[NUMBUFLEN];
20775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020776 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777}
20778
20779 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020780get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020781 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020782 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020784 char_u *res = get_tv_string_buf_chk(varp, buf);
20785
20786 return res != NULL ? res : (char_u *)"";
20787}
20788
Bram Moolenaar7d647822014-04-05 21:28:56 +020020789/*
20790 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20791 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020792 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020793get_tv_string_chk(varp)
20794 typval_T *varp;
20795{
20796 static char_u mybuf[NUMBUFLEN];
20797
20798 return get_tv_string_buf_chk(varp, mybuf);
20799}
20800
20801 static char_u *
20802get_tv_string_buf_chk(varp, buf)
20803 typval_T *varp;
20804 char_u *buf;
20805{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020806 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020808 case VAR_NUMBER:
20809 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20810 return buf;
20811 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020812 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020813 break;
20814 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020815 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020816 break;
20817 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020818 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020819 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020820#ifdef FEAT_FLOAT
20821 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020822 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020823 break;
20824#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020825 case VAR_STRING:
20826 if (varp->vval.v_string != NULL)
20827 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020828 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020829 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020830 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020833 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834}
20835
20836/*
20837 * Find variable "name" in the list of variables.
20838 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020839 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020840 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020841 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020843 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020844find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020847 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020850 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851
Bram Moolenaara7043832005-01-21 11:56:39 +000020852 ht = find_var_ht(name, &varname);
20853 if (htp != NULL)
20854 *htp = ht;
20855 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020857 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858}
20859
20860/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020861 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020862 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020864 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020865find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020866 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020867 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020868 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020869 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020870{
Bram Moolenaar33570922005-01-25 22:26:29 +000020871 hashitem_T *hi;
20872
20873 if (*varname == NUL)
20874 {
20875 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020876 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020877 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020878 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020879 case 'g': return &globvars_var;
20880 case 'v': return &vimvars_var;
20881 case 'b': return &curbuf->b_bufvar;
20882 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020883#ifdef FEAT_WINDOWS
20884 case 't': return &curtab->tp_winvar;
20885#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020886 case 'l': return current_funccal == NULL
20887 ? NULL : &current_funccal->l_vars_var;
20888 case 'a': return current_funccal == NULL
20889 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020890 }
20891 return NULL;
20892 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020893
20894 hi = hash_find(ht, varname);
20895 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020896 {
20897 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020898 * worked find the variable again. Don't auto-load a script if it was
20899 * loaded already, otherwise it would be loaded every time when
20900 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020901 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020902 {
20903 /* Note: script_autoload() may make "hi" invalid. It must either
20904 * be obtained again or not used. */
20905 if (!script_autoload(varname, FALSE) || aborting())
20906 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020907 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020908 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020909 if (HASHITEM_EMPTY(hi))
20910 return NULL;
20911 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020912 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020913}
20914
20915/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020916 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020917 * Set "varname" to the start of name without ':'.
20918 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020919 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020920find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 char_u *name;
20922 char_u **varname;
20923{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020924 hashitem_T *hi;
20925
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 if (name[1] != ':')
20927 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020928 /* The name must not start with a colon or #. */
20929 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 return NULL;
20931 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020932
20933 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020934 hi = hash_find(&compat_hashtab, name);
20935 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020936 return &compat_hashtab;
20937
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020939 return &globvarht; /* global variable */
20940 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 }
20942 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020943 if (*name == 'g') /* global variable */
20944 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020945 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20946 */
20947 if (vim_strchr(name + 2, ':') != NULL
20948 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020951 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020953 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020954#ifdef FEAT_WINDOWS
20955 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020956 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020957#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020958 if (*name == 'v') /* v: variable */
20959 return &vimvarht;
20960 if (*name == 'a' && current_funccal != NULL) /* function argument */
20961 return &current_funccal->l_avars.dv_hashtab;
20962 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20963 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (*name == 's' /* script variable */
20965 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20966 return &SCRIPT_VARS(current_SID);
20967 return NULL;
20968}
20969
20970/*
20971 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020972 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 * Returns NULL when it doesn't exist.
20974 */
20975 char_u *
20976get_var_value(name)
20977 char_u *name;
20978{
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020981 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 if (v == NULL)
20983 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020984 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985}
20986
20987/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020988 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 * sourcing this script and when executing functions defined in the script.
20990 */
20991 void
20992new_script_vars(id)
20993 scid_T id;
20994{
Bram Moolenaara7043832005-01-21 11:56:39 +000020995 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020996 hashtab_T *ht;
20997 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020998
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21000 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021001 /* Re-allocating ga_data means that an ht_array pointing to
21002 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021004 for (i = 1; i <= ga_scripts.ga_len; ++i)
21005 {
21006 ht = &SCRIPT_VARS(i);
21007 if (ht->ht_mask == HT_INIT_SIZE - 1)
21008 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021009 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021011 }
21012
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 while (ga_scripts.ga_len < id)
21014 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021015 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021016 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021017 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 }
21020 }
21021}
21022
21023/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21025 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 */
21027 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021028init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021029 dict_T *dict;
21030 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021031 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032{
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021034 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021035 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021036 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021037 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021038 dict_var->di_tv.vval.v_dict = dict;
21039 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021040 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021041 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21042 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043}
21044
21045/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021046 * Unreference a dictionary initialized by init_var_dict().
21047 */
21048 void
21049unref_var_dict(dict)
21050 dict_T *dict;
21051{
21052 /* Now the dict needs to be freed if no one else is using it, go back to
21053 * normal reference counting. */
21054 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21055 dict_unref(dict);
21056}
21057
21058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021060 * Frees all allocated variables and the value they contain.
21061 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 */
21063 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021064vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021065 hashtab_T *ht;
21066{
21067 vars_clear_ext(ht, TRUE);
21068}
21069
21070/*
21071 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21072 */
21073 static void
21074vars_clear_ext(ht, free_val)
21075 hashtab_T *ht;
21076 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
Bram Moolenaara7043832005-01-21 11:56:39 +000021078 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021079 hashitem_T *hi;
21080 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081
Bram Moolenaar33570922005-01-25 22:26:29 +000021082 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021084 for (hi = ht->ht_array; todo > 0; ++hi)
21085 {
21086 if (!HASHITEM_EMPTY(hi))
21087 {
21088 --todo;
21089
Bram Moolenaar33570922005-01-25 22:26:29 +000021090 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021091 * ht_array might change then. hash_clear() takes care of it
21092 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 v = HI2DI(hi);
21094 if (free_val)
21095 clear_tv(&v->di_tv);
21096 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21097 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021098 }
21099 }
21100 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021101 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102}
21103
Bram Moolenaara7043832005-01-21 11:56:39 +000021104/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021105 * Delete a variable from hashtab "ht" at item "hi".
21106 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021109delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021110 hashtab_T *ht;
21111 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112{
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021114
21115 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 clear_tv(&di->di_tv);
21117 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118}
21119
21120/*
21121 * List the value of one internal variable.
21122 */
21123 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021124list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021125 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021127 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021129 char_u *tofree;
21130 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021131 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021132
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021133 current_copyID += COPYID_INC;
21134 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021136 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021137 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138}
21139
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021141list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 char_u *prefix;
21143 char_u *name;
21144 int type;
21145 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021146 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147{
Bram Moolenaar31859182007-08-14 20:41:13 +000021148 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21149 msg_start();
21150 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 if (name != NULL) /* "a:" vars don't have a name stored */
21152 msg_puts(name);
21153 msg_putchar(' ');
21154 msg_advance(22);
21155 if (type == VAR_NUMBER)
21156 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021157 else if (type == VAR_FUNC)
21158 msg_putchar('*');
21159 else if (type == VAR_LIST)
21160 {
21161 msg_putchar('[');
21162 if (*string == '[')
21163 ++string;
21164 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021165 else if (type == VAR_DICT)
21166 {
21167 msg_putchar('{');
21168 if (*string == '{')
21169 ++string;
21170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 else
21172 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021173
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021175
21176 if (type == VAR_FUNC)
21177 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021178 if (*first)
21179 {
21180 msg_clr_eos();
21181 *first = FALSE;
21182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183}
21184
21185/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021186 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 * If the variable already exists, the value is updated.
21188 * Otherwise the variable is created.
21189 */
21190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021193 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021194 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195{
Bram Moolenaar33570922005-01-25 22:26:29 +000021196 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021200 ht = find_var_ht(name, &varname);
21201 if (ht == NULL || *varname == NUL)
21202 {
21203 EMSG2(_(e_illvar), name);
21204 return;
21205 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021206 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021207
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021208 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21209 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021210
Bram Moolenaar33570922005-01-25 22:26:29 +000021211 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021213 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021214 if (var_check_ro(v->di_flags, name)
21215 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021216 return;
21217 if (v->di_tv.v_type != tv->v_type
21218 && !((v->di_tv.v_type == VAR_STRING
21219 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021220 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021221 || tv->v_type == VAR_NUMBER))
21222#ifdef FEAT_FLOAT
21223 && !((v->di_tv.v_type == VAR_NUMBER
21224 || v->di_tv.v_type == VAR_FLOAT)
21225 && (tv->v_type == VAR_NUMBER
21226 || tv->v_type == VAR_FLOAT))
21227#endif
21228 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021229 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021230 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231 return;
21232 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021233
21234 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021235 * Handle setting internal v: variables separately: we don't change
21236 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021237 */
21238 if (ht == &vimvarht)
21239 {
21240 if (v->di_tv.v_type == VAR_STRING)
21241 {
21242 vim_free(v->di_tv.vval.v_string);
21243 if (copy || tv->v_type != VAR_STRING)
21244 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21245 else
21246 {
21247 /* Take over the string to avoid an extra alloc/free. */
21248 v->di_tv.vval.v_string = tv->vval.v_string;
21249 tv->vval.v_string = NULL;
21250 }
21251 }
21252 else if (v->di_tv.v_type != VAR_NUMBER)
21253 EMSG2(_(e_intern2), "set_var()");
21254 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021257 if (STRCMP(varname, "searchforward") == 0)
21258 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021259#ifdef FEAT_SEARCH_EXTRA
21260 else if (STRCMP(varname, "hlsearch") == 0)
21261 {
21262 no_hlsearch = !v->di_tv.vval.v_number;
21263 redraw_all_later(SOME_VALID);
21264 }
21265#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021266 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021267 return;
21268 }
21269
21270 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 }
21272 else /* add a new variable */
21273 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021274 /* Can't add "v:" variable. */
21275 if (ht == &vimvarht)
21276 {
21277 EMSG2(_(e_illvar), name);
21278 return;
21279 }
21280
Bram Moolenaar92124a32005-06-17 22:03:40 +000021281 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021282 if (!valid_varname(varname))
21283 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021284
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021285 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21286 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021287 if (v == NULL)
21288 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021290 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021292 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 return;
21294 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021295 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021297
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021298 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021300 else
21301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021303 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306}
21307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021308/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021309 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021310 * Also give an error message.
21311 */
21312 static int
21313var_check_ro(flags, name)
21314 int flags;
21315 char_u *name;
21316{
21317 if (flags & DI_FLAGS_RO)
21318 {
21319 EMSG2(_(e_readonlyvar), name);
21320 return TRUE;
21321 }
21322 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21323 {
21324 EMSG2(_(e_readonlysbx), name);
21325 return TRUE;
21326 }
21327 return FALSE;
21328}
21329
21330/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021331 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21332 * Also give an error message.
21333 */
21334 static int
21335var_check_fixed(flags, name)
21336 int flags;
21337 char_u *name;
21338{
21339 if (flags & DI_FLAGS_FIX)
21340 {
21341 EMSG2(_("E795: Cannot delete variable %s"), name);
21342 return TRUE;
21343 }
21344 return FALSE;
21345}
21346
21347/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021348 * Check if a funcref is assigned to a valid variable name.
21349 * Return TRUE and give an error if not.
21350 */
21351 static int
21352var_check_func_name(name, new_var)
21353 char_u *name; /* points to start of variable name */
21354 int new_var; /* TRUE when creating the variable */
21355{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021356 /* Allow for w: b: s: and t:. */
21357 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021358 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21359 ? name[2] : name[0]))
21360 {
21361 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21362 name);
21363 return TRUE;
21364 }
21365 /* Don't allow hiding a function. When "v" is not NULL we might be
21366 * assigning another function to the same var, the type is checked
21367 * below. */
21368 if (new_var && function_exists(name))
21369 {
21370 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21371 name);
21372 return TRUE;
21373 }
21374 return FALSE;
21375}
21376
21377/*
21378 * Check if a variable name is valid.
21379 * Return FALSE and give an error if not.
21380 */
21381 static int
21382valid_varname(varname)
21383 char_u *varname;
21384{
21385 char_u *p;
21386
21387 for (p = varname; *p != NUL; ++p)
21388 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21389 && *p != AUTOLOAD_CHAR)
21390 {
21391 EMSG2(_(e_illvar), varname);
21392 return FALSE;
21393 }
21394 return TRUE;
21395}
21396
21397/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021398 * Return TRUE if typeval "tv" is set to be locked (immutable).
21399 * Also give an error message, using "name".
21400 */
21401 static int
21402tv_check_lock(lock, name)
21403 int lock;
21404 char_u *name;
21405{
21406 if (lock & VAR_LOCKED)
21407 {
21408 EMSG2(_("E741: Value is locked: %s"),
21409 name == NULL ? (char_u *)_("Unknown") : name);
21410 return TRUE;
21411 }
21412 if (lock & VAR_FIXED)
21413 {
21414 EMSG2(_("E742: Cannot change value of %s"),
21415 name == NULL ? (char_u *)_("Unknown") : name);
21416 return TRUE;
21417 }
21418 return FALSE;
21419}
21420
21421/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021422 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021423 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021424 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021425 * It is OK for "from" and "to" to point to the same item. This is used to
21426 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021430 typval_T *from;
21431 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021433 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021434 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021435 switch (from->v_type)
21436 {
21437 case VAR_NUMBER:
21438 to->vval.v_number = from->vval.v_number;
21439 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021440#ifdef FEAT_FLOAT
21441 case VAR_FLOAT:
21442 to->vval.v_float = from->vval.v_float;
21443 break;
21444#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 case VAR_STRING:
21446 case VAR_FUNC:
21447 if (from->vval.v_string == NULL)
21448 to->vval.v_string = NULL;
21449 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021451 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 if (from->v_type == VAR_FUNC)
21453 func_ref(to->vval.v_string);
21454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455 break;
21456 case VAR_LIST:
21457 if (from->vval.v_list == NULL)
21458 to->vval.v_list = NULL;
21459 else
21460 {
21461 to->vval.v_list = from->vval.v_list;
21462 ++to->vval.v_list->lv_refcount;
21463 }
21464 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021465 case VAR_DICT:
21466 if (from->vval.v_dict == NULL)
21467 to->vval.v_dict = NULL;
21468 else
21469 {
21470 to->vval.v_dict = from->vval.v_dict;
21471 ++to->vval.v_dict->dv_refcount;
21472 }
21473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021475 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021476 break;
21477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478}
21479
21480/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021481 * Make a copy of an item.
21482 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021483 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21484 * reference to an already copied list/dict can be used.
21485 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021486 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021487 static int
21488item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 typval_T *from;
21490 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021492 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021493{
21494 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021495 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021496
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021498 {
21499 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021500 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021501 }
21502 ++recurse;
21503
21504 switch (from->v_type)
21505 {
21506 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507#ifdef FEAT_FLOAT
21508 case VAR_FLOAT:
21509#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021510 case VAR_STRING:
21511 case VAR_FUNC:
21512 copy_tv(from, to);
21513 break;
21514 case VAR_LIST:
21515 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021516 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021517 if (from->vval.v_list == NULL)
21518 to->vval.v_list = NULL;
21519 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21520 {
21521 /* use the copy made earlier */
21522 to->vval.v_list = from->vval.v_list->lv_copylist;
21523 ++to->vval.v_list->lv_refcount;
21524 }
21525 else
21526 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21527 if (to->vval.v_list == NULL)
21528 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021529 break;
21530 case VAR_DICT:
21531 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021532 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021533 if (from->vval.v_dict == NULL)
21534 to->vval.v_dict = NULL;
21535 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21536 {
21537 /* use the copy made earlier */
21538 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21539 ++to->vval.v_dict->dv_refcount;
21540 }
21541 else
21542 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21543 if (to->vval.v_dict == NULL)
21544 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021545 break;
21546 default:
21547 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021548 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021549 }
21550 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021551 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021552}
21553
21554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 * ":echo expr1 ..." print each argument separated with a space, add a
21556 * newline at the end.
21557 * ":echon expr1 ..." print each argument plain.
21558 */
21559 void
21560ex_echo(eap)
21561 exarg_T *eap;
21562{
21563 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021565 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 char_u *p;
21567 int needclr = TRUE;
21568 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570
21571 if (eap->skip)
21572 ++emsg_skip;
21573 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21574 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021575 /* If eval1() causes an error message the text from the command may
21576 * still need to be cleared. E.g., "echo 22,44". */
21577 need_clr_eos = needclr;
21578
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021580 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 {
21582 /*
21583 * Report the invalid expression unless the expression evaluation
21584 * has been cancelled due to an aborting error, an interrupt, or an
21585 * exception.
21586 */
21587 if (!aborting())
21588 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021589 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 break;
21591 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021592 need_clr_eos = FALSE;
21593
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 if (!eap->skip)
21595 {
21596 if (atstart)
21597 {
21598 atstart = FALSE;
21599 /* Call msg_start() after eval1(), evaluating the expression
21600 * may cause a message to appear. */
21601 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021602 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021603 /* Mark the saved text as finishing the line, so that what
21604 * follows is displayed on a new line when scrolling back
21605 * at the more prompt. */
21606 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 }
21610 else if (eap->cmdidx == CMD_echo)
21611 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021612 current_copyID += COPYID_INC;
21613 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021614 if (p != NULL)
21615 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021617 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021619 if (*p != TAB && needclr)
21620 {
21621 /* remove any text still there from the command */
21622 msg_clr_eos();
21623 needclr = FALSE;
21624 }
21625 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 }
21627 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021628 {
21629#ifdef FEAT_MBYTE
21630 if (has_mbyte)
21631 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021632 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021633
21634 (void)msg_outtrans_len_attr(p, i, echo_attr);
21635 p += i - 1;
21636 }
21637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021639 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021642 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021644 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 arg = skipwhite(arg);
21646 }
21647 eap->nextcmd = check_nextcmd(arg);
21648
21649 if (eap->skip)
21650 --emsg_skip;
21651 else
21652 {
21653 /* remove text that may still be there from the command */
21654 if (needclr)
21655 msg_clr_eos();
21656 if (eap->cmdidx == CMD_echo)
21657 msg_end();
21658 }
21659}
21660
21661/*
21662 * ":echohl {name}".
21663 */
21664 void
21665ex_echohl(eap)
21666 exarg_T *eap;
21667{
21668 int id;
21669
21670 id = syn_name2id(eap->arg);
21671 if (id == 0)
21672 echo_attr = 0;
21673 else
21674 echo_attr = syn_id2attr(id);
21675}
21676
21677/*
21678 * ":execute expr1 ..." execute the result of an expression.
21679 * ":echomsg expr1 ..." Print a message
21680 * ":echoerr expr1 ..." Print an error
21681 * Each gets spaces around each argument and a newline at the end for
21682 * echo commands
21683 */
21684 void
21685ex_execute(eap)
21686 exarg_T *eap;
21687{
21688 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 int ret = OK;
21691 char_u *p;
21692 garray_T ga;
21693 int len;
21694 int save_did_emsg;
21695
21696 ga_init2(&ga, 1, 80);
21697
21698 if (eap->skip)
21699 ++emsg_skip;
21700 while (*arg != NUL && *arg != '|' && *arg != '\n')
21701 {
21702 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 {
21705 /*
21706 * Report the invalid expression unless the expression evaluation
21707 * has been cancelled due to an aborting error, an interrupt, or an
21708 * exception.
21709 */
21710 if (!aborting())
21711 EMSG2(_(e_invexpr2), p);
21712 ret = FAIL;
21713 break;
21714 }
21715
21716 if (!eap->skip)
21717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021718 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 len = (int)STRLEN(p);
21720 if (ga_grow(&ga, len + 2) == FAIL)
21721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021722 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 ret = FAIL;
21724 break;
21725 }
21726 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 ga.ga_len += len;
21730 }
21731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 arg = skipwhite(arg);
21734 }
21735
21736 if (ret != FAIL && ga.ga_data != NULL)
21737 {
21738 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021741 out_flush();
21742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 else if (eap->cmdidx == CMD_echoerr)
21744 {
21745 /* We don't want to abort following commands, restore did_emsg. */
21746 save_did_emsg = did_emsg;
21747 EMSG((char_u *)ga.ga_data);
21748 if (!force_abort)
21749 did_emsg = save_did_emsg;
21750 }
21751 else if (eap->cmdidx == CMD_execute)
21752 do_cmdline((char_u *)ga.ga_data,
21753 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21754 }
21755
21756 ga_clear(&ga);
21757
21758 if (eap->skip)
21759 --emsg_skip;
21760
21761 eap->nextcmd = check_nextcmd(arg);
21762}
21763
21764/*
21765 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21766 * "arg" points to the "&" or '+' when called, to "option" when returning.
21767 * Returns NULL when no option name found. Otherwise pointer to the char
21768 * after the option name.
21769 */
21770 static char_u *
21771find_option_end(arg, opt_flags)
21772 char_u **arg;
21773 int *opt_flags;
21774{
21775 char_u *p = *arg;
21776
21777 ++p;
21778 if (*p == 'g' && p[1] == ':')
21779 {
21780 *opt_flags = OPT_GLOBAL;
21781 p += 2;
21782 }
21783 else if (*p == 'l' && p[1] == ':')
21784 {
21785 *opt_flags = OPT_LOCAL;
21786 p += 2;
21787 }
21788 else
21789 *opt_flags = 0;
21790
21791 if (!ASCII_ISALPHA(*p))
21792 return NULL;
21793 *arg = p;
21794
21795 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21796 p += 4; /* termcap option */
21797 else
21798 while (ASCII_ISALPHA(*p))
21799 ++p;
21800 return p;
21801}
21802
21803/*
21804 * ":function"
21805 */
21806 void
21807ex_function(eap)
21808 exarg_T *eap;
21809{
21810 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021811 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 int j;
21813 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021815 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 char_u *name = NULL;
21817 char_u *p;
21818 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021819 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 garray_T newargs;
21821 garray_T newlines;
21822 int varargs = FALSE;
21823 int mustend = FALSE;
21824 int flags = 0;
21825 ufunc_T *fp;
21826 int indent;
21827 int nesting;
21828 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 dictitem_T *v;
21830 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021831 static int func_nr = 0; /* number for nameless function */
21832 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021833 hashtab_T *ht;
21834 int todo;
21835 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021836 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837
21838 /*
21839 * ":function" without argument: list functions.
21840 */
21841 if (ends_excmd(*eap->arg))
21842 {
21843 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021845 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021846 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021847 {
21848 if (!HASHITEM_EMPTY(hi))
21849 {
21850 --todo;
21851 fp = HI2UF(hi);
21852 if (!isdigit(*fp->uf_name))
21853 list_func_head(fp, FALSE);
21854 }
21855 }
21856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 eap->nextcmd = check_nextcmd(eap->arg);
21858 return;
21859 }
21860
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021861 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021862 * ":function /pat": list functions matching pattern.
21863 */
21864 if (*eap->arg == '/')
21865 {
21866 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21867 if (!eap->skip)
21868 {
21869 regmatch_T regmatch;
21870
21871 c = *p;
21872 *p = NUL;
21873 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21874 *p = c;
21875 if (regmatch.regprog != NULL)
21876 {
21877 regmatch.rm_ic = p_ic;
21878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021879 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021880 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21881 {
21882 if (!HASHITEM_EMPTY(hi))
21883 {
21884 --todo;
21885 fp = HI2UF(hi);
21886 if (!isdigit(*fp->uf_name)
21887 && vim_regexec(&regmatch, fp->uf_name, 0))
21888 list_func_head(fp, FALSE);
21889 }
21890 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021891 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021892 }
21893 }
21894 if (*p == '/')
21895 ++p;
21896 eap->nextcmd = check_nextcmd(p);
21897 return;
21898 }
21899
21900 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021901 * Get the function name. There are these situations:
21902 * func normal function name
21903 * "name" == func, "fudi.fd_dict" == NULL
21904 * dict.func new dictionary entry
21905 * "name" == NULL, "fudi.fd_dict" set,
21906 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21907 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021908 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021909 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21910 * dict.func existing dict entry that's not a Funcref
21911 * "name" == NULL, "fudi.fd_dict" set,
21912 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021913 * s:func script-local function name
21914 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021917 name = trans_function_name(&p, eap->skip, 0, &fudi);
21918 paren = (vim_strchr(p, '(') != NULL);
21919 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 {
21921 /*
21922 * Return on an invalid expression in braces, unless the expression
21923 * evaluation has been cancelled due to an aborting error, an
21924 * interrupt, or an exception.
21925 */
21926 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021927 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021928 if (!eap->skip && fudi.fd_newkey != NULL)
21929 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021930 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 else
21934 eap->skip = TRUE;
21935 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021936
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 /* An error in a function call during evaluation of an expression in magic
21938 * braces should not cause the function not to be defined. */
21939 saved_did_emsg = did_emsg;
21940 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941
21942 /*
21943 * ":function func" with only function name: list function.
21944 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021945 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 {
21947 if (!ends_excmd(*skipwhite(p)))
21948 {
21949 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021950 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 }
21952 eap->nextcmd = check_nextcmd(p);
21953 if (eap->nextcmd != NULL)
21954 *p = NUL;
21955 if (!eap->skip && !got_int)
21956 {
21957 fp = find_func(name);
21958 if (fp != NULL)
21959 {
21960 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021961 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021963 if (FUNCLINE(fp, j) == NULL)
21964 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965 msg_putchar('\n');
21966 msg_outnum((long)(j + 1));
21967 if (j < 9)
21968 msg_putchar(' ');
21969 if (j < 99)
21970 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021971 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 out_flush(); /* show a line at a time */
21973 ui_breakcheck();
21974 }
21975 if (!got_int)
21976 {
21977 msg_putchar('\n');
21978 msg_puts((char_u *)" endfunction");
21979 }
21980 }
21981 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021982 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021984 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 }
21986
21987 /*
21988 * ":function name(arg1, arg2)" Define function.
21989 */
21990 p = skipwhite(p);
21991 if (*p != '(')
21992 {
21993 if (!eap->skip)
21994 {
21995 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021996 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 }
21998 /* attempt to continue by skipping some text */
21999 if (vim_strchr(p, '(') != NULL)
22000 p = vim_strchr(p, '(');
22001 }
22002 p = skipwhite(p + 1);
22003
22004 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22005 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22006
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022007 if (!eap->skip)
22008 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022009 /* Check the name of the function. Unless it's a dictionary function
22010 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022011 if (name != NULL)
22012 arg = name;
22013 else
22014 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022015 if (arg != NULL && (fudi.fd_di == NULL
22016 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022017 {
22018 if (*arg == K_SPECIAL)
22019 j = 3;
22020 else
22021 j = 0;
22022 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22023 : eval_isnamec(arg[j])))
22024 ++j;
22025 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022026 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022027 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022028 /* Disallow using the g: dict. */
22029 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22030 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022031 }
22032
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 /*
22034 * Isolate the arguments: "arg1, arg2, ...)"
22035 */
22036 while (*p != ')')
22037 {
22038 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22039 {
22040 varargs = TRUE;
22041 p += 3;
22042 mustend = TRUE;
22043 }
22044 else
22045 {
22046 arg = p;
22047 while (ASCII_ISALNUM(*p) || *p == '_')
22048 ++p;
22049 if (arg == p || isdigit(*arg)
22050 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22051 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22052 {
22053 if (!eap->skip)
22054 EMSG2(_("E125: Illegal argument: %s"), arg);
22055 break;
22056 }
22057 if (ga_grow(&newargs, 1) == FAIL)
22058 goto erret;
22059 c = *p;
22060 *p = NUL;
22061 arg = vim_strsave(arg);
22062 if (arg == NULL)
22063 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022064
22065 /* Check for duplicate argument name. */
22066 for (i = 0; i < newargs.ga_len; ++i)
22067 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22068 {
22069 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022070 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022071 goto erret;
22072 }
22073
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22075 *p = c;
22076 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 if (*p == ',')
22078 ++p;
22079 else
22080 mustend = TRUE;
22081 }
22082 p = skipwhite(p);
22083 if (mustend && *p != ')')
22084 {
22085 if (!eap->skip)
22086 EMSG2(_(e_invarg2), eap->arg);
22087 break;
22088 }
22089 }
22090 ++p; /* skip the ')' */
22091
Bram Moolenaare9a41262005-01-15 22:18:47 +000022092 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 for (;;)
22094 {
22095 p = skipwhite(p);
22096 if (STRNCMP(p, "range", 5) == 0)
22097 {
22098 flags |= FC_RANGE;
22099 p += 5;
22100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022101 else if (STRNCMP(p, "dict", 4) == 0)
22102 {
22103 flags |= FC_DICT;
22104 p += 4;
22105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 else if (STRNCMP(p, "abort", 5) == 0)
22107 {
22108 flags |= FC_ABORT;
22109 p += 5;
22110 }
22111 else
22112 break;
22113 }
22114
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022115 /* When there is a line break use what follows for the function body.
22116 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22117 if (*p == '\n')
22118 line_arg = p + 1;
22119 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 EMSG(_(e_trailing));
22121
22122 /*
22123 * Read the body of the function, until ":endfunction" is found.
22124 */
22125 if (KeyTyped)
22126 {
22127 /* Check if the function already exists, don't let the user type the
22128 * whole function before telling him it doesn't work! For a script we
22129 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022130 if (!eap->skip && !eap->forceit)
22131 {
22132 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22133 EMSG(_(e_funcdict));
22134 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022135 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022138 if (!eap->skip && did_emsg)
22139 goto erret;
22140
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 msg_putchar('\n'); /* don't overwrite the function name */
22142 cmdline_row = msg_row;
22143 }
22144
22145 indent = 2;
22146 nesting = 0;
22147 for (;;)
22148 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022149 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022150 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022151 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022152 saved_wait_return = FALSE;
22153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022155 sourcing_lnum_off = sourcing_lnum;
22156
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022157 if (line_arg != NULL)
22158 {
22159 /* Use eap->arg, split up in parts by line breaks. */
22160 theline = line_arg;
22161 p = vim_strchr(theline, '\n');
22162 if (p == NULL)
22163 line_arg += STRLEN(line_arg);
22164 else
22165 {
22166 *p = NUL;
22167 line_arg = p + 1;
22168 }
22169 }
22170 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 theline = getcmdline(':', 0L, indent);
22172 else
22173 theline = eap->getline(':', eap->cookie, indent);
22174 if (KeyTyped)
22175 lines_left = Rows - 1;
22176 if (theline == NULL)
22177 {
22178 EMSG(_("E126: Missing :endfunction"));
22179 goto erret;
22180 }
22181
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022182 /* Detect line continuation: sourcing_lnum increased more than one. */
22183 if (sourcing_lnum > sourcing_lnum_off + 1)
22184 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22185 else
22186 sourcing_lnum_off = 0;
22187
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 if (skip_until != NULL)
22189 {
22190 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22191 * don't check for ":endfunc". */
22192 if (STRCMP(theline, skip_until) == 0)
22193 {
22194 vim_free(skip_until);
22195 skip_until = NULL;
22196 }
22197 }
22198 else
22199 {
22200 /* skip ':' and blanks*/
22201 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22202 ;
22203
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022204 /* Check for "endfunction". */
22205 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022207 if (line_arg == NULL)
22208 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 break;
22210 }
22211
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022212 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 * at "end". */
22214 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22215 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022216 else if (STRNCMP(p, "if", 2) == 0
22217 || STRNCMP(p, "wh", 2) == 0
22218 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 || STRNCMP(p, "try", 3) == 0)
22220 indent += 2;
22221
22222 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022223 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022225 if (*p == '!')
22226 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 p += eval_fname_script(p);
22228 if (ASCII_ISALPHA(*p))
22229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022230 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 if (*skipwhite(p) == '(')
22232 {
22233 ++nesting;
22234 indent += 2;
22235 }
22236 }
22237 }
22238
22239 /* Check for ":append" or ":insert". */
22240 p = skip_range(p, NULL);
22241 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22242 || (p[0] == 'i'
22243 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22244 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22245 skip_until = vim_strsave((char_u *)".");
22246
22247 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22248 arg = skipwhite(skiptowhite(p));
22249 if (arg[0] == '<' && arg[1] =='<'
22250 && ((p[0] == 'p' && p[1] == 'y'
22251 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22252 || (p[0] == 'p' && p[1] == 'e'
22253 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22254 || (p[0] == 't' && p[1] == 'c'
22255 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022256 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22257 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22259 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022260 || (p[0] == 'm' && p[1] == 'z'
22261 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 ))
22263 {
22264 /* ":python <<" continues until a dot, like ":append" */
22265 p = skipwhite(arg + 2);
22266 if (*p == NUL)
22267 skip_until = vim_strsave((char_u *)".");
22268 else
22269 skip_until = vim_strsave(p);
22270 }
22271 }
22272
22273 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022274 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022275 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022276 if (line_arg == NULL)
22277 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022279 }
22280
22281 /* Copy the line to newly allocated memory. get_one_sourceline()
22282 * allocates 250 bytes per line, this saves 80% on average. The cost
22283 * is an extra alloc/free. */
22284 p = vim_strsave(theline);
22285 if (p != NULL)
22286 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022287 if (line_arg == NULL)
22288 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022289 theline = p;
22290 }
22291
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022292 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22293
22294 /* Add NULL lines for continuation lines, so that the line count is
22295 * equal to the index in the growarray. */
22296 while (sourcing_lnum_off-- > 0)
22297 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022298
22299 /* Check for end of eap->arg. */
22300 if (line_arg != NULL && *line_arg == NUL)
22301 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 }
22303
22304 /* Don't define the function when skipping commands or when an error was
22305 * detected. */
22306 if (eap->skip || did_emsg)
22307 goto erret;
22308
22309 /*
22310 * If there are no errors, add the function
22311 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022312 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022314 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022315 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022316 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022317 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022318 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 goto erret;
22320 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022321
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022322 fp = find_func(name);
22323 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325 if (!eap->forceit)
22326 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022327 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022328 goto erret;
22329 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022330 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022331 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022332 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022333 name);
22334 goto erret;
22335 }
22336 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022337 ga_clear_strings(&(fp->uf_args));
22338 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 vim_free(name);
22340 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 }
22343 else
22344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 char numbuf[20];
22346
22347 fp = NULL;
22348 if (fudi.fd_newkey == NULL && !eap->forceit)
22349 {
22350 EMSG(_(e_funcdict));
22351 goto erret;
22352 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022353 if (fudi.fd_di == NULL)
22354 {
22355 /* Can't add a function to a locked dictionary */
22356 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22357 goto erret;
22358 }
22359 /* Can't change an existing function if it is locked */
22360 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22361 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362
22363 /* Give the function a sequential number. Can only be used with a
22364 * Funcref! */
22365 vim_free(name);
22366 sprintf(numbuf, "%d", ++func_nr);
22367 name = vim_strsave((char_u *)numbuf);
22368 if (name == NULL)
22369 goto erret;
22370 }
22371
22372 if (fp == NULL)
22373 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022374 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022375 {
22376 int slen, plen;
22377 char_u *scriptname;
22378
22379 /* Check that the autoload name matches the script name. */
22380 j = FAIL;
22381 if (sourcing_name != NULL)
22382 {
22383 scriptname = autoload_name(name);
22384 if (scriptname != NULL)
22385 {
22386 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022387 plen = (int)STRLEN(p);
22388 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022389 if (slen > plen && fnamecmp(p,
22390 sourcing_name + slen - plen) == 0)
22391 j = OK;
22392 vim_free(scriptname);
22393 }
22394 }
22395 if (j == FAIL)
22396 {
22397 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22398 goto erret;
22399 }
22400 }
22401
22402 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 if (fp == NULL)
22404 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022405
22406 if (fudi.fd_dict != NULL)
22407 {
22408 if (fudi.fd_di == NULL)
22409 {
22410 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022411 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022412 if (fudi.fd_di == NULL)
22413 {
22414 vim_free(fp);
22415 goto erret;
22416 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022417 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22418 {
22419 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022420 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022421 goto erret;
22422 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 }
22424 else
22425 /* overwrite existing dict entry */
22426 clear_tv(&fudi.fd_di->di_tv);
22427 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022428 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022429 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022430 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022431
22432 /* behave like "dict" was used */
22433 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022434 }
22435
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022437 STRCPY(fp->uf_name, name);
22438 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022440 fp->uf_args = newargs;
22441 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022442#ifdef FEAT_PROFILE
22443 fp->uf_tml_count = NULL;
22444 fp->uf_tml_total = NULL;
22445 fp->uf_tml_self = NULL;
22446 fp->uf_profiling = FALSE;
22447 if (prof_def_func())
22448 func_do_profile(fp);
22449#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022450 fp->uf_varargs = varargs;
22451 fp->uf_flags = flags;
22452 fp->uf_calls = 0;
22453 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022454 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455
22456erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 ga_clear_strings(&newargs);
22458 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022459ret_free:
22460 vim_free(skip_until);
22461 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022464 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465}
22466
22467/*
22468 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022469 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022471 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022472 * TFN_INT: internal function name OK
22473 * TFN_QUIET: be quiet
22474 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 * Advances "pp" to just after the function name (if no error).
22476 */
22477 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022478trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 char_u **pp;
22480 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022481 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022484 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 char_u *start;
22486 char_u *end;
22487 int lead;
22488 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022491
22492 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022495
22496 /* Check for hard coded <SNR>: already translated function ID (from a user
22497 * command). */
22498 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22499 && (*pp)[2] == (int)KE_SNR)
22500 {
22501 *pp += 3;
22502 len = get_id_len(pp) + 3;
22503 return vim_strnsave(start, len);
22504 }
22505
22506 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22507 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022509 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022511
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022512 /* Note that TFN_ flags use the same values as GLV_ flags. */
22513 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022514 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022515 if (end == start)
22516 {
22517 if (!skip)
22518 EMSG(_("E129: Function name required"));
22519 goto theend;
22520 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022521 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022522 {
22523 /*
22524 * Report an invalid expression in braces, unless the expression
22525 * evaluation has been cancelled due to an aborting error, an
22526 * interrupt, or an exception.
22527 */
22528 if (!aborting())
22529 {
22530 if (end != NULL)
22531 EMSG2(_(e_invarg2), start);
22532 }
22533 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022534 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022535 goto theend;
22536 }
22537
22538 if (lv.ll_tv != NULL)
22539 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 if (fdp != NULL)
22541 {
22542 fdp->fd_dict = lv.ll_dict;
22543 fdp->fd_newkey = lv.ll_newkey;
22544 lv.ll_newkey = NULL;
22545 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022547 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22548 {
22549 name = vim_strsave(lv.ll_tv->vval.v_string);
22550 *pp = end;
22551 }
22552 else
22553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022554 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22555 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 EMSG(_(e_funcref));
22557 else
22558 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022559 name = NULL;
22560 }
22561 goto theend;
22562 }
22563
22564 if (lv.ll_name == NULL)
22565 {
22566 /* Error found, but continue after the function name. */
22567 *pp = end;
22568 goto theend;
22569 }
22570
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022571 /* Check if the name is a Funcref. If so, use the value. */
22572 if (lv.ll_exp_name != NULL)
22573 {
22574 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022575 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022576 if (name == lv.ll_exp_name)
22577 name = NULL;
22578 }
22579 else
22580 {
22581 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022582 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022583 if (name == *pp)
22584 name = NULL;
22585 }
22586 if (name != NULL)
22587 {
22588 name = vim_strsave(name);
22589 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022590 if (STRNCMP(name, "<SNR>", 5) == 0)
22591 {
22592 /* Change "<SNR>" to the byte sequence. */
22593 name[0] = K_SPECIAL;
22594 name[1] = KS_EXTRA;
22595 name[2] = (int)KE_SNR;
22596 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22597 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022598 goto theend;
22599 }
22600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022601 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022602 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022603 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022604 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22605 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22606 {
22607 /* When there was "s:" already or the name expanded to get a
22608 * leading "s:" then remove it. */
22609 lv.ll_name += 2;
22610 len -= 2;
22611 lead = 2;
22612 }
22613 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022614 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022615 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022616 /* skip over "s:" and "g:" */
22617 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022618 lv.ll_name += 2;
22619 len = (int)(end - lv.ll_name);
22620 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022621
22622 /*
22623 * Copy the function name to allocated memory.
22624 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22625 * Accept <SNR>123_name() outside a script.
22626 */
22627 if (skip)
22628 lead = 0; /* do nothing */
22629 else if (lead > 0)
22630 {
22631 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022632 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22633 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022634 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022635 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022636 if (current_SID <= 0)
22637 {
22638 EMSG(_(e_usingsid));
22639 goto theend;
22640 }
22641 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22642 lead += (int)STRLEN(sid_buf);
22643 }
22644 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022645 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022646 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022647 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022648 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022649 goto theend;
22650 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022651 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022652 {
22653 char_u *cp = vim_strchr(lv.ll_name, ':');
22654
22655 if (cp != NULL && cp < end)
22656 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022657 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022658 goto theend;
22659 }
22660 }
22661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022662 name = alloc((unsigned)(len + lead + 1));
22663 if (name != NULL)
22664 {
22665 if (lead > 0)
22666 {
22667 name[0] = K_SPECIAL;
22668 name[1] = KS_EXTRA;
22669 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022670 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022671 STRCPY(name + 3, sid_buf);
22672 }
22673 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022674 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022675 }
22676 *pp = end;
22677
22678theend:
22679 clear_lval(&lv);
22680 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681}
22682
22683/*
22684 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22685 * Return 2 if "p" starts with "s:".
22686 * Return 0 otherwise.
22687 */
22688 static int
22689eval_fname_script(p)
22690 char_u *p;
22691{
22692 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22693 || STRNICMP(p + 1, "SNR>", 4) == 0))
22694 return 5;
22695 if (p[0] == 's' && p[1] == ':')
22696 return 2;
22697 return 0;
22698}
22699
22700/*
22701 * Return TRUE if "p" starts with "<SID>" or "s:".
22702 * Only works if eval_fname_script() returned non-zero for "p"!
22703 */
22704 static int
22705eval_fname_sid(p)
22706 char_u *p;
22707{
22708 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22709}
22710
22711/*
22712 * List the head of the function: "name(arg1, arg2)".
22713 */
22714 static void
22715list_func_head(fp, indent)
22716 ufunc_T *fp;
22717 int indent;
22718{
22719 int j;
22720
22721 msg_start();
22722 if (indent)
22723 MSG_PUTS(" ");
22724 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022725 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 {
22727 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022728 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 }
22730 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022731 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022733 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 {
22735 if (j)
22736 MSG_PUTS(", ");
22737 msg_puts(FUNCARG(fp, j));
22738 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022739 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 {
22741 if (j)
22742 MSG_PUTS(", ");
22743 MSG_PUTS("...");
22744 }
22745 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022746 if (fp->uf_flags & FC_ABORT)
22747 MSG_PUTS(" abort");
22748 if (fp->uf_flags & FC_RANGE)
22749 MSG_PUTS(" range");
22750 if (fp->uf_flags & FC_DICT)
22751 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022752 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022753 if (p_verbose > 0)
22754 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755}
22756
22757/*
22758 * Find a function by name, return pointer to it in ufuncs.
22759 * Return NULL for unknown function.
22760 */
22761 static ufunc_T *
22762find_func(name)
22763 char_u *name;
22764{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022765 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022767 hi = hash_find(&func_hashtab, name);
22768 if (!HASHITEM_EMPTY(hi))
22769 return HI2UF(hi);
22770 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771}
22772
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022773#if defined(EXITFREE) || defined(PROTO)
22774 void
22775free_all_functions()
22776{
22777 hashitem_T *hi;
22778
22779 /* Need to start all over every time, because func_free() may change the
22780 * hash table. */
22781 while (func_hashtab.ht_used > 0)
22782 for (hi = func_hashtab.ht_array; ; ++hi)
22783 if (!HASHITEM_EMPTY(hi))
22784 {
22785 func_free(HI2UF(hi));
22786 break;
22787 }
22788}
22789#endif
22790
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022791 int
22792translated_function_exists(name)
22793 char_u *name;
22794{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022795 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022796 return find_internal_func(name) >= 0;
22797 return find_func(name) != NULL;
22798}
22799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022800/*
22801 * Return TRUE if a function "name" exists.
22802 */
22803 static int
22804function_exists(name)
22805 char_u *name;
22806{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022807 char_u *nm = name;
22808 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022809 int n = FALSE;
22810
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022811 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22812 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022813 nm = skipwhite(nm);
22814
22815 /* Only accept "funcname", "funcname ", "funcname (..." and
22816 * "funcname(...", not "funcname!...". */
22817 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022818 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022819 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 return n;
22821}
22822
Bram Moolenaara1544c02013-05-30 12:35:52 +020022823 char_u *
22824get_expanded_name(name, check)
22825 char_u *name;
22826 int check;
22827{
22828 char_u *nm = name;
22829 char_u *p;
22830
22831 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22832
22833 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022834 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022835 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022836
Bram Moolenaara1544c02013-05-30 12:35:52 +020022837 vim_free(p);
22838 return NULL;
22839}
22840
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022841/*
22842 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022843 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22844 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022845 */
22846 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022847builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022848 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022849 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022850{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022851 char_u *p;
22852
22853 if (!ASCII_ISLOWER(name[0]))
22854 return FALSE;
22855 p = vim_strchr(name, AUTOLOAD_CHAR);
22856 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022857}
22858
Bram Moolenaar05159a02005-02-26 23:04:13 +000022859#if defined(FEAT_PROFILE) || defined(PROTO)
22860/*
22861 * Start profiling function "fp".
22862 */
22863 static void
22864func_do_profile(fp)
22865 ufunc_T *fp;
22866{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022867 int len = fp->uf_lines.ga_len;
22868
22869 if (len == 0)
22870 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022871 fp->uf_tm_count = 0;
22872 profile_zero(&fp->uf_tm_self);
22873 profile_zero(&fp->uf_tm_total);
22874 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022875 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022876 if (fp->uf_tml_total == NULL)
22877 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022878 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022879 if (fp->uf_tml_self == NULL)
22880 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022881 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022882 fp->uf_tml_idx = -1;
22883 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22884 || fp->uf_tml_self == NULL)
22885 return; /* out of memory */
22886
22887 fp->uf_profiling = TRUE;
22888}
22889
22890/*
22891 * Dump the profiling results for all functions in file "fd".
22892 */
22893 void
22894func_dump_profile(fd)
22895 FILE *fd;
22896{
22897 hashitem_T *hi;
22898 int todo;
22899 ufunc_T *fp;
22900 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022901 ufunc_T **sorttab;
22902 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022904 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022905 if (todo == 0)
22906 return; /* nothing to dump */
22907
Bram Moolenaar73830342005-02-28 22:48:19 +000022908 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22909
Bram Moolenaar05159a02005-02-26 23:04:13 +000022910 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22911 {
22912 if (!HASHITEM_EMPTY(hi))
22913 {
22914 --todo;
22915 fp = HI2UF(hi);
22916 if (fp->uf_profiling)
22917 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022918 if (sorttab != NULL)
22919 sorttab[st_len++] = fp;
22920
Bram Moolenaar05159a02005-02-26 23:04:13 +000022921 if (fp->uf_name[0] == K_SPECIAL)
22922 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22923 else
22924 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22925 if (fp->uf_tm_count == 1)
22926 fprintf(fd, "Called 1 time\n");
22927 else
22928 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22929 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22930 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22931 fprintf(fd, "\n");
22932 fprintf(fd, "count total (s) self (s)\n");
22933
22934 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22935 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022936 if (FUNCLINE(fp, i) == NULL)
22937 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022938 prof_func_line(fd, fp->uf_tml_count[i],
22939 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022940 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22941 }
22942 fprintf(fd, "\n");
22943 }
22944 }
22945 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022946
22947 if (sorttab != NULL && st_len > 0)
22948 {
22949 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22950 prof_total_cmp);
22951 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22952 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22953 prof_self_cmp);
22954 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22955 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022956
22957 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022958}
Bram Moolenaar73830342005-02-28 22:48:19 +000022959
22960 static void
22961prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22962 FILE *fd;
22963 ufunc_T **sorttab;
22964 int st_len;
22965 char *title;
22966 int prefer_self; /* when equal print only self time */
22967{
22968 int i;
22969 ufunc_T *fp;
22970
22971 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22972 fprintf(fd, "count total (s) self (s) function\n");
22973 for (i = 0; i < 20 && i < st_len; ++i)
22974 {
22975 fp = sorttab[i];
22976 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22977 prefer_self);
22978 if (fp->uf_name[0] == K_SPECIAL)
22979 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22980 else
22981 fprintf(fd, " %s()\n", fp->uf_name);
22982 }
22983 fprintf(fd, "\n");
22984}
22985
22986/*
22987 * Print the count and times for one function or function line.
22988 */
22989 static void
22990prof_func_line(fd, count, total, self, prefer_self)
22991 FILE *fd;
22992 int count;
22993 proftime_T *total;
22994 proftime_T *self;
22995 int prefer_self; /* when equal print only self time */
22996{
22997 if (count > 0)
22998 {
22999 fprintf(fd, "%5d ", count);
23000 if (prefer_self && profile_equal(total, self))
23001 fprintf(fd, " ");
23002 else
23003 fprintf(fd, "%s ", profile_msg(total));
23004 if (!prefer_self && profile_equal(total, self))
23005 fprintf(fd, " ");
23006 else
23007 fprintf(fd, "%s ", profile_msg(self));
23008 }
23009 else
23010 fprintf(fd, " ");
23011}
23012
23013/*
23014 * Compare function for total time sorting.
23015 */
23016 static int
23017#ifdef __BORLANDC__
23018_RTLENTRYF
23019#endif
23020prof_total_cmp(s1, s2)
23021 const void *s1;
23022 const void *s2;
23023{
23024 ufunc_T *p1, *p2;
23025
23026 p1 = *(ufunc_T **)s1;
23027 p2 = *(ufunc_T **)s2;
23028 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23029}
23030
23031/*
23032 * Compare function for self time sorting.
23033 */
23034 static int
23035#ifdef __BORLANDC__
23036_RTLENTRYF
23037#endif
23038prof_self_cmp(s1, s2)
23039 const void *s1;
23040 const void *s2;
23041{
23042 ufunc_T *p1, *p2;
23043
23044 p1 = *(ufunc_T **)s1;
23045 p2 = *(ufunc_T **)s2;
23046 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23047}
23048
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049#endif
23050
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023051/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023052 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023053 * Return TRUE if a package was loaded.
23054 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023055 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023056script_autoload(name, reload)
23057 char_u *name;
23058 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023059{
23060 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023061 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023062 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023063 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023065 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023066 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023067 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023068 return FALSE;
23069
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023070 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023071
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023072 /* Find the name in the list of previously loaded package names. Skip
23073 * "autoload/", it's always the same. */
23074 for (i = 0; i < ga_loaded.ga_len; ++i)
23075 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23076 break;
23077 if (!reload && i < ga_loaded.ga_len)
23078 ret = FALSE; /* was loaded already */
23079 else
23080 {
23081 /* Remember the name if it wasn't loaded already. */
23082 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23083 {
23084 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23085 tofree = NULL;
23086 }
23087
23088 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023089 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023090 ret = TRUE;
23091 }
23092
23093 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023094 return ret;
23095}
23096
23097/*
23098 * Return the autoload script name for a function or variable name.
23099 * Returns NULL when out of memory.
23100 */
23101 static char_u *
23102autoload_name(name)
23103 char_u *name;
23104{
23105 char_u *p;
23106 char_u *scriptname;
23107
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023108 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023109 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23110 if (scriptname == NULL)
23111 return FALSE;
23112 STRCPY(scriptname, "autoload/");
23113 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023114 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023115 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023116 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023117 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023118 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023119}
23120
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23122
23123/*
23124 * Function given to ExpandGeneric() to obtain the list of user defined
23125 * function names.
23126 */
23127 char_u *
23128get_user_func_name(xp, idx)
23129 expand_T *xp;
23130 int idx;
23131{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023132 static long_u done;
23133 static hashitem_T *hi;
23134 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135
23136 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023138 done = 0;
23139 hi = func_hashtab.ht_array;
23140 }
23141 if (done < func_hashtab.ht_used)
23142 {
23143 if (done++ > 0)
23144 ++hi;
23145 while (HASHITEM_EMPTY(hi))
23146 ++hi;
23147 fp = HI2UF(hi);
23148
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023149 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023150 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023151
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023152 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23153 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154
23155 cat_func_name(IObuff, fp);
23156 if (xp->xp_context != EXPAND_USER_FUNC)
23157 {
23158 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023159 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 STRCAT(IObuff, ")");
23161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 return IObuff;
23163 }
23164 return NULL;
23165}
23166
23167#endif /* FEAT_CMDL_COMPL */
23168
23169/*
23170 * Copy the function name of "fp" to buffer "buf".
23171 * "buf" must be able to hold the function name plus three bytes.
23172 * Takes care of script-local function names.
23173 */
23174 static void
23175cat_func_name(buf, fp)
23176 char_u *buf;
23177 ufunc_T *fp;
23178{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 {
23181 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 }
23184 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023185 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
23188/*
23189 * ":delfunction {name}"
23190 */
23191 void
23192ex_delfunction(eap)
23193 exarg_T *eap;
23194{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023195 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 char_u *p;
23197 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199
23200 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023201 name = trans_function_name(&p, eap->skip, 0, &fudi);
23202 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023204 {
23205 if (fudi.fd_dict != NULL && !eap->skip)
23206 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 if (!ends_excmd(*skipwhite(p)))
23210 {
23211 vim_free(name);
23212 EMSG(_(e_trailing));
23213 return;
23214 }
23215 eap->nextcmd = check_nextcmd(p);
23216 if (eap->nextcmd != NULL)
23217 *p = NUL;
23218
23219 if (!eap->skip)
23220 fp = find_func(name);
23221 vim_free(name);
23222
23223 if (!eap->skip)
23224 {
23225 if (fp == NULL)
23226 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023227 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 return;
23229 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023230 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 {
23232 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23233 return;
23234 }
23235
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023238 /* Delete the dict item that refers to the function, it will
23239 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023240 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242 else
23243 func_free(fp);
23244 }
23245}
23246
23247/*
23248 * Free a function and remove it from the list of functions.
23249 */
23250 static void
23251func_free(fp)
23252 ufunc_T *fp;
23253{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023254 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023255
23256 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023257 ga_clear_strings(&(fp->uf_args));
23258 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259#ifdef FEAT_PROFILE
23260 vim_free(fp->uf_tml_count);
23261 vim_free(fp->uf_tml_total);
23262 vim_free(fp->uf_tml_self);
23263#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023264
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023265 /* remove the function from the function hashtable */
23266 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23267 if (HASHITEM_EMPTY(hi))
23268 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023269 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023270 hash_remove(&func_hashtab, hi);
23271
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 vim_free(fp);
23273}
23274
23275/*
23276 * Unreference a Function: decrement the reference count and free it when it
23277 * becomes zero. Only for numbered functions.
23278 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023279 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023280func_unref(name)
23281 char_u *name;
23282{
23283 ufunc_T *fp;
23284
23285 if (name != NULL && isdigit(*name))
23286 {
23287 fp = find_func(name);
23288 if (fp == NULL)
23289 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023290 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023291 {
23292 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023293 * when "uf_calls" becomes zero. */
23294 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023295 func_free(fp);
23296 }
23297 }
23298}
23299
23300/*
23301 * Count a reference to a Function.
23302 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023303 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023304func_ref(name)
23305 char_u *name;
23306{
23307 ufunc_T *fp;
23308
23309 if (name != NULL && isdigit(*name))
23310 {
23311 fp = find_func(name);
23312 if (fp == NULL)
23313 EMSG2(_(e_intern2), "func_ref()");
23314 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023315 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 }
23317}
23318
23319/*
23320 * Call a user function.
23321 */
23322 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023323call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 ufunc_T *fp; /* pointer to function */
23325 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 typval_T *argvars; /* arguments */
23327 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 linenr_T firstline; /* first line of range */
23329 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023330 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331{
Bram Moolenaar33570922005-01-25 22:26:29 +000023332 char_u *save_sourcing_name;
23333 linenr_T save_sourcing_lnum;
23334 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023335 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 int save_did_emsg;
23337 static int depth = 0;
23338 dictitem_T *v;
23339 int fixvar_idx = 0; /* index in fixvar[] */
23340 int i;
23341 int ai;
23342 char_u numbuf[NUMBUFLEN];
23343 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023344#ifdef FEAT_PROFILE
23345 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023346 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348
23349 /* If depth of calling is getting too high, don't execute the function */
23350 if (depth >= p_mfd)
23351 {
23352 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023353 rettv->v_type = VAR_NUMBER;
23354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 return;
23356 }
23357 ++depth;
23358
23359 line_breakcheck(); /* check for CTRL-C hit */
23360
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023361 fc = (funccall_T *)alloc(sizeof(funccall_T));
23362 fc->caller = current_funccal;
23363 current_funccal = fc;
23364 fc->func = fp;
23365 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023366 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023367 fc->linenr = 0;
23368 fc->returned = FALSE;
23369 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023371 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23372 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373
Bram Moolenaar33570922005-01-25 22:26:29 +000023374 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023375 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023376 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23377 * each argument variable and saves a lot of time.
23378 */
23379 /*
23380 * Init l: variables.
23381 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023382 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023383 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023384 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023385 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23386 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023387 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023388 name = v->di_key;
23389 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023391 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023392 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023393 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 v->di_tv.vval.v_dict = selfdict;
23395 ++selfdict->dv_refcount;
23396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023397
Bram Moolenaar33570922005-01-25 22:26:29 +000023398 /*
23399 * Init a: variables.
23400 * Set a:0 to "argcount".
23401 * Set a:000 to a list with room for the "..." arguments.
23402 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023403 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023404 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023405 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023406 /* Use "name" to avoid a warning from some compiler that checks the
23407 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023408 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023409 name = v->di_key;
23410 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023411 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023412 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023413 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023414 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023415 v->di_tv.vval.v_list = &fc->l_varlist;
23416 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23417 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23418 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023419
23420 /*
23421 * Set a:firstline to "firstline" and a:lastline to "lastline".
23422 * Set a:name to named arguments.
23423 * Set a:N to the "..." arguments.
23424 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023425 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023426 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023427 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023428 (varnumber_T)lastline);
23429 for (i = 0; i < argcount; ++i)
23430 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023431 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023432 if (ai < 0)
23433 /* named argument a:name */
23434 name = FUNCARG(fp, i);
23435 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023436 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023437 /* "..." argument a:1, a:2, etc. */
23438 sprintf((char *)numbuf, "%d", ai + 1);
23439 name = numbuf;
23440 }
23441 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23442 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023443 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023444 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23445 }
23446 else
23447 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023448 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23449 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023450 if (v == NULL)
23451 break;
23452 v->di_flags = DI_FLAGS_RO;
23453 }
23454 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023455 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023456
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023457 /* Note: the values are copied directly to avoid alloc/free.
23458 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023459 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023460 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023461
23462 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23463 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023464 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23465 fc->l_listitems[ai].li_tv = argvars[i];
23466 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023467 }
23468 }
23469
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 /* Don't redraw while executing the function. */
23471 ++RedrawingDisabled;
23472 save_sourcing_name = sourcing_name;
23473 save_sourcing_lnum = sourcing_lnum;
23474 sourcing_lnum = 1;
23475 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023476 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 if (sourcing_name != NULL)
23478 {
23479 if (save_sourcing_name != NULL
23480 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23481 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23482 else
23483 STRCPY(sourcing_name, "function ");
23484 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23485
23486 if (p_verbose >= 12)
23487 {
23488 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023489 verbose_enter_scroll();
23490
Bram Moolenaar555b2802005-05-19 21:08:39 +000023491 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 if (p_verbose >= 14)
23493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023495 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023496 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023497 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498
23499 msg_puts((char_u *)"(");
23500 for (i = 0; i < argcount; ++i)
23501 {
23502 if (i > 0)
23503 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023504 if (argvars[i].v_type == VAR_NUMBER)
23505 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 else
23507 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023508 /* Do not want errors such as E724 here. */
23509 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023510 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023511 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023512 if (s != NULL)
23513 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023514 if (vim_strsize(s) > MSG_BUF_CLEN)
23515 {
23516 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23517 s = buf;
23518 }
23519 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023520 vim_free(tofree);
23521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 }
23523 }
23524 msg_puts((char_u *)")");
23525 }
23526 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023527
23528 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 --no_wait_return;
23530 }
23531 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023532#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023533 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023534 {
23535 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23536 func_do_profile(fp);
23537 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023538 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023539 {
23540 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023541 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023542 profile_zero(&fp->uf_tm_children);
23543 }
23544 script_prof_save(&wait_start);
23545 }
23546#endif
23547
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023549 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 save_did_emsg = did_emsg;
23551 did_emsg = FALSE;
23552
23553 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023554 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23556
23557 --RedrawingDisabled;
23558
23559 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023560 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023562 clear_tv(rettv);
23563 rettv->v_type = VAR_NUMBER;
23564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 }
23566
Bram Moolenaar05159a02005-02-26 23:04:13 +000023567#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023568 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023569 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023570 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023571 profile_end(&call_start);
23572 profile_sub_wait(&wait_start, &call_start);
23573 profile_add(&fp->uf_tm_total, &call_start);
23574 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023575 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023576 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023577 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23578 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023579 }
23580 }
23581#endif
23582
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 /* when being verbose, mention the return value */
23584 if (p_verbose >= 12)
23585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023587 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023590 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023591 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023592 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023593 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023596 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023597 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023598 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023599 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023600
Bram Moolenaar555b2802005-05-19 21:08:39 +000023601 /* The value may be very long. Skip the middle part, so that we
23602 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023603 * truncate it at the end. Don't want errors such as E724 here. */
23604 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023605 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023606 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023607 if (s != NULL)
23608 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023609 if (vim_strsize(s) > MSG_BUF_CLEN)
23610 {
23611 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23612 s = buf;
23613 }
23614 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023615 vim_free(tofree);
23616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 }
23618 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023619
23620 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 --no_wait_return;
23622 }
23623
23624 vim_free(sourcing_name);
23625 sourcing_name = save_sourcing_name;
23626 sourcing_lnum = save_sourcing_lnum;
23627 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023628#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023629 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023630 script_prof_restore(&wait_start);
23631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632
23633 if (p_verbose >= 12 && sourcing_name != NULL)
23634 {
23635 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023636 verbose_enter_scroll();
23637
Bram Moolenaar555b2802005-05-19 21:08:39 +000023638 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023640
23641 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 --no_wait_return;
23643 }
23644
23645 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023646 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023648
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023649 /* If the a:000 list and the l: and a: dicts are not referenced we can
23650 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023651 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23652 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23653 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23654 {
23655 free_funccal(fc, FALSE);
23656 }
23657 else
23658 {
23659 hashitem_T *hi;
23660 listitem_T *li;
23661 int todo;
23662
23663 /* "fc" is still in use. This can happen when returning "a:000" or
23664 * assigning "l:" to a global variable.
23665 * Link "fc" in the list for garbage collection later. */
23666 fc->caller = previous_funccal;
23667 previous_funccal = fc;
23668
23669 /* Make a copy of the a: variables, since we didn't do that above. */
23670 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23671 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23672 {
23673 if (!HASHITEM_EMPTY(hi))
23674 {
23675 --todo;
23676 v = HI2DI(hi);
23677 copy_tv(&v->di_tv, &v->di_tv);
23678 }
23679 }
23680
23681 /* Make a copy of the a:000 items, since we didn't do that above. */
23682 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23683 copy_tv(&li->li_tv, &li->li_tv);
23684 }
23685}
23686
23687/*
23688 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023689 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023690 */
23691 static int
23692can_free_funccal(fc, copyID)
23693 funccall_T *fc;
23694 int copyID;
23695{
23696 return (fc->l_varlist.lv_copyID != copyID
23697 && fc->l_vars.dv_copyID != copyID
23698 && fc->l_avars.dv_copyID != copyID);
23699}
23700
23701/*
23702 * Free "fc" and what it contains.
23703 */
23704 static void
23705free_funccal(fc, free_val)
23706 funccall_T *fc;
23707 int free_val; /* a: vars were allocated */
23708{
23709 listitem_T *li;
23710
23711 /* The a: variables typevals may not have been allocated, only free the
23712 * allocated variables. */
23713 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23714
23715 /* free all l: variables */
23716 vars_clear(&fc->l_vars.dv_hashtab);
23717
23718 /* Free the a:000 variables if they were allocated. */
23719 if (free_val)
23720 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23721 clear_tv(&li->li_tv);
23722
23723 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724}
23725
23726/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023727 * Add a number variable "name" to dict "dp" with value "nr".
23728 */
23729 static void
23730add_nr_var(dp, v, name, nr)
23731 dict_T *dp;
23732 dictitem_T *v;
23733 char *name;
23734 varnumber_T nr;
23735{
23736 STRCPY(v->di_key, name);
23737 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23738 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23739 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023740 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 v->di_tv.vval.v_number = nr;
23742}
23743
23744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 * ":return [expr]"
23746 */
23747 void
23748ex_return(eap)
23749 exarg_T *eap;
23750{
23751 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023752 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 int returning = FALSE;
23754
23755 if (current_funccal == NULL)
23756 {
23757 EMSG(_("E133: :return not inside a function"));
23758 return;
23759 }
23760
23761 if (eap->skip)
23762 ++emsg_skip;
23763
23764 eap->nextcmd = NULL;
23765 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023766 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 {
23768 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023769 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023771 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 }
23773 /* It's safer to return also on error. */
23774 else if (!eap->skip)
23775 {
23776 /*
23777 * Return unless the expression evaluation has been cancelled due to an
23778 * aborting error, an interrupt, or an exception.
23779 */
23780 if (!aborting())
23781 returning = do_return(eap, FALSE, TRUE, NULL);
23782 }
23783
23784 /* When skipping or the return gets pending, advance to the next command
23785 * in this line (!returning). Otherwise, ignore the rest of the line.
23786 * Following lines will be ignored by get_func_line(). */
23787 if (returning)
23788 eap->nextcmd = NULL;
23789 else if (eap->nextcmd == NULL) /* no argument */
23790 eap->nextcmd = check_nextcmd(arg);
23791
23792 if (eap->skip)
23793 --emsg_skip;
23794}
23795
23796/*
23797 * Return from a function. Possibly makes the return pending. Also called
23798 * for a pending return at the ":endtry" or after returning from an extra
23799 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023800 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023801 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 * FALSE when the return gets pending.
23803 */
23804 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023805do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 exarg_T *eap;
23807 int reanimate;
23808 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023809 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810{
23811 int idx;
23812 struct condstack *cstack = eap->cstack;
23813
23814 if (reanimate)
23815 /* Undo the return. */
23816 current_funccal->returned = FALSE;
23817
23818 /*
23819 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23820 * not in its finally clause (which then is to be executed next) is found.
23821 * In this case, make the ":return" pending for execution at the ":endtry".
23822 * Otherwise, return normally.
23823 */
23824 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23825 if (idx >= 0)
23826 {
23827 cstack->cs_pending[idx] = CSTP_RETURN;
23828
23829 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023830 /* A pending return again gets pending. "rettv" points to an
23831 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023833 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 else
23835 {
23836 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023837 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023839 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023841 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 {
23843 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023844 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 else
23847 EMSG(_(e_outofmem));
23848 }
23849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023850 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851
23852 if (reanimate)
23853 {
23854 /* The pending return value could be overwritten by a ":return"
23855 * without argument in a finally clause; reset the default
23856 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023857 current_funccal->rettv->v_type = VAR_NUMBER;
23858 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 }
23860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023861 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 }
23863 else
23864 {
23865 current_funccal->returned = TRUE;
23866
23867 /* If the return is carried out now, store the return value. For
23868 * a return immediately after reanimation, the value is already
23869 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023870 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023872 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023873 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023875 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 }
23877 }
23878
23879 return idx < 0;
23880}
23881
23882/*
23883 * Free the variable with a pending return value.
23884 */
23885 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023886discard_pending_return(rettv)
23887 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888{
Bram Moolenaar33570922005-01-25 22:26:29 +000023889 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890}
23891
23892/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023893 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 * is an allocated string. Used by report_pending() for verbose messages.
23895 */
23896 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023897get_return_cmd(rettv)
23898 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023900 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023901 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023902 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023904 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023905 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023906 if (s == NULL)
23907 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023908
23909 STRCPY(IObuff, ":return ");
23910 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23911 if (STRLEN(s) + 8 >= IOSIZE)
23912 STRCPY(IObuff + IOSIZE - 4, "...");
23913 vim_free(tofree);
23914 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915}
23916
23917/*
23918 * Get next function line.
23919 * Called by do_cmdline() to get the next line.
23920 * Returns allocated string, or NULL for end of function.
23921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 char_u *
23923get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023924 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023926 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927{
Bram Moolenaar33570922005-01-25 22:26:29 +000023928 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023929 ufunc_T *fp = fcp->func;
23930 char_u *retval;
23931 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932
23933 /* If breakpoints have been added/deleted need to check for it. */
23934 if (fcp->dbg_tick != debug_tick)
23935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023936 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 sourcing_lnum);
23938 fcp->dbg_tick = debug_tick;
23939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023940#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023942 func_line_end(cookie);
23943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944
Bram Moolenaar05159a02005-02-26 23:04:13 +000023945 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023946 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23947 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 retval = NULL;
23949 else
23950 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023951 /* Skip NULL lines (continuation lines). */
23952 while (fcp->linenr < gap->ga_len
23953 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23954 ++fcp->linenr;
23955 if (fcp->linenr >= gap->ga_len)
23956 retval = NULL;
23957 else
23958 {
23959 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23960 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023961#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023962 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023963 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023964#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967
23968 /* Did we encounter a breakpoint? */
23969 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23970 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023971 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023973 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 sourcing_lnum);
23975 fcp->dbg_tick = debug_tick;
23976 }
23977
23978 return retval;
23979}
23980
Bram Moolenaar05159a02005-02-26 23:04:13 +000023981#if defined(FEAT_PROFILE) || defined(PROTO)
23982/*
23983 * Called when starting to read a function line.
23984 * "sourcing_lnum" must be correct!
23985 * When skipping lines it may not actually be executed, but we won't find out
23986 * until later and we need to store the time now.
23987 */
23988 void
23989func_line_start(cookie)
23990 void *cookie;
23991{
23992 funccall_T *fcp = (funccall_T *)cookie;
23993 ufunc_T *fp = fcp->func;
23994
23995 if (fp->uf_profiling && sourcing_lnum >= 1
23996 && sourcing_lnum <= fp->uf_lines.ga_len)
23997 {
23998 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023999 /* Skip continuation lines. */
24000 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24001 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024002 fp->uf_tml_execed = FALSE;
24003 profile_start(&fp->uf_tml_start);
24004 profile_zero(&fp->uf_tml_children);
24005 profile_get_wait(&fp->uf_tml_wait);
24006 }
24007}
24008
24009/*
24010 * Called when actually executing a function line.
24011 */
24012 void
24013func_line_exec(cookie)
24014 void *cookie;
24015{
24016 funccall_T *fcp = (funccall_T *)cookie;
24017 ufunc_T *fp = fcp->func;
24018
24019 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24020 fp->uf_tml_execed = TRUE;
24021}
24022
24023/*
24024 * Called when done with a function line.
24025 */
24026 void
24027func_line_end(cookie)
24028 void *cookie;
24029{
24030 funccall_T *fcp = (funccall_T *)cookie;
24031 ufunc_T *fp = fcp->func;
24032
24033 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24034 {
24035 if (fp->uf_tml_execed)
24036 {
24037 ++fp->uf_tml_count[fp->uf_tml_idx];
24038 profile_end(&fp->uf_tml_start);
24039 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024040 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024041 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24042 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024043 }
24044 fp->uf_tml_idx = -1;
24045 }
24046}
24047#endif
24048
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049/*
24050 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024051 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 */
24053 int
24054func_has_ended(cookie)
24055 void *cookie;
24056{
Bram Moolenaar33570922005-01-25 22:26:29 +000024057 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058
24059 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24060 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024061 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 || fcp->returned);
24063}
24064
24065/*
24066 * return TRUE if cookie indicates a function which "abort"s on errors.
24067 */
24068 int
24069func_has_abort(cookie)
24070 void *cookie;
24071{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024072 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073}
24074
24075#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24076typedef enum
24077{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024078 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24079 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24080 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081} var_flavour_T;
24082
24083static var_flavour_T var_flavour __ARGS((char_u *varname));
24084
24085 static var_flavour_T
24086var_flavour(varname)
24087 char_u *varname;
24088{
24089 char_u *p = varname;
24090
24091 if (ASCII_ISUPPER(*p))
24092 {
24093 while (*(++p))
24094 if (ASCII_ISLOWER(*p))
24095 return VAR_FLAVOUR_SESSION;
24096 return VAR_FLAVOUR_VIMINFO;
24097 }
24098 else
24099 return VAR_FLAVOUR_DEFAULT;
24100}
24101#endif
24102
24103#if defined(FEAT_VIMINFO) || defined(PROTO)
24104/*
24105 * Restore global vars that start with a capital from the viminfo file
24106 */
24107 int
24108read_viminfo_varlist(virp, writing)
24109 vir_T *virp;
24110 int writing;
24111{
24112 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024113 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024114 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115
24116 if (!writing && (find_viminfo_parameter('!') != NULL))
24117 {
24118 tab = vim_strchr(virp->vir_line + 1, '\t');
24119 if (tab != NULL)
24120 {
24121 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024122 switch (*tab)
24123 {
24124 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024125#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024126 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024127#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024128 case 'D': type = VAR_DICT; break;
24129 case 'L': type = VAR_LIST; break;
24130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131
24132 tab = vim_strchr(tab, '\t');
24133 if (tab != NULL)
24134 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024135 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024136 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024137 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024139#ifdef FEAT_FLOAT
24140 else if (type == VAR_FLOAT)
24141 (void)string2float(tab + 1, &tv.vval.v_float);
24142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024144 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024145 if (type == VAR_DICT || type == VAR_LIST)
24146 {
24147 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24148
24149 if (etv == NULL)
24150 /* Failed to parse back the dict or list, use it as a
24151 * string. */
24152 tv.v_type = VAR_STRING;
24153 else
24154 {
24155 vim_free(tv.vval.v_string);
24156 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024157 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024158 }
24159 }
24160
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024161 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024162
24163 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024164 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024165 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24166 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 }
24168 }
24169 }
24170
24171 return viminfo_readline(virp);
24172}
24173
24174/*
24175 * Write global vars that start with a capital to the viminfo file
24176 */
24177 void
24178write_viminfo_varlist(fp)
24179 FILE *fp;
24180{
Bram Moolenaar33570922005-01-25 22:26:29 +000024181 hashitem_T *hi;
24182 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024183 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024184 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024185 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024186 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024187 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188
24189 if (find_viminfo_parameter('!') == NULL)
24190 return;
24191
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024192 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024193
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024194 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024195 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024197 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024199 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 this_var = HI2DI(hi);
24201 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024202 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024204 {
24205 case VAR_STRING: s = "STR"; break;
24206 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024207#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024208 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024209#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024210 case VAR_DICT: s = "DIC"; break;
24211 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024212 default: continue;
24213 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024214 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024215 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024216 if (p != NULL)
24217 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024218 vim_free(tofree);
24219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 }
24221 }
24222}
24223#endif
24224
24225#if defined(FEAT_SESSION) || defined(PROTO)
24226 int
24227store_session_globals(fd)
24228 FILE *fd;
24229{
Bram Moolenaar33570922005-01-25 22:26:29 +000024230 hashitem_T *hi;
24231 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024232 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 char_u *p, *t;
24234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024235 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024236 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024238 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024240 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024241 this_var = HI2DI(hi);
24242 if ((this_var->di_tv.v_type == VAR_NUMBER
24243 || this_var->di_tv.v_type == VAR_STRING)
24244 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024245 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024246 /* Escape special characters with a backslash. Turn a LF and
24247 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024248 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024249 (char_u *)"\\\"\n\r");
24250 if (p == NULL) /* out of memory */
24251 break;
24252 for (t = p; *t != NUL; ++t)
24253 if (*t == '\n')
24254 *t = 'n';
24255 else if (*t == '\r')
24256 *t = 'r';
24257 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024258 this_var->di_key,
24259 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24260 : ' ',
24261 p,
24262 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24263 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024264 || put_eol(fd) == FAIL)
24265 {
24266 vim_free(p);
24267 return FAIL;
24268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 vim_free(p);
24270 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024271#ifdef FEAT_FLOAT
24272 else if (this_var->di_tv.v_type == VAR_FLOAT
24273 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24274 {
24275 float_T f = this_var->di_tv.vval.v_float;
24276 int sign = ' ';
24277
24278 if (f < 0)
24279 {
24280 f = -f;
24281 sign = '-';
24282 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024283 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024284 this_var->di_key, sign, f) < 0)
24285 || put_eol(fd) == FAIL)
24286 return FAIL;
24287 }
24288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 }
24290 }
24291 return OK;
24292}
24293#endif
24294
Bram Moolenaar661b1822005-07-28 22:36:45 +000024295/*
24296 * Display script name where an item was last set.
24297 * Should only be invoked when 'verbose' is non-zero.
24298 */
24299 void
24300last_set_msg(scriptID)
24301 scid_T scriptID;
24302{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024303 char_u *p;
24304
Bram Moolenaar661b1822005-07-28 22:36:45 +000024305 if (scriptID != 0)
24306 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024307 p = home_replace_save(NULL, get_scriptname(scriptID));
24308 if (p != NULL)
24309 {
24310 verbose_enter();
24311 MSG_PUTS(_("\n\tLast set from "));
24312 MSG_PUTS(p);
24313 vim_free(p);
24314 verbose_leave();
24315 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024316 }
24317}
24318
Bram Moolenaard812df62008-11-09 12:46:09 +000024319/*
24320 * List v:oldfiles in a nice way.
24321 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024322 void
24323ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024324 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024325{
24326 list_T *l = vimvars[VV_OLDFILES].vv_list;
24327 listitem_T *li;
24328 int nr = 0;
24329
24330 if (l == NULL)
24331 msg((char_u *)_("No old files"));
24332 else
24333 {
24334 msg_start();
24335 msg_scroll = TRUE;
24336 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24337 {
24338 msg_outnum((long)++nr);
24339 MSG_PUTS(": ");
24340 msg_outtrans(get_tv_string(&li->li_tv));
24341 msg_putchar('\n');
24342 out_flush(); /* output one line at a time */
24343 ui_breakcheck();
24344 }
24345 /* Assume "got_int" was set to truncate the listing. */
24346 got_int = FALSE;
24347
24348#ifdef FEAT_BROWSE_CMD
24349 if (cmdmod.browse)
24350 {
24351 quit_more = FALSE;
24352 nr = prompt_for_number(FALSE);
24353 msg_starthere();
24354 if (nr > 0)
24355 {
24356 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24357 (long)nr);
24358
24359 if (p != NULL)
24360 {
24361 p = expand_env_save(p);
24362 eap->arg = p;
24363 eap->cmdidx = CMD_edit;
24364 cmdmod.browse = FALSE;
24365 do_exedit(eap, NULL);
24366 vim_free(p);
24367 }
24368 }
24369 }
24370#endif
24371 }
24372}
24373
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374#endif /* FEAT_EVAL */
24375
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024377#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378
24379#ifdef WIN3264
24380/*
24381 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24382 */
24383static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24384static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24385static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24386
24387/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024388 * Get the short path (8.3) for the filename in "fnamep".
24389 * Only works for a valid file name.
24390 * When the path gets longer "fnamep" is changed and the allocated buffer
24391 * is put in "bufp".
24392 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24393 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 */
24395 static int
24396get_short_pathname(fnamep, bufp, fnamelen)
24397 char_u **fnamep;
24398 char_u **bufp;
24399 int *fnamelen;
24400{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024401 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 char_u *newbuf;
24403
24404 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 l = GetShortPathName(*fnamep, *fnamep, len);
24406 if (l > len - 1)
24407 {
24408 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024409 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 newbuf = vim_strnsave(*fnamep, l);
24411 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413
24414 vim_free(*bufp);
24415 *fnamep = *bufp = newbuf;
24416
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024417 /* Really should always succeed, as the buffer is big enough. */
24418 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 }
24420
24421 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024422 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423}
24424
24425/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024426 * Get the short path (8.3) for the filename in "fname". The converted
24427 * path is returned in "bufp".
24428 *
24429 * Some of the directories specified in "fname" may not exist. This function
24430 * will shorten the existing directories at the beginning of the path and then
24431 * append the remaining non-existing path.
24432 *
24433 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024434 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024435 * bufp - Pointer to an allocated buffer for the filename.
24436 * fnamelen - Length of the filename pointed to by fname
24437 *
24438 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 */
24440 static int
24441shortpath_for_invalid_fname(fname, bufp, fnamelen)
24442 char_u **fname;
24443 char_u **bufp;
24444 int *fnamelen;
24445{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024446 char_u *short_fname, *save_fname, *pbuf_unused;
24447 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024449 int old_len, len;
24450 int new_len, sfx_len;
24451 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452
24453 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024454 old_len = *fnamelen;
24455 save_fname = vim_strnsave(*fname, old_len);
24456 pbuf_unused = NULL;
24457 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024459 endp = save_fname + old_len - 1; /* Find the end of the copy */
24460 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024462 /*
24463 * Try shortening the supplied path till it succeeds by removing one
24464 * directory at a time from the tail of the path.
24465 */
24466 len = 0;
24467 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024469 /* go back one path-separator */
24470 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24471 --endp;
24472 if (endp <= save_fname)
24473 break; /* processed the complete path */
24474
24475 /*
24476 * Replace the path separator with a NUL and try to shorten the
24477 * resulting path.
24478 */
24479 ch = *endp;
24480 *endp = 0;
24481 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024482 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024483 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24484 {
24485 retval = FAIL;
24486 goto theend;
24487 }
24488 *endp = ch; /* preserve the string */
24489
24490 if (len > 0)
24491 break; /* successfully shortened the path */
24492
24493 /* failed to shorten the path. Skip the path separator */
24494 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 }
24496
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024497 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024499 /*
24500 * Succeeded in shortening the path. Now concatenate the shortened
24501 * path with the remaining path at the tail.
24502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024504 /* Compute the length of the new path. */
24505 sfx_len = (int)(save_endp - endp) + 1;
24506 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024508 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024510 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024512 /* There is not enough space in the currently allocated string,
24513 * copy it to a buffer big enough. */
24514 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024516 {
24517 retval = FAIL;
24518 goto theend;
24519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 }
24521 else
24522 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024523 /* Transfer short_fname to the main buffer (it's big enough),
24524 * unless get_short_pathname() did its work in-place. */
24525 *fname = *bufp = save_fname;
24526 if (short_fname != save_fname)
24527 vim_strncpy(save_fname, short_fname, len);
24528 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024530
24531 /* concat the not-shortened part of the path */
24532 vim_strncpy(*fname + len, endp, sfx_len);
24533 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024535
24536theend:
24537 vim_free(pbuf_unused);
24538 vim_free(save_fname);
24539
24540 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541}
24542
24543/*
24544 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024545 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 */
24547 static int
24548shortpath_for_partial(fnamep, bufp, fnamelen)
24549 char_u **fnamep;
24550 char_u **bufp;
24551 int *fnamelen;
24552{
24553 int sepcount, len, tflen;
24554 char_u *p;
24555 char_u *pbuf, *tfname;
24556 int hasTilde;
24557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024558 /* Count up the path separators from the RHS.. so we know which part
24559 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024561 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 if (vim_ispathsep(*p))
24563 ++sepcount;
24564
24565 /* Need full path first (use expand_env() to remove a "~/") */
24566 hasTilde = (**fnamep == '~');
24567 if (hasTilde)
24568 pbuf = tfname = expand_env_save(*fnamep);
24569 else
24570 pbuf = tfname = FullName_save(*fnamep, FALSE);
24571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024572 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024574 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576
24577 if (len == 0)
24578 {
24579 /* Don't have a valid filename, so shorten the rest of the
24580 * path if we can. This CAN give us invalid 8.3 filenames, but
24581 * there's not a lot of point in guessing what it might be.
24582 */
24583 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024584 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 }
24587
24588 /* Count the paths backward to find the beginning of the desired string. */
24589 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024590 {
24591#ifdef FEAT_MBYTE
24592 if (has_mbyte)
24593 p -= mb_head_off(tfname, p);
24594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 if (vim_ispathsep(*p))
24596 {
24597 if (sepcount == 0 || (hasTilde && sepcount == 1))
24598 break;
24599 else
24600 sepcount --;
24601 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 if (hasTilde)
24604 {
24605 --p;
24606 if (p >= tfname)
24607 *p = '~';
24608 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 }
24611 else
24612 ++p;
24613
24614 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24615 vim_free(*bufp);
24616 *fnamelen = (int)STRLEN(p);
24617 *bufp = pbuf;
24618 *fnamep = p;
24619
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024620 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621}
24622#endif /* WIN3264 */
24623
24624/*
24625 * Adjust a filename, according to a string of modifiers.
24626 * *fnamep must be NUL terminated when called. When returning, the length is
24627 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024628 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 * When there is an error, *fnamep is set to NULL.
24630 */
24631 int
24632modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24633 char_u *src; /* string with modifiers */
24634 int *usedlen; /* characters after src that are used */
24635 char_u **fnamep; /* file name so far */
24636 char_u **bufp; /* buffer for allocated file name or NULL */
24637 int *fnamelen; /* length of fnamep */
24638{
24639 int valid = 0;
24640 char_u *tail;
24641 char_u *s, *p, *pbuf;
24642 char_u dirname[MAXPATHL];
24643 int c;
24644 int has_fullname = 0;
24645#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024646 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 int has_shortname = 0;
24648#endif
24649
24650repeat:
24651 /* ":p" - full path/file_name */
24652 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24653 {
24654 has_fullname = 1;
24655
24656 valid |= VALID_PATH;
24657 *usedlen += 2;
24658
24659 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24660 if ((*fnamep)[0] == '~'
24661#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24662 && ((*fnamep)[1] == '/'
24663# ifdef BACKSLASH_IN_FILENAME
24664 || (*fnamep)[1] == '\\'
24665# endif
24666 || (*fnamep)[1] == NUL)
24667
24668#endif
24669 )
24670 {
24671 *fnamep = expand_env_save(*fnamep);
24672 vim_free(*bufp); /* free any allocated file name */
24673 *bufp = *fnamep;
24674 if (*fnamep == NULL)
24675 return -1;
24676 }
24677
24678 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024679 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680 {
24681 if (vim_ispathsep(*p)
24682 && p[1] == '.'
24683 && (p[2] == NUL
24684 || vim_ispathsep(p[2])
24685 || (p[2] == '.'
24686 && (p[3] == NUL || vim_ispathsep(p[3])))))
24687 break;
24688 }
24689
24690 /* FullName_save() is slow, don't use it when not needed. */
24691 if (*p != NUL || !vim_isAbsName(*fnamep))
24692 {
24693 *fnamep = FullName_save(*fnamep, *p != NUL);
24694 vim_free(*bufp); /* free any allocated file name */
24695 *bufp = *fnamep;
24696 if (*fnamep == NULL)
24697 return -1;
24698 }
24699
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024700#ifdef WIN3264
24701# if _WIN32_WINNT >= 0x0500
24702 if (vim_strchr(*fnamep, '~') != NULL)
24703 {
24704 /* Expand 8.3 filename to full path. Needed to make sure the same
24705 * file does not have two different names.
24706 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24707 p = alloc(_MAX_PATH + 1);
24708 if (p != NULL)
24709 {
24710 if (GetLongPathName(*fnamep, p, MAXPATHL))
24711 {
24712 vim_free(*bufp);
24713 *bufp = *fnamep = p;
24714 }
24715 else
24716 vim_free(p);
24717 }
24718 }
24719# endif
24720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 /* Append a path separator to a directory. */
24722 if (mch_isdir(*fnamep))
24723 {
24724 /* Make room for one or two extra characters. */
24725 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24726 vim_free(*bufp); /* free any allocated file name */
24727 *bufp = *fnamep;
24728 if (*fnamep == NULL)
24729 return -1;
24730 add_pathsep(*fnamep);
24731 }
24732 }
24733
24734 /* ":." - path relative to the current directory */
24735 /* ":~" - path relative to the home directory */
24736 /* ":8" - shortname path - postponed till after */
24737 while (src[*usedlen] == ':'
24738 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24739 {
24740 *usedlen += 2;
24741 if (c == '8')
24742 {
24743#ifdef WIN3264
24744 has_shortname = 1; /* Postpone this. */
24745#endif
24746 continue;
24747 }
24748 pbuf = NULL;
24749 /* Need full path first (use expand_env() to remove a "~/") */
24750 if (!has_fullname)
24751 {
24752 if (c == '.' && **fnamep == '~')
24753 p = pbuf = expand_env_save(*fnamep);
24754 else
24755 p = pbuf = FullName_save(*fnamep, FALSE);
24756 }
24757 else
24758 p = *fnamep;
24759
24760 has_fullname = 0;
24761
24762 if (p != NULL)
24763 {
24764 if (c == '.')
24765 {
24766 mch_dirname(dirname, MAXPATHL);
24767 s = shorten_fname(p, dirname);
24768 if (s != NULL)
24769 {
24770 *fnamep = s;
24771 if (pbuf != NULL)
24772 {
24773 vim_free(*bufp); /* free any allocated file name */
24774 *bufp = pbuf;
24775 pbuf = NULL;
24776 }
24777 }
24778 }
24779 else
24780 {
24781 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24782 /* Only replace it when it starts with '~' */
24783 if (*dirname == '~')
24784 {
24785 s = vim_strsave(dirname);
24786 if (s != NULL)
24787 {
24788 *fnamep = s;
24789 vim_free(*bufp);
24790 *bufp = s;
24791 }
24792 }
24793 }
24794 vim_free(pbuf);
24795 }
24796 }
24797
24798 tail = gettail(*fnamep);
24799 *fnamelen = (int)STRLEN(*fnamep);
24800
24801 /* ":h" - head, remove "/file_name", can be repeated */
24802 /* Don't remove the first "/" or "c:\" */
24803 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24804 {
24805 valid |= VALID_HEAD;
24806 *usedlen += 2;
24807 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024808 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024809 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 *fnamelen = (int)(tail - *fnamep);
24811#ifdef VMS
24812 if (*fnamelen > 0)
24813 *fnamelen += 1; /* the path separator is part of the path */
24814#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024815 if (*fnamelen == 0)
24816 {
24817 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24818 p = vim_strsave((char_u *)".");
24819 if (p == NULL)
24820 return -1;
24821 vim_free(*bufp);
24822 *bufp = *fnamep = tail = p;
24823 *fnamelen = 1;
24824 }
24825 else
24826 {
24827 while (tail > s && !after_pathsep(s, tail))
24828 mb_ptr_back(*fnamep, tail);
24829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 }
24831
24832 /* ":8" - shortname */
24833 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24834 {
24835 *usedlen += 2;
24836#ifdef WIN3264
24837 has_shortname = 1;
24838#endif
24839 }
24840
24841#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024842 /*
24843 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844 */
24845 if (has_shortname)
24846 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024847 /* Copy the string if it is shortened by :h and when it wasn't copied
24848 * yet, because we are going to change it in place. Avoids changing
24849 * the buffer name for "%:8". */
24850 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 {
24852 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024853 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 return -1;
24855 vim_free(*bufp);
24856 *bufp = *fnamep = p;
24857 }
24858
24859 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024860 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861 if (!has_fullname && !vim_isAbsName(*fnamep))
24862 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 return -1;
24865 }
24866 else
24867 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024868 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869
Bram Moolenaardc935552011-08-17 15:23:23 +020024870 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 return -1;
24874
24875 if (l == 0)
24876 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024877 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 return -1;
24881 }
24882 *fnamelen = l;
24883 }
24884 }
24885#endif /* WIN3264 */
24886
24887 /* ":t" - tail, just the basename */
24888 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24889 {
24890 *usedlen += 2;
24891 *fnamelen -= (int)(tail - *fnamep);
24892 *fnamep = tail;
24893 }
24894
24895 /* ":e" - extension, can be repeated */
24896 /* ":r" - root, without extension, can be repeated */
24897 while (src[*usedlen] == ':'
24898 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24899 {
24900 /* find a '.' in the tail:
24901 * - for second :e: before the current fname
24902 * - otherwise: The last '.'
24903 */
24904 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24905 s = *fnamep - 2;
24906 else
24907 s = *fnamep + *fnamelen - 1;
24908 for ( ; s > tail; --s)
24909 if (s[0] == '.')
24910 break;
24911 if (src[*usedlen + 1] == 'e') /* :e */
24912 {
24913 if (s > tail)
24914 {
24915 *fnamelen += (int)(*fnamep - (s + 1));
24916 *fnamep = s + 1;
24917#ifdef VMS
24918 /* cut version from the extension */
24919 s = *fnamep + *fnamelen - 1;
24920 for ( ; s > *fnamep; --s)
24921 if (s[0] == ';')
24922 break;
24923 if (s > *fnamep)
24924 *fnamelen = s - *fnamep;
24925#endif
24926 }
24927 else if (*fnamep <= tail)
24928 *fnamelen = 0;
24929 }
24930 else /* :r */
24931 {
24932 if (s > tail) /* remove one extension */
24933 *fnamelen = (int)(s - *fnamep);
24934 }
24935 *usedlen += 2;
24936 }
24937
24938 /* ":s?pat?foo?" - substitute */
24939 /* ":gs?pat?foo?" - global substitute */
24940 if (src[*usedlen] == ':'
24941 && (src[*usedlen + 1] == 's'
24942 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24943 {
24944 char_u *str;
24945 char_u *pat;
24946 char_u *sub;
24947 int sep;
24948 char_u *flags;
24949 int didit = FALSE;
24950
24951 flags = (char_u *)"";
24952 s = src + *usedlen + 2;
24953 if (src[*usedlen + 1] == 'g')
24954 {
24955 flags = (char_u *)"g";
24956 ++s;
24957 }
24958
24959 sep = *s++;
24960 if (sep)
24961 {
24962 /* find end of pattern */
24963 p = vim_strchr(s, sep);
24964 if (p != NULL)
24965 {
24966 pat = vim_strnsave(s, (int)(p - s));
24967 if (pat != NULL)
24968 {
24969 s = p + 1;
24970 /* find end of substitution */
24971 p = vim_strchr(s, sep);
24972 if (p != NULL)
24973 {
24974 sub = vim_strnsave(s, (int)(p - s));
24975 str = vim_strnsave(*fnamep, *fnamelen);
24976 if (sub != NULL && str != NULL)
24977 {
24978 *usedlen = (int)(p + 1 - src);
24979 s = do_string_sub(str, pat, sub, flags);
24980 if (s != NULL)
24981 {
24982 *fnamep = s;
24983 *fnamelen = (int)STRLEN(s);
24984 vim_free(*bufp);
24985 *bufp = s;
24986 didit = TRUE;
24987 }
24988 }
24989 vim_free(sub);
24990 vim_free(str);
24991 }
24992 vim_free(pat);
24993 }
24994 }
24995 /* after using ":s", repeat all the modifiers */
24996 if (didit)
24997 goto repeat;
24998 }
24999 }
25000
Bram Moolenaar26df0922014-02-23 23:39:13 +010025001 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25002 {
25003 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25004 if (p == NULL)
25005 return -1;
25006 vim_free(*bufp);
25007 *bufp = *fnamep = p;
25008 *fnamelen = (int)STRLEN(p);
25009 *usedlen += 2;
25010 }
25011
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 return valid;
25013}
25014
25015/*
25016 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25017 * "flags" can be "g" to do a global substitute.
25018 * Returns an allocated string, NULL for error.
25019 */
25020 char_u *
25021do_string_sub(str, pat, sub, flags)
25022 char_u *str;
25023 char_u *pat;
25024 char_u *sub;
25025 char_u *flags;
25026{
25027 int sublen;
25028 regmatch_T regmatch;
25029 int i;
25030 int do_all;
25031 char_u *tail;
25032 garray_T ga;
25033 char_u *ret;
25034 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025035 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036
25037 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25038 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025039 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040
25041 ga_init2(&ga, 1, 200);
25042
25043 do_all = (flags[0] == 'g');
25044
25045 regmatch.rm_ic = p_ic;
25046 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25047 if (regmatch.regprog != NULL)
25048 {
25049 tail = str;
25050 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25051 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025052 /* Skip empty match except for first match. */
25053 if (regmatch.startp[0] == regmatch.endp[0])
25054 {
25055 if (zero_width == regmatch.startp[0])
25056 {
25057 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025058 i = MB_PTR2LEN(tail);
25059 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25060 (size_t)i);
25061 ga.ga_len += i;
25062 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025063 continue;
25064 }
25065 zero_width = regmatch.startp[0];
25066 }
25067
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 /*
25069 * Get some space for a temporary buffer to do the substitution
25070 * into. It will contain:
25071 * - The text up to where the match is.
25072 * - The substituted text.
25073 * - The text after the match.
25074 */
25075 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
25076 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
25077 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25078 {
25079 ga_clear(&ga);
25080 break;
25081 }
25082
25083 /* copy the text up to where the match is */
25084 i = (int)(regmatch.startp[0] - tail);
25085 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25086 /* add the substituted text */
25087 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25088 + ga.ga_len + i, TRUE, TRUE, FALSE);
25089 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025090 tail = regmatch.endp[0];
25091 if (*tail == NUL)
25092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 if (!do_all)
25094 break;
25095 }
25096
25097 if (ga.ga_data != NULL)
25098 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25099
Bram Moolenaar473de612013-06-08 18:19:48 +020025100 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 }
25102
25103 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25104 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025105 if (p_cpo == empty_option)
25106 p_cpo = save_cpo;
25107 else
25108 /* Darn, evaluating {sub} expression changed the value. */
25109 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110
25111 return ret;
25112}
25113
25114#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */