blob: 2aa4e5407ad3b96a86c098329e4aff19bc7f8168 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000368};
369
370/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_type vv_di.di_tv.v_type
372#define vv_nr vv_di.di_tv.vval.v_number
373#define vv_float vv_di.di_tv.vval.v_float
374#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000375#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000376#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200378static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000429static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000431static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000435static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100436static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000438static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200439static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
447static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000449#ifdef FEAT_FLOAT
450static int string2float __ARGS((char_u *text, float_T *value));
451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100454static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200456static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000458static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200462static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100465static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200469static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200472static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100485static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100487static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
490static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000492static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000495static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000497#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000498static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000499static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200506static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
511static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200521static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000580static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000594static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100599static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000601static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200614static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000615static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
616#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200617#ifdef FEAT_LUA
618static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000624static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200625static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100642static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000643static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
645static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000648static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000649static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200650#ifdef FEAT_PYTHON3
651static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
653#ifdef FEAT_PYTHON
654static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000658static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
671static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
672#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200673static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100675static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000678static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000680static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000687static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000688static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000689static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000690static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200692static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000693static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100695#ifdef FEAT_CRYPT
696static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
697#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000698static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200699static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
702static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200703static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000706static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000707static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000710#ifdef FEAT_FLOAT
711static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
713#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000714static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200715static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716#ifdef HAVE_STRFTIME
717static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
718#endif
719static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200725static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200726static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000732static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200733static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200735static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000736static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000737static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000738static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000739static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000740static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000742static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200743#ifdef FEAT_FLOAT
744static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
751static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
752#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200754static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200755static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100756static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100760static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000767static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100771static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772
Bram Moolenaar493c1782014-05-28 14:34:46 +0200773static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000774static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static int get_env_len __ARGS((char_u **arg));
776static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000777static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
779#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
780#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
781 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000782static 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 +0000783static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000784static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100785static 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 +0000786static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static typval_T *alloc_tv __ARGS((void));
788static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void init_tv __ARGS((typval_T *varp));
790static long get_tv_number __ARGS((typval_T *varp));
791static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000792static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static char_u *get_tv_string __ARGS((typval_T *varp));
794static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000795static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100796static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
797static 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 +0000798static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
799static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
800static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000801static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
802static 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 +0000803static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
804static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000805static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200806static int var_check_func_name __ARGS((char_u *name, int new_var));
807static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000808static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000809static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
811static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
812static int eval_fname_script __ARGS((char_u *p));
813static int eval_fname_sid __ARGS((char_u *p));
814static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static ufunc_T *find_func __ARGS((char_u *name));
816static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200817static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#ifdef FEAT_PROFILE
819static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000820static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
821static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
822static int
823# ifdef __BORLANDC__
824 _RTLENTRYF
825# endif
826 prof_total_cmp __ARGS((const void *s1, const void *s2));
827static int
828# ifdef __BORLANDC__
829 _RTLENTRYF
830# endif
831 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200833static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000834static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000835static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static 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 +0000838static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
839static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000841static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
842static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000843static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000845static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200846static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200847static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200849
850#ifdef EBCDIC
851static int compare_func_name __ARGS((const void *s1, const void *s2));
852static void sortFunctions __ARGS(());
853#endif
854
Bram Moolenaar33570922005-01-25 22:26:29 +0000855/*
856 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000857 */
858 void
859eval_init()
860{
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 int i;
862 struct vimvar *p;
863
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200864 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
865 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200866 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000867 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000868 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869
870 for (i = 0; i < VV_LEN; ++i)
871 {
872 p = &vimvars[i];
873 STRCPY(p->vv_di.di_key, p->vv_name);
874 if (p->vv_flags & VV_RO)
875 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
876 else if (p->vv_flags & VV_RO_SBX)
877 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
878 else
879 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000880
881 /* add to v: scope dict, unless the value is not always available */
882 if (p->vv_type != VAR_UNKNOWN)
883 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000885 /* add to compat scope dict */
886 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000888 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100889 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200890 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200891
892#ifdef EBCDIC
893 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100894 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895 */
896 sortFunctions();
897#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000898}
899
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900#if defined(EXITFREE) || defined(PROTO)
901 void
902eval_clear()
903{
904 int i;
905 struct vimvar *p;
906
907 for (i = 0; i < VV_LEN; ++i)
908 {
909 p = &vimvars[i];
910 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000912 vim_free(p->vv_str);
913 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000914 }
915 else if (p->vv_di.di_tv.v_type == VAR_LIST)
916 {
917 list_unref(p->vv_list);
918 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920 }
921 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000922 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923 hash_clear(&compat_hashtab);
924
Bram Moolenaard9fba312005-06-26 22:34:35 +0000925 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100926# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200927 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100928# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929
930 /* global variables */
931 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000933 /* autoloaded script names */
934 ga_clear_strings(&ga_loaded);
935
Bram Moolenaarcca74132013-09-25 21:00:28 +0200936 /* Script-local variables. First clear all the variables and in a second
937 * loop free the scriptvar_T, because a variable in one script might hold
938 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200939 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200941 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200942 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 ga_clear(&ga_scripts);
944
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000945 /* unreferenced lists and dicts */
946 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000947
948 /* functions */
949 free_all_functions();
950 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000951}
952#endif
953
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * Return the name of the executed function.
956 */
957 char_u *
958func_name(cookie)
959 void *cookie;
960{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Return the address holding the next breakpoint line for a funccall cookie.
966 */
967 linenr_T *
968func_breakpoint(cookie)
969 void *cookie;
970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
974/*
975 * Return the address holding the debug tick for a funccall cookie.
976 */
977 int *
978func_dbg_tick(cookie)
979 void *cookie;
980{
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/*
985 * Return the nesting level for a funccall cookie.
986 */
987 int
988func_level(cookie)
989 void *cookie;
990{
Bram Moolenaar33570922005-01-25 22:26:29 +0000991 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992}
993
994/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000995funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000997/* pointer to list of previously used funccal, still around because some
998 * item in it is still being used. */
999funccall_T *previous_funccal = NULL;
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Return TRUE when a function was ended by a ":return" command.
1003 */
1004 int
1005current_func_returned()
1006{
1007 return current_funccal->returned;
1008}
1009
1010
1011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * Set an internal variable to a string value. Creates the variable if it does
1013 * not already exist.
1014 */
1015 void
1016set_internal_string_var(name, value)
1017 char_u *name;
1018 char_u *value;
1019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 val = vim_strsave(value);
1024 if (val != NULL)
1025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001027 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001029 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 }
1033}
1034
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001036static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037static char_u *redir_endp = NULL;
1038static char_u *redir_varname = NULL;
1039
1040/*
1041 * Start recording command output to a variable
1042 * Returns OK if successfully completed the setup. FAIL otherwise.
1043 */
1044 int
1045var_redir_start(name, append)
1046 char_u *name;
1047 int append; /* append to an existing variable */
1048{
1049 int save_emsg;
1050 int err;
1051 typval_T tv;
1052
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001053 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001054 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 {
1056 EMSG(_(e_invarg));
1057 return FAIL;
1058 }
1059
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 redir_varname = vim_strsave(name);
1062 if (redir_varname == NULL)
1063 return FAIL;
1064
1065 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1066 if (redir_lval == NULL)
1067 {
1068 var_redir_stop();
1069 return FAIL;
1070 }
1071
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072 /* The output is stored in growarray "redir_ga" until redirection ends. */
1073 ga_init2(&redir_ga, (int)sizeof(char), 500);
1074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001076 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001077 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1079 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 if (redir_endp != NULL && *redir_endp != NUL)
1082 /* Trailing characters are present after the variable name */
1083 EMSG(_(e_trailing));
1084 else
1085 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001086 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 var_redir_stop();
1088 return FAIL;
1089 }
1090
1091 /* check if we can write to the variable: set it to or append an empty
1092 * string */
1093 save_emsg = did_emsg;
1094 did_emsg = FALSE;
1095 tv.v_type = VAR_STRING;
1096 tv.vval.v_string = (char_u *)"";
1097 if (append)
1098 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1099 else
1100 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001101 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001103 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 if (err)
1105 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001106 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 var_redir_stop();
1108 return FAIL;
1109 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 return OK;
1112}
1113
1114/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 * Append "value[value_len]" to the variable set by var_redir_start().
1116 * The actual appending is postponed until redirection ends, because the value
1117 * appended may in fact be the string we write to, changing it may cause freed
1118 * memory to be used:
1119 * :redir => foo
1120 * :let foo
1121 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 */
1123 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129
1130 if (redir_lval == NULL)
1131 return;
1132
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001134 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001136 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001138 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 {
1140 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 }
1143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145}
1146
1147/*
1148 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 */
1151 void
1152var_redir_stop()
1153{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154 typval_T tv;
1155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 if (redir_lval != NULL)
1157 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* If there was no error: assign the text to the variable. */
1159 if (redir_endp != NULL)
1160 {
1161 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1162 tv.v_type = VAR_STRING;
1163 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001164 /* Call get_lval() again, if it's inside a Dict or List it may
1165 * have changed. */
1166 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001167 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001168 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1169 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1170 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001173 /* free the collected output */
1174 vim_free(redir_ga.ga_data);
1175 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 vim_free(redir_lval);
1178 redir_lval = NULL;
1179 }
1180 vim_free(redir_varname);
1181 redir_varname = NULL;
1182}
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184# if defined(FEAT_MBYTE) || defined(PROTO)
1185 int
1186eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1187 char_u *enc_from;
1188 char_u *enc_to;
1189 char_u *fname_from;
1190 char_u *fname_to;
1191{
1192 int err = FALSE;
1193
1194 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1195 set_vim_var_string(VV_CC_TO, enc_to, -1);
1196 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1197 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1198 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1199 err = TRUE;
1200 set_vim_var_string(VV_CC_FROM, NULL, -1);
1201 set_vim_var_string(VV_CC_TO, NULL, -1);
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1204
1205 if (err)
1206 return FAIL;
1207 return OK;
1208}
1209# endif
1210
1211# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1212 int
1213eval_printexpr(fname, args)
1214 char_u *fname;
1215 char_u *args;
1216{
1217 int err = FALSE;
1218
1219 set_vim_var_string(VV_FNAME_IN, fname, -1);
1220 set_vim_var_string(VV_CMDARG, args, -1);
1221 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1222 err = TRUE;
1223 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1224 set_vim_var_string(VV_CMDARG, NULL, -1);
1225
1226 if (err)
1227 {
1228 mch_remove(fname);
1229 return FAIL;
1230 }
1231 return OK;
1232}
1233# endif
1234
1235# if defined(FEAT_DIFF) || defined(PROTO)
1236 void
1237eval_diff(origfile, newfile, outfile)
1238 char_u *origfile;
1239 char_u *newfile;
1240 char_u *outfile;
1241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1245 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1246 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1247 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251}
1252
1253 void
1254eval_patch(origfile, difffile, outfile)
1255 char_u *origfile;
1256 char_u *difffile;
1257 char_u *outfile;
1258{
1259 int err;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269# endif
1270
1271/*
1272 * Top level evaluation function, returning a boolean.
1273 * Sets "error" to TRUE if there was an error.
1274 * Return TRUE or FALSE.
1275 */
1276 int
1277eval_to_bool(arg, error, nextcmd, skip)
1278 char_u *arg;
1279 int *error;
1280 char_u **nextcmd;
1281 int skip; /* only parse, don't execute */
1282{
Bram Moolenaar33570922005-01-25 22:26:29 +00001283 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int retval = FALSE;
1285
1286 if (skip)
1287 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 else
1291 {
1292 *error = FALSE;
1293 if (!skip)
1294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001295 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 }
1299 if (skip)
1300 --emsg_skip;
1301
1302 return retval;
1303}
1304
1305/*
1306 * Top level evaluation function, returning a string. If "skip" is TRUE,
1307 * only parsing to "nextcmd" is done, without reporting errors. Return
1308 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1309 */
1310 char_u *
1311eval_to_string_skip(arg, nextcmd, skip)
1312 char_u *arg;
1313 char_u **nextcmd;
1314 int skip; /* only parse, don't execute */
1315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 char_u *retval;
1318
1319 if (skip)
1320 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 retval = NULL;
1323 else
1324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 retval = vim_strsave(get_tv_string(&tv));
1326 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 if (skip)
1329 --emsg_skip;
1330
1331 return retval;
1332}
1333
1334/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001335 * Skip over an expression at "*pp".
1336 * Return FAIL for an error, OK otherwise.
1337 */
1338 int
1339skip_expr(pp)
1340 char_u **pp;
1341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001343
1344 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001346}
1347
1348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 * When "convert" is TRUE convert a List into a sequence of lines and convert
1351 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * Return pointer to allocated memory, or NULL for failure.
1353 */
1354 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 char_u *arg;
1357 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001363#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 retval = NULL;
1369 else
1370 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 {
1373 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001374 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001375 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001376 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001377 if (tv.vval.v_list->lv_len > 0)
1378 ga_append(&ga, NL);
1379 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 ga_append(&ga, NUL);
1381 retval = (char_u *)ga.ga_data;
1382 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001383#ifdef FEAT_FLOAT
1384 else if (convert && tv.v_type == VAR_FLOAT)
1385 {
1386 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1387 retval = vim_strsave(numbuf);
1388 }
1389#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 else
1391 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394
1395 return retval;
1396}
1397
1398/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001399 * Call eval_to_string() without using current local variables and using
1400 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
1402 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 char_u *arg;
1405 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 char_u *retval;
1409 void *save_funccalp;
1410
1411 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412 if (use_sandbox)
1413 ++sandbox;
1414 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 if (use_sandbox)
1417 --sandbox;
1418 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 restore_funccal(save_funccalp);
1420 return retval;
1421}
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
1424 * Top level evaluation function, returning a number.
1425 * Evaluates "expr" silently.
1426 * Returns -1 for an error.
1427 */
1428 int
1429eval_to_number(expr)
1430 char_u *expr;
1431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001434 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 ++emsg_off;
1437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 retval = -1;
1440 else
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 --emsg_off;
1446
1447 return retval;
1448}
1449
Bram Moolenaara40058a2005-07-11 22:42:07 +00001450/*
1451 * Prepare v: variable "idx" to be used.
1452 * Save the current typeval in "save_tv".
1453 * When not used yet add the variable to the v: hashtable.
1454 */
1455 static void
1456prepare_vimvar(idx, save_tv)
1457 int idx;
1458 typval_T *save_tv;
1459{
1460 *save_tv = vimvars[idx].vv_tv;
1461 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1462 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1463}
1464
1465/*
1466 * Restore v: variable "idx" to typeval "save_tv".
1467 * When no longer defined, remove the variable from the v: hashtable.
1468 */
1469 static void
1470restore_vimvar(idx, save_tv)
1471 int idx;
1472 typval_T *save_tv;
1473{
1474 hashitem_T *hi;
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476 vimvars[idx].vv_tv = *save_tv;
1477 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1478 {
1479 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1480 if (HASHITEM_EMPTY(hi))
1481 EMSG2(_(e_intern2), "restore_vimvar()");
1482 else
1483 hash_remove(&vimvarht, hi);
1484 }
1485}
1486
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001487#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001488/*
1489 * Evaluate an expression to a list with suggestions.
1490 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001491 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001492 */
1493 list_T *
1494eval_spell_expr(badword, expr)
1495 char_u *badword;
1496 char_u *expr;
1497{
1498 typval_T save_val;
1499 typval_T rettv;
1500 list_T *list = NULL;
1501 char_u *p = skipwhite(expr);
1502
1503 /* Set "v:val" to the bad word. */
1504 prepare_vimvar(VV_VAL, &save_val);
1505 vimvars[VV_VAL].vv_type = VAR_STRING;
1506 vimvars[VV_VAL].vv_str = badword;
1507 if (p_verbose == 0)
1508 ++emsg_off;
1509
1510 if (eval1(&p, &rettv, TRUE) == OK)
1511 {
1512 if (rettv.v_type != VAR_LIST)
1513 clear_tv(&rettv);
1514 else
1515 list = rettv.vval.v_list;
1516 }
1517
1518 if (p_verbose == 0)
1519 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001520 restore_vimvar(VV_VAL, &save_val);
1521
1522 return list;
1523}
1524
1525/*
1526 * "list" is supposed to contain two items: a word and a number. Return the
1527 * word in "pp" and the number as the return value.
1528 * Return -1 if anything isn't right.
1529 * Used to get the good word and score from the eval_spell_expr() result.
1530 */
1531 int
1532get_spellword(list, pp)
1533 list_T *list;
1534 char_u **pp;
1535{
1536 listitem_T *li;
1537
1538 li = list->lv_first;
1539 if (li == NULL)
1540 return -1;
1541 *pp = get_tv_string(&li->li_tv);
1542
1543 li = li->li_next;
1544 if (li == NULL)
1545 return -1;
1546 return get_tv_number(&li->li_tv);
1547}
1548#endif
1549
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 * Top level evaluation function.
1552 * Returns an allocated typval_T with the result.
1553 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001554 */
1555 typval_T *
1556eval_expr(arg, nextcmd)
1557 char_u *arg;
1558 char_u **nextcmd;
1559{
1560 typval_T *tv;
1561
1562 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001563 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001564 {
1565 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001566 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567 }
1568
1569 return tv;
1570}
1571
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001575 * Uses argv[argc] for the function arguments. Only Number and String
1576 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001579 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001580call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 char_u *func;
1582 int argc;
1583 char_u **argv;
1584 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001585 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
Bram Moolenaar33570922005-01-25 22:26:29 +00001588 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 long n;
1590 int len;
1591 int i;
1592 int doesrange;
1593 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001596 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 for (i = 0; i < argc; i++)
1601 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001602 /* Pass a NULL or empty argument as an empty string */
1603 if (argv[i] == NULL || *argv[i] == NUL)
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001607 continue;
1608 }
1609
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001610 if (str_arg_only)
1611 len = 0;
1612 else
1613 /* Recognize a number argument, the others must be strings. */
1614 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (len != 0 && len == (int)STRLEN(argv[i]))
1616 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001617 argvars[i].v_type = VAR_NUMBER;
1618 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 else
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 }
1625 }
1626
1627 if (safe)
1628 {
1629 save_funccalp = save_funccal();
1630 ++sandbox;
1631 }
1632
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1634 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (safe)
1638 {
1639 --sandbox;
1640 restore_funccal(save_funccalp);
1641 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 vim_free(argvars);
1643
1644 if (ret == FAIL)
1645 clear_tv(rettv);
1646
1647 return ret;
1648}
1649
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001650/*
1651 * Call vimL function "func" and return the result as a number.
1652 * Returns -1 when calling the function fails.
1653 * Uses argv[argc] for the function arguments.
1654 */
1655 long
1656call_func_retnr(func, argc, argv, safe)
1657 char_u *func;
1658 int argc;
1659 char_u **argv;
1660 int safe; /* use the sandbox */
1661{
1662 typval_T rettv;
1663 long retval;
1664
1665 /* All arguments are passed as strings, no conversion to number. */
1666 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1667 return -1;
1668
1669 retval = get_tv_number_chk(&rettv, NULL);
1670 clear_tv(&rettv);
1671 return retval;
1672}
1673
1674#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1675 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1676
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 * Call vimL function "func" and return the result as a string.
1680 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
1682 */
1683 void *
1684call_func_retstr(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 /* All arguments are passed as strings, no conversion to number. */
1694 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 return NULL;
1696
1697 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return retval;
1700}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001701# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001703/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001704 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001706 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 */
1708 void *
1709call_func_retlist(func, argc, argv, safe)
1710 char_u *func;
1711 int argc;
1712 char_u **argv;
1713 int safe; /* use the sandbox */
1714{
1715 typval_T rettv;
1716
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001717 /* All arguments are passed as strings, no conversion to number. */
1718 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 return NULL;
1720
1721 if (rettv.v_type != VAR_LIST)
1722 {
1723 clear_tv(&rettv);
1724 return NULL;
1725 }
1726
1727 return rettv.vval.v_list;
1728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730
1731/*
1732 * Save the current function call pointer, and set it to NULL.
1733 * Used when executing autocommands and for ":source".
1734 */
1735 void *
1736save_funccal()
1737{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 current_funccal = NULL;
1741 return (void *)fc;
1742}
1743
1744 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001745restore_funccal(vfc)
1746 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001748 funccall_T *fc = (funccall_T *)vfc;
1749
1750 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751}
1752
Bram Moolenaar05159a02005-02-26 23:04:13 +00001753#if defined(FEAT_PROFILE) || defined(PROTO)
1754/*
1755 * Prepare profiling for entering a child or something else that is not
1756 * counted for the script/function itself.
1757 * Should always be called in pair with prof_child_exit().
1758 */
1759 void
1760prof_child_enter(tm)
1761 proftime_T *tm; /* place to store waittime */
1762{
1763 funccall_T *fc = current_funccal;
1764
1765 if (fc != NULL && fc->func->uf_profiling)
1766 profile_start(&fc->prof_child);
1767 script_prof_save(tm);
1768}
1769
1770/*
1771 * Take care of time spent in a child.
1772 * Should always be called after prof_child_enter().
1773 */
1774 void
1775prof_child_exit(tm)
1776 proftime_T *tm; /* where waittime was stored */
1777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 {
1782 profile_end(&fc->prof_child);
1783 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1784 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1785 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1786 }
1787 script_prof_restore(tm);
1788}
1789#endif
1790
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#ifdef FEAT_FOLDING
1793/*
1794 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1795 * it in "*cp". Doesn't give error messages.
1796 */
1797 int
1798eval_foldexpr(arg, cp)
1799 char_u *arg;
1800 int *cp;
1801{
Bram Moolenaar33570922005-01-25 22:26:29 +00001802 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 int retval;
1804 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001805 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1806 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 ++sandbox;
1811 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 retval = 0;
1815 else
1816 {
1817 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 if (tv.v_type == VAR_NUMBER)
1819 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001820 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 retval = 0;
1822 else
1823 {
1824 /* If the result is a string, check if there is a non-digit before
1825 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!VIM_ISDIGIT(*s) && *s != '-')
1828 *cp = *s++;
1829 retval = atol((char *)s);
1830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001834 if (use_sandbox)
1835 --sandbox;
1836 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 return retval;
1839}
1840#endif
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let" list all variable values
1844 * ":let var1 var2" list variable values
1845 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 * ":let var += expr" assignment command.
1847 * ":let var -= expr" assignment command.
1848 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 void
1852ex_let(eap)
1853 exarg_T *eap;
1854{
1855 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 int var_count = 0;
1860 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001862 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaardb552d602006-03-23 22:59:57 +00001865 argend = skip_var_list(arg, &var_count, &semicolon);
1866 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001868 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1869 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001870 expr = skipwhite(argend);
1871 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1872 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001874 /*
1875 * ":let" without "=": list variables
1876 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 if (*arg == '[')
1878 EMSG(_(e_invarg));
1879 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_glob_vars(&first);
1886 list_buf_vars(&first);
1887 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001888#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001889 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001890#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001891 list_script_vars(&first);
1892 list_func_vars(&first);
1893 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 eap->nextcmd = check_nextcmd(arg);
1896 }
1897 else
1898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op[0] = '=';
1900 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001903 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1904 op[0] = *expr; /* +=, -= or .= */
1905 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001907 else
1908 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (eap->skip)
1911 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (eap->skip)
1914 {
1915 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 --emsg_skip;
1918 }
1919 else if (i != FAIL)
1920 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 }
1926}
1927
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928/*
1929 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1930 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 * When "nextchars" is not NULL it points to a string with characters that
1932 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1933 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 * Returns OK or FAIL;
1935 */
1936 static int
1937ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1938 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 int copy; /* copy values from "tv", don't move */
1941 int semicolon; /* from skip_var_list() */
1942 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944{
1945 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 listitem_T *item;
1949 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950
1951 if (*arg != '[')
1952 {
1953 /*
1954 * ":let var = expr" or ":for var in list"
1955 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 return OK;
1959 }
1960
1961 /*
1962 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1963 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001964 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 {
1966 EMSG(_(e_listreq));
1967 return FAIL;
1968 }
1969
1970 i = list_len(l);
1971 if (semicolon == 0 && var_count < i)
1972 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001973 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 }
1976 if (var_count - semicolon > i)
1977 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001978 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 return FAIL;
1980 }
1981
1982 item = l->lv_first;
1983 while (*arg != ']')
1984 {
1985 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 item = item->li_next;
1988 if (arg == NULL)
1989 return FAIL;
1990
1991 arg = skipwhite(arg);
1992 if (*arg == ';')
1993 {
1994 /* Put the rest of the list (may be empty) in the var after ';'.
1995 * Create a new list for this. */
1996 l = list_alloc();
1997 if (l == NULL)
1998 return FAIL;
1999 while (item != NULL)
2000 {
2001 list_append_tv(l, &item->li_tv);
2002 item = item->li_next;
2003 }
2004
2005 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002006 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 ltv.vval.v_list = l;
2008 l->lv_refcount = 1;
2009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2011 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 clear_tv(&ltv);
2013 if (arg == NULL)
2014 return FAIL;
2015 break;
2016 }
2017 else if (*arg != ',' && *arg != ']')
2018 {
2019 EMSG2(_(e_intern2), "ex_let_vars()");
2020 return FAIL;
2021 }
2022 }
2023
2024 return OK;
2025}
2026
2027/*
2028 * Skip over assignable variable "var" or list of variables "[var, var]".
2029 * Used for ":let varvar = expr" and ":for varvar in expr".
2030 * For "[var, var]" increment "*var_count" for each variable.
2031 * for "[var, var; var]" set "semicolon".
2032 * Return NULL for an error.
2033 */
2034 static char_u *
2035skip_var_list(arg, var_count, semicolon)
2036 char_u *arg;
2037 int *var_count;
2038 int *semicolon;
2039{
2040 char_u *p, *s;
2041
2042 if (*arg == '[')
2043 {
2044 /* "[var, var]": find the matching ']'. */
2045 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002046 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 {
2048 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2049 s = skip_var_one(p);
2050 if (s == p)
2051 {
2052 EMSG2(_(e_invarg2), p);
2053 return NULL;
2054 }
2055 ++*var_count;
2056
2057 p = skipwhite(s);
2058 if (*p == ']')
2059 break;
2060 else if (*p == ';')
2061 {
2062 if (*semicolon == 1)
2063 {
2064 EMSG(_("Double ; in list of variables"));
2065 return NULL;
2066 }
2067 *semicolon = 1;
2068 }
2069 else if (*p != ',')
2070 {
2071 EMSG2(_(e_invarg2), p);
2072 return NULL;
2073 }
2074 }
2075 return p + 1;
2076 }
2077 else
2078 return skip_var_one(arg);
2079}
2080
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002081/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002082 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 static char_u *
2086skip_var_one(arg)
2087 char_u *arg;
2088{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002089 if (*arg == '@' && arg[1] != NUL)
2090 return arg + 2;
2091 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2092 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093}
2094
Bram Moolenaara7043832005-01-21 11:56:39 +00002095/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002096 * List variables for hashtab "ht" with prefix "prefix".
2097 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105{
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 hashitem_T *hi;
2107 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 int todo;
2109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002110 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2112 {
2113 if (!HASHITEM_EMPTY(hi))
2114 {
2115 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 di = HI2DI(hi);
2117 if (empty || di->di_tv.v_type != VAR_STRING
2118 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
2121 }
2122}
2123
2124/*
2125 * List global variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_glob_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
2134/*
2135 * List buffer variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_buf_vars(first)
2139 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 char_u numbuf[NUMBUFLEN];
2142
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145
2146 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2148 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List window variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_win_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002158 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002160}
2161
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162#ifdef FEAT_WINDOWS
2163/*
2164 * List tab page variables.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_tab_vars(first)
2168 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002169{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002170 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172}
2173#endif
2174
Bram Moolenaara7043832005-01-21 11:56:39 +00002175/*
2176 * List Vim variables.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_vim_vars(first)
2180 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183}
2184
2185/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186 * List script-local variables, if there is a script.
2187 */
2188 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_script_vars(first)
2190 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002191{
2192 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2194 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195}
2196
2197/*
2198 * List function variables, if there is a function.
2199 */
2200 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201list_func_vars(first)
2202 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203{
2204 if (current_funccal != NULL)
2205 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207}
2208
2209/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 * List variables in "arg".
2211 */
2212 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 exarg_T *eap;
2215 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217{
2218 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 char_u *name_start;
2222 char_u *arg_subsc;
2223 char_u *tofree;
2224 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225
2226 while (!ends_excmd(*arg) && !got_int)
2227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002230 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2232 {
2233 emsg_severe = TRUE;
2234 EMSG(_(e_trailing));
2235 break;
2236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* get_name_len() takes care of expanding curly braces */
2241 name_start = name = arg;
2242 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2243 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 /* This is mainly to keep test 49 working: when expanding
2246 * curly braces fails overrule the exception error message. */
2247 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 emsg_severe = TRUE;
2250 EMSG2(_(e_invarg2), arg);
2251 break;
2252 }
2253 error = TRUE;
2254 }
2255 else
2256 {
2257 if (tofree != NULL)
2258 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002259 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
2262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* handle d.key, l[idx], f(expr) */
2264 arg_subsc = arg;
2265 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'g': list_glob_vars(first); break;
2274 case 'b': list_buf_vars(first); break;
2275 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002278#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 case 'v': list_vim_vars(first); break;
2280 case 's': list_script_vars(first); break;
2281 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 default:
2283 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
2286 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 {
2288 char_u numbuf[NUMBUFLEN];
2289 char_u *tf;
2290 int c;
2291 char_u *s;
2292
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002293 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 c = *arg;
2295 *arg = NUL;
2296 list_one_var_a((char_u *)"",
2297 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 tv.v_type,
2299 s == NULL ? (char_u *)"" : s,
2300 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 *arg = c;
2302 vim_free(tf);
2303 }
2304 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 }
2307 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308
2309 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311
2312 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314
2315 return arg;
2316}
2317
2318/*
2319 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2320 * Returns a pointer to the char just after the var name.
2321 * Returns NULL if there is an error.
2322 */
2323 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330{
2331 int c1;
2332 char_u *name;
2333 char_u *p;
2334 char_u *arg_end = NULL;
2335 int len;
2336 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338
2339 /*
2340 * ":let $VAR = expr": Set environment variable.
2341 */
2342 if (*arg == '$')
2343 {
2344 /* Find the end of the name. */
2345 ++arg;
2346 name = arg;
2347 len = get_env_len(&arg);
2348 if (len == 0)
2349 EMSG2(_(e_invarg2), name - 1);
2350 else
2351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 if (op != NULL && (*op == '+' || *op == '-'))
2353 EMSG2(_(e_letwrong), op);
2354 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002355 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002357 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 {
2359 c1 = name[len];
2360 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 p = get_tv_string_chk(tv);
2362 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 {
2364 int mustfree = FALSE;
2365 char_u *s = vim_getenv(name, &mustfree);
2366
2367 if (s != NULL)
2368 {
2369 p = tofree = concat_str(s, p);
2370 if (mustfree)
2371 vim_free(s);
2372 }
2373 }
2374 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 if (STRICMP(name, "HOME") == 0)
2378 init_homedir();
2379 else if (didset_vim && STRICMP(name, "VIM") == 0)
2380 didset_vim = FALSE;
2381 else if (didset_vimruntime
2382 && STRICMP(name, "VIMRUNTIME") == 0)
2383 didset_vimruntime = FALSE;
2384 arg_end = arg;
2385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 }
2389 }
2390 }
2391
2392 /*
2393 * ":let &option = expr": Set option value.
2394 * ":let &l:option = expr": Set local option value.
2395 * ":let &g:option = expr": Set global option value.
2396 */
2397 else if (*arg == '&')
2398 {
2399 /* Find the end of the name. */
2400 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002401 if (p == NULL || (endchars != NULL
2402 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 EMSG(_(e_letunexp));
2404 else
2405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 long n;
2407 int opt_type;
2408 long numval;
2409 char_u *stringval = NULL;
2410 char_u *s;
2411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 c1 = *p;
2413 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414
2415 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 s = get_tv_string_chk(tv); /* != NULL if number or string */
2417 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 {
2419 opt_type = get_option_value(arg, &numval,
2420 &stringval, opt_flags);
2421 if ((opt_type == 1 && *op == '.')
2422 || (opt_type == 0 && *op != '.'))
2423 EMSG2(_(e_letwrong), op);
2424 else
2425 {
2426 if (opt_type == 1) /* number */
2427 {
2428 if (*op == '+')
2429 n = numval + n;
2430 else
2431 n = numval - n;
2432 }
2433 else if (opt_type == 0 && stringval != NULL) /* string */
2434 {
2435 s = concat_str(stringval, s);
2436 vim_free(stringval);
2437 stringval = s;
2438 }
2439 }
2440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 if (s != NULL)
2442 {
2443 set_option_value(arg, n, s, opt_flags);
2444 arg_end = p;
2445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
2449 }
2450
2451 /*
2452 * ":let @r = expr": Set register contents.
2453 */
2454 else if (*arg == '@')
2455 {
2456 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 if (op != NULL && (*op == '+' || *op == '-'))
2458 EMSG2(_(e_letwrong), op);
2459 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002460 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 EMSG(_(e_letunexp));
2462 else
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 char_u *s;
2466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 p = get_tv_string_chk(tv);
2468 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002470 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (s != NULL)
2472 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 vim_free(s);
2475 }
2476 }
2477 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 arg_end = arg + 1;
2481 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484 }
2485
2486 /*
2487 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002490 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002492 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002494 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2498 EMSG(_(e_letunexp));
2499 else
2500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 arg_end = p;
2503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 }
2507
2508 else
2509 EMSG2(_(e_invarg2), arg);
2510
2511 return arg_end;
2512}
2513
2514/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2516 */
2517 static int
2518check_changedtick(arg)
2519 char_u *arg;
2520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 typval_T *rettv;
2551 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 int unlet;
2553 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002554 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
2810 else if (var_check_ro(lp->ll_di->di_flags, name))
2811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
2893clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895{
2896 vim_free(lp->ll_exp_name);
2897 vim_free(lp->ll_newkey);
2898}
2899
2900/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 listitem_T *ri;
2915 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916
2917 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 cc = *endp;
2922 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 if (op != NULL && *op != '=')
2924 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002929 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
2931 if (tv_op(&tv, rettv, op) == OK)
2932 set_var(lp->ll_name, &tv, FALSE);
2933 clear_tv(&tv);
2934 }
2935 }
2936 else
2937 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002941 else if (tv_check_lock(lp->ll_newkey == NULL
2942 ? lp->ll_tv->v_lock
2943 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2944 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 else if (lp->ll_range)
2946 {
2947 /*
2948 * Assign the List values to the list items.
2949 */
2950 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 if (op != NULL && *op != '=')
2953 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2954 else
2955 {
2956 clear_tv(&lp->ll_li->li_tv);
2957 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 ri = ri->li_next;
2960 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2961 break;
2962 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002965 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002966 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 ri = NULL;
2968 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 lp->ll_li = lp->ll_li->li_next;
2972 ++lp->ll_n1;
2973 }
2974 if (ri != NULL)
2975 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 else if (lp->ll_empty2
2977 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 : lp->ll_n1 != lp->ll_n2)
2979 EMSG(_("E711: List value has not enough items"));
2980 }
2981 else
2982 {
2983 /*
2984 * Assign to a List or Dictionary item.
2985 */
2986 if (lp->ll_newkey != NULL)
2987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 if (op != NULL && *op != '=')
2989 {
2990 EMSG2(_(e_letwrong), op);
2991 return;
2992 }
2993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002995 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 if (di == NULL)
2997 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002998 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2999 {
3000 vim_free(di);
3001 return;
3002 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 else if (op != NULL && *op != '=')
3006 {
3007 tv_op(lp->ll_tv, rettv, op);
3008 return;
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003012
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /*
3014 * Assign the value to the variable or list item.
3015 */
3016 if (copy)
3017 copy_tv(rettv, lp->ll_tv);
3018 else
3019 {
3020 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003021 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003023 }
3024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003025}
3026
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3029 * Returns OK or FAIL.
3030 */
3031 static int
3032tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003033 typval_T *tv1;
3034 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 char_u *op;
3036{
3037 long n;
3038 char_u numbuf[NUMBUFLEN];
3039 char_u *s;
3040
3041 /* Can't do anything with a Funcref or a Dict on the right. */
3042 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3043 {
3044 switch (tv1->v_type)
3045 {
3046 case VAR_DICT:
3047 case VAR_FUNC:
3048 break;
3049
3050 case VAR_LIST:
3051 if (*op != '+' || tv2->v_type != VAR_LIST)
3052 break;
3053 /* List += List */
3054 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3055 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3056 return OK;
3057
3058 case VAR_NUMBER:
3059 case VAR_STRING:
3060 if (tv2->v_type == VAR_LIST)
3061 break;
3062 if (*op == '+' || *op == '-')
3063 {
3064 /* nr += nr or nr -= nr*/
3065 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066#ifdef FEAT_FLOAT
3067 if (tv2->v_type == VAR_FLOAT)
3068 {
3069 float_T f = n;
3070
3071 if (*op == '+')
3072 f += tv2->vval.v_float;
3073 else
3074 f -= tv2->vval.v_float;
3075 clear_tv(tv1);
3076 tv1->v_type = VAR_FLOAT;
3077 tv1->vval.v_float = f;
3078 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003080#endif
3081 {
3082 if (*op == '+')
3083 n += get_tv_number(tv2);
3084 else
3085 n -= get_tv_number(tv2);
3086 clear_tv(tv1);
3087 tv1->v_type = VAR_NUMBER;
3088 tv1->vval.v_number = n;
3089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 }
3091 else
3092 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003093 if (tv2->v_type == VAR_FLOAT)
3094 break;
3095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 /* str .= str */
3097 s = get_tv_string(tv1);
3098 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3099 clear_tv(tv1);
3100 tv1->v_type = VAR_STRING;
3101 tv1->vval.v_string = s;
3102 }
3103 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003104
3105#ifdef FEAT_FLOAT
3106 case VAR_FLOAT:
3107 {
3108 float_T f;
3109
3110 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3111 && tv2->v_type != VAR_NUMBER
3112 && tv2->v_type != VAR_STRING))
3113 break;
3114 if (tv2->v_type == VAR_FLOAT)
3115 f = tv2->vval.v_float;
3116 else
3117 f = get_tv_number(tv2);
3118 if (*op == '+')
3119 tv1->vval.v_float += f;
3120 else
3121 tv1->vval.v_float -= f;
3122 }
3123 return OK;
3124#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 }
3126 }
3127
3128 EMSG2(_(e_letwrong), op);
3129 return FAIL;
3130}
3131
3132/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 * Add a watcher to a list.
3134 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003135 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 list_T *l;
3138 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139{
3140 lw->lw_next = l->lv_watch;
3141 l->lv_watch = lw;
3142}
3143
3144/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003145 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 * No warning when it isn't found...
3147 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003148 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 list_T *l;
3151 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152{
Bram Moolenaar33570922005-01-25 22:26:29 +00003153 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154
3155 lwp = &l->lv_watch;
3156 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3157 {
3158 if (lw == lwrem)
3159 {
3160 *lwp = lw->lw_next;
3161 break;
3162 }
3163 lwp = &lw->lw_next;
3164 }
3165}
3166
3167/*
3168 * Just before removing an item from a list: advance watchers to the next
3169 * item.
3170 */
3171 static void
3172list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 list_T *l;
3174 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175{
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177
3178 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3179 if (lw->lw_item == item)
3180 lw->lw_item = item->li_next;
3181}
3182
3183/*
3184 * Evaluate the expression used in a ":for var in expr" command.
3185 * "arg" points to "var".
3186 * Set "*errp" to TRUE for an error, FALSE otherwise;
3187 * Return a pointer that holds the info. Null when there is an error.
3188 */
3189 void *
3190eval_for_line(arg, errp, nextcmdp, skip)
3191 char_u *arg;
3192 int *errp;
3193 char_u **nextcmdp;
3194 int skip;
3195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 typval_T tv;
3199 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 *errp = TRUE; /* default: there is an error */
3202
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 if (fi == NULL)
3205 return NULL;
3206
3207 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3208 if (expr == NULL)
3209 return fi;
3210
3211 expr = skipwhite(expr);
3212 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3213 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003214 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 return fi;
3216 }
3217
3218 if (skip)
3219 ++emsg_skip;
3220 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3221 {
3222 *errp = FALSE;
3223 if (!skip)
3224 {
3225 l = tv.vval.v_list;
3226 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003229 clear_tv(&tv);
3230 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231 else
3232 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003233 /* No need to increment the refcount, it's already set for the
3234 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 fi->fi_list = l;
3236 list_add_watch(l, &fi->fi_lw);
3237 fi->fi_lw.lw_item = l->lv_first;
3238 }
3239 }
3240 }
3241 if (skip)
3242 --emsg_skip;
3243
3244 return fi;
3245}
3246
3247/*
3248 * Use the first item in a ":for" list. Advance to the next.
3249 * Assign the values to the variable (list). "arg" points to the first one.
3250 * Return TRUE when a valid item was found, FALSE when at end of list or
3251 * something wrong.
3252 */
3253 int
3254next_for_item(fi_void, arg)
3255 void *fi_void;
3256 char_u *arg;
3257{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003258 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261
3262 item = fi->fi_lw.lw_item;
3263 if (item == NULL)
3264 result = FALSE;
3265 else
3266 {
3267 fi->fi_lw.lw_item = item->li_next;
3268 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3269 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3270 }
3271 return result;
3272}
3273
3274/*
3275 * Free the structure used to store info used by ":for".
3276 */
3277 void
3278free_for_info(fi_void)
3279 void *fi_void;
3280{
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003283 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003284 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003286 list_unref(fi->fi_list);
3287 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 vim_free(fi);
3289}
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3292
3293 void
3294set_context_for_expression(xp, arg, cmdidx)
3295 expand_T *xp;
3296 char_u *arg;
3297 cmdidx_T cmdidx;
3298{
3299 int got_eq = FALSE;
3300 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 if (cmdidx == CMD_let)
3304 {
3305 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003306 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 {
3308 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003309 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310 {
3311 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313 if (vim_iswhite(*p))
3314 break;
3315 }
3316 return;
3317 }
3318 }
3319 else
3320 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3321 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 while ((xp->xp_pattern = vim_strpbrk(arg,
3323 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3324 {
3325 c = *xp->xp_pattern;
3326 if (c == '&')
3327 {
3328 c = xp->xp_pattern[1];
3329 if (c == '&')
3330 {
3331 ++xp->xp_pattern;
3332 xp->xp_context = cmdidx != CMD_let || got_eq
3333 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3334 }
3335 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003338 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3339 xp->xp_pattern += 2;
3340
3341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 else if (c == '$')
3344 {
3345 /* environment variable */
3346 xp->xp_context = EXPAND_ENV_VARS;
3347 }
3348 else if (c == '=')
3349 {
3350 got_eq = TRUE;
3351 xp->xp_context = EXPAND_EXPRESSION;
3352 }
3353 else if (c == '<'
3354 && xp->xp_context == EXPAND_FUNCTIONS
3355 && vim_strchr(xp->xp_pattern, '(') == NULL)
3356 {
3357 /* Function name can start with "<SNR>" */
3358 break;
3359 }
3360 else if (cmdidx != CMD_let || got_eq)
3361 {
3362 if (c == '"') /* string */
3363 {
3364 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3365 if (c == '\\' && xp->xp_pattern[1] != NUL)
3366 ++xp->xp_pattern;
3367 xp->xp_context = EXPAND_NOTHING;
3368 }
3369 else if (c == '\'') /* literal string */
3370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3373 /* skip */ ;
3374 xp->xp_context = EXPAND_NOTHING;
3375 }
3376 else if (c == '|')
3377 {
3378 if (xp->xp_pattern[1] == '|')
3379 {
3380 ++xp->xp_pattern;
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
3384 xp->xp_context = EXPAND_COMMANDS;
3385 }
3386 else
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
3389 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 /* Doesn't look like something valid, expand as an expression
3391 * anyway. */
3392 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 arg = xp->xp_pattern;
3394 if (*arg != NUL)
3395 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3396 /* skip */ ;
3397 }
3398 xp->xp_pattern = arg;
3399}
3400
3401#endif /* FEAT_CMDL_COMPL */
3402
3403/*
3404 * ":1,25call func(arg1, arg2)" function call.
3405 */
3406 void
3407ex_call(eap)
3408 exarg_T *eap;
3409{
3410 char_u *arg = eap->arg;
3411 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003415 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 linenr_T lnum;
3417 int doesrange;
3418 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003419 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003421 if (eap->skip)
3422 {
3423 /* trans_function_name() doesn't work well when skipping, use eval0()
3424 * instead to skip to any following command, e.g. for:
3425 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003426 ++emsg_skip;
3427 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3428 clear_tv(&rettv);
3429 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003430 return;
3431 }
3432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003433 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003434 if (fudi.fd_newkey != NULL)
3435 {
3436 /* Still need to give an error message for missing key. */
3437 EMSG2(_(e_dictkey), fudi.fd_newkey);
3438 vim_free(fudi.fd_newkey);
3439 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003440 if (tofree == NULL)
3441 return;
3442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003443 /* Increase refcount on dictionary, it could get deleted when evaluating
3444 * the arguments. */
3445 if (fudi.fd_dict != NULL)
3446 ++fudi.fd_dict->dv_refcount;
3447
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003450 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar532c7802005-01-27 14:44:31 +00003452 /* Skip white space to allow ":call func ()". Not good, but required for
3453 * backward compatibility. */
3454 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 if (*startarg != '(')
3458 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003459 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 goto end;
3461 }
3462
3463 /*
3464 * When skipping, evaluate the function once, to find the end of the
3465 * arguments.
3466 * When the function takes a range, this is discovered after the first
3467 * call, and the loop is broken.
3468 */
3469 if (eap->skip)
3470 {
3471 ++emsg_skip;
3472 lnum = eap->line2; /* do it once, also with an invalid range */
3473 }
3474 else
3475 lnum = eap->line1;
3476 for ( ; lnum <= eap->line2; ++lnum)
3477 {
3478 if (!eap->skip && eap->addr_count > 0)
3479 {
3480 curwin->w_cursor.lnum = lnum;
3481 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003482#ifdef FEAT_VIRTUALEDIT
3483 curwin->w_cursor.coladd = 0;
3484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 eap->line1, eap->line2, &doesrange,
3489 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 failed = TRUE;
3492 break;
3493 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003494
3495 /* Handle a function returning a Funcref, Dictionary or List. */
3496 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3497 {
3498 failed = TRUE;
3499 break;
3500 }
3501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (doesrange || eap->skip)
3504 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003507 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003508 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (aborting())
3511 break;
3512 }
3513 if (eap->skip)
3514 --emsg_skip;
3515
3516 if (!failed)
3517 {
3518 /* Check for trailing illegal characters and a following command. */
3519 if (!ends_excmd(*arg))
3520 {
3521 emsg_severe = TRUE;
3522 EMSG(_(e_trailing));
3523 }
3524 else
3525 eap->nextcmd = check_nextcmd(arg);
3526 }
3527
3528end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003530 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531}
3532
3533/*
3534 * ":unlet[!] var1 ... " command.
3535 */
3536 void
3537ex_unlet(eap)
3538 exarg_T *eap;
3539{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003540 ex_unletlock(eap, eap->arg, 0);
3541}
3542
3543/*
3544 * ":lockvar" and ":unlockvar" commands
3545 */
3546 void
3547ex_lockvar(eap)
3548 exarg_T *eap;
3549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003551 int deep = 2;
3552
3553 if (eap->forceit)
3554 deep = -1;
3555 else if (vim_isdigit(*arg))
3556 {
3557 deep = getdigits(&arg);
3558 arg = skipwhite(arg);
3559 }
3560
3561 ex_unletlock(eap, arg, deep);
3562}
3563
3564/*
3565 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3566 */
3567 static void
3568ex_unletlock(eap, argstart, deep)
3569 exarg_T *eap;
3570 char_u *argstart;
3571 int deep;
3572{
3573 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 do
3579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003580 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003581 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003582 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 if (lv.ll_name == NULL)
3584 error = TRUE; /* error but continue parsing */
3585 if (name_end == NULL || (!vim_iswhite(*name_end)
3586 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 if (name_end != NULL)
3589 {
3590 emsg_severe = TRUE;
3591 EMSG(_(e_trailing));
3592 }
3593 if (!(eap->skip || error))
3594 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 break;
3596 }
3597
3598 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 {
3600 if (eap->cmdidx == CMD_unlet)
3601 {
3602 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3603 error = TRUE;
3604 }
3605 else
3606 {
3607 if (do_lock_var(&lv, name_end, deep,
3608 eap->cmdidx == CMD_lockvar) == FAIL)
3609 error = TRUE;
3610 }
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 if (!eap->skip)
3614 clear_lval(&lv);
3615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 arg = skipwhite(name_end);
3617 } while (!ends_excmd(*arg));
3618
3619 eap->nextcmd = check_nextcmd(arg);
3620}
3621
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 int forceit;
3627{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 int ret = OK;
3629 int cc;
3630
3631 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 cc = *name_end;
3634 *name_end = NUL;
3635
3636 /* Normal name or expanded name. */
3637 if (check_changedtick(lp->ll_name))
3638 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3644 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 else if (lp->ll_range)
3646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648
3649 /* Delete a range of List items. */
3650 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3651 {
3652 li = lp->ll_li->li_next;
3653 listitem_remove(lp->ll_list, lp->ll_li);
3654 lp->ll_li = li;
3655 ++lp->ll_n1;
3656 }
3657 }
3658 else
3659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 /* unlet a List item. */
3662 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003665 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 }
3667
3668 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669}
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671/*
3672 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 hashtab_T *ht;
3681 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003683 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 ht = find_var_ht(name, &varname);
3686 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 hi = hash_find(ht, varname);
3689 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003690 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003691 di = HI2DI(hi);
3692 if (var_check_fixed(di->di_flags, name)
3693 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003694 return FAIL;
3695 delete_var(ht, hi);
3696 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699 if (forceit)
3700 return OK;
3701 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 return FAIL;
3703}
3704
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705/*
3706 * Lock or unlock variable indicated by "lp".
3707 * "deep" is the levels to go (-1 for unlimited);
3708 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3709 */
3710 static int
3711do_lock_var(lp, name_end, deep, lock)
3712 lval_T *lp;
3713 char_u *name_end;
3714 int deep;
3715 int lock;
3716{
3717 int ret = OK;
3718 int cc;
3719 dictitem_T *di;
3720
3721 if (deep == 0) /* nothing to do */
3722 return OK;
3723
3724 if (lp->ll_tv == NULL)
3725 {
3726 cc = *name_end;
3727 *name_end = NUL;
3728
3729 /* Normal name or expanded name. */
3730 if (check_changedtick(lp->ll_name))
3731 ret = FAIL;
3732 else
3733 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003734 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 if (di == NULL)
3736 ret = FAIL;
3737 else
3738 {
3739 if (lock)
3740 di->di_flags |= DI_FLAGS_LOCK;
3741 else
3742 di->di_flags &= ~DI_FLAGS_LOCK;
3743 item_lock(&di->di_tv, deep, lock);
3744 }
3745 }
3746 *name_end = cc;
3747 }
3748 else if (lp->ll_range)
3749 {
3750 listitem_T *li = lp->ll_li;
3751
3752 /* (un)lock a range of List items. */
3753 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3754 {
3755 item_lock(&li->li_tv, deep, lock);
3756 li = li->li_next;
3757 ++lp->ll_n1;
3758 }
3759 }
3760 else if (lp->ll_list != NULL)
3761 /* (un)lock a List item. */
3762 item_lock(&lp->ll_li->li_tv, deep, lock);
3763 else
3764 /* un(lock) a Dictionary item. */
3765 item_lock(&lp->ll_di->di_tv, deep, lock);
3766
3767 return ret;
3768}
3769
3770/*
3771 * Lock or unlock an item. "deep" is nr of levels to go.
3772 */
3773 static void
3774item_lock(tv, deep, lock)
3775 typval_T *tv;
3776 int deep;
3777 int lock;
3778{
3779 static int recurse = 0;
3780 list_T *l;
3781 listitem_T *li;
3782 dict_T *d;
3783 hashitem_T *hi;
3784 int todo;
3785
3786 if (recurse >= DICT_MAXNEST)
3787 {
3788 EMSG(_("E743: variable nested too deep for (un)lock"));
3789 return;
3790 }
3791 if (deep == 0)
3792 return;
3793 ++recurse;
3794
3795 /* lock/unlock the item itself */
3796 if (lock)
3797 tv->v_lock |= VAR_LOCKED;
3798 else
3799 tv->v_lock &= ~VAR_LOCKED;
3800
3801 switch (tv->v_type)
3802 {
3803 case VAR_LIST:
3804 if ((l = tv->vval.v_list) != NULL)
3805 {
3806 if (lock)
3807 l->lv_lock |= VAR_LOCKED;
3808 else
3809 l->lv_lock &= ~VAR_LOCKED;
3810 if (deep < 0 || deep > 1)
3811 /* recursive: lock/unlock the items the List contains */
3812 for (li = l->lv_first; li != NULL; li = li->li_next)
3813 item_lock(&li->li_tv, deep - 1, lock);
3814 }
3815 break;
3816 case VAR_DICT:
3817 if ((d = tv->vval.v_dict) != NULL)
3818 {
3819 if (lock)
3820 d->dv_lock |= VAR_LOCKED;
3821 else
3822 d->dv_lock &= ~VAR_LOCKED;
3823 if (deep < 0 || deep > 1)
3824 {
3825 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003826 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3828 {
3829 if (!HASHITEM_EMPTY(hi))
3830 {
3831 --todo;
3832 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3833 }
3834 }
3835 }
3836 }
3837 }
3838 --recurse;
3839}
3840
Bram Moolenaara40058a2005-07-11 22:42:07 +00003841/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003842 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3843 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003844 */
3845 static int
3846tv_islocked(tv)
3847 typval_T *tv;
3848{
3849 return (tv->v_lock & VAR_LOCKED)
3850 || (tv->v_type == VAR_LIST
3851 && tv->vval.v_list != NULL
3852 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3853 || (tv->v_type == VAR_DICT
3854 && tv->vval.v_dict != NULL
3855 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3856}
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3859/*
3860 * Delete all "menutrans_" variables.
3861 */
3862 void
3863del_menutrans_vars()
3864{
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar33570922005-01-25 22:26:29 +00003868 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003869 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 {
3872 if (!HASHITEM_EMPTY(hi))
3873 {
3874 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3876 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003877 }
3878 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003879 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880}
3881#endif
3882
3883#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3884
3885/*
3886 * Local string buffer for the next two functions to store a variable name
3887 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3888 * get_user_var_name().
3889 */
3890
3891static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3892
3893static char_u *varnamebuf = NULL;
3894static int varnamebuflen = 0;
3895
3896/*
3897 * Function to concatenate a prefix and a variable name.
3898 */
3899 static char_u *
3900cat_prefix_varname(prefix, name)
3901 int prefix;
3902 char_u *name;
3903{
3904 int len;
3905
3906 len = (int)STRLEN(name) + 3;
3907 if (len > varnamebuflen)
3908 {
3909 vim_free(varnamebuf);
3910 len += 10; /* some additional space */
3911 varnamebuf = alloc(len);
3912 if (varnamebuf == NULL)
3913 {
3914 varnamebuflen = 0;
3915 return NULL;
3916 }
3917 varnamebuflen = len;
3918 }
3919 *varnamebuf = prefix;
3920 varnamebuf[1] = ':';
3921 STRCPY(varnamebuf + 2, name);
3922 return varnamebuf;
3923}
3924
3925/*
3926 * Function given to ExpandGeneric() to obtain the list of user defined
3927 * (global/buffer/window/built-in) variable names.
3928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 char_u *
3930get_user_var_name(xp, idx)
3931 expand_T *xp;
3932 int idx;
3933{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static long_u gdone;
3935 static long_u bdone;
3936 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003937#ifdef FEAT_WINDOWS
3938 static long_u tdone;
3939#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003940 static int vidx;
3941 static hashitem_T *hi;
3942 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003947#ifdef FEAT_WINDOWS
3948 tdone = 0;
3949#endif
3950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951
3952 /* Global variables */
3953 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3962 return cat_prefix_varname('g', hi->hi_key);
3963 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003965
3966 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003967 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003970 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003972 else
3973 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 while (HASHITEM_EMPTY(hi))
3975 ++hi;
3976 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003980 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return (char_u *)"b:changedtick";
3982 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003983
3984 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003985 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003988 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003990 else
3991 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 while (HASHITEM_EMPTY(hi))
3993 ++hi;
3994 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003996
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003997#ifdef FEAT_WINDOWS
3998 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003999 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000 if (tdone < ht->ht_used)
4001 {
4002 if (tdone++ == 0)
4003 hi = ht->ht_array;
4004 else
4005 ++hi;
4006 while (HASHITEM_EMPTY(hi))
4007 ++hi;
4008 return cat_prefix_varname('t', hi->hi_key);
4009 }
4010#endif
4011
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 /* v: variables */
4013 if (vidx < VV_LEN)
4014 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 vim_free(varnamebuf);
4017 varnamebuf = NULL;
4018 varnamebuflen = 0;
4019 return NULL;
4020}
4021
4022#endif /* FEAT_CMDL_COMPL */
4023
4024/*
4025 * types for expressions.
4026 */
4027typedef enum
4028{
4029 TYPE_UNKNOWN = 0
4030 , TYPE_EQUAL /* == */
4031 , TYPE_NEQUAL /* != */
4032 , TYPE_GREATER /* > */
4033 , TYPE_GEQUAL /* >= */
4034 , TYPE_SMALLER /* < */
4035 , TYPE_SEQUAL /* <= */
4036 , TYPE_MATCH /* =~ */
4037 , TYPE_NOMATCH /* !~ */
4038} exptype_T;
4039
4040/*
4041 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4044 */
4045
4046/*
4047 * Handle zero level expression.
4048 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004050 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 * Return OK or FAIL.
4052 */
4053 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 char_u **nextcmd;
4058 int evaluate;
4059{
4060 int ret;
4061 char_u *p;
4062
4063 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (ret == FAIL || !ends_excmd(*p))
4066 {
4067 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /*
4070 * Report the invalid expression unless the expression evaluation has
4071 * been cancelled due to an aborting error, an interrupt, or an
4072 * exception.
4073 */
4074 if (!aborting())
4075 EMSG2(_(e_invexpr2), arg);
4076 ret = FAIL;
4077 }
4078 if (nextcmd != NULL)
4079 *nextcmd = check_nextcmd(p);
4080
4081 return ret;
4082}
4083
4084/*
4085 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004086 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 *
4088 * "arg" must point to the first non-white of the expression.
4089 * "arg" is advanced to the next non-white after the recognized expression.
4090 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004091 * Note: "rettv.v_lock" is not set.
4092 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * Return OK or FAIL.
4094 */
4095 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 int evaluate;
4100{
4101 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004102 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104 /*
4105 * Get the first variable.
4106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 return FAIL;
4109
4110 if ((*arg)[0] == '?')
4111 {
4112 result = FALSE;
4113 if (evaluate)
4114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 int error = FALSE;
4116
4117 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004120 if (error)
4121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123
4124 /*
4125 * Get the second variable.
4126 */
4127 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return FAIL;
4130
4131 /*
4132 * Check for the ":".
4133 */
4134 if ((*arg)[0] != ':')
4135 {
4136 EMSG(_("E109: Missing ':' after '?'"));
4137 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 return FAIL;
4140 }
4141
4142 /*
4143 * Get the third variable.
4144 */
4145 *arg = skipwhite(*arg + 1);
4146 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4147 {
4148 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 return FAIL;
4151 }
4152 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155
4156 return OK;
4157}
4158
4159/*
4160 * Handle first level expression:
4161 * expr2 || expr2 || expr2 logical OR
4162 *
4163 * "arg" must point to the first non-white of the expression.
4164 * "arg" is advanced to the next non-white after the recognized expression.
4165 *
4166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int evaluate;
4173{
Bram Moolenaar33570922005-01-25 22:26:29 +00004174 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 long result;
4176 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
4179 /*
4180 * Get the first variable.
4181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184
4185 /*
4186 * Repeat until there is no following "||".
4187 */
4188 first = TRUE;
4189 result = FALSE;
4190 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4191 {
4192 if (evaluate && first)
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 if (error)
4198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 first = FALSE;
4200 }
4201
4202 /*
4203 * Get the second variable.
4204 */
4205 *arg = skipwhite(*arg + 2);
4206 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4207 return FAIL;
4208
4209 /*
4210 * Compute the result.
4211 */
4212 if (evaluate && !result)
4213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004214 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004217 if (error)
4218 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 if (evaluate)
4221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 rettv->v_type = VAR_NUMBER;
4223 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 }
4226
4227 return OK;
4228}
4229
4230/*
4231 * Handle second level expression:
4232 * expr3 && expr3 && expr3 logical AND
4233 *
4234 * "arg" must point to the first non-white of the expression.
4235 * "arg" is advanced to the next non-white after the recognized expression.
4236 *
4237 * Return OK or FAIL.
4238 */
4239 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 int evaluate;
4244{
Bram Moolenaar33570922005-01-25 22:26:29 +00004245 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 long result;
4247 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 /*
4251 * Get the first variable.
4252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 return FAIL;
4255
4256 /*
4257 * Repeat until there is no following "&&".
4258 */
4259 first = TRUE;
4260 result = TRUE;
4261 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4262 {
4263 if (evaluate && first)
4264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 first = FALSE;
4271 }
4272
4273 /*
4274 * Get the second variable.
4275 */
4276 *arg = skipwhite(*arg + 2);
4277 if (eval4(arg, &var2, evaluate && result) == FAIL)
4278 return FAIL;
4279
4280 /*
4281 * Compute the result.
4282 */
4283 if (evaluate && result)
4284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (error)
4289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 if (evaluate)
4292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 rettv->v_type = VAR_NUMBER;
4294 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
4297
4298 return OK;
4299}
4300
4301/*
4302 * Handle third level expression:
4303 * var1 == var2
4304 * var1 =~ var2
4305 * var1 != var2
4306 * var1 !~ var2
4307 * var1 > var2
4308 * var1 >= var2
4309 * var1 < var2
4310 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004311 * var1 is var2
4312 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 *
4314 * "arg" must point to the first non-white of the expression.
4315 * "arg" is advanced to the next non-white after the recognized expression.
4316 *
4317 * Return OK or FAIL.
4318 */
4319 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int evaluate;
4324{
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 char_u *p;
4327 int i;
4328 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004329 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 int len = 2;
4331 long n1, n2;
4332 char_u *s1, *s2;
4333 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4334 regmatch_T regmatch;
4335 int ic;
4336 char_u *save_cpo;
4337
4338 /*
4339 * Get the first variable.
4340 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 return FAIL;
4343
4344 p = *arg;
4345 switch (p[0])
4346 {
4347 case '=': if (p[1] == '=')
4348 type = TYPE_EQUAL;
4349 else if (p[1] == '~')
4350 type = TYPE_MATCH;
4351 break;
4352 case '!': if (p[1] == '=')
4353 type = TYPE_NEQUAL;
4354 else if (p[1] == '~')
4355 type = TYPE_NOMATCH;
4356 break;
4357 case '>': if (p[1] != '=')
4358 {
4359 type = TYPE_GREATER;
4360 len = 1;
4361 }
4362 else
4363 type = TYPE_GEQUAL;
4364 break;
4365 case '<': if (p[1] != '=')
4366 {
4367 type = TYPE_SMALLER;
4368 len = 1;
4369 }
4370 else
4371 type = TYPE_SEQUAL;
4372 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 case 'i': if (p[1] == 's')
4374 {
4375 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4376 len = 5;
4377 if (!vim_isIDc(p[len]))
4378 {
4379 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4380 type_is = TRUE;
4381 }
4382 }
4383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385
4386 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 */
4389 if (type != TYPE_UNKNOWN)
4390 {
4391 /* extra question mark appended: ignore case */
4392 if (p[len] == '?')
4393 {
4394 ic = TRUE;
4395 ++len;
4396 }
4397 /* extra '#' appended: match case */
4398 else if (p[len] == '#')
4399 {
4400 ic = FALSE;
4401 ++len;
4402 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004403 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 else
4405 ic = p_ic;
4406
4407 /*
4408 * Get the second variable.
4409 */
4410 *arg = skipwhite(p + len);
4411 if (eval5(arg, &var2, evaluate) == FAIL)
4412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
4415 }
4416
4417 if (evaluate)
4418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 if (type_is && rettv->v_type != var2.v_type)
4420 {
4421 /* For "is" a different type always means FALSE, for "notis"
4422 * it means TRUE. */
4423 n1 = (type == TYPE_NEQUAL);
4424 }
4425 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4426 {
4427 if (type_is)
4428 {
4429 n1 = (rettv->v_type == var2.v_type
4430 && rettv->vval.v_list == var2.vval.v_list);
4431 if (type == TYPE_NEQUAL)
4432 n1 = !n1;
4433 }
4434 else if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004438 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004440 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004448 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4449 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 if (type == TYPE_NEQUAL)
4451 n1 = !n1;
4452 }
4453 }
4454
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004455 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4456 {
4457 if (type_is)
4458 {
4459 n1 = (rettv->v_type == var2.v_type
4460 && rettv->vval.v_dict == var2.vval.v_dict);
4461 if (type == TYPE_NEQUAL)
4462 n1 = !n1;
4463 }
4464 else if (rettv->v_type != var2.v_type
4465 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4466 {
4467 if (rettv->v_type != var2.v_type)
4468 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4469 else
4470 EMSG(_("E736: Invalid operation for Dictionary"));
4471 clear_tv(rettv);
4472 clear_tv(&var2);
4473 return FAIL;
4474 }
4475 else
4476 {
4477 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004478 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4479 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4486 {
4487 if (rettv->v_type != var2.v_type
4488 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4489 {
4490 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004491 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004493 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 clear_tv(rettv);
4495 clear_tv(&var2);
4496 return FAIL;
4497 }
4498 else
4499 {
4500 /* Compare two Funcrefs for being equal or unequal. */
4501 if (rettv->vval.v_string == NULL
4502 || var2.vval.v_string == NULL)
4503 n1 = FALSE;
4504 else
4505 n1 = STRCMP(rettv->vval.v_string,
4506 var2.vval.v_string) == 0;
4507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 }
4511
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512#ifdef FEAT_FLOAT
4513 /*
4514 * If one of the two variables is a float, compare as a float.
4515 * When using "=~" or "!~", always compare as string.
4516 */
4517 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4518 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4519 {
4520 float_T f1, f2;
4521
4522 if (rettv->v_type == VAR_FLOAT)
4523 f1 = rettv->vval.v_float;
4524 else
4525 f1 = get_tv_number(rettv);
4526 if (var2.v_type == VAR_FLOAT)
4527 f2 = var2.vval.v_float;
4528 else
4529 f2 = get_tv_number(&var2);
4530 n1 = FALSE;
4531 switch (type)
4532 {
4533 case TYPE_EQUAL: n1 = (f1 == f2); break;
4534 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4535 case TYPE_GREATER: n1 = (f1 > f2); break;
4536 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4537 case TYPE_SMALLER: n1 = (f1 < f2); break;
4538 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4539 case TYPE_UNKNOWN:
4540 case TYPE_MATCH:
4541 case TYPE_NOMATCH: break; /* avoid gcc warning */
4542 }
4543 }
4544#endif
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /*
4547 * If one of the two variables is a number, compare as a number.
4548 * When using "=~" or "!~", always compare as string.
4549 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004553 n1 = get_tv_number(rettv);
4554 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 switch (type)
4556 {
4557 case TYPE_EQUAL: n1 = (n1 == n2); break;
4558 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4559 case TYPE_GREATER: n1 = (n1 > n2); break;
4560 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4561 case TYPE_SMALLER: n1 = (n1 < n2); break;
4562 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4563 case TYPE_UNKNOWN:
4564 case TYPE_MATCH:
4565 case TYPE_NOMATCH: break; /* avoid gcc warning */
4566 }
4567 }
4568 else
4569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 s1 = get_tv_string_buf(rettv, buf1);
4571 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4574 else
4575 i = 0;
4576 n1 = FALSE;
4577 switch (type)
4578 {
4579 case TYPE_EQUAL: n1 = (i == 0); break;
4580 case TYPE_NEQUAL: n1 = (i != 0); break;
4581 case TYPE_GREATER: n1 = (i > 0); break;
4582 case TYPE_GEQUAL: n1 = (i >= 0); break;
4583 case TYPE_SMALLER: n1 = (i < 0); break;
4584 case TYPE_SEQUAL: n1 = (i <= 0); break;
4585
4586 case TYPE_MATCH:
4587 case TYPE_NOMATCH:
4588 /* avoid 'l' flag in 'cpoptions' */
4589 save_cpo = p_cpo;
4590 p_cpo = (char_u *)"";
4591 regmatch.regprog = vim_regcomp(s2,
4592 RE_MAGIC + RE_STRING);
4593 regmatch.rm_ic = ic;
4594 if (regmatch.regprog != NULL)
4595 {
4596 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004597 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (type == TYPE_NOMATCH)
4599 n1 = !n1;
4600 }
4601 p_cpo = save_cpo;
4602 break;
4603
4604 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4605 }
4606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 clear_tv(rettv);
4608 clear_tv(&var2);
4609 rettv->v_type = VAR_NUMBER;
4610 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612 }
4613
4614 return OK;
4615}
4616
4617/*
4618 * Handle fourth level expression:
4619 * + number addition
4620 * - number subtraction
4621 * . string concatenation
4622 *
4623 * "arg" must point to the first non-white of the expression.
4624 * "arg" is advanced to the next non-white after the recognized expression.
4625 *
4626 * Return OK or FAIL.
4627 */
4628 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 int evaluate;
4633{
Bram Moolenaar33570922005-01-25 22:26:29 +00004634 typval_T var2;
4635 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 int op;
4637 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004638#ifdef FEAT_FLOAT
4639 float_T f1 = 0, f2 = 0;
4640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 char_u *s1, *s2;
4642 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4643 char_u *p;
4644
4645 /*
4646 * Get the first variable.
4647 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004648 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return FAIL;
4650
4651 /*
4652 * Repeat computing, until no '+', '-' or '.' is following.
4653 */
4654 for (;;)
4655 {
4656 op = **arg;
4657 if (op != '+' && op != '-' && op != '.')
4658 break;
4659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660 if ((op != '+' || rettv->v_type != VAR_LIST)
4661#ifdef FEAT_FLOAT
4662 && (op == '.' || rettv->v_type != VAR_FLOAT)
4663#endif
4664 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004665 {
4666 /* For "list + ...", an illegal use of the first operand as
4667 * a number cannot be determined before evaluating the 2nd
4668 * operand: if this is also a list, all is ok.
4669 * For "something . ...", "something - ..." or "non-list + ...",
4670 * we know that the first operand needs to be a string or number
4671 * without evaluating the 2nd operand. So check before to avoid
4672 * side effects after an error. */
4673 if (evaluate && get_tv_string_chk(rettv) == NULL)
4674 {
4675 clear_tv(rettv);
4676 return FAIL;
4677 }
4678 }
4679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 /*
4681 * Get the second variable.
4682 */
4683 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004684 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return FAIL;
4688 }
4689
4690 if (evaluate)
4691 {
4692 /*
4693 * Compute the result.
4694 */
4695 if (op == '.')
4696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004697 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4698 s2 = get_tv_string_buf_chk(&var2, buf2);
4699 if (s2 == NULL) /* type error ? */
4700 {
4701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 return FAIL;
4704 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004705 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004706 clear_tv(rettv);
4707 rettv->v_type = VAR_STRING;
4708 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004710 else if (op == '+' && rettv->v_type == VAR_LIST
4711 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004712 {
4713 /* concatenate Lists */
4714 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4715 &var3) == FAIL)
4716 {
4717 clear_tv(rettv);
4718 clear_tv(&var2);
4719 return FAIL;
4720 }
4721 clear_tv(rettv);
4722 *rettv = var3;
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 else
4725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 int error = FALSE;
4727
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728#ifdef FEAT_FLOAT
4729 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 f1 = rettv->vval.v_float;
4732 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 else
4735#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004736 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737 n1 = get_tv_number_chk(rettv, &error);
4738 if (error)
4739 {
4740 /* This can only happen for "list + non-list". For
4741 * "non-list + ..." or "something - ...", we returned
4742 * before evaluating the 2nd operand. */
4743 clear_tv(rettv);
4744 return FAIL;
4745 }
4746#ifdef FEAT_FLOAT
4747 if (var2.v_type == VAR_FLOAT)
4748 f1 = n1;
4749#endif
4750 }
4751#ifdef FEAT_FLOAT
4752 if (var2.v_type == VAR_FLOAT)
4753 {
4754 f2 = var2.vval.v_float;
4755 n2 = 0;
4756 }
4757 else
4758#endif
4759 {
4760 n2 = get_tv_number_chk(&var2, &error);
4761 if (error)
4762 {
4763 clear_tv(rettv);
4764 clear_tv(&var2);
4765 return FAIL;
4766 }
4767#ifdef FEAT_FLOAT
4768 if (rettv->v_type == VAR_FLOAT)
4769 f2 = n2;
4770#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773
4774#ifdef FEAT_FLOAT
4775 /* If there is a float on either side the result is a float. */
4776 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4777 {
4778 if (op == '+')
4779 f1 = f1 + f2;
4780 else
4781 f1 = f1 - f2;
4782 rettv->v_type = VAR_FLOAT;
4783 rettv->vval.v_float = f1;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#endif
4787 {
4788 if (op == '+')
4789 n1 = n1 + n2;
4790 else
4791 n1 = n1 - n2;
4792 rettv->v_type = VAR_NUMBER;
4793 rettv->vval.v_number = n1;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 }
4799 return OK;
4800}
4801
4802/*
4803 * Handle fifth level expression:
4804 * * number multiplication
4805 * / number division
4806 * % number modulo
4807 *
4808 * "arg" must point to the first non-white of the expression.
4809 * "arg" is advanced to the next non-white after the recognized expression.
4810 *
4811 * Return OK or FAIL.
4812 */
4813 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004814eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004818 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar33570922005-01-25 22:26:29 +00004820 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 int op;
4822 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823#ifdef FEAT_FLOAT
4824 int use_float = FALSE;
4825 float_T f1 = 0, f2;
4826#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 /*
4830 * Get the first variable.
4831 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004832 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834
4835 /*
4836 * Repeat computing, until no '*', '/' or '%' is following.
4837 */
4838 for (;;)
4839 {
4840 op = **arg;
4841 if (op != '*' && op != '/' && op != '%')
4842 break;
4843
4844 if (evaluate)
4845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#ifdef FEAT_FLOAT
4847 if (rettv->v_type == VAR_FLOAT)
4848 {
4849 f1 = rettv->vval.v_float;
4850 use_float = TRUE;
4851 n1 = 0;
4852 }
4853 else
4854#endif
4855 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857 if (error)
4858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 else
4861 n1 = 0;
4862
4863 /*
4864 * Get the second variable.
4865 */
4866 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004867 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 return FAIL;
4869
4870 if (evaluate)
4871 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#ifdef FEAT_FLOAT
4873 if (var2.v_type == VAR_FLOAT)
4874 {
4875 if (!use_float)
4876 {
4877 f1 = n1;
4878 use_float = TRUE;
4879 }
4880 f2 = var2.vval.v_float;
4881 n2 = 0;
4882 }
4883 else
4884#endif
4885 {
4886 n2 = get_tv_number_chk(&var2, &error);
4887 clear_tv(&var2);
4888 if (error)
4889 return FAIL;
4890#ifdef FEAT_FLOAT
4891 if (use_float)
4892 f2 = n2;
4893#endif
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /*
4897 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900#ifdef FEAT_FLOAT
4901 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 if (op == '*')
4904 f1 = f1 * f2;
4905 else if (op == '/')
4906 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907# ifdef VMS
4908 /* VMS crashes on divide by zero, work around it */
4909 if (f2 == 0.0)
4910 {
4911 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004912 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004913 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004914 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004915 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004916 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004917 }
4918 else
4919 f1 = f1 / f2;
4920# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 /* We rely on the floating point library to handle divide
4922 * by zero to result in "inf" and not a crash. */
4923 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004924# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004928 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929 return FAIL;
4930 }
4931 rettv->v_type = VAR_FLOAT;
4932 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937 if (op == '*')
4938 n1 = n1 * n2;
4939 else if (op == '/')
4940 {
4941 if (n2 == 0) /* give an error message? */
4942 {
4943 if (n1 == 0)
4944 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4945 else if (n1 < 0)
4946 n1 = -0x7fffffffL;
4947 else
4948 n1 = 0x7fffffffL;
4949 }
4950 else
4951 n1 = n1 / n2;
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 {
4955 if (n2 == 0) /* give an error message? */
4956 n1 = 0;
4957 else
4958 n1 = n1 % n2;
4959 }
4960 rettv->v_type = VAR_NUMBER;
4961 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 }
4965
4966 return OK;
4967}
4968
4969/*
4970 * Handle sixth level expression:
4971 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004972 * "string" string constant
4973 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 * &option-name option value
4975 * @r register contents
4976 * identifier variable value
4977 * function() function call
4978 * $VAR environment variable
4979 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004980 * [expr, expr] List
4981 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 *
4983 * Also handle:
4984 * ! in front logical NOT
4985 * - in front unary minus
4986 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 * trailing [] subscript in String or List
4988 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 *
4990 * "arg" must point to the first non-white of the expression.
4991 * "arg" is advanced to the next non-white after the recognized expression.
4992 *
4993 * Return OK or FAIL.
4994 */
4995 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004996eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005000 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 long n;
5003 int len;
5004 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 char_u *start_leader, *end_leader;
5006 int ret = OK;
5007 char_u *alias;
5008
5009 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 /*
5016 * Skip '!' and '-' characters. They are handled later.
5017 */
5018 start_leader = *arg;
5019 while (**arg == '!' || **arg == '-' || **arg == '+')
5020 *arg = skipwhite(*arg + 1);
5021 end_leader = *arg;
5022
5023 switch (**arg)
5024 {
5025 /*
5026 * Number constant.
5027 */
5028 case '0':
5029 case '1':
5030 case '2':
5031 case '3':
5032 case '4':
5033 case '5':
5034 case '6':
5035 case '7':
5036 case '8':
5037 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 {
5039#ifdef FEAT_FLOAT
5040 char_u *p = skipdigits(*arg + 1);
5041 int get_float = FALSE;
5042
5043 /* We accept a float when the format matches
5044 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005045 * strict to avoid backwards compatibility problems.
5046 * Don't look for a float after the "." operator, so that
5047 * ":let vers = 1.2.3" doesn't fail. */
5048 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005050 get_float = TRUE;
5051 p = skipdigits(p + 2);
5052 if (*p == 'e' || *p == 'E')
5053 {
5054 ++p;
5055 if (*p == '-' || *p == '+')
5056 ++p;
5057 if (!vim_isdigit(*p))
5058 get_float = FALSE;
5059 else
5060 p = skipdigits(p + 1);
5061 }
5062 if (ASCII_ISALPHA(*p) || *p == '.')
5063 get_float = FALSE;
5064 }
5065 if (get_float)
5066 {
5067 float_T f;
5068
5069 *arg += string2float(*arg, &f);
5070 if (evaluate)
5071 {
5072 rettv->v_type = VAR_FLOAT;
5073 rettv->vval.v_float = f;
5074 }
5075 }
5076 else
5077#endif
5078 {
5079 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5080 *arg += len;
5081 if (evaluate)
5082 {
5083 rettv->v_type = VAR_NUMBER;
5084 rettv->vval.v_number = n;
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090 /*
5091 * String constant: "string".
5092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 break;
5101
5102 /*
5103 * List: [expr, expr]
5104 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 break;
5107
5108 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 * Dictionary: {key: val, key: val}
5110 */
5111 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5112 break;
5113
5114 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005117 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Environment variable: $VAR.
5122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /*
5127 * Register contents: @r.
5128 */
5129 case '@': ++*arg;
5130 if (evaluate)
5131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005133 rettv->vval.v_string = get_reg_contents(**arg,
5134 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 if (**arg != NUL)
5137 ++*arg;
5138 break;
5139
5140 /*
5141 * nested expression: (expression).
5142 */
5143 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (**arg == ')')
5146 ++*arg;
5147 else if (ret == OK)
5148 {
5149 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 ret = FAIL;
5152 }
5153 break;
5154
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158
5159 if (ret == NOTDONE)
5160 {
5161 /*
5162 * Must be a variable or function name.
5163 * Can also be a curly-braces kind of name: {expr}.
5164 */
5165 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005166 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 if (alias != NULL)
5168 s = alias;
5169
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005170 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 ret = FAIL;
5172 else
5173 {
5174 if (**arg == '(') /* recursive! */
5175 {
5176 /* If "s" is the name of a variable of type VAR_FUNC
5177 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005178 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179
5180 /* Invoke the function. */
5181 ret = get_func_tv(s, len, rettv, arg,
5182 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005184
5185 /* If evaluate is FALSE rettv->v_type was not set in
5186 * get_func_tv, but it's needed in handle_subscript() to parse
5187 * what follows. So set it here. */
5188 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5189 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005190 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005191 rettv->v_type = VAR_FUNC;
5192 }
5193
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 /* Stop the expression evaluation when immediately
5195 * aborting on error, or when an interrupt occurred or
5196 * an exception was thrown but not caught. */
5197 if (aborting())
5198 {
5199 if (ret == OK)
5200 clear_tv(rettv);
5201 ret = FAIL;
5202 }
5203 }
5204 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005205 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005206 else
5207 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005209 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 }
5211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 *arg = skipwhite(*arg);
5213
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005214 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5215 * expr(expr). */
5216 if (ret == OK)
5217 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
5219 /*
5220 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5221 */
5222 if (ret == OK && evaluate && end_leader > start_leader)
5223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005225 int val = 0;
5226#ifdef FEAT_FLOAT
5227 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 if (rettv->v_type == VAR_FLOAT)
5230 f = rettv->vval.v_float;
5231 else
5232#endif
5233 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 clear_tv(rettv);
5237 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 else
5240 {
5241 while (end_leader > start_leader)
5242 {
5243 --end_leader;
5244 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005245 {
5246#ifdef FEAT_FLOAT
5247 if (rettv->v_type == VAR_FLOAT)
5248 f = !f;
5249 else
5250#endif
5251 val = !val;
5252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254 {
5255#ifdef FEAT_FLOAT
5256 if (rettv->v_type == VAR_FLOAT)
5257 f = -f;
5258 else
5259#endif
5260 val = -val;
5261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005263#ifdef FEAT_FLOAT
5264 if (rettv->v_type == VAR_FLOAT)
5265 {
5266 clear_tv(rettv);
5267 rettv->vval.v_float = f;
5268 }
5269 else
5270#endif
5271 {
5272 clear_tv(rettv);
5273 rettv->v_type = VAR_NUMBER;
5274 rettv->vval.v_number = val;
5275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278
5279 return ret;
5280}
5281
5282/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005283 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5284 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5286 */
5287 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293{
5294 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 long len = -1;
5298 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005304 if (verbose)
5305 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 return FAIL;
5307 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005308#ifdef FEAT_FLOAT
5309 else if (rettv->v_type == VAR_FLOAT)
5310 {
5311 if (verbose)
5312 EMSG(_(e_float_as_string));
5313 return FAIL;
5314 }
5315#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 /*
5320 * dict.name
5321 */
5322 key = *arg + 1;
5323 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5324 ;
5325 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 }
5329 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 /*
5332 * something[idx]
5333 *
5334 * Get the (first) variable from inside the [].
5335 */
5336 *arg = skipwhite(*arg + 1);
5337 if (**arg == ':')
5338 empty1 = TRUE;
5339 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5340 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005341 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5342 {
5343 /* not a number or string */
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347
5348 /*
5349 * Get the second variable from inside the [:].
5350 */
5351 if (**arg == ':')
5352 {
5353 range = TRUE;
5354 *arg = skipwhite(*arg + 1);
5355 if (**arg == ']')
5356 empty2 = TRUE;
5357 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 if (!empty1)
5360 clear_tv(&var1);
5361 return FAIL;
5362 }
5363 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5364 {
5365 /* not a number or string */
5366 if (!empty1)
5367 clear_tv(&var1);
5368 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 return FAIL;
5370 }
5371 }
5372
5373 /* Check for the ']'. */
5374 if (**arg != ']')
5375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 if (verbose)
5377 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 clear_tv(&var1);
5379 if (range)
5380 clear_tv(&var2);
5381 return FAIL;
5382 }
5383 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 }
5385
5386 if (evaluate)
5387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 n1 = 0;
5389 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 n1 = get_tv_number(&var1);
5392 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 if (range)
5395 {
5396 if (empty2)
5397 n2 = -1;
5398 else
5399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 n2 = get_tv_number(&var2);
5401 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 }
5403 }
5404
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
5407 case VAR_NUMBER:
5408 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 len = (long)STRLEN(s);
5411 if (range)
5412 {
5413 /* The resulting variable is a substring. If the indexes
5414 * are out of range the result is empty. */
5415 if (n1 < 0)
5416 {
5417 n1 = len + n1;
5418 if (n1 < 0)
5419 n1 = 0;
5420 }
5421 if (n2 < 0)
5422 n2 = len + n2;
5423 else if (n2 >= len)
5424 n2 = len;
5425 if (n1 >= len || n2 < 0 || n1 > n2)
5426 s = NULL;
5427 else
5428 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5429 }
5430 else
5431 {
5432 /* The resulting variable is a string of a single
5433 * character. If the index is too big or negative the
5434 * result is empty. */
5435 if (n1 >= len || n1 < 0)
5436 s = NULL;
5437 else
5438 s = vim_strnsave(s + n1, 1);
5439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 clear_tv(rettv);
5441 rettv->v_type = VAR_STRING;
5442 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 break;
5444
5445 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 if (n1 < 0)
5448 n1 = len + n1;
5449 if (!empty1 && (n1 < 0 || n1 >= len))
5450 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005451 /* For a range we allow invalid values and return an empty
5452 * list. A list index out of range is an error. */
5453 if (!range)
5454 {
5455 if (verbose)
5456 EMSGN(_(e_listidx), n1);
5457 return FAIL;
5458 }
5459 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 if (range)
5462 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 list_T *l;
5464 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465
5466 if (n2 < 0)
5467 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005468 else if (n2 >= len)
5469 n2 = len - 1;
5470 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005471 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 l = list_alloc();
5473 if (l == NULL)
5474 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 n1 <= n2; ++n1)
5477 {
5478 if (list_append_tv(l, &item->li_tv) == FAIL)
5479 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005480 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 return FAIL;
5482 }
5483 item = item->li_next;
5484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 clear_tv(rettv);
5486 rettv->v_type = VAR_LIST;
5487 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005488 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 else
5491 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005492 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 clear_tv(rettv);
5494 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 }
5496 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497
5498 case VAR_DICT:
5499 if (range)
5500 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005501 if (verbose)
5502 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503 if (len == -1)
5504 clear_tv(&var1);
5505 return FAIL;
5506 }
5507 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005509
5510 if (len == -1)
5511 {
5512 key = get_tv_string(&var1);
5513 if (*key == NUL)
5514 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005515 if (verbose)
5516 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 clear_tv(&var1);
5518 return FAIL;
5519 }
5520 }
5521
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005522 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005524 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005525 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 if (len == -1)
5527 clear_tv(&var1);
5528 if (item == NULL)
5529 return FAIL;
5530
5531 copy_tv(&item->di_tv, &var1);
5532 clear_tv(rettv);
5533 *rettv = var1;
5534 }
5535 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 }
5538
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 return OK;
5540}
5541
5542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 * Get an option value.
5544 * "arg" points to the '&' or '+' before the option name.
5545 * "arg" is advanced to character after the option name.
5546 * Return OK or FAIL.
5547 */
5548 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 int evaluate;
5553{
5554 char_u *option_end;
5555 long numval;
5556 char_u *stringval;
5557 int opt_type;
5558 int c;
5559 int working = (**arg == '+'); /* has("+option") */
5560 int ret = OK;
5561 int opt_flags;
5562
5563 /*
5564 * Isolate the option name and find its value.
5565 */
5566 option_end = find_option_end(arg, &opt_flags);
5567 if (option_end == NULL)
5568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 EMSG2(_("E112: Option name missing: %s"), *arg);
5571 return FAIL;
5572 }
5573
5574 if (!evaluate)
5575 {
5576 *arg = option_end;
5577 return OK;
5578 }
5579
5580 c = *option_end;
5581 *option_end = NUL;
5582 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585 if (opt_type == -3) /* invalid name */
5586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 EMSG2(_("E113: Unknown option: %s"), *arg);
5589 ret = FAIL;
5590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 if (opt_type == -2) /* hidden string option */
5594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 rettv->v_type = VAR_STRING;
5596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598 else if (opt_type == -1) /* hidden number option */
5599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 rettv->v_type = VAR_NUMBER;
5601 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 else if (opt_type == 1) /* number option */
5604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 rettv->v_type = VAR_NUMBER;
5606 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 else /* string option */
5609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610 rettv->v_type = VAR_STRING;
5611 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613 }
5614 else if (working && (opt_type == -2 || opt_type == -1))
5615 ret = FAIL;
5616
5617 *option_end = c; /* put back for error messages */
5618 *arg = option_end;
5619
5620 return ret;
5621}
5622
5623/*
5624 * Allocate a variable for a string constant.
5625 * Return OK or FAIL.
5626 */
5627 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 int evaluate;
5632{
5633 char_u *p;
5634 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 int extra = 0;
5636
5637 /*
5638 * Find the end of the string, skipping backslashed characters.
5639 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (*p == '\\' && p[1] != NUL)
5643 {
5644 ++p;
5645 /* A "\<x>" form occupies at least 4 characters, and produces up
5646 * to 6 characters: reserve space for 2 extra */
5647 if (*p == '<')
5648 extra += 2;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651
5652 if (*p != '"')
5653 {
5654 EMSG2(_("E114: Missing quote: %s"), *arg);
5655 return FAIL;
5656 }
5657
5658 /* If only parsing, set *arg and return here */
5659 if (!evaluate)
5660 {
5661 *arg = p + 1;
5662 return OK;
5663 }
5664
5665 /*
5666 * Copy the string into allocated memory, handling backslashed
5667 * characters.
5668 */
5669 name = alloc((unsigned)(p - *arg + extra));
5670 if (name == NULL)
5671 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 if (*p == '\\')
5678 {
5679 switch (*++p)
5680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 case 'b': *name++ = BS; ++p; break;
5682 case 'e': *name++ = ESC; ++p; break;
5683 case 'f': *name++ = FF; ++p; break;
5684 case 'n': *name++ = NL; ++p; break;
5685 case 'r': *name++ = CAR; ++p; break;
5686 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 case 'X': /* hex: "\x1", "\x12" */
5689 case 'x':
5690 case 'u': /* Unicode: "\u0023" */
5691 case 'U':
5692 if (vim_isxdigit(p[1]))
5693 {
5694 int n, nr;
5695 int c = toupper(*p);
5696
5697 if (c == 'X')
5698 n = 2;
5699 else
5700 n = 4;
5701 nr = 0;
5702 while (--n >= 0 && vim_isxdigit(p[1]))
5703 {
5704 ++p;
5705 nr = (nr << 4) + hex2nr(*p);
5706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#ifdef FEAT_MBYTE
5709 /* For "\u" store the number according to
5710 * 'encoding'. */
5711 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
5714#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 break;
5718
5719 /* octal: "\1", "\12", "\123" */
5720 case '0':
5721 case '1':
5722 case '2':
5723 case '3':
5724 case '4':
5725 case '5':
5726 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 case '7': *name = *p++ - '0';
5728 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 *name = (*name << 3) + *p++ - '0';
5731 if (*p >= '0' && *p <= '7')
5732 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 break;
5736
5737 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (extra != 0)
5740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 break;
5743 }
5744 /* FALLTHROUGH */
5745
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 break;
5748 }
5749 }
5750 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 *arg = p + 1;
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return OK;
5758}
5759
5760/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 * Return OK or FAIL.
5763 */
5764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 int evaluate;
5769{
5770 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 int reduce = 0;
5773
5774 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 */
5777 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 break;
5783 ++reduce;
5784 ++p;
5785 }
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 return FAIL;
5792 }
5793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 if (!evaluate)
5796 {
5797 *arg = p + 1;
5798 return OK;
5799 }
5800
5801 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 */
5804 str = alloc((unsigned)((p - *arg) - reduce));
5805 if (str == NULL)
5806 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 rettv->v_type = VAR_STRING;
5808 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 break;
5816 ++p;
5817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005821 *arg = p + 1;
5822
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 return OK;
5824}
5825
5826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 * Allocate a variable for a List and fill it from "*arg".
5828 * Return OK or FAIL.
5829 */
5830 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 int evaluate;
5835{
Bram Moolenaar33570922005-01-25 22:26:29 +00005836 list_T *l = NULL;
5837 typval_T tv;
5838 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839
5840 if (evaluate)
5841 {
5842 l = list_alloc();
5843 if (l == NULL)
5844 return FAIL;
5845 }
5846
5847 *arg = skipwhite(*arg + 1);
5848 while (**arg != ']' && **arg != NUL)
5849 {
5850 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5851 goto failret;
5852 if (evaluate)
5853 {
5854 item = listitem_alloc();
5855 if (item != NULL)
5856 {
5857 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005858 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 list_append(l, item);
5860 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 else
5862 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 }
5864
5865 if (**arg == ']')
5866 break;
5867 if (**arg != ',')
5868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 goto failret;
5871 }
5872 *arg = skipwhite(*arg + 1);
5873 }
5874
5875 if (**arg != ']')
5876 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878failret:
5879 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005880 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 return FAIL;
5882 }
5883
5884 *arg = skipwhite(*arg + 1);
5885 if (evaluate)
5886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005887 rettv->v_type = VAR_LIST;
5888 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 ++l->lv_refcount;
5890 }
5891
5892 return OK;
5893}
5894
5895/*
5896 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005897 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005899 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900list_alloc()
5901{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005902 list_T *l;
5903
5904 l = (list_T *)alloc_clear(sizeof(list_T));
5905 if (l != NULL)
5906 {
5907 /* Prepend the list to the list of lists for garbage collection. */
5908 if (first_list != NULL)
5909 first_list->lv_used_prev = l;
5910 l->lv_used_prev = NULL;
5911 l->lv_used_next = first_list;
5912 first_list = l;
5913 }
5914 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915}
5916
5917/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005918 * Allocate an empty list for a return value.
5919 * Returns OK or FAIL.
5920 */
5921 static int
5922rettv_list_alloc(rettv)
5923 typval_T *rettv;
5924{
5925 list_T *l = list_alloc();
5926
5927 if (l == NULL)
5928 return FAIL;
5929
5930 rettv->vval.v_list = l;
5931 rettv->v_type = VAR_LIST;
5932 ++l->lv_refcount;
5933 return OK;
5934}
5935
5936/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 * Unreference a list: decrement the reference count and free it when it
5938 * becomes zero.
5939 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005940 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005942 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005944 if (l != NULL && --l->lv_refcount <= 0)
5945 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946}
5947
5948/*
5949 * Free a list, including all items it points to.
5950 * Ignores the reference count.
5951 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005952 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005953list_free(l, recurse)
5954 list_T *l;
5955 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956{
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 /* Remove the list from the list of lists for garbage collection. */
5960 if (l->lv_used_prev == NULL)
5961 first_list = l->lv_used_next;
5962 else
5963 l->lv_used_prev->lv_used_next = l->lv_used_next;
5964 if (l->lv_used_next != NULL)
5965 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5966
Bram Moolenaard9fba312005-06-26 22:34:35 +00005967 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005969 /* Remove the item before deleting it. */
5970 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005971 if (recurse || (item->li_tv.v_type != VAR_LIST
5972 && item->li_tv.v_type != VAR_DICT))
5973 clear_tv(&item->li_tv);
5974 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 }
5976 vim_free(l);
5977}
5978
5979/*
5980 * Allocate a list item.
5981 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005982 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983listitem_alloc()
5984{
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005989 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005991 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005995 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 vim_free(item);
5997}
5998
5999/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000 * Remove a list item from a List and free it. Also clears the value.
6001 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006002 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006003listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 list_T *l;
6005 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006006{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006007 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006008 listitem_free(item);
6009}
6010
6011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Get the number of items in a list.
6013 */
6014 static long
6015list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 if (l == NULL)
6019 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006020 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021}
6022
6023/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 * Return TRUE when two lists have exactly the same values.
6025 */
6026 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006027list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 list_T *l1;
6029 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006031 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006032{
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006034
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006035 if (l1 == NULL || l2 == NULL)
6036 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006037 if (l1 == l2)
6038 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 if (list_len(l1) != list_len(l2))
6040 return FALSE;
6041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042 for (item1 = l1->lv_first, item2 = l2->lv_first;
6043 item1 != NULL && item2 != NULL;
6044 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006045 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046 return FALSE;
6047 return item1 == NULL && item2 == NULL;
6048}
6049
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006050#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6051 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006052/*
6053 * Return the dictitem that an entry in a hashtable points to.
6054 */
6055 dictitem_T *
6056dict_lookup(hi)
6057 hashitem_T *hi;
6058{
6059 return HI2DI(hi);
6060}
6061#endif
6062
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 * Return TRUE when two dictionaries have exactly the same key/values.
6065 */
6066 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 dict_T *d1;
6069 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072{
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 hashitem_T *hi;
6074 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006076
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006077 if (d1 == NULL || d2 == NULL)
6078 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006079 if (d1 == d2)
6080 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081 if (dict_len(d1) != dict_len(d2))
6082 return FALSE;
6083
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006084 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 if (!HASHITEM_EMPTY(hi))
6088 {
6089 item2 = dict_find(d2, hi->hi_key, -1);
6090 if (item2 == NULL)
6091 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006093 return FALSE;
6094 --todo;
6095 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096 }
6097 return TRUE;
6098}
6099
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100static int tv_equal_recurse_limit;
6101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006105 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 */
6107 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 typval_T *tv1;
6110 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 int ic; /* ignore case */
6112 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113{
6114 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006117 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006119 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006120 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006121
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006122 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 * recursiveness to a limit. We guess they are equal then.
6124 * A fixed limit has the problem of still taking an awful long time.
6125 * Reduce the limit every time running into it. That should work fine for
6126 * deeply linked structures that are not recursively linked and catch
6127 * recursiveness quickly. */
6128 if (!recursive)
6129 tv_equal_recurse_limit = 1000;
6130 if (recursive_cnt >= tv_equal_recurse_limit)
6131 {
6132 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006133 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135
6136 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006138 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 ++recursive_cnt;
6146 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6147 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006148 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149
6150 case VAR_FUNC:
6151 return (tv1->vval.v_string != NULL
6152 && tv2->vval.v_string != NULL
6153 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6154
6155 case VAR_NUMBER:
6156 return tv1->vval.v_number == tv2->vval.v_number;
6157
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006158#ifdef FEAT_FLOAT
6159 case VAR_FLOAT:
6160 return tv1->vval.v_float == tv2->vval.v_float;
6161#endif
6162
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006163 case VAR_STRING:
6164 s1 = get_tv_string_buf(tv1, buf1);
6165 s2 = get_tv_string_buf(tv2, buf2);
6166 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168
6169 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 return TRUE;
6171}
6172
6173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 * Locate item with index "n" in list "l" and return it.
6175 * A negative index is counted from the end; -1 is the last item.
6176 * Returns NULL when "n" is out of range.
6177 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006178 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 long n;
6182{
Bram Moolenaar33570922005-01-25 22:26:29 +00006183 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 long idx;
6185
6186 if (l == NULL)
6187 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006188
6189 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006191 n = l->lv_len + n;
6192
6193 /* Check for index out of range. */
6194 if (n < 0 || n >= l->lv_len)
6195 return NULL;
6196
6197 /* When there is a cached index may start search from there. */
6198 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 if (n < l->lv_idx / 2)
6201 {
6202 /* closest to the start of the list */
6203 item = l->lv_first;
6204 idx = 0;
6205 }
6206 else if (n > (l->lv_idx + l->lv_len) / 2)
6207 {
6208 /* closest to the end of the list */
6209 item = l->lv_last;
6210 idx = l->lv_len - 1;
6211 }
6212 else
6213 {
6214 /* closest to the cached index */
6215 item = l->lv_idx_item;
6216 idx = l->lv_idx;
6217 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006218 }
6219 else
6220 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006221 if (n < l->lv_len / 2)
6222 {
6223 /* closest to the start of the list */
6224 item = l->lv_first;
6225 idx = 0;
6226 }
6227 else
6228 {
6229 /* closest to the end of the list */
6230 item = l->lv_last;
6231 idx = l->lv_len - 1;
6232 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006234
6235 while (n > idx)
6236 {
6237 /* search forward */
6238 item = item->li_next;
6239 ++idx;
6240 }
6241 while (n < idx)
6242 {
6243 /* search backward */
6244 item = item->li_prev;
6245 --idx;
6246 }
6247
6248 /* cache the used index */
6249 l->lv_idx = idx;
6250 l->lv_idx_item = item;
6251
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 return item;
6253}
6254
6255/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006256 * Get list item "l[idx]" as a number.
6257 */
6258 static long
6259list_find_nr(l, idx, errorp)
6260 list_T *l;
6261 long idx;
6262 int *errorp; /* set to TRUE when something wrong */
6263{
6264 listitem_T *li;
6265
6266 li = list_find(l, idx);
6267 if (li == NULL)
6268 {
6269 if (errorp != NULL)
6270 *errorp = TRUE;
6271 return -1L;
6272 }
6273 return get_tv_number_chk(&li->li_tv, errorp);
6274}
6275
6276/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006277 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6278 */
6279 char_u *
6280list_find_str(l, idx)
6281 list_T *l;
6282 long idx;
6283{
6284 listitem_T *li;
6285
6286 li = list_find(l, idx - 1);
6287 if (li == NULL)
6288 {
6289 EMSGN(_(e_listidx), idx);
6290 return NULL;
6291 }
6292 return get_tv_string(&li->li_tv);
6293}
6294
6295/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006296 * Locate "item" list "l" and return its index.
6297 * Returns -1 when "item" is not in the list.
6298 */
6299 static long
6300list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 list_T *l;
6302 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006303{
6304 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006306
6307 if (l == NULL)
6308 return -1;
6309 idx = 0;
6310 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6311 ++idx;
6312 if (li == NULL)
6313 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006314 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006315}
6316
6317/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 * Append item "item" to the end of list "l".
6319 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006320 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 list_T *l;
6323 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324{
6325 if (l->lv_last == NULL)
6326 {
6327 /* empty list */
6328 l->lv_first = item;
6329 l->lv_last = item;
6330 item->li_prev = NULL;
6331 }
6332 else
6333 {
6334 l->lv_last->li_next = item;
6335 item->li_prev = l->lv_last;
6336 l->lv_last = item;
6337 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006338 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 item->li_next = NULL;
6340}
6341
6342/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006344 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006346 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006348 list_T *l;
6349 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006351 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352
Bram Moolenaar05159a02005-02-26 23:04:13 +00006353 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006355 copy_tv(tv, &li->li_tv);
6356 list_append(l, li);
6357 return OK;
6358}
6359
6360/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006361 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006362 * Return FAIL when out of memory.
6363 */
6364 int
6365list_append_dict(list, dict)
6366 list_T *list;
6367 dict_T *dict;
6368{
6369 listitem_T *li = listitem_alloc();
6370
6371 if (li == NULL)
6372 return FAIL;
6373 li->li_tv.v_type = VAR_DICT;
6374 li->li_tv.v_lock = 0;
6375 li->li_tv.vval.v_dict = dict;
6376 list_append(list, li);
6377 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 return OK;
6379}
6380
6381/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006383 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006384 * Returns FAIL when out of memory.
6385 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006386 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006387list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006388 list_T *l;
6389 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006390 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006391{
6392 listitem_T *li = listitem_alloc();
6393
6394 if (li == NULL)
6395 return FAIL;
6396 list_append(l, li);
6397 li->li_tv.v_type = VAR_STRING;
6398 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006399 if (str == NULL)
6400 li->li_tv.vval.v_string = NULL;
6401 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006402 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006403 return FAIL;
6404 return OK;
6405}
6406
6407/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006408 * Append "n" to list "l".
6409 * Returns FAIL when out of memory.
6410 */
6411 static int
6412list_append_number(l, n)
6413 list_T *l;
6414 varnumber_T n;
6415{
6416 listitem_T *li;
6417
6418 li = listitem_alloc();
6419 if (li == NULL)
6420 return FAIL;
6421 li->li_tv.v_type = VAR_NUMBER;
6422 li->li_tv.v_lock = 0;
6423 li->li_tv.vval.v_number = n;
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 * If "item" is NULL append at the end.
6431 * Return FAIL when out of memory.
6432 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006433 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 list_T *l;
6436 typval_T *tv;
6437 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438{
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440
6441 if (ni == NULL)
6442 return FAIL;
6443 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006444 list_insert(l, ni, item);
6445 return OK;
6446}
6447
6448 void
6449list_insert(l, ni, item)
6450 list_T *l;
6451 listitem_T *ni;
6452 listitem_T *item;
6453{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 if (item == NULL)
6455 /* Append new item at end of list. */
6456 list_append(l, ni);
6457 else
6458 {
6459 /* Insert new item before existing item. */
6460 ni->li_prev = item->li_prev;
6461 ni->li_next = item;
6462 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006465 ++l->lv_idx;
6466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006468 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006470 l->lv_idx_item = NULL;
6471 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006473 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475}
6476
6477/*
6478 * Extend "l1" with "l2".
6479 * If "bef" is NULL append at the end, otherwise insert before this item.
6480 * Returns FAIL when out of memory.
6481 */
6482 static int
6483list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 list_T *l1;
6485 list_T *l2;
6486 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006487{
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006489 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006491 /* We also quit the loop when we have inserted the original item count of
6492 * the list, avoid a hang when we extend a list with itself. */
6493 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6495 return FAIL;
6496 return OK;
6497}
6498
6499/*
6500 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6501 * Return FAIL when out of memory.
6502 */
6503 static int
6504list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 list_T *l1;
6506 list_T *l2;
6507 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508{
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006511 if (l1 == NULL || l2 == NULL)
6512 return FAIL;
6513
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 if (l == NULL)
6517 return FAIL;
6518 tv->v_type = VAR_LIST;
6519 tv->vval.v_list = l;
6520
6521 /* append all items from the second list */
6522 return list_extend(l, l2, NULL);
6523}
6524
6525/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006528 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 * Returns NULL when out of memory.
6530 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006532list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536{
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 list_T *copy;
6538 listitem_T *item;
6539 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540
6541 if (orig == NULL)
6542 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543
6544 copy = list_alloc();
6545 if (copy != NULL)
6546 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006547 if (copyID != 0)
6548 {
6549 /* Do this before adding the items, because one of the items may
6550 * refer back to this list. */
6551 orig->lv_copyID = copyID;
6552 orig->lv_copylist = copy;
6553 }
6554 for (item = orig->lv_first; item != NULL && !got_int;
6555 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 {
6557 ni = listitem_alloc();
6558 if (ni == NULL)
6559 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 {
6562 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6563 {
6564 vim_free(ni);
6565 break;
6566 }
6567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 list_append(copy, ni);
6571 }
6572 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 if (item != NULL)
6574 {
6575 list_unref(copy);
6576 copy = NULL;
6577 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 }
6579
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 return copy;
6581}
6582
6583/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006585 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006586 * This used to be called list_remove, but that conflicts with a Sun header
6587 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006589 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006590vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *l;
6592 listitem_T *item;
6593 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594{
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 /* notify watchers */
6598 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006600 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601 list_fix_watch(l, ip);
6602 if (ip == item2)
6603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006605
6606 if (item2->li_next == NULL)
6607 l->lv_last = item->li_prev;
6608 else
6609 item2->li_next->li_prev = item->li_prev;
6610 if (item->li_prev == NULL)
6611 l->lv_first = item2->li_next;
6612 else
6613 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006614 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615}
6616
6617/*
6618 * Return an allocated string with the string representation of a list.
6619 * May return NULL.
6620 */
6621 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006622list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625{
6626 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627
6628 if (tv->vval.v_list == NULL)
6629 return NULL;
6630 ga_init2(&ga, (int)sizeof(char), 80);
6631 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006632 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 {
6634 vim_free(ga.ga_data);
6635 return NULL;
6636 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 ga_append(&ga, ']');
6638 ga_append(&ga, NUL);
6639 return (char_u *)ga.ga_data;
6640}
6641
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006642typedef struct join_S {
6643 char_u *s;
6644 char_u *tofree;
6645} join_T;
6646
6647 static int
6648list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6649 garray_T *gap; /* to store the result in */
6650 list_T *l;
6651 char_u *sep;
6652 int echo_style;
6653 int copyID;
6654 garray_T *join_gap; /* to keep each list item string */
6655{
6656 int i;
6657 join_T *p;
6658 int len;
6659 int sumlen = 0;
6660 int first = TRUE;
6661 char_u *tofree;
6662 char_u numbuf[NUMBUFLEN];
6663 listitem_T *item;
6664 char_u *s;
6665
6666 /* Stringify each item in the list. */
6667 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6668 {
6669 if (echo_style)
6670 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6671 else
6672 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6673 if (s == NULL)
6674 return FAIL;
6675
6676 len = (int)STRLEN(s);
6677 sumlen += len;
6678
6679 ga_grow(join_gap, 1);
6680 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6681 if (tofree != NULL || s != numbuf)
6682 {
6683 p->s = s;
6684 p->tofree = tofree;
6685 }
6686 else
6687 {
6688 p->s = vim_strnsave(s, len);
6689 p->tofree = p->s;
6690 }
6691
6692 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006693 if (did_echo_string_emsg) /* recursion error, bail out */
6694 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006695 }
6696
6697 /* Allocate result buffer with its total size, avoid re-allocation and
6698 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6699 if (join_gap->ga_len >= 2)
6700 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6701 if (ga_grow(gap, sumlen + 2) == FAIL)
6702 return FAIL;
6703
6704 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6705 {
6706 if (first)
6707 first = FALSE;
6708 else
6709 ga_concat(gap, sep);
6710 p = ((join_T *)join_gap->ga_data) + i;
6711
6712 if (p->s != NULL)
6713 ga_concat(gap, p->s);
6714 line_breakcheck();
6715 }
6716
6717 return OK;
6718}
6719
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006721 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006722 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006723 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006725 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006726list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006728 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006730 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006731 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006732{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006733 garray_T join_ga;
6734 int retval;
6735 join_T *p;
6736 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006737
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6739 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6740
6741 /* Dispose each item in join_ga. */
6742 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006744 p = (join_T *)join_ga.ga_data;
6745 for (i = 0; i < join_ga.ga_len; ++i)
6746 {
6747 vim_free(p->tofree);
6748 ++p;
6749 }
6750 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006752
6753 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754}
6755
6756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 * Garbage collection for lists and dictionaries.
6758 *
6759 * We use reference counts to be able to free most items right away when they
6760 * are no longer used. But for composite items it's possible that it becomes
6761 * unused while the reference count is > 0: When there is a recursive
6762 * reference. Example:
6763 * :let l = [1, 2, 3]
6764 * :let d = {9: l}
6765 * :let l[1] = d
6766 *
6767 * Since this is quite unusual we handle this with garbage collection: every
6768 * once in a while find out which lists and dicts are not referenced from any
6769 * variable.
6770 *
6771 * Here is a good reference text about garbage collection (refers to Python
6772 * but it applies to all reference-counting mechanisms):
6773 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775
6776/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 * Do garbage collection for lists and dicts.
6778 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006779 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780 int
6781garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006782{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006783 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 buf_T *buf;
6785 win_T *wp;
6786 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006787 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788 int did_free;
6789 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006790#ifdef FEAT_WINDOWS
6791 tabpage_T *tp;
6792#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006794 /* Only do this once. */
6795 want_garbage_collect = FALSE;
6796 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006797 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006798
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 /* We advance by two because we add one for items referenced through
6800 * previous_funccal. */
6801 current_copyID += COPYID_INC;
6802 copyID = current_copyID;
6803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 /*
6805 * 1. Go through all accessible variables and mark all lists and dicts
6806 * with copyID.
6807 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006808
6809 /* Don't free variables in the previous_funccal list unless they are only
6810 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006811 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006812 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6813 {
6814 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6815 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6816 }
6817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818 /* script-local variables */
6819 for (i = 1; i <= ga_scripts.ga_len; ++i)
6820 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6821
6822 /* buffer-local variables */
6823 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006824 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
6826 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006827 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006828 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006829#ifdef FEAT_AUTOCMD
6830 if (aucmd_win != NULL)
6831 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6832#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006834#ifdef FEAT_WINDOWS
6835 /* tabpage-local variables */
6836 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006837 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006838#endif
6839
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 /* global variables */
6841 set_ref_in_ht(&globvarht, copyID);
6842
6843 /* function-local variables */
6844 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6845 {
6846 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6847 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6848 }
6849
Bram Moolenaard812df62008-11-09 12:46:09 +00006850 /* v: vars */
6851 set_ref_in_ht(&vimvarht, copyID);
6852
Bram Moolenaar1dced572012-04-05 16:54:08 +02006853#ifdef FEAT_LUA
6854 set_ref_in_lua(copyID);
6855#endif
6856
Bram Moolenaardb913952012-06-29 12:54:53 +02006857#ifdef FEAT_PYTHON
6858 set_ref_in_python(copyID);
6859#endif
6860
6861#ifdef FEAT_PYTHON3
6862 set_ref_in_python3(copyID);
6863#endif
6864
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006865 /*
6866 * 2. Free lists and dictionaries that are not referenced.
6867 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 did_free = free_unref_items(copyID);
6869
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006870 /*
6871 * 3. Check if any funccal can be freed now.
6872 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 for (pfc = &previous_funccal; *pfc != NULL; )
6874 {
6875 if (can_free_funccal(*pfc, copyID))
6876 {
6877 fc = *pfc;
6878 *pfc = fc->caller;
6879 free_funccal(fc, TRUE);
6880 did_free = TRUE;
6881 did_free_funccal = TRUE;
6882 }
6883 else
6884 pfc = &(*pfc)->caller;
6885 }
6886 if (did_free_funccal)
6887 /* When a funccal was freed some more items might be garbage
6888 * collected, so run again. */
6889 (void)garbage_collect();
6890
6891 return did_free;
6892}
6893
6894/*
6895 * Free lists and dictionaries that are no longer referenced.
6896 */
6897 static int
6898free_unref_items(copyID)
6899 int copyID;
6900{
6901 dict_T *dd;
6902 list_T *ll;
6903 int did_free = FALSE;
6904
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 */
6908 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006911 /* Free the Dictionary and ordinary items it contains, but don't
6912 * recurse into Lists and Dictionaries, they will be in the list
6913 * of dicts or list of lists. */
6914 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 did_free = TRUE;
6916
6917 /* restart, next dict may also have been freed */
6918 dd = first_dict;
6919 }
6920 else
6921 dd = dd->dv_used_next;
6922
6923 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006924 * Go through the list of lists and free items without the copyID.
6925 * But don't free a list that has a watcher (used in a for loop), these
6926 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 */
6928 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6930 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006932 /* Free the List and ordinary items it contains, but don't recurse
6933 * into Lists and Dictionaries, they will be in the list of dicts
6934 * or list of lists. */
6935 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 did_free = TRUE;
6937
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006938 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 ll = first_list;
6940 }
6941 else
6942 ll = ll->lv_used_next;
6943
6944 return did_free;
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_ht(ht, copyID)
6952 hashtab_T *ht;
6953 int copyID;
6954{
6955 int todo;
6956 hashitem_T *hi;
6957
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006958 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 for (hi = ht->ht_array; todo > 0; ++hi)
6960 if (!HASHITEM_EMPTY(hi))
6961 {
6962 --todo;
6963 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6964 }
6965}
6966
6967/*
6968 * Mark all lists and dicts referenced through list "l" with "copyID".
6969 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006970 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971set_ref_in_list(l, copyID)
6972 list_T *l;
6973 int copyID;
6974{
6975 listitem_T *li;
6976
6977 for (li = l->lv_first; li != NULL; li = li->li_next)
6978 set_ref_in_item(&li->li_tv, copyID);
6979}
6980
6981/*
6982 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6983 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006984 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985set_ref_in_item(tv, copyID)
6986 typval_T *tv;
6987 int copyID;
6988{
6989 dict_T *dd;
6990 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991
6992 switch (tv->v_type)
6993 {
6994 case VAR_DICT:
6995 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006996 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 /* Didn't see this dict yet. */
6999 dd->dv_copyID = copyID;
7000 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003
7004 case VAR_LIST:
7005 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007006 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007007 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 /* Didn't see this list yet. */
7009 ll->lv_copyID = copyID;
7010 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007011 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007013 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007015}
7016
7017/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007018 * Allocate an empty header for a dictionary.
7019 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007020 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021dict_alloc()
7022{
Bram Moolenaar33570922005-01-25 22:26:29 +00007023 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007024
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007026 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007027 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007028 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 if (first_dict != NULL)
7030 first_dict->dv_used_prev = d;
7031 d->dv_used_next = first_dict;
7032 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007033 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007036 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007037 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007038 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007039 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007040 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042}
7043
7044/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007045 * Allocate an empty dict for a return value.
7046 * Returns OK or FAIL.
7047 */
7048 static int
7049rettv_dict_alloc(rettv)
7050 typval_T *rettv;
7051{
7052 dict_T *d = dict_alloc();
7053
7054 if (d == NULL)
7055 return FAIL;
7056
7057 rettv->vval.v_dict = d;
7058 rettv->v_type = VAR_DICT;
7059 ++d->dv_refcount;
7060 return OK;
7061}
7062
7063
7064/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065 * Unreference a Dictionary: decrement the reference count and free it when it
7066 * becomes zero.
7067 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007068 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007069dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007072 if (d != NULL && --d->dv_refcount <= 0)
7073 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074}
7075
7076/*
7077 * Free a Dictionary, including all items it contains.
7078 * Ignores the reference count.
7079 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007080 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007081dict_free(d, recurse)
7082 dict_T *d;
7083 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007087 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089 /* Remove the dict from the list of dicts for garbage collection. */
7090 if (d->dv_used_prev == NULL)
7091 first_dict = d->dv_used_next;
7092 else
7093 d->dv_used_prev->dv_used_next = d->dv_used_next;
7094 if (d->dv_used_next != NULL)
7095 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7096
7097 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007098 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007099 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102 if (!HASHITEM_EMPTY(hi))
7103 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104 /* Remove the item before deleting it, just in case there is
7105 * something recursive causing trouble. */
7106 di = HI2DI(hi);
7107 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007108 if (recurse || (di->di_tv.v_type != VAR_LIST
7109 && di->di_tv.v_type != VAR_DICT))
7110 clear_tv(&di->di_tv);
7111 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 --todo;
7113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 vim_free(d);
7117}
7118
7119/*
7120 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 * The "key" is copied to the new item.
7122 * Note that the value of the item "di_tv" still needs to be initialized!
7123 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007125 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126dictitem_alloc(key)
7127 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128{
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007131 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007133 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007135 di->di_flags = 0;
7136 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138}
7139
7140/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Make a copy of a Dictionary item.
7142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146{
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007149 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7150 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151 if (di != NULL)
7152 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007154 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 copy_tv(&org->di_tv, &di->di_tv);
7156 }
7157 return di;
7158}
7159
7160/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 * Remove item "item" from Dictionary "dict" and free it.
7162 */
7163 static void
7164dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 dict_T *dict;
7166 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167{
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007171 if (HASHITEM_EMPTY(hi))
7172 EMSG2(_(e_intern2), "dictitem_remove()");
7173 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175 dictitem_free(item);
7176}
7177
7178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 * Free a dict item. Also clears the value.
7180 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007181 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 clear_tv(&item->di_tv);
7186 vim_free(item);
7187}
7188
7189/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7191 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007193 * Returns NULL when out of memory.
7194 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007196dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200{
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 dict_T *copy;
7202 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205
7206 if (orig == NULL)
7207 return NULL;
7208
7209 copy = dict_alloc();
7210 if (copy != NULL)
7211 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007212 if (copyID != 0)
7213 {
7214 orig->dv_copyID = copyID;
7215 orig->dv_copydict = copy;
7216 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007217 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 --todo;
7223
7224 di = dictitem_alloc(hi->hi_key);
7225 if (di == NULL)
7226 break;
7227 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007228 {
7229 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7230 copyID) == FAIL)
7231 {
7232 vim_free(di);
7233 break;
7234 }
7235 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 else
7237 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7238 if (dict_add(copy, di) == FAIL)
7239 {
7240 dictitem_free(di);
7241 break;
7242 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245
Bram Moolenaare9a41262005-01-15 22:18:47 +00007246 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007247 if (todo > 0)
7248 {
7249 dict_unref(copy);
7250 copy = NULL;
7251 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007252 }
7253
7254 return copy;
7255}
7256
7257/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007259 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007261 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 dict_T *d;
7264 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267}
7268
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007270 * Add a number or string entry to dictionary "d".
7271 * When "str" is NULL use number "nr", otherwise use "str".
7272 * Returns FAIL when out of memory and when key already exists.
7273 */
7274 int
7275dict_add_nr_str(d, key, nr, str)
7276 dict_T *d;
7277 char *key;
7278 long nr;
7279 char_u *str;
7280{
7281 dictitem_T *item;
7282
7283 item = dictitem_alloc((char_u *)key);
7284 if (item == NULL)
7285 return FAIL;
7286 item->di_tv.v_lock = 0;
7287 if (str == NULL)
7288 {
7289 item->di_tv.v_type = VAR_NUMBER;
7290 item->di_tv.vval.v_number = nr;
7291 }
7292 else
7293 {
7294 item->di_tv.v_type = VAR_STRING;
7295 item->di_tv.vval.v_string = vim_strsave(str);
7296 }
7297 if (dict_add(d, item) == FAIL)
7298 {
7299 dictitem_free(item);
7300 return FAIL;
7301 }
7302 return OK;
7303}
7304
7305/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007306 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007307 * Returns FAIL when out of memory and when key already exists.
7308 */
7309 int
7310dict_add_list(d, key, list)
7311 dict_T *d;
7312 char *key;
7313 list_T *list;
7314{
7315 dictitem_T *item;
7316
7317 item = dictitem_alloc((char_u *)key);
7318 if (item == NULL)
7319 return FAIL;
7320 item->di_tv.v_lock = 0;
7321 item->di_tv.v_type = VAR_LIST;
7322 item->di_tv.vval.v_list = list;
7323 if (dict_add(d, item) == FAIL)
7324 {
7325 dictitem_free(item);
7326 return FAIL;
7327 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007328 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007329 return OK;
7330}
7331
7332/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 * Get the number of items in a Dictionary.
7334 */
7335 static long
7336dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (d == NULL)
7340 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007341 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342}
7343
7344/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 * Find item "key[len]" in Dictionary "d".
7346 * If "len" is negative use strlen(key).
7347 * Returns NULL when not found.
7348 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007349 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352 char_u *key;
7353 int len;
7354{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355#define AKEYLEN 200
7356 char_u buf[AKEYLEN];
7357 char_u *akey;
7358 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 if (len < 0)
7362 akey = key;
7363 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 tofree = akey = vim_strnsave(key, len);
7366 if (akey == NULL)
7367 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 else
7370 {
7371 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007372 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 akey = buf;
7374 }
7375
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 vim_free(tofree);
7378 if (HASHITEM_EMPTY(hi))
7379 return NULL;
7380 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381}
7382
7383/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007384 * Get a string item from a dictionary.
7385 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007386 * Returns NULL if the entry doesn't exist or out of memory.
7387 */
7388 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007389get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007390 dict_T *d;
7391 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007392 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007393{
7394 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007395 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007396
7397 di = dict_find(d, key, -1);
7398 if (di == NULL)
7399 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007400 s = get_tv_string(&di->di_tv);
7401 if (save && s != NULL)
7402 s = vim_strsave(s);
7403 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007404}
7405
7406/*
7407 * Get a number item from a dictionary.
7408 * Returns 0 if the entry doesn't exist or out of memory.
7409 */
7410 long
7411get_dict_number(d, key)
7412 dict_T *d;
7413 char_u *key;
7414{
7415 dictitem_T *di;
7416
7417 di = dict_find(d, key, -1);
7418 if (di == NULL)
7419 return 0;
7420 return get_tv_number(&di->di_tv);
7421}
7422
7423/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 * Return an allocated string with the string representation of a Dictionary.
7425 * May return NULL.
7426 */
7427 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007428dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007430 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431{
7432 garray_T ga;
7433 int first = TRUE;
7434 char_u *tofree;
7435 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007439 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 return NULL;
7443 ga_init2(&ga, (int)sizeof(char), 80);
7444 ga_append(&ga, '{');
7445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007446 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007447 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007451 --todo;
7452
7453 if (first)
7454 first = FALSE;
7455 else
7456 ga_concat(&ga, (char_u *)", ");
7457
7458 tofree = string_quote(hi->hi_key, FALSE);
7459 if (tofree != NULL)
7460 {
7461 ga_concat(&ga, tofree);
7462 vim_free(tofree);
7463 }
7464 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007465 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007466 if (s != NULL)
7467 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007469 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007470 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007471 line_breakcheck();
7472
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007475 if (todo > 0)
7476 {
7477 vim_free(ga.ga_data);
7478 return NULL;
7479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480
7481 ga_append(&ga, '}');
7482 ga_append(&ga, NUL);
7483 return (char_u *)ga.ga_data;
7484}
7485
7486/*
7487 * Allocate a variable for a Dictionary and fill it from "*arg".
7488 * Return OK or FAIL. Returns NOTDONE for {expr}.
7489 */
7490 static int
7491get_dict_tv(arg, rettv, evaluate)
7492 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 int evaluate;
7495{
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 dict_T *d = NULL;
7497 typval_T tvkey;
7498 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007499 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503
7504 /*
7505 * First check if it's not a curly-braces thing: {expr}.
7506 * Must do this without evaluating, otherwise a function may be called
7507 * twice. Unfortunately this means we need to call eval1() twice for the
7508 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 if (*start != '}')
7512 {
7513 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7514 return FAIL;
7515 if (*start == '}')
7516 return NOTDONE;
7517 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518
7519 if (evaluate)
7520 {
7521 d = dict_alloc();
7522 if (d == NULL)
7523 return FAIL;
7524 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 tvkey.v_type = VAR_UNKNOWN;
7526 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527
7528 *arg = skipwhite(*arg + 1);
7529 while (**arg != '}' && **arg != NUL)
7530 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 goto failret;
7533 if (**arg != ':')
7534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007535 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 goto failret;
7538 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007539 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007541 key = get_tv_string_buf_chk(&tvkey, buf);
7542 if (key == NULL || *key == NUL)
7543 {
7544 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7545 if (key != NULL)
7546 EMSG(_(e_emptykey));
7547 clear_tv(&tvkey);
7548 goto failret;
7549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551
7552 *arg = skipwhite(*arg + 1);
7553 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7554 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007555 if (evaluate)
7556 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 goto failret;
7558 }
7559 if (evaluate)
7560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 if (item != NULL)
7563 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007564 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 clear_tv(&tv);
7567 goto failret;
7568 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 item = dictitem_alloc(key);
7570 clear_tv(&tvkey);
7571 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007574 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if (dict_add(d, item) == FAIL)
7576 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 }
7578 }
7579
7580 if (**arg == '}')
7581 break;
7582 if (**arg != ',')
7583 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007584 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 goto failret;
7586 }
7587 *arg = skipwhite(*arg + 1);
7588 }
7589
7590 if (**arg != '}')
7591 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007592 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593failret:
7594 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007595 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 return FAIL;
7597 }
7598
7599 *arg = skipwhite(*arg + 1);
7600 if (evaluate)
7601 {
7602 rettv->v_type = VAR_DICT;
7603 rettv->vval.v_dict = d;
7604 ++d->dv_refcount;
7605 }
7606
7607 return OK;
7608}
7609
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 * Return a string with the string representation of a variable.
7612 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007613 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007614 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007615 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007616 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007617 */
7618 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007622 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625 static int recurse = 0;
7626 char_u *r = NULL;
7627
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007630 if (!did_echo_string_emsg)
7631 {
7632 /* Only give this message once for a recursive call to avoid
7633 * flooding the user with errors. And stop iterating over lists
7634 * and dicts. */
7635 did_echo_string_emsg = TRUE;
7636 EMSG(_("E724: variable nested too deep for displaying"));
7637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007639 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 }
7641 ++recurse;
7642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643 switch (tv->v_type)
7644 {
7645 case VAR_FUNC:
7646 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 r = tv->vval.v_string;
7648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 if (tv->vval.v_list == NULL)
7652 {
7653 *tofree = NULL;
7654 r = NULL;
7655 }
7656 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7657 {
7658 *tofree = NULL;
7659 r = (char_u *)"[...]";
7660 }
7661 else
7662 {
7663 tv->vval.v_list->lv_copyID = copyID;
7664 *tofree = list2string(tv, copyID);
7665 r = *tofree;
7666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007668
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670 if (tv->vval.v_dict == NULL)
7671 {
7672 *tofree = NULL;
7673 r = NULL;
7674 }
7675 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7676 {
7677 *tofree = NULL;
7678 r = (char_u *)"{...}";
7679 }
7680 else
7681 {
7682 tv->vval.v_dict->dv_copyID = copyID;
7683 *tofree = dict2string(tv, copyID);
7684 r = *tofree;
7685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007686 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 case VAR_STRING:
7689 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 *tofree = NULL;
7691 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007693
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007694#ifdef FEAT_FLOAT
7695 case VAR_FLOAT:
7696 *tofree = NULL;
7697 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7698 r = numbuf;
7699 break;
7700#endif
7701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007704 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007706
Bram Moolenaar8502c702014-06-17 12:51:16 +02007707 if (--recurse == 0)
7708 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007709 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710}
7711
7712/*
7713 * Return a string with the string representation of a variable.
7714 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7715 * "numbuf" is used for a number.
7716 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007717 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 */
7719 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007720tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007722 char_u **tofree;
7723 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007724 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007725{
7726 switch (tv->v_type)
7727 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007728 case VAR_FUNC:
7729 *tofree = string_quote(tv->vval.v_string, TRUE);
7730 return *tofree;
7731 case VAR_STRING:
7732 *tofree = string_quote(tv->vval.v_string, FALSE);
7733 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735 case VAR_FLOAT:
7736 *tofree = NULL;
7737 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7738 return numbuf;
7739#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007744 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007746 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007747 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748}
7749
7750/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 * Return string "str" in ' quotes, doubling ' characters.
7752 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007754 */
7755 static char_u *
7756string_quote(str, function)
7757 char_u *str;
7758 int function;
7759{
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007761 char_u *p, *r, *s;
7762
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 len = (function ? 13 : 3);
7764 if (str != NULL)
7765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007766 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 for (p = str; *p != NUL; mb_ptr_adv(p))
7768 if (*p == '\'')
7769 ++len;
7770 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 s = r = alloc(len);
7772 if (r != NULL)
7773 {
7774 if (function)
7775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007776 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007777 r += 10;
7778 }
7779 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 if (str != NULL)
7782 for (p = str; *p != NUL; )
7783 {
7784 if (*p == '\'')
7785 *r++ = '\'';
7786 MB_COPY_CHAR(p, r);
7787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007789 if (function)
7790 *r++ = ')';
7791 *r++ = NUL;
7792 }
7793 return s;
7794}
7795
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007796#ifdef FEAT_FLOAT
7797/*
7798 * Convert the string "text" to a floating point number.
7799 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7800 * this always uses a decimal point.
7801 * Returns the length of the text that was consumed.
7802 */
7803 static int
7804string2float(text, value)
7805 char_u *text;
7806 float_T *value; /* result stored here */
7807{
7808 char *s = (char *)text;
7809 float_T f;
7810
7811 f = strtod(s, &s);
7812 *value = f;
7813 return (int)((char_u *)s - text);
7814}
7815#endif
7816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 * Get the value of an environment variable.
7819 * "arg" is pointing to the '$'. It is advanced to after the name.
7820 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007821 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 */
7823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007824get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 int evaluate;
7828{
7829 char_u *string = NULL;
7830 int len;
7831 int cc;
7832 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007833 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835 ++*arg;
7836 name = *arg;
7837 len = get_env_len(arg);
7838 if (evaluate)
7839 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007840 if (len == 0)
7841 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007842
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007843 cc = name[len];
7844 name[len] = NUL;
7845 /* first try vim_getenv(), fast for normal environment vars */
7846 string = vim_getenv(name, &mustfree);
7847 if (string != NULL && *string != NUL)
7848 {
7849 if (!mustfree)
7850 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007852 else
7853 {
7854 if (mustfree)
7855 vim_free(string);
7856
7857 /* next try expanding things like $VIM and ${HOME} */
7858 string = expand_env_save(name - 1);
7859 if (string != NULL && *string == '$')
7860 {
7861 vim_free(string);
7862 string = NULL;
7863 }
7864 }
7865 name[len] = cc;
7866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 rettv->v_type = VAR_STRING;
7868 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870
7871 return OK;
7872}
7873
7874/*
7875 * Array with names and number of arguments of all internal functions
7876 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7877 */
7878static struct fst
7879{
7880 char *f_name; /* function name */
7881 char f_min_argc; /* minimal number of arguments */
7882 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007884 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885} functions[] =
7886{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887#ifdef FEAT_FLOAT
7888 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007889 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007890#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007891 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007892 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"append", 2, 2, f_append},
7894 {"argc", 0, 0, f_argc},
7895 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007896 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007897 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007899 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007900 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007901 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007904 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"bufexists", 1, 1, f_bufexists},
7906 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7907 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7908 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7909 {"buflisted", 1, 1, f_buflisted},
7910 {"bufloaded", 1, 1, f_bufloaded},
7911 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007912 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"bufwinnr", 1, 1, f_bufwinnr},
7914 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007915 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007916 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007918#ifdef FEAT_FLOAT
7919 {"ceil", 1, 1, f_ceil},
7920#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007921 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007922 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007924 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007926#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007927 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007928 {"complete_add", 1, 1, f_complete_add},
7929 {"complete_check", 0, 0, f_complete_check},
7930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007932 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007933#ifdef FEAT_FLOAT
7934 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007935 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007936#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007939 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007940 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"delete", 1, 1, f_delete},
7942 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007943 {"diff_filler", 1, 1, f_diff_filler},
7944 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007945 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"eventhandler", 0, 0, f_eventhandler},
7949 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007950 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007952#ifdef FEAT_FLOAT
7953 {"exp", 1, 1, f_exp},
7954#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007955 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007956 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007957 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7959 {"filereadable", 1, 1, f_filereadable},
7960 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007962 {"finddir", 1, 3, f_finddir},
7963 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965 {"float2nr", 1, 1, f_float2nr},
7966 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007967 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007968#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007969 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"fnamemodify", 2, 2, f_fnamemodify},
7971 {"foldclosed", 1, 1, f_foldclosed},
7972 {"foldclosedend", 1, 1, f_foldclosedend},
7973 {"foldlevel", 1, 1, f_foldlevel},
7974 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007975 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007977 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007978 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007979 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007980 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007981 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"getchar", 0, 1, f_getchar},
7983 {"getcharmod", 0, 0, f_getcharmod},
7984 {"getcmdline", 0, 0, f_getcmdline},
7985 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007986 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02007987 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007989 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007990 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"getfsize", 1, 1, f_getfsize},
7992 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007993 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007994 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007995 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007996 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007997 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007998 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007999 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008000 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008002 {"gettabvar", 2, 3, f_gettabvar},
8003 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"getwinposx", 0, 0, f_getwinposx},
8005 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008006 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008007 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008008 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008011 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008012 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8014 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8015 {"histadd", 2, 2, f_histadd},
8016 {"histdel", 1, 2, f_histdel},
8017 {"histget", 1, 2, f_histget},
8018 {"histnr", 1, 1, f_histnr},
8019 {"hlID", 1, 1, f_hlID},
8020 {"hlexists", 1, 1, f_hlexists},
8021 {"hostname", 0, 0, f_hostname},
8022 {"iconv", 3, 3, f_iconv},
8023 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008025 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008027 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"inputrestore", 0, 0, f_inputrestore},
8029 {"inputsave", 0, 0, f_inputsave},
8030 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008032 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008034 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008036 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"libcall", 3, 3, f_libcall},
8041 {"libcallnr", 3, 3, f_libcallnr},
8042 {"line", 1, 1, f_line},
8043 {"line2byte", 1, 1, f_line2byte},
8044 {"lispindent", 1, 1, f_lispindent},
8045 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008047 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048 {"log10", 1, 1, f_log10},
8049#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008050#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008051 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008052#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008053 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008054 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008055 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008056 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008057 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008059 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008060 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008061 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008062 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008063 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008064 {"max", 1, 1, f_max},
8065 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008066#ifdef vim_mkdir
8067 {"mkdir", 1, 3, f_mkdir},
8068#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008070#ifdef FEAT_MZSCHEME
8071 {"mzeval", 1, 1, f_mzeval},
8072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008074 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008075 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008076 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"pow", 2, 2, f_pow},
8079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008081 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008082 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008083#ifdef FEAT_PYTHON3
8084 {"py3eval", 1, 1, f_py3eval},
8085#endif
8086#ifdef FEAT_PYTHON
8087 {"pyeval", 1, 1, f_pyeval},
8088#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008089 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008090 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008091 {"reltime", 0, 2, f_reltime},
8092 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"remote_expr", 2, 3, f_remote_expr},
8094 {"remote_foreground", 1, 1, f_remote_foreground},
8095 {"remote_peek", 1, 2, f_remote_peek},
8096 {"remote_read", 1, 1, f_remote_read},
8097 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008098 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008100 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008102 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"round", 1, 1, f_round},
8105#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008106 {"screenattr", 2, 2, f_screenattr},
8107 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008108 {"screencol", 0, 0, f_screencol},
8109 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008110 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008111 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008112 {"searchpair", 3, 7, f_searchpair},
8113 {"searchpairpos", 3, 7, f_searchpairpos},
8114 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"server2client", 2, 2, f_server2client},
8116 {"serverlist", 0, 0, f_serverlist},
8117 {"setbufvar", 3, 3, f_setbufvar},
8118 {"setcmdpos", 1, 1, f_setcmdpos},
8119 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008120 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008121 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008122 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008123 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008125 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008126 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008128#ifdef FEAT_CRYPT
8129 {"sha256", 1, 1, f_sha256},
8130#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008131 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008132 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#ifdef FEAT_FLOAT
8135 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008136 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008138 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008139 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008140 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008141 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008142 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143#ifdef FEAT_FLOAT
8144 {"sqrt", 1, 1, f_sqrt},
8145 {"str2float", 1, 1, f_str2float},
8146#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008147 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008148 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008149 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#ifdef HAVE_STRFTIME
8151 {"strftime", 1, 2, f_strftime},
8152#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008153 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"strlen", 1, 1, f_strlen},
8156 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008157 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008159 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008160 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"substitute", 4, 4, f_substitute},
8162 {"synID", 3, 3, f_synID},
8163 {"synIDattr", 2, 3, f_synIDattr},
8164 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008165 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008166 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008167 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008168 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008169 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008170 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008171 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008172 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008173 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008174#ifdef FEAT_FLOAT
8175 {"tan", 1, 1, f_tan},
8176 {"tanh", 1, 1, f_tanh},
8177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008179 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"tolower", 1, 1, f_tolower},
8181 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008182 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008183#ifdef FEAT_FLOAT
8184 {"trunc", 1, 1, f_trunc},
8185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008187 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008188 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008189 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"virtcol", 1, 1, f_virtcol},
8192 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008193 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"winbufnr", 1, 1, f_winbufnr},
8195 {"wincol", 0, 0, f_wincol},
8196 {"winheight", 1, 1, f_winheight},
8197 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008198 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008200 {"winrestview", 1, 1, f_winrestview},
8201 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008203 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008204 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205};
8206
8207#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8208
8209/*
8210 * Function given to ExpandGeneric() to obtain the list of internal
8211 * or user defined function names.
8212 */
8213 char_u *
8214get_function_name(xp, idx)
8215 expand_T *xp;
8216 int idx;
8217{
8218 static int intidx = -1;
8219 char_u *name;
8220
8221 if (idx == 0)
8222 intidx = -1;
8223 if (intidx < 0)
8224 {
8225 name = get_user_func_name(xp, idx);
8226 if (name != NULL)
8227 return name;
8228 }
8229 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8230 {
8231 STRCPY(IObuff, functions[intidx].f_name);
8232 STRCAT(IObuff, "(");
8233 if (functions[intidx].f_max_argc == 0)
8234 STRCAT(IObuff, ")");
8235 return IObuff;
8236 }
8237
8238 return NULL;
8239}
8240
8241/*
8242 * Function given to ExpandGeneric() to obtain the list of internal or
8243 * user defined variable or function names.
8244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 char_u *
8246get_expr_name(xp, idx)
8247 expand_T *xp;
8248 int idx;
8249{
8250 static int intidx = -1;
8251 char_u *name;
8252
8253 if (idx == 0)
8254 intidx = -1;
8255 if (intidx < 0)
8256 {
8257 name = get_function_name(xp, idx);
8258 if (name != NULL)
8259 return name;
8260 }
8261 return get_user_var_name(xp, ++intidx);
8262}
8263
8264#endif /* FEAT_CMDL_COMPL */
8265
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008266#if defined(EBCDIC) || defined(PROTO)
8267/*
8268 * Compare struct fst by function name.
8269 */
8270 static int
8271compare_func_name(s1, s2)
8272 const void *s1;
8273 const void *s2;
8274{
8275 struct fst *p1 = (struct fst *)s1;
8276 struct fst *p2 = (struct fst *)s2;
8277
8278 return STRCMP(p1->f_name, p2->f_name);
8279}
8280
8281/*
8282 * Sort the function table by function name.
8283 * The sorting of the table above is ASCII dependant.
8284 * On machines using EBCDIC we have to sort it.
8285 */
8286 static void
8287sortFunctions()
8288{
8289 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8290
8291 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8292}
8293#endif
8294
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296/*
8297 * Find internal function in table above.
8298 * Return index, or -1 if not found
8299 */
8300 static int
8301find_internal_func(name)
8302 char_u *name; /* name of the function */
8303{
8304 int first = 0;
8305 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8306 int cmp;
8307 int x;
8308
8309 /*
8310 * Find the function name in the table. Binary search.
8311 */
8312 while (first <= last)
8313 {
8314 x = first + ((unsigned)(last - first) >> 1);
8315 cmp = STRCMP(name, functions[x].f_name);
8316 if (cmp < 0)
8317 last = x - 1;
8318 else if (cmp > 0)
8319 first = x + 1;
8320 else
8321 return x;
8322 }
8323 return -1;
8324}
8325
8326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008327 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8328 * name it contains, otherwise return "name".
8329 */
8330 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008331deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008332 char_u *name;
8333 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008334 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335{
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337 int cc;
8338
8339 cc = name[*lenp];
8340 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008341 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008346 {
8347 *lenp = 0;
8348 return (char_u *)""; /* just in case */
8349 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008350 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 }
8353
8354 return name;
8355}
8356
8357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 * Allocate a variable for the result of a function.
8359 * Return OK or FAIL.
8360 */
8361 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008362get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8363 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 char_u *name; /* name of the function */
8365 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 char_u **arg; /* argument, pointing to the '(' */
8368 linenr_T firstline; /* first line of range */
8369 linenr_T lastline; /* last line of range */
8370 int *doesrange; /* return: function handled range */
8371 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 char_u *argp;
8375 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008376 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 int argcount = 0; /* number of arguments found */
8378
8379 /*
8380 * Get the arguments.
8381 */
8382 argp = *arg;
8383 while (argcount < MAX_FUNC_ARGS)
8384 {
8385 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8386 if (*argp == ')' || *argp == ',' || *argp == NUL)
8387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8389 {
8390 ret = FAIL;
8391 break;
8392 }
8393 ++argcount;
8394 if (*argp != ',')
8395 break;
8396 }
8397 if (*argp == ')')
8398 ++argp;
8399 else
8400 ret = FAIL;
8401
8402 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008404 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 {
8407 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008408 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008409 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008410 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
8413 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
8416 *arg = skipwhite(argp);
8417 return ret;
8418}
8419
8420
8421/*
8422 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008423 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008424 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 */
8426 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008427call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008428 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008429 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008433 typval_T *argvars; /* vars for arguments, must have "argcount"
8434 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 linenr_T firstline; /* first line of range */
8436 linenr_T lastline; /* last line of range */
8437 int *doesrange; /* return: function handled range */
8438 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442#define ERROR_UNKNOWN 0
8443#define ERROR_TOOMANY 1
8444#define ERROR_TOOFEW 2
8445#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008446#define ERROR_DICT 4
8447#define ERROR_NONE 5
8448#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 int error = ERROR_NONE;
8450 int i;
8451 int llen;
8452 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#define FLEN_FIXED 40
8454 char_u fname_buf[FLEN_FIXED + 1];
8455 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008456 char_u *name;
8457
8458 /* Make a copy of the name, if it comes from a funcref variable it could
8459 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008460 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008461 if (name == NULL)
8462 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464 /*
8465 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8466 * Change <SNR>123_name() to K_SNR 123_name().
8467 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 llen = eval_fname_script(name);
8470 if (llen > 0)
8471 {
8472 fname_buf[0] = K_SPECIAL;
8473 fname_buf[1] = KS_EXTRA;
8474 fname_buf[2] = (int)KE_SNR;
8475 i = 3;
8476 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8477 {
8478 if (current_SID <= 0)
8479 error = ERROR_SCRIPT;
8480 else
8481 {
8482 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8483 i = (int)STRLEN(fname_buf);
8484 }
8485 }
8486 if (i + STRLEN(name + llen) < FLEN_FIXED)
8487 {
8488 STRCPY(fname_buf + i, name + llen);
8489 fname = fname_buf;
8490 }
8491 else
8492 {
8493 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8494 if (fname == NULL)
8495 error = ERROR_OTHER;
8496 else
8497 {
8498 mch_memmove(fname, fname_buf, (size_t)i);
8499 STRCPY(fname + i, name + llen);
8500 }
8501 }
8502 }
8503 else
8504 fname = name;
8505
8506 *doesrange = FALSE;
8507
8508
8509 /* execute the function if no errors detected and executing */
8510 if (evaluate && error == ERROR_NONE)
8511 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008512 char_u *rfname = fname;
8513
8514 /* Ignore "g:" before a function name. */
8515 if (fname[0] == 'g' && fname[1] == ':')
8516 rfname = fname + 2;
8517
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008518 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8519 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 error = ERROR_UNKNOWN;
8521
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008522 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {
8524 /*
8525 * User defined function.
8526 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008527 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008530 /* Trigger FuncUndefined event, may load the function. */
8531 if (fp == NULL
8532 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008533 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008534 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008536 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008537 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 }
8539#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008540 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008541 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008542 {
8543 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008544 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008545 }
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (fp != NULL)
8548 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008549 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008551 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008553 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008555 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008556 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 else
8558 {
8559 /*
8560 * Call the user function.
8561 * Save and restore search patterns, script variables and
8562 * redo buffer.
8563 */
8564 save_search_patterns();
8565 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008566 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008568 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008569 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8570 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8571 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008572 /* Function was unreferenced while being used, free it
8573 * now. */
8574 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 restoreRedobuff();
8576 restore_search_patterns();
8577 error = ERROR_NONE;
8578 }
8579 }
8580 }
8581 else
8582 {
8583 /*
8584 * Find the function name in the table, call its implementation.
8585 */
8586 i = find_internal_func(fname);
8587 if (i >= 0)
8588 {
8589 if (argcount < functions[i].f_min_argc)
8590 error = ERROR_TOOFEW;
8591 else if (argcount > functions[i].f_max_argc)
8592 error = ERROR_TOOMANY;
8593 else
8594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008595 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 error = ERROR_NONE;
8598 }
8599 }
8600 }
8601 /*
8602 * The function call (or "FuncUndefined" autocommand sequence) might
8603 * have been aborted by an error, an interrupt, or an explicitly thrown
8604 * exception that has not been caught so far. This situation can be
8605 * tested for by calling aborting(). For an error in an internal
8606 * function or for the "E132" error in call_user_func(), however, the
8607 * throw point at which the "force_abort" flag (temporarily reset by
8608 * emsg()) is normally updated has not been reached yet. We need to
8609 * update that flag first to make aborting() reliable.
8610 */
8611 update_force_abort();
8612 }
8613 if (error == ERROR_NONE)
8614 ret = OK;
8615
8616 /*
8617 * Report an error unless the argument evaluation or function call has been
8618 * cancelled due to an aborting error, an interrupt, or an exception.
8619 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008620 if (!aborting())
8621 {
8622 switch (error)
8623 {
8624 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008625 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008626 break;
8627 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008628 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008629 break;
8630 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008631 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008632 name);
8633 break;
8634 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008635 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008636 name);
8637 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008638 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008639 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 name);
8641 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008642 }
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 if (fname != name && fname != fname_buf)
8646 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008647 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 return ret;
8650}
8651
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008652/*
8653 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008654 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008655 */
8656 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008657emsg_funcname(ermsg, name)
8658 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008659 char_u *name;
8660{
8661 char_u *p;
8662
8663 if (*name == K_SPECIAL)
8664 p = concat_str((char_u *)"<SNR>", name + 3);
8665 else
8666 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008667 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008668 if (p != name)
8669 vim_free(p);
8670}
8671
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008672/*
8673 * Return TRUE for a non-zero Number and a non-empty String.
8674 */
8675 static int
8676non_zero_arg(argvars)
8677 typval_T *argvars;
8678{
8679 return ((argvars[0].v_type == VAR_NUMBER
8680 && argvars[0].vval.v_number != 0)
8681 || (argvars[0].v_type == VAR_STRING
8682 && argvars[0].vval.v_string != NULL
8683 && *argvars[0].vval.v_string != NUL));
8684}
8685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686/*********************************************
8687 * Implementation of the built-in functions
8688 */
8689
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008690#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008691static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8692
8693/*
8694 * Get the float value of "argvars[0]" into "f".
8695 * Returns FAIL when the argument is not a Number or Float.
8696 */
8697 static int
8698get_float_arg(argvars, f)
8699 typval_T *argvars;
8700 float_T *f;
8701{
8702 if (argvars[0].v_type == VAR_FLOAT)
8703 {
8704 *f = argvars[0].vval.v_float;
8705 return OK;
8706 }
8707 if (argvars[0].v_type == VAR_NUMBER)
8708 {
8709 *f = (float_T)argvars[0].vval.v_number;
8710 return OK;
8711 }
8712 EMSG(_("E808: Number or Float required"));
8713 return FAIL;
8714}
8715
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008716/*
8717 * "abs(expr)" function
8718 */
8719 static void
8720f_abs(argvars, rettv)
8721 typval_T *argvars;
8722 typval_T *rettv;
8723{
8724 if (argvars[0].v_type == VAR_FLOAT)
8725 {
8726 rettv->v_type = VAR_FLOAT;
8727 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8728 }
8729 else
8730 {
8731 varnumber_T n;
8732 int error = FALSE;
8733
8734 n = get_tv_number_chk(&argvars[0], &error);
8735 if (error)
8736 rettv->vval.v_number = -1;
8737 else if (n > 0)
8738 rettv->vval.v_number = n;
8739 else
8740 rettv->vval.v_number = -n;
8741 }
8742}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008743
8744/*
8745 * "acos()" function
8746 */
8747 static void
8748f_acos(argvars, rettv)
8749 typval_T *argvars;
8750 typval_T *rettv;
8751{
8752 float_T f;
8753
8754 rettv->v_type = VAR_FLOAT;
8755 if (get_float_arg(argvars, &f) == OK)
8756 rettv->vval.v_float = acos(f);
8757 else
8758 rettv->vval.v_float = 0.0;
8759}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008760#endif
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008763 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 */
8765 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008773 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008775 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008776 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008777 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008779 }
8780 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 EMSG(_(e_listreq));
8782}
8783
8784/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008785 * "and(expr, expr)" function
8786 */
8787 static void
8788f_and(argvars, rettv)
8789 typval_T *argvars;
8790 typval_T *rettv;
8791{
8792 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8793 & get_tv_number_chk(&argvars[1], NULL);
8794}
8795
8796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008797 * "append(lnum, string/list)" function
8798 */
8799 static void
8800f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *argvars;
8802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803{
8804 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008805 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 list_T *l = NULL;
8807 listitem_T *li = NULL;
8808 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008809 long added = 0;
8810
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008811 /* When coming here from Insert mode, sync undo, so that this can be
8812 * undone separately from what was previously inserted. */
8813 if (u_sync_once == 2)
8814 {
8815 u_sync_once = 1; /* notify that u_sync() was called */
8816 u_sync(TRUE);
8817 }
8818
Bram Moolenaar0d660222005-01-07 21:51:51 +00008819 lnum = get_tv_lnum(argvars);
8820 if (lnum >= 0
8821 && lnum <= curbuf->b_ml.ml_line_count
8822 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008823 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008824 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008826 l = argvars[1].vval.v_list;
8827 if (l == NULL)
8828 return;
8829 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008830 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008831 for (;;)
8832 {
8833 if (l == NULL)
8834 tv = &argvars[1]; /* append a string */
8835 else if (li == NULL)
8836 break; /* end of list */
8837 else
8838 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 line = get_tv_string_chk(tv);
8840 if (line == NULL) /* type error */
8841 {
8842 rettv->vval.v_number = 1; /* Failed */
8843 break;
8844 }
8845 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008846 ++added;
8847 if (l == NULL)
8848 break;
8849 li = li->li_next;
8850 }
8851
8852 appended_lines_mark(lnum, added);
8853 if (curwin->w_cursor.lnum > lnum)
8854 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008856 else
8857 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858}
8859
8860/*
8861 * "argc()" function
8862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008865 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871/*
8872 * "argidx()" function
8873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008876 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881
8882/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008883 * "arglistid()" function
8884 */
8885 static void
8886f_arglistid(argvars, rettv)
8887 typval_T *argvars UNUSED;
8888 typval_T *rettv;
8889{
8890 win_T *wp;
8891 tabpage_T *tp = NULL;
8892 long n;
8893
8894 rettv->vval.v_number = -1;
8895 if (argvars[0].v_type != VAR_UNKNOWN)
8896 {
8897 if (argvars[1].v_type != VAR_UNKNOWN)
8898 {
8899 n = get_tv_number(&argvars[1]);
8900 if (n >= 0)
8901 tp = find_tabpage(n);
8902 }
8903 else
8904 tp = curtab;
8905
8906 if (tp != NULL)
8907 {
8908 wp = find_win_by_nr(&argvars[0], tp);
8909 if (wp != NULL)
8910 rettv->vval.v_number = wp->w_alist->id;
8911 }
8912 }
8913 else
8914 rettv->vval.v_number = curwin->w_alist->id;
8915}
8916
8917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 * "argv(nr)" function
8919 */
8920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 typval_T *argvars;
8923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 int idx;
8926
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008927 if (argvars[0].v_type != VAR_UNKNOWN)
8928 {
8929 idx = get_tv_number_chk(&argvars[0], NULL);
8930 if (idx >= 0 && idx < ARGCOUNT)
8931 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8932 else
8933 rettv->vval.v_string = NULL;
8934 rettv->v_type = VAR_STRING;
8935 }
8936 else if (rettv_list_alloc(rettv) == OK)
8937 for (idx = 0; idx < ARGCOUNT; ++idx)
8938 list_append_string(rettv->vval.v_list,
8939 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940}
8941
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008942#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008943/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008944 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008945 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008946 static void
8947f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008949 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008951 float_T f;
8952
8953 rettv->v_type = VAR_FLOAT;
8954 if (get_float_arg(argvars, &f) == OK)
8955 rettv->vval.v_float = asin(f);
8956 else
8957 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008958}
8959
8960/*
8961 * "atan()" function
8962 */
8963 static void
8964f_atan(argvars, rettv)
8965 typval_T *argvars;
8966 typval_T *rettv;
8967{
8968 float_T f;
8969
8970 rettv->v_type = VAR_FLOAT;
8971 if (get_float_arg(argvars, &f) == OK)
8972 rettv->vval.v_float = atan(f);
8973 else
8974 rettv->vval.v_float = 0.0;
8975}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008976
8977/*
8978 * "atan2()" function
8979 */
8980 static void
8981f_atan2(argvars, rettv)
8982 typval_T *argvars;
8983 typval_T *rettv;
8984{
8985 float_T fx, fy;
8986
8987 rettv->v_type = VAR_FLOAT;
8988 if (get_float_arg(argvars, &fx) == OK
8989 && get_float_arg(&argvars[1], &fy) == OK)
8990 rettv->vval.v_float = atan2(fx, fy);
8991 else
8992 rettv->vval.v_float = 0.0;
8993}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008994#endif
8995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996/*
8997 * "browse(save, title, initdir, default)" function
8998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009001 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004#ifdef FEAT_BROWSE
9005 int save;
9006 char_u *title;
9007 char_u *initdir;
9008 char_u *defname;
9009 char_u buf[NUMBUFLEN];
9010 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 save = get_tv_number_chk(&argvars[0], &error);
9014 title = get_tv_string_chk(&argvars[1]);
9015 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9016 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 if (error || title == NULL || initdir == NULL || defname == NULL)
9019 rettv->vval.v_string = NULL;
9020 else
9021 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009022 do_browse(save ? BROWSE_SAVE : 0,
9023 title, defname, NULL, initdir, NULL, curbuf);
9024#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009028}
9029
9030/*
9031 * "browsedir(title, initdir)" function
9032 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009037{
9038#ifdef FEAT_BROWSE
9039 char_u *title;
9040 char_u *initdir;
9041 char_u buf[NUMBUFLEN];
9042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 title = get_tv_string_chk(&argvars[0]);
9044 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009046 if (title == NULL || initdir == NULL)
9047 rettv->vval.v_string = NULL;
9048 else
9049 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009050 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
Bram Moolenaar33570922005-01-25 22:26:29 +00009057static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009058
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059/*
9060 * Find a buffer by number or exact name.
9061 */
9062 static buf_T *
9063find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009068 if (avar->v_type == VAR_NUMBER)
9069 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009070 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009072 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009073 if (buf == NULL)
9074 {
9075 /* No full path name match, try a match with a URL or a "nofile"
9076 * buffer, these don't use the full path. */
9077 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9078 if (buf->b_fname != NULL
9079 && (path_with_url(buf->b_fname)
9080#ifdef FEAT_QUICKFIX
9081 || bt_nofile(buf)
9082#endif
9083 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009084 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009085 break;
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 }
9088 return buf;
9089}
9090
9091/*
9092 * "bufexists(expr)" function
9093 */
9094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "buflisted(expr)" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 buf_T *buf;
9111
9112 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114}
9115
9116/*
9117 * "bufloaded(expr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 buf_T *buf;
9125
9126 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009130static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132/*
9133 * Get buffer by number or pattern.
9134 */
9135 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009136get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009138 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 int save_magic;
9142 char_u *save_cpo;
9143 buf_T *buf;
9144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 if (tv->v_type == VAR_NUMBER)
9146 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009147 if (tv->v_type != VAR_STRING)
9148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 if (name == NULL || *name == NUL)
9150 return curbuf;
9151 if (name[0] == '$' && name[1] == NUL)
9152 return lastbuf;
9153
9154 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9155 save_magic = p_magic;
9156 p_magic = TRUE;
9157 save_cpo = p_cpo;
9158 p_cpo = (char_u *)"";
9159
9160 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009161 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
9163 p_magic = save_magic;
9164 p_cpo = save_cpo;
9165
9166 /* If not found, try expanding the name, like done for bufexists(). */
9167 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
9170 return buf;
9171}
9172
9173/*
9174 * "bufname(expr)" function
9175 */
9176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009178 typval_T *argvars;
9179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180{
9181 buf_T *buf;
9182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009185 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 --emsg_off;
9192}
9193
9194/*
9195 * "bufnr(expr)" function
9196 */
9197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009199 typval_T *argvars;
9200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009203 int error = FALSE;
9204 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009208 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009209 --emsg_off;
9210
9211 /* If the buffer isn't found and the second argument is not zero create a
9212 * new buffer. */
9213 if (buf == NULL
9214 && argvars[1].v_type != VAR_UNKNOWN
9215 && get_tv_number_chk(&argvars[1], &error) != 0
9216 && !error
9217 && (name = get_tv_string_chk(&argvars[0])) != NULL
9218 && !error)
9219 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225}
9226
9227/*
9228 * "bufwinnr(nr)" function
9229 */
9230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235#ifdef FEAT_WINDOWS
9236 win_T *wp;
9237 int winnr = 0;
9238#endif
9239 buf_T *buf;
9240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009243 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#ifdef FEAT_WINDOWS
9245 for (wp = firstwin; wp; wp = wp->w_next)
9246 {
9247 ++winnr;
9248 if (wp->w_buffer == buf)
9249 break;
9250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#endif
9255 --emsg_off;
9256}
9257
9258/*
9259 * "byte2line(byte)" function
9260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009262f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009263 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265{
9266#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268#else
9269 long boff = 0;
9270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 (linenr_T)0, &boff);
9277#endif
9278}
9279
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009280 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009281byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 typval_T *argvars;
9283 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009284 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009285{
9286#ifdef FEAT_MBYTE
9287 char_u *t;
9288#endif
9289 char_u *str;
9290 long idx;
9291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 str = get_tv_string_chk(&argvars[0]);
9293 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009296 return;
9297
9298#ifdef FEAT_MBYTE
9299 t = str;
9300 for ( ; idx > 0; idx--)
9301 {
9302 if (*t == NUL) /* EOL reached */
9303 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009304 if (enc_utf8 && comp)
9305 t += utf_ptr2len(t);
9306 else
9307 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009309 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009310#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009311 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009313#endif
9314}
9315
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009316/*
9317 * "byteidx()" function
9318 */
9319 static void
9320f_byteidx(argvars, rettv)
9321 typval_T *argvars;
9322 typval_T *rettv;
9323{
9324 byteidx(argvars, rettv, FALSE);
9325}
9326
9327/*
9328 * "byteidxcomp()" function
9329 */
9330 static void
9331f_byteidxcomp(argvars, rettv)
9332 typval_T *argvars;
9333 typval_T *rettv;
9334{
9335 byteidx(argvars, rettv, TRUE);
9336}
9337
Bram Moolenaardb913952012-06-29 12:54:53 +02009338 int
9339func_call(name, args, selfdict, rettv)
9340 char_u *name;
9341 typval_T *args;
9342 dict_T *selfdict;
9343 typval_T *rettv;
9344{
9345 listitem_T *item;
9346 typval_T argv[MAX_FUNC_ARGS + 1];
9347 int argc = 0;
9348 int dummy;
9349 int r = 0;
9350
9351 for (item = args->vval.v_list->lv_first; item != NULL;
9352 item = item->li_next)
9353 {
9354 if (argc == MAX_FUNC_ARGS)
9355 {
9356 EMSG(_("E699: Too many arguments"));
9357 break;
9358 }
9359 /* Make a copy of each argument. This is needed to be able to set
9360 * v_lock to VAR_FIXED in the copy without changing the original list.
9361 */
9362 copy_tv(&item->li_tv, &argv[argc++]);
9363 }
9364
9365 if (item == NULL)
9366 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9367 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9368 &dummy, TRUE, selfdict);
9369
9370 /* Free the arguments. */
9371 while (argc > 0)
9372 clear_tv(&argv[--argc]);
9373
9374 return r;
9375}
9376
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009378 * "call(func, arglist)" function
9379 */
9380 static void
9381f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009384{
9385 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009387
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009388 if (argvars[1].v_type != VAR_LIST)
9389 {
9390 EMSG(_(e_listreq));
9391 return;
9392 }
9393 if (argvars[1].vval.v_list == NULL)
9394 return;
9395
9396 if (argvars[0].v_type == VAR_FUNC)
9397 func = argvars[0].vval.v_string;
9398 else
9399 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009400 if (*func == NUL)
9401 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009402
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 if (argvars[2].v_type != VAR_UNKNOWN)
9404 {
9405 if (argvars[2].v_type != VAR_DICT)
9406 {
9407 EMSG(_(e_dictreq));
9408 return;
9409 }
9410 selfdict = argvars[2].vval.v_dict;
9411 }
9412
Bram Moolenaardb913952012-06-29 12:54:53 +02009413 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009414}
9415
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009416#ifdef FEAT_FLOAT
9417/*
9418 * "ceil({float})" function
9419 */
9420 static void
9421f_ceil(argvars, rettv)
9422 typval_T *argvars;
9423 typval_T *rettv;
9424{
9425 float_T f;
9426
9427 rettv->v_type = VAR_FLOAT;
9428 if (get_float_arg(argvars, &f) == OK)
9429 rettv->vval.v_float = ceil(f);
9430 else
9431 rettv->vval.v_float = 0.0;
9432}
9433#endif
9434
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009435/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009436 * "changenr()" function
9437 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009438 static void
9439f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009440 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009441 typval_T *rettv;
9442{
9443 rettv->vval.v_number = curbuf->b_u_seq_cur;
9444}
9445
9446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 * "char2nr(string)" function
9448 */
9449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454#ifdef FEAT_MBYTE
9455 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009456 {
9457 int utf8 = 0;
9458
9459 if (argvars[1].v_type != VAR_UNKNOWN)
9460 utf8 = get_tv_number_chk(&argvars[1], NULL);
9461
9462 if (utf8)
9463 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9464 else
9465 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 else
9468#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471
9472/*
9473 * "cindent(lnum)" function
9474 */
9475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
9480#ifdef FEAT_CINDENT
9481 pos_T pos;
9482 linenr_T lnum;
9483
9484 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9487 {
9488 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 curwin->w_cursor = pos;
9491 }
9492 else
9493#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496
9497/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009498 * "clearmatches()" function
9499 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009500 static void
9501f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009502 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009503 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009504{
9505#ifdef FEAT_SEARCH_EXTRA
9506 clear_matches(curwin);
9507#endif
9508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * "col(string)" function
9512 */
9513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 colnr_T col = 0;
9519 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009520 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009522 fp = var2fpos(&argvars[0], FALSE, &fnum);
9523 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
9525 if (fp->col == MAXCOL)
9526 {
9527 /* '> can be MAXCOL, get the length of the line then */
9528 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009529 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 else
9531 col = MAXCOL;
9532 }
9533 else
9534 {
9535 col = fp->col + 1;
9536#ifdef FEAT_VIRTUALEDIT
9537 /* col(".") when the cursor is on the NUL at the end of the line
9538 * because of "coladd" can be seen as an extra column. */
9539 if (virtual_active() && fp == &curwin->w_cursor)
9540 {
9541 char_u *p = ml_get_cursor();
9542
9543 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9544 curwin->w_virtcol - curwin->w_cursor.coladd))
9545 {
9546# ifdef FEAT_MBYTE
9547 int l;
9548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009549 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 col += l;
9551# else
9552 if (*p != NUL && p[1] == NUL)
9553 ++col;
9554# endif
9555 }
9556 }
9557#endif
9558 }
9559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
Bram Moolenaar572cb562005-08-05 21:35:02 +00009563#if defined(FEAT_INS_EXPAND)
9564/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009565 * "complete()" function
9566 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009567 static void
9568f_complete(argvars, rettv)
9569 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009570 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009571{
9572 int startcol;
9573
9574 if ((State & INSERT) == 0)
9575 {
9576 EMSG(_("E785: complete() can only be used in Insert mode"));
9577 return;
9578 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009579
9580 /* Check for undo allowed here, because if something was already inserted
9581 * the line was already saved for undo and this check isn't done. */
9582 if (!undo_allowed())
9583 return;
9584
Bram Moolenaarade00832006-03-10 21:46:58 +00009585 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9586 {
9587 EMSG(_(e_invarg));
9588 return;
9589 }
9590
9591 startcol = get_tv_number_chk(&argvars[0], NULL);
9592 if (startcol <= 0)
9593 return;
9594
9595 set_completion(startcol - 1, argvars[1].vval.v_list);
9596}
9597
9598/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009599 * "complete_add()" function
9600 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009601 static void
9602f_complete_add(argvars, rettv)
9603 typval_T *argvars;
9604 typval_T *rettv;
9605{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009606 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009607}
9608
9609/*
9610 * "complete_check()" function
9611 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009612 static void
9613f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009614 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009615 typval_T *rettv;
9616{
9617 int saved = RedrawingDisabled;
9618
9619 RedrawingDisabled = 0;
9620 ins_compl_check_keys(0);
9621 rettv->vval.v_number = compl_interrupted;
9622 RedrawingDisabled = saved;
9623}
9624#endif
9625
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626/*
9627 * "confirm(message, buttons[, default [, type]])" function
9628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009631 typval_T *argvars UNUSED;
9632 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9635 char_u *message;
9636 char_u *buttons = NULL;
9637 char_u buf[NUMBUFLEN];
9638 char_u buf2[NUMBUFLEN];
9639 int def = 1;
9640 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 char_u *typestr;
9642 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 message = get_tv_string_chk(&argvars[0]);
9645 if (message == NULL)
9646 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009647 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009649 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9650 if (buttons == NULL)
9651 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009655 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009657 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9658 if (typestr == NULL)
9659 error = TRUE;
9660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009662 switch (TOUPPER_ASC(*typestr))
9663 {
9664 case 'E': type = VIM_ERROR; break;
9665 case 'Q': type = VIM_QUESTION; break;
9666 case 'I': type = VIM_INFO; break;
9667 case 'W': type = VIM_WARNING; break;
9668 case 'G': type = VIM_GENERIC; break;
9669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 }
9671 }
9672 }
9673 }
9674
9675 if (buttons == NULL || *buttons == NUL)
9676 buttons = (char_u *)_("&Ok");
9677
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009678 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009679 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009680 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681#endif
9682}
9683
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009684/*
9685 * "copy()" function
9686 */
9687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009692 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009695#ifdef FEAT_FLOAT
9696/*
9697 * "cos()" function
9698 */
9699 static void
9700f_cos(argvars, rettv)
9701 typval_T *argvars;
9702 typval_T *rettv;
9703{
9704 float_T f;
9705
9706 rettv->v_type = VAR_FLOAT;
9707 if (get_float_arg(argvars, &f) == OK)
9708 rettv->vval.v_float = cos(f);
9709 else
9710 rettv->vval.v_float = 0.0;
9711}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009712
9713/*
9714 * "cosh()" function
9715 */
9716 static void
9717f_cosh(argvars, rettv)
9718 typval_T *argvars;
9719 typval_T *rettv;
9720{
9721 float_T f;
9722
9723 rettv->v_type = VAR_FLOAT;
9724 if (get_float_arg(argvars, &f) == OK)
9725 rettv->vval.v_float = cosh(f);
9726 else
9727 rettv->vval.v_float = 0.0;
9728}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009729#endif
9730
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009732 * "count()" function
9733 */
9734 static void
9735f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009736 typval_T *argvars;
9737 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009738{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009739 long n = 0;
9740 int ic = FALSE;
9741
Bram Moolenaare9a41262005-01-15 22:18:47 +00009742 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009743 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 listitem_T *li;
9745 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009747
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 if ((l = argvars[0].vval.v_list) != NULL)
9749 {
9750 li = l->lv_first;
9751 if (argvars[2].v_type != VAR_UNKNOWN)
9752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 int error = FALSE;
9754
9755 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009756 if (argvars[3].v_type != VAR_UNKNOWN)
9757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 idx = get_tv_number_chk(&argvars[3], &error);
9759 if (!error)
9760 {
9761 li = list_find(l, idx);
9762 if (li == NULL)
9763 EMSGN(_(e_listidx), idx);
9764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009765 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009766 if (error)
9767 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009768 }
9769
9770 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009771 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009772 ++n;
9773 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009774 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009775 else if (argvars[0].v_type == VAR_DICT)
9776 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 int todo;
9778 dict_T *d;
9779 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009780
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009781 if ((d = argvars[0].vval.v_dict) != NULL)
9782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 int error = FALSE;
9784
Bram Moolenaare9a41262005-01-15 22:18:47 +00009785 if (argvars[2].v_type != VAR_UNKNOWN)
9786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009788 if (argvars[3].v_type != VAR_UNKNOWN)
9789 EMSG(_(e_invarg));
9790 }
9791
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009792 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009794 {
9795 if (!HASHITEM_EMPTY(hi))
9796 {
9797 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009798 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009799 ++n;
9800 }
9801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009802 }
9803 }
9804 else
9805 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009806 rettv->vval.v_number = n;
9807}
9808
9809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9811 *
9812 * Checks the existence of a cscope connection.
9813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009816 typval_T *argvars UNUSED;
9817 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819#ifdef FEAT_CSCOPE
9820 int num = 0;
9821 char_u *dbpath = NULL;
9822 char_u *prepend = NULL;
9823 char_u buf[NUMBUFLEN];
9824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 if (argvars[0].v_type != VAR_UNKNOWN
9826 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 num = (int)get_tv_number(&argvars[0]);
9829 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 }
9833
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835#endif
9836}
9837
9838/*
9839 * "cursor(lnum, col)" function
9840 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009841 * Moves the cursor to the specified line and column.
9842 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009846 typval_T *argvars;
9847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
9849 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009850#ifdef FEAT_VIRTUALEDIT
9851 long coladd = 0;
9852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009854 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009855 if (argvars[1].v_type == VAR_UNKNOWN)
9856 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009857 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009858 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009859
Bram Moolenaar493c1782014-05-28 14:34:46 +02009860 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009861 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009862 line = pos.lnum;
9863 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009864#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009865 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009866#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009867 if (curswant >= 0)
9868 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009869 }
9870 else
9871 {
9872 line = get_tv_lnum(argvars);
9873 col = get_tv_number_chk(&argvars[1], NULL);
9874#ifdef FEAT_VIRTUALEDIT
9875 if (argvars[2].v_type != VAR_UNKNOWN)
9876 coladd = get_tv_number_chk(&argvars[2], NULL);
9877#endif
9878 }
9879 if (line < 0 || col < 0
9880#ifdef FEAT_VIRTUALEDIT
9881 || coladd < 0
9882#endif
9883 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (line > 0)
9886 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (col > 0)
9888 curwin->w_cursor.col = col - 1;
9889#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009890 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#endif
9892
9893 /* Make sure the cursor is in a valid position. */
9894 check_cursor();
9895#ifdef FEAT_MBYTE
9896 /* Correct cursor for multi-byte character. */
9897 if (has_mbyte)
9898 mb_adjust_cursor();
9899#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009900
9901 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009902 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903}
9904
9905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009906 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 */
9908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 typval_T *argvars;
9911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009913 int noref = 0;
9914
9915 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009917 if (noref < 0 || noref > 1)
9918 EMSG(_(e_invarg));
9919 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009920 {
9921 current_copyID += COPYID_INC;
9922 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924}
9925
9926/*
9927 * "delete()" function
9928 */
9929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 typval_T *argvars;
9932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940/*
9941 * "did_filetype()" function
9942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009945 typval_T *argvars UNUSED;
9946 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950#endif
9951}
9952
9953/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009954 * "diff_filler()" function
9955 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009958 typval_T *argvars UNUSED;
9959 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009960{
9961#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009963#endif
9964}
9965
9966/*
9967 * "diff_hlID()" function
9968 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009971 typval_T *argvars UNUSED;
9972 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009973{
9974#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009976 static linenr_T prev_lnum = 0;
9977 static int changedtick = 0;
9978 static int fnum = 0;
9979 static int change_start = 0;
9980 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009981 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009982 int filler_lines;
9983 int col;
9984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 if (lnum < 0) /* ignore type error in {lnum} arg */
9986 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009987 if (lnum != prev_lnum
9988 || changedtick != curbuf->b_changedtick
9989 || fnum != curbuf->b_fnum)
9990 {
9991 /* New line, buffer, change: need to get the values. */
9992 filler_lines = diff_check(curwin, lnum);
9993 if (filler_lines < 0)
9994 {
9995 if (filler_lines == -1)
9996 {
9997 change_start = MAXCOL;
9998 change_end = -1;
9999 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10000 hlID = HLF_ADD; /* added line */
10001 else
10002 hlID = HLF_CHD; /* changed line */
10003 }
10004 else
10005 hlID = HLF_ADD; /* added line */
10006 }
10007 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010008 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010009 prev_lnum = lnum;
10010 changedtick = curbuf->b_changedtick;
10011 fnum = curbuf->b_fnum;
10012 }
10013
10014 if (hlID == HLF_CHD || hlID == HLF_TXD)
10015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010017 if (col >= change_start && col <= change_end)
10018 hlID = HLF_TXD; /* changed text */
10019 else
10020 hlID = HLF_CHD; /* changed line */
10021 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010022 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010023#endif
10024}
10025
10026/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010027 * "empty({expr})" function
10028 */
10029 static void
10030f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010033{
10034 int n;
10035
10036 switch (argvars[0].v_type)
10037 {
10038 case VAR_STRING:
10039 case VAR_FUNC:
10040 n = argvars[0].vval.v_string == NULL
10041 || *argvars[0].vval.v_string == NUL;
10042 break;
10043 case VAR_NUMBER:
10044 n = argvars[0].vval.v_number == 0;
10045 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010046#ifdef FEAT_FLOAT
10047 case VAR_FLOAT:
10048 n = argvars[0].vval.v_float == 0.0;
10049 break;
10050#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010051 case VAR_LIST:
10052 n = argvars[0].vval.v_list == NULL
10053 || argvars[0].vval.v_list->lv_first == NULL;
10054 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010055 case VAR_DICT:
10056 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010057 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010058 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010059 default:
10060 EMSG2(_(e_intern2), "f_empty()");
10061 n = 0;
10062 }
10063
10064 rettv->vval.v_number = n;
10065}
10066
10067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 * "escape({string}, {chars})" function
10069 */
10070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
10075 char_u buf[NUMBUFLEN];
10076
Bram Moolenaar758711c2005-02-02 23:11:38 +000010077 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10078 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
10081
10082/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010083 * "eval()" function
10084 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010085 static void
10086f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010087 typval_T *argvars;
10088 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010089{
10090 char_u *s;
10091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010092 s = get_tv_string_chk(&argvars[0]);
10093 if (s != NULL)
10094 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10097 {
10098 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010099 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 else if (*s != NUL)
10102 EMSG(_(e_trailing));
10103}
10104
10105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 * "eventhandler()" function
10107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010110 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114}
10115
10116/*
10117 * "executable()" function
10118 */
10119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 typval_T *argvars;
10122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010124 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10125}
10126
10127/*
10128 * "exepath()" function
10129 */
10130 static void
10131f_exepath(argvars, rettv)
10132 typval_T *argvars;
10133 typval_T *rettv;
10134{
10135 char_u *p = NULL;
10136
10137 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10138 rettv->v_type = VAR_STRING;
10139 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
10142/*
10143 * "exists()" function
10144 */
10145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 typval_T *argvars;
10148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149{
10150 char_u *p;
10151 char_u *name;
10152 int n = FALSE;
10153 int len = 0;
10154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 if (*p == '$') /* environment variable */
10157 {
10158 /* first try "normal" environment variables (fast) */
10159 if (mch_getenv(p + 1) != NULL)
10160 n = TRUE;
10161 else
10162 {
10163 /* try expanding things like $VIM and ${HOME} */
10164 p = expand_env_save(p);
10165 if (p != NULL && *p != '$')
10166 n = TRUE;
10167 vim_free(p);
10168 }
10169 }
10170 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010173 if (*skipwhite(p) != NUL)
10174 n = FALSE; /* trailing garbage */
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 else if (*p == '*') /* internal or user defined function */
10177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010178 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 }
10180 else if (*p == ':')
10181 {
10182 n = cmd_exists(p + 1);
10183 }
10184 else if (*p == '#')
10185 {
10186#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010187 if (p[1] == '#')
10188 n = autocmd_supported(p + 2);
10189 else
10190 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191#endif
10192 }
10193 else /* internal variable */
10194 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010195 char_u *tofree;
10196 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010198 /* get_name_len() takes care of expanding curly braces */
10199 name = p;
10200 len = get_name_len(&p, &tofree, TRUE, FALSE);
10201 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010203 if (tofree != NULL)
10204 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010205 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010206 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010208 /* handle d.key, l[idx], f(expr) */
10209 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10210 if (n)
10211 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 }
10213 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010214 if (*p != NUL)
10215 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010217 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 }
10219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221}
10222
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010223#ifdef FEAT_FLOAT
10224/*
10225 * "exp()" function
10226 */
10227 static void
10228f_exp(argvars, rettv)
10229 typval_T *argvars;
10230 typval_T *rettv;
10231{
10232 float_T f;
10233
10234 rettv->v_type = VAR_FLOAT;
10235 if (get_float_arg(argvars, &f) == OK)
10236 rettv->vval.v_float = exp(f);
10237 else
10238 rettv->vval.v_float = 0.0;
10239}
10240#endif
10241
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242/*
10243 * "expand()" function
10244 */
10245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *argvars;
10248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250 char_u *s;
10251 int len;
10252 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010253 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010256 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010259 if (argvars[1].v_type != VAR_UNKNOWN
10260 && argvars[2].v_type != VAR_UNKNOWN
10261 && get_tv_number_chk(&argvars[2], &error)
10262 && !error)
10263 {
10264 rettv->v_type = VAR_LIST;
10265 rettv->vval.v_list = NULL;
10266 }
10267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 if (*s == '%' || *s == '#' || *s == '<')
10270 {
10271 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010272 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010274 if (rettv->v_type == VAR_LIST)
10275 {
10276 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10277 list_append_string(rettv->vval.v_list, result, -1);
10278 else
10279 vim_free(result);
10280 }
10281 else
10282 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 }
10284 else
10285 {
10286 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010287 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 if (argvars[1].v_type != VAR_UNKNOWN
10289 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010290 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 if (!error)
10292 {
10293 ExpandInit(&xpc);
10294 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010295 if (p_wic)
10296 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010297 if (rettv->v_type == VAR_STRING)
10298 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10299 options, WILD_ALL);
10300 else if (rettv_list_alloc(rettv) != FAIL)
10301 {
10302 int i;
10303
10304 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10305 for (i = 0; i < xpc.xp_numfiles; i++)
10306 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10307 ExpandCleanup(&xpc);
10308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 }
10310 else
10311 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 }
10313}
10314
10315/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010316 * Go over all entries in "d2" and add them to "d1".
10317 * When "action" is "error" then a duplicate key is an error.
10318 * When "action" is "force" then a duplicate key is overwritten.
10319 * Otherwise duplicate keys are ignored ("action" is "keep").
10320 */
10321 void
10322dict_extend(d1, d2, action)
10323 dict_T *d1;
10324 dict_T *d2;
10325 char_u *action;
10326{
10327 dictitem_T *di1;
10328 hashitem_T *hi2;
10329 int todo;
10330
10331 todo = (int)d2->dv_hashtab.ht_used;
10332 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10333 {
10334 if (!HASHITEM_EMPTY(hi2))
10335 {
10336 --todo;
10337 di1 = dict_find(d1, hi2->hi_key, -1);
10338 if (d1->dv_scope != 0)
10339 {
10340 /* Disallow replacing a builtin function in l: and g:.
10341 * Check the key to be valid when adding to any
10342 * scope. */
10343 if (d1->dv_scope == VAR_DEF_SCOPE
10344 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10345 && var_check_func_name(hi2->hi_key,
10346 di1 == NULL))
10347 break;
10348 if (!valid_varname(hi2->hi_key))
10349 break;
10350 }
10351 if (di1 == NULL)
10352 {
10353 di1 = dictitem_copy(HI2DI(hi2));
10354 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10355 dictitem_free(di1);
10356 }
10357 else if (*action == 'e')
10358 {
10359 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10360 break;
10361 }
10362 else if (*action == 'f' && HI2DI(hi2) != di1)
10363 {
10364 clear_tv(&di1->di_tv);
10365 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10366 }
10367 }
10368 }
10369}
10370
10371/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010372 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010373 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010374 */
10375 static void
10376f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010379{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010380 char *arg_errmsg = N_("extend() argument");
10381
Bram Moolenaare9a41262005-01-15 22:18:47 +000010382 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010383 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 list_T *l1, *l2;
10385 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010387 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010388
Bram Moolenaare9a41262005-01-15 22:18:47 +000010389 l1 = argvars[0].vval.v_list;
10390 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010391 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010392 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010393 {
10394 if (argvars[2].v_type != VAR_UNKNOWN)
10395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010396 before = get_tv_number_chk(&argvars[2], &error);
10397 if (error)
10398 return; /* type error; errmsg already given */
10399
Bram Moolenaar758711c2005-02-02 23:11:38 +000010400 if (before == l1->lv_len)
10401 item = NULL;
10402 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010404 item = list_find(l1, before);
10405 if (item == NULL)
10406 {
10407 EMSGN(_(e_listidx), before);
10408 return;
10409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010410 }
10411 }
10412 else
10413 item = NULL;
10414 list_extend(l1, l2, item);
10415
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 copy_tv(&argvars[0], rettv);
10417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010419 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10420 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010421 dict_T *d1, *d2;
10422 char_u *action;
10423 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424
10425 d1 = argvars[0].vval.v_dict;
10426 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010427 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010428 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010429 {
10430 /* Check the third argument. */
10431 if (argvars[2].v_type != VAR_UNKNOWN)
10432 {
10433 static char *(av[]) = {"keep", "force", "error"};
10434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 action = get_tv_string_chk(&argvars[2]);
10436 if (action == NULL)
10437 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 for (i = 0; i < 3; ++i)
10439 if (STRCMP(action, av[i]) == 0)
10440 break;
10441 if (i == 3)
10442 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010443 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 return;
10445 }
10446 }
10447 else
10448 action = (char_u *)"force";
10449
Bram Moolenaara9922d62013-05-30 13:01:18 +020010450 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010451
Bram Moolenaare9a41262005-01-15 22:18:47 +000010452 copy_tv(&argvars[0], rettv);
10453 }
10454 }
10455 else
10456 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010457}
10458
10459/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010460 * "feedkeys()" function
10461 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010462 static void
10463f_feedkeys(argvars, rettv)
10464 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010465 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010466{
10467 int remap = TRUE;
10468 char_u *keys, *flags;
10469 char_u nbuf[NUMBUFLEN];
10470 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010471 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010472
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010473 /* This is not allowed in the sandbox. If the commands would still be
10474 * executed in the sandbox it would be OK, but it probably happens later,
10475 * when "sandbox" is no longer set. */
10476 if (check_secure())
10477 return;
10478
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010479 keys = get_tv_string(&argvars[0]);
10480 if (*keys != NUL)
10481 {
10482 if (argvars[1].v_type != VAR_UNKNOWN)
10483 {
10484 flags = get_tv_string_buf(&argvars[1], nbuf);
10485 for ( ; *flags != NUL; ++flags)
10486 {
10487 switch (*flags)
10488 {
10489 case 'n': remap = FALSE; break;
10490 case 'm': remap = TRUE; break;
10491 case 't': typed = TRUE; break;
10492 }
10493 }
10494 }
10495
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010496 /* Need to escape K_SPECIAL and CSI before putting the string in the
10497 * typeahead buffer. */
10498 keys_esc = vim_strsave_escape_csi(keys);
10499 if (keys_esc != NULL)
10500 {
10501 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010502 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010503 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010504 if (vgetc_busy)
10505 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010506 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010507 }
10508}
10509
10510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 * "filereadable()" function
10512 */
10513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010515 typval_T *argvars;
10516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010518 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 char_u *p;
10520 int n;
10521
Bram Moolenaarc236c162008-07-13 17:41:49 +000010522#ifndef O_NONBLOCK
10523# define O_NONBLOCK 0
10524#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010526 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10527 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 {
10529 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010530 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
10532 else
10533 n = FALSE;
10534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536}
10537
10538/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010539 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 * rights to write into.
10541 */
10542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010544 typval_T *argvars;
10545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010547 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548}
10549
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010550static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010551
10552 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010553findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010554 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010556 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010557{
10558#ifdef FEAT_SEARCHPATH
10559 char_u *fname;
10560 char_u *fresult = NULL;
10561 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10562 char_u *p;
10563 char_u pathbuf[NUMBUFLEN];
10564 int count = 1;
10565 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010566 int error = FALSE;
10567#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010568
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010569 rettv->vval.v_string = NULL;
10570 rettv->v_type = VAR_STRING;
10571
10572#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010574
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10578 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010579 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 else
10581 {
10582 if (*p != NUL)
10583 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010586 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010588 }
10589
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010590 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10591 error = TRUE;
10592
10593 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 do
10596 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010597 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010598 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 fresult = find_file_in_path_option(first ? fname : NULL,
10600 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010601 0, first, path,
10602 find_what,
10603 curbuf->b_ffname,
10604 find_what == FINDFILE_DIR
10605 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010607
10608 if (fresult != NULL && rettv->v_type == VAR_LIST)
10609 list_append_string(rettv->vval.v_list, fresult, -1);
10610
10611 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010613
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010614 if (rettv->v_type == VAR_STRING)
10615 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010616#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010617}
10618
Bram Moolenaar33570922005-01-25 22:26:29 +000010619static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10620static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621
10622/*
10623 * Implementation of map() and filter().
10624 */
10625 static void
10626filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010627 typval_T *argvars;
10628 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 int map;
10630{
10631 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 listitem_T *li, *nli;
10634 list_T *l = NULL;
10635 dictitem_T *di;
10636 hashtab_T *ht;
10637 hashitem_T *hi;
10638 dict_T *d = NULL;
10639 typval_T save_val;
10640 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010642 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010643 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10644 char *arg_errmsg = (map ? N_("map() argument")
10645 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010646 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010647 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010651 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010652 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 return;
10654 }
10655 else if (argvars[0].v_type == VAR_DICT)
10656 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010657 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010658 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 return;
10660 }
10661 else
10662 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010663 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010664 return;
10665 }
10666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 expr = get_tv_string_buf_chk(&argvars[1], buf);
10668 /* On type errors, the preceding call has already displayed an error
10669 * message. Avoid a misleading error message for an empty string that
10670 * was not passed as argument. */
10671 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 prepare_vimvar(VV_VAL, &save_val);
10674 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010675
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010676 /* We reset "did_emsg" to be able to detect whether an error
10677 * occurred during evaluation of the expression. */
10678 save_did_emsg = did_emsg;
10679 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010680
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010681 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010682 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010684 vimvars[VV_KEY].vv_type = VAR_STRING;
10685
10686 ht = &d->dv_hashtab;
10687 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010688 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 if (!HASHITEM_EMPTY(hi))
10692 {
10693 --todo;
10694 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010695 if (tv_check_lock(di->di_tv.v_lock,
10696 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 break;
10698 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010699 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010700 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 break;
10702 if (!map && rem)
10703 dictitem_remove(d, di);
10704 clear_tv(&vimvars[VV_KEY].vv_tv);
10705 }
10706 }
10707 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 }
10709 else
10710 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010711 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 for (li = l->lv_first; li != NULL; li = nli)
10714 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010715 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010716 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010718 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010719 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010720 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010721 break;
10722 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010724 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010725 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010726 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010727
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010728 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010730
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010731 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733
10734 copy_tv(&argvars[0], rettv);
10735}
10736
10737 static int
10738filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010740 char_u *expr;
10741 int map;
10742 int *remp;
10743{
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010745 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010746 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749 s = expr;
10750 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010751 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010752 if (*s != NUL) /* check for trailing chars after expr */
10753 {
10754 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010755 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 }
10757 if (map)
10758 {
10759 /* map(): replace the list item value */
10760 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010761 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010762 *tv = rettv;
10763 }
10764 else
10765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 int error = FALSE;
10767
Bram Moolenaare9a41262005-01-15 22:18:47 +000010768 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010770 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 /* On type error, nothing has been removed; return FAIL to stop the
10772 * loop. The error message was given by get_tv_number_chk(). */
10773 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010774 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010775 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010776 retval = OK;
10777theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010779 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010780}
10781
10782/*
10783 * "filter()" function
10784 */
10785 static void
10786f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010787 typval_T *argvars;
10788 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010789{
10790 filter_map(argvars, rettv, FALSE);
10791}
10792
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010793/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794 * "finddir({fname}[, {path}[, {count}]])" function
10795 */
10796 static void
10797f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 typval_T *argvars;
10799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010801 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802}
10803
10804/*
10805 * "findfile({fname}[, {path}[, {count}]])" function
10806 */
10807 static void
10808f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010812 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813}
10814
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010815#ifdef FEAT_FLOAT
10816/*
10817 * "float2nr({float})" function
10818 */
10819 static void
10820f_float2nr(argvars, rettv)
10821 typval_T *argvars;
10822 typval_T *rettv;
10823{
10824 float_T f;
10825
10826 if (get_float_arg(argvars, &f) == OK)
10827 {
10828 if (f < -0x7fffffff)
10829 rettv->vval.v_number = -0x7fffffff;
10830 else if (f > 0x7fffffff)
10831 rettv->vval.v_number = 0x7fffffff;
10832 else
10833 rettv->vval.v_number = (varnumber_T)f;
10834 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010835}
10836
10837/*
10838 * "floor({float})" function
10839 */
10840 static void
10841f_floor(argvars, rettv)
10842 typval_T *argvars;
10843 typval_T *rettv;
10844{
10845 float_T f;
10846
10847 rettv->v_type = VAR_FLOAT;
10848 if (get_float_arg(argvars, &f) == OK)
10849 rettv->vval.v_float = floor(f);
10850 else
10851 rettv->vval.v_float = 0.0;
10852}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010853
10854/*
10855 * "fmod()" function
10856 */
10857 static void
10858f_fmod(argvars, rettv)
10859 typval_T *argvars;
10860 typval_T *rettv;
10861{
10862 float_T fx, fy;
10863
10864 rettv->v_type = VAR_FLOAT;
10865 if (get_float_arg(argvars, &fx) == OK
10866 && get_float_arg(&argvars[1], &fy) == OK)
10867 rettv->vval.v_float = fmod(fx, fy);
10868 else
10869 rettv->vval.v_float = 0.0;
10870}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010871#endif
10872
Bram Moolenaar0d660222005-01-07 21:51:51 +000010873/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010874 * "fnameescape({string})" function
10875 */
10876 static void
10877f_fnameescape(argvars, rettv)
10878 typval_T *argvars;
10879 typval_T *rettv;
10880{
10881 rettv->vval.v_string = vim_strsave_fnameescape(
10882 get_tv_string(&argvars[0]), FALSE);
10883 rettv->v_type = VAR_STRING;
10884}
10885
10886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 * "fnamemodify({fname}, {mods})" function
10888 */
10889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010891 typval_T *argvars;
10892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 char_u *fname;
10895 char_u *mods;
10896 int usedlen = 0;
10897 int len;
10898 char_u *fbuf = NULL;
10899 char_u buf[NUMBUFLEN];
10900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 fname = get_tv_string_chk(&argvars[0]);
10902 mods = get_tv_string_buf_chk(&argvars[1], buf);
10903 if (fname == NULL || mods == NULL)
10904 fname = NULL;
10905 else
10906 {
10907 len = (int)STRLEN(fname);
10908 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 vim_free(fbuf);
10917}
10918
Bram Moolenaar33570922005-01-25 22:26:29 +000010919static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920
10921/*
10922 * "foldclosed()" function
10923 */
10924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010928 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
10930#ifdef FEAT_FOLDING
10931 linenr_T lnum;
10932 linenr_T first, last;
10933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10936 {
10937 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10938 {
10939 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 return;
10944 }
10945 }
10946#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948}
10949
10950/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010951 * "foldclosed()" function
10952 */
10953 static void
10954f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010957{
10958 foldclosed_both(argvars, rettv, FALSE);
10959}
10960
10961/*
10962 * "foldclosedend()" function
10963 */
10964 static void
10965f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010968{
10969 foldclosed_both(argvars, rettv, TRUE);
10970}
10971
10972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 * "foldlevel()" function
10974 */
10975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010977 typval_T *argvars UNUSED;
10978 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979{
10980#ifdef FEAT_FOLDING
10981 linenr_T lnum;
10982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987}
10988
10989/*
10990 * "foldtext()" function
10991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996{
10997#ifdef FEAT_FOLDING
10998 linenr_T lnum;
10999 char_u *s;
11000 char_u *r;
11001 int len;
11002 char *txt;
11003#endif
11004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 rettv->v_type = VAR_STRING;
11006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011008 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11009 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11010 <= curbuf->b_ml.ml_line_count
11011 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 {
11013 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11015 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 {
11017 if (!linewhite(lnum))
11018 break;
11019 ++lnum;
11020 }
11021
11022 /* Find interesting text in this line. */
11023 s = skipwhite(ml_get(lnum));
11024 /* skip C comment-start */
11025 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011028 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011030 {
11031 s = skipwhite(ml_get(lnum + 1));
11032 if (*s == '*')
11033 s = skipwhite(s + 1);
11034 }
11035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 txt = _("+-%s%3ld lines: ");
11037 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011038 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 + 20 /* for %3ld */
11040 + STRLEN(s))); /* concatenated */
11041 if (r != NULL)
11042 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11044 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11045 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 len = (int)STRLEN(r);
11047 STRCAT(r, s);
11048 /* remove 'foldmarker' and 'commentstring' */
11049 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 }
11052 }
11053#endif
11054}
11055
11056/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011057 * "foldtextresult(lnum)" function
11058 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011062 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011063{
11064#ifdef FEAT_FOLDING
11065 linenr_T lnum;
11066 char_u *text;
11067 char_u buf[51];
11068 foldinfo_T foldinfo;
11069 int fold_count;
11070#endif
11071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072 rettv->v_type = VAR_STRING;
11073 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011074#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 /* treat illegal types and illegal string values for {lnum} the same */
11077 if (lnum < 0)
11078 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011079 fold_count = foldedCount(curwin, lnum, &foldinfo);
11080 if (fold_count > 0)
11081 {
11082 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11083 &foldinfo, buf);
11084 if (text == buf)
11085 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011087 }
11088#endif
11089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "foreground()" function
11093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011096 typval_T *argvars UNUSED;
11097 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099#ifdef FEAT_GUI
11100 if (gui.in_use)
11101 gui_mch_set_foreground();
11102#else
11103# ifdef WIN32
11104 win32_set_foreground();
11105# endif
11106#endif
11107}
11108
11109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011110 * "function()" function
11111 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T *argvars;
11115 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011116{
11117 char_u *s;
11118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011120 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011121 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011122 /* Don't check an autoload name for existence here. */
11123 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011124 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011125 else
11126 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011127 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011128 {
11129 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011130 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011131
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011132 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11133 * also be called from another script. Using trans_function_name()
11134 * would also work, but some plugins depend on the name being
11135 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011136 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011137 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011138 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011139 if (rettv->vval.v_string != NULL)
11140 {
11141 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011142 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011143 }
11144 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011145 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011146 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011148 }
11149}
11150
11151/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011152 * "garbagecollect()" function
11153 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011154 static void
11155f_garbagecollect(argvars, rettv)
11156 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011157 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011158{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011159 /* This is postponed until we are back at the toplevel, because we may be
11160 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11161 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011162
11163 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11164 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011165}
11166
11167/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 * "get()" function
11169 */
11170 static void
11171f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 typval_T *argvars;
11173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011174{
Bram Moolenaar33570922005-01-25 22:26:29 +000011175 listitem_T *li;
11176 list_T *l;
11177 dictitem_T *di;
11178 dict_T *d;
11179 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 int error = FALSE;
11186
11187 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11188 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011192 else if (argvars[0].v_type == VAR_DICT)
11193 {
11194 if ((d = argvars[0].vval.v_dict) != NULL)
11195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011196 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011197 if (di != NULL)
11198 tv = &di->di_tv;
11199 }
11200 }
11201 else
11202 EMSG2(_(e_listdictarg), "get()");
11203
11204 if (tv == NULL)
11205 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011207 copy_tv(&argvars[2], rettv);
11208 }
11209 else
11210 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211}
11212
Bram Moolenaar342337a2005-07-21 21:11:17 +000011213static 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 +000011214
11215/*
11216 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011217 * Return a range (from start to end) of lines in rettv from the specified
11218 * buffer.
11219 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011220 */
11221 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011222get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011223 buf_T *buf;
11224 linenr_T start;
11225 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011226 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011227 typval_T *rettv;
11228{
11229 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011230
Bram Moolenaar959a1432013-12-14 12:17:38 +010011231 rettv->v_type = VAR_STRING;
11232 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011233 if (retlist && rettv_list_alloc(rettv) == FAIL)
11234 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011235
11236 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11237 return;
11238
11239 if (!retlist)
11240 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011241 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11242 p = ml_get_buf(buf, start, FALSE);
11243 else
11244 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011245 rettv->vval.v_string = vim_strsave(p);
11246 }
11247 else
11248 {
11249 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011250 return;
11251
11252 if (start < 1)
11253 start = 1;
11254 if (end > buf->b_ml.ml_line_count)
11255 end = buf->b_ml.ml_line_count;
11256 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011257 if (list_append_string(rettv->vval.v_list,
11258 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011259 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011260 }
11261}
11262
11263/*
11264 * "getbufline()" function
11265 */
11266 static void
11267f_getbufline(argvars, rettv)
11268 typval_T *argvars;
11269 typval_T *rettv;
11270{
11271 linenr_T lnum;
11272 linenr_T end;
11273 buf_T *buf;
11274
11275 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11276 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011277 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011278 --emsg_off;
11279
Bram Moolenaar661b1822005-07-28 22:36:45 +000011280 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011281 if (argvars[2].v_type == VAR_UNKNOWN)
11282 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011283 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011284 end = get_tv_lnum_buf(&argvars[2], buf);
11285
Bram Moolenaar342337a2005-07-21 21:11:17 +000011286 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011287}
11288
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289/*
11290 * "getbufvar()" function
11291 */
11292 static void
11293f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296{
11297 buf_T *buf;
11298 buf_T *save_curbuf;
11299 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011301 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11304 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011306 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011308 rettv->v_type = VAR_STRING;
11309 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011310
11311 if (buf != NULL && varname != NULL)
11312 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011313 /* set curbuf to be our buf, temporarily */
11314 save_curbuf = curbuf;
11315 curbuf = buf;
11316
Bram Moolenaar0d660222005-01-07 21:51:51 +000011317 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011318 {
11319 if (get_option_tv(&varname, rettv, TRUE) == OK)
11320 done = TRUE;
11321 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011322 else if (STRCMP(varname, "changedtick") == 0)
11323 {
11324 rettv->v_type = VAR_NUMBER;
11325 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011326 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011328 else
11329 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011330 /* Look up the variable. */
11331 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11332 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11333 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011334 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011335 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011337 done = TRUE;
11338 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011340
11341 /* restore previous notion of curbuf */
11342 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343 }
11344
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011345 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11346 /* use the default value */
11347 copy_tv(&argvars[2], rettv);
11348
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349 --emsg_off;
11350}
11351
11352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 * "getchar()" function
11354 */
11355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011357 typval_T *argvars;
11358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
11360 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011363 /* Position the cursor. Needed after a message that ends in a space. */
11364 windgoto(msg_row, msg_col);
11365
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 ++no_mapping;
11367 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011368 for (;;)
11369 {
11370 if (argvars[0].v_type == VAR_UNKNOWN)
11371 /* getchar(): blocking wait. */
11372 n = safe_vgetc();
11373 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11374 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011375 n = vpeekc_any();
11376 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011377 /* illegal argument or getchar(0) and no char avail: return zero */
11378 n = 0;
11379 else
11380 /* getchar(0) and char avail: return char */
11381 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011382
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011383 if (n == K_IGNORE)
11384 continue;
11385 break;
11386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 --no_mapping;
11388 --allow_keys;
11389
Bram Moolenaar219b8702006-11-01 14:32:36 +000011390 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11391 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11392 vimvars[VV_MOUSE_COL].vv_nr = 0;
11393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 if (IS_SPECIAL(n) || mod_mask != 0)
11396 {
11397 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11398 int i = 0;
11399
11400 /* Turn a special key into three bytes, plus modifier. */
11401 if (mod_mask != 0)
11402 {
11403 temp[i++] = K_SPECIAL;
11404 temp[i++] = KS_MODIFIER;
11405 temp[i++] = mod_mask;
11406 }
11407 if (IS_SPECIAL(n))
11408 {
11409 temp[i++] = K_SPECIAL;
11410 temp[i++] = K_SECOND(n);
11411 temp[i++] = K_THIRD(n);
11412 }
11413#ifdef FEAT_MBYTE
11414 else if (has_mbyte)
11415 i += (*mb_char2bytes)(n, temp + i);
11416#endif
11417 else
11418 temp[i++] = n;
11419 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->v_type = VAR_STRING;
11421 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011422
11423#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011424 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011425 {
11426 int row = mouse_row;
11427 int col = mouse_col;
11428 win_T *win;
11429 linenr_T lnum;
11430# ifdef FEAT_WINDOWS
11431 win_T *wp;
11432# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011433 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011434
11435 if (row >= 0 && col >= 0)
11436 {
11437 /* Find the window at the mouse coordinates and compute the
11438 * text position. */
11439 win = mouse_find_win(&row, &col);
11440 (void)mouse_comp_pos(win, &row, &col, &lnum);
11441# ifdef FEAT_WINDOWS
11442 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011443 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011444# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011445 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011446 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11447 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11448 }
11449 }
11450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 }
11452}
11453
11454/*
11455 * "getcharmod()" function
11456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011459 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
11465/*
11466 * "getcmdline()" function
11467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011470 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->v_type = VAR_STRING;
11474 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475}
11476
11477/*
11478 * "getcmdpos()" function
11479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011489 * "getcmdtype()" function
11490 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011491 static void
11492f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011493 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011494 typval_T *rettv;
11495{
11496 rettv->v_type = VAR_STRING;
11497 rettv->vval.v_string = alloc(2);
11498 if (rettv->vval.v_string != NULL)
11499 {
11500 rettv->vval.v_string[0] = get_cmdline_type();
11501 rettv->vval.v_string[1] = NUL;
11502 }
11503}
11504
11505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 * "getcwd()" function
11507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011513 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011516 rettv->vval.v_string = NULL;
11517 cwd = alloc(MAXPATHL);
11518 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011520 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11521 {
11522 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011524 if (rettv->vval.v_string != NULL)
11525 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011527 }
11528 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 }
11530}
11531
11532/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011533 * "getfontname()" function
11534 */
11535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011538 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->v_type = VAR_STRING;
11541 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011542#ifdef FEAT_GUI
11543 if (gui.in_use)
11544 {
11545 GuiFont font;
11546 char_u *name = NULL;
11547
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011548 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011549 {
11550 /* Get the "Normal" font. Either the name saved by
11551 * hl_set_font_name() or from the font ID. */
11552 font = gui.norm_font;
11553 name = hl_get_font_name();
11554 }
11555 else
11556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011558 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11559 return;
11560 font = gui_mch_get_font(name, FALSE);
11561 if (font == NOFONT)
11562 return; /* Invalid font name, return empty string. */
11563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011565 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011566 gui_mch_free_font(font);
11567 }
11568#endif
11569}
11570
11571/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011572 * "getfperm({fname})" function
11573 */
11574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011576 typval_T *argvars;
11577 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011578{
11579 char_u *fname;
11580 struct stat st;
11581 char_u *perm = NULL;
11582 char_u flags[] = "rwx";
11583 int i;
11584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011588 if (mch_stat((char *)fname, &st) >= 0)
11589 {
11590 perm = vim_strsave((char_u *)"---------");
11591 if (perm != NULL)
11592 {
11593 for (i = 0; i < 9; i++)
11594 {
11595 if (st.st_mode & (1 << (8 - i)))
11596 perm[i] = flags[i % 3];
11597 }
11598 }
11599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011601}
11602
11603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 * "getfsize({fname})" function
11605 */
11606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011608 typval_T *argvars;
11609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
11611 char_u *fname;
11612 struct stat st;
11613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617
11618 if (mch_stat((char *)fname, &st) >= 0)
11619 {
11620 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011625
11626 /* non-perfect check for overflow */
11627 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11628 rettv->vval.v_number = -2;
11629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 }
11631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
11635/*
11636 * "getftime({fname})" function
11637 */
11638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011640 typval_T *argvars;
11641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
11643 char_u *fname;
11644 struct stat st;
11645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647
11648 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652}
11653
11654/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011655 * "getftype({fname})" function
11656 */
11657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011661{
11662 char_u *fname;
11663 struct stat st;
11664 char_u *type = NULL;
11665 char *t;
11666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011670 if (mch_lstat((char *)fname, &st) >= 0)
11671 {
11672#ifdef S_ISREG
11673 if (S_ISREG(st.st_mode))
11674 t = "file";
11675 else if (S_ISDIR(st.st_mode))
11676 t = "dir";
11677# ifdef S_ISLNK
11678 else if (S_ISLNK(st.st_mode))
11679 t = "link";
11680# endif
11681# ifdef S_ISBLK
11682 else if (S_ISBLK(st.st_mode))
11683 t = "bdev";
11684# endif
11685# ifdef S_ISCHR
11686 else if (S_ISCHR(st.st_mode))
11687 t = "cdev";
11688# endif
11689# ifdef S_ISFIFO
11690 else if (S_ISFIFO(st.st_mode))
11691 t = "fifo";
11692# endif
11693# ifdef S_ISSOCK
11694 else if (S_ISSOCK(st.st_mode))
11695 t = "fifo";
11696# endif
11697 else
11698 t = "other";
11699#else
11700# ifdef S_IFMT
11701 switch (st.st_mode & S_IFMT)
11702 {
11703 case S_IFREG: t = "file"; break;
11704 case S_IFDIR: t = "dir"; break;
11705# ifdef S_IFLNK
11706 case S_IFLNK: t = "link"; break;
11707# endif
11708# ifdef S_IFBLK
11709 case S_IFBLK: t = "bdev"; break;
11710# endif
11711# ifdef S_IFCHR
11712 case S_IFCHR: t = "cdev"; break;
11713# endif
11714# ifdef S_IFIFO
11715 case S_IFIFO: t = "fifo"; break;
11716# endif
11717# ifdef S_IFSOCK
11718 case S_IFSOCK: t = "socket"; break;
11719# endif
11720 default: t = "other";
11721 }
11722# else
11723 if (mch_isdir(fname))
11724 t = "dir";
11725 else
11726 t = "file";
11727# endif
11728#endif
11729 type = vim_strsave((char_u *)t);
11730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011732}
11733
11734/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011735 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736 */
11737 static void
11738f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 typval_T *argvars;
11740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741{
11742 linenr_T lnum;
11743 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011744 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011745
11746 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011747 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011748 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011749 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011750 retlist = FALSE;
11751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011753 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011754 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011755 retlist = TRUE;
11756 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011757
Bram Moolenaar342337a2005-07-21 21:11:17 +000011758 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011759}
11760
11761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011762 * "getmatches()" function
11763 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011764 static void
11765f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011766 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011767 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011768{
11769#ifdef FEAT_SEARCH_EXTRA
11770 dict_T *dict;
11771 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011772 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011773
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011774 if (rettv_list_alloc(rettv) == OK)
11775 {
11776 while (cur != NULL)
11777 {
11778 dict = dict_alloc();
11779 if (dict == NULL)
11780 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011781 if (cur->match.regprog == NULL)
11782 {
11783 /* match added with matchaddpos() */
11784 for (i = 0; i < MAXPOSMATCH; ++i)
11785 {
11786 llpos_T *llpos;
11787 char buf[6];
11788 list_T *l;
11789
11790 llpos = &cur->pos.pos[i];
11791 if (llpos->lnum == 0)
11792 break;
11793 l = list_alloc();
11794 if (l == NULL)
11795 break;
11796 list_append_number(l, (varnumber_T)llpos->lnum);
11797 if (llpos->col > 0)
11798 {
11799 list_append_number(l, (varnumber_T)llpos->col);
11800 list_append_number(l, (varnumber_T)llpos->len);
11801 }
11802 sprintf(buf, "pos%d", i + 1);
11803 dict_add_list(dict, buf, l);
11804 }
11805 }
11806 else
11807 {
11808 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11809 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011810 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011811 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11812 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11813 list_append_dict(rettv->vval.v_list, dict);
11814 cur = cur->next;
11815 }
11816 }
11817#endif
11818}
11819
11820/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011821 * "getpid()" function
11822 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011823 static void
11824f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011825 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011826 typval_T *rettv;
11827{
11828 rettv->vval.v_number = mch_get_pid();
11829}
11830
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011831static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11832
11833/*
11834 * "getcurpos()" function
11835 */
11836 static void
11837f_getcurpos(argvars, rettv)
11838 typval_T *argvars;
11839 typval_T *rettv;
11840{
11841 getpos_both(argvars, rettv, TRUE);
11842}
11843
Bram Moolenaar18081e32008-02-20 19:11:07 +000011844/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011845 * "getpos(string)" function
11846 */
11847 static void
11848f_getpos(argvars, rettv)
11849 typval_T *argvars;
11850 typval_T *rettv;
11851{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011852 getpos_both(argvars, rettv, FALSE);
11853}
11854
11855 static void
11856getpos_both(argvars, rettv, getcurpos)
11857 typval_T *argvars;
11858 typval_T *rettv;
11859 int getcurpos;
11860{
Bram Moolenaara5525202006-03-02 22:52:09 +000011861 pos_T *fp;
11862 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011863 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011864
11865 if (rettv_list_alloc(rettv) == OK)
11866 {
11867 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011868 if (getcurpos)
11869 fp = &curwin->w_cursor;
11870 else
11871 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011872 if (fnum != -1)
11873 list_append_number(l, (varnumber_T)fnum);
11874 else
11875 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011876 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11877 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011878 list_append_number(l, (fp != NULL)
11879 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011880 : (varnumber_T)0);
11881 list_append_number(l,
11882#ifdef FEAT_VIRTUALEDIT
11883 (fp != NULL) ? (varnumber_T)fp->coladd :
11884#endif
11885 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011886 if (getcurpos)
Bram Moolenaar493c1782014-05-28 14:34:46 +020011887 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011888 }
11889 else
11890 rettv->vval.v_number = FALSE;
11891}
11892
11893/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011894 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011895 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011896 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011897f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011898 typval_T *argvars UNUSED;
11899 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011900{
11901#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011902 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011903#endif
11904
Bram Moolenaar2641f772005-03-25 21:58:17 +000011905#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011906 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011907 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011908 wp = NULL;
11909 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11910 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011911 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011912 if (wp == NULL)
11913 return;
11914 }
11915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011916 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011917 }
11918#endif
11919}
11920
11921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 * "getreg()" function
11923 */
11924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 typval_T *argvars;
11927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928{
11929 char_u *strregname;
11930 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011931 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011932 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 strregname = get_tv_string_chk(&argvars[0]);
11938 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011942 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11943 return_list = get_tv_number_chk(&argvars[2], &error);
11944 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011948
11949 if (error)
11950 return;
11951
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 regname = (strregname == NULL ? '"' : *strregname);
11953 if (regname == 0)
11954 regname = '"';
11955
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011956 if (return_list)
11957 {
11958 rettv->v_type = VAR_LIST;
11959 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11960 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11961 }
11962 else
11963 {
11964 rettv->v_type = VAR_STRING;
11965 rettv->vval.v_string = get_reg_contents(regname,
11966 arg2 ? GREG_EXPR_SRC : 0);
11967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968}
11969
11970/*
11971 * "getregtype()" function
11972 */
11973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978 char_u *strregname;
11979 int regname;
11980 char_u buf[NUMBUFLEN + 2];
11981 long reglen = 0;
11982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011983 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 {
11985 strregname = get_tv_string_chk(&argvars[0]);
11986 if (strregname == NULL) /* type error; errmsg already given */
11987 {
11988 rettv->v_type = VAR_STRING;
11989 rettv->vval.v_string = NULL;
11990 return;
11991 }
11992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 else
11994 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011995 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996
11997 regname = (strregname == NULL ? '"' : *strregname);
11998 if (regname == 0)
11999 regname = '"';
12000
12001 buf[0] = NUL;
12002 buf[1] = NUL;
12003 switch (get_reg_type(regname, &reglen))
12004 {
12005 case MLINE: buf[0] = 'V'; break;
12006 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 case MBLOCK:
12008 buf[0] = Ctrl_V;
12009 sprintf((char *)buf + 1, "%ld", reglen + 1);
12010 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 rettv->v_type = VAR_STRING;
12013 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012017 * "gettabvar()" function
12018 */
12019 static void
12020f_gettabvar(argvars, rettv)
12021 typval_T *argvars;
12022 typval_T *rettv;
12023{
12024 tabpage_T *tp;
12025 dictitem_T *v;
12026 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012027 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012028
12029 rettv->v_type = VAR_STRING;
12030 rettv->vval.v_string = NULL;
12031
12032 varname = get_tv_string_chk(&argvars[1]);
12033 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12034 if (tp != NULL && varname != NULL)
12035 {
12036 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020012037 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012038 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012039 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012040 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012041 done = TRUE;
12042 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012043 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012044
12045 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012046 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012047}
12048
12049/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012050 * "gettabwinvar()" function
12051 */
12052 static void
12053f_gettabwinvar(argvars, rettv)
12054 typval_T *argvars;
12055 typval_T *rettv;
12056{
12057 getwinvar(argvars, rettv, 1);
12058}
12059
12060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 * "getwinposx()" function
12062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012065 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069#ifdef FEAT_GUI
12070 if (gui.in_use)
12071 {
12072 int x, y;
12073
12074 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 }
12077#endif
12078}
12079
12080/*
12081 * "getwinposy()" function
12082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#ifdef FEAT_GUI
12090 if (gui.in_use)
12091 {
12092 int x, y;
12093
12094 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 }
12097#endif
12098}
12099
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012100/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012101 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012102 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012103 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012104find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012105 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012106 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012107{
12108#ifdef FEAT_WINDOWS
12109 win_T *wp;
12110#endif
12111 int nr;
12112
12113 nr = get_tv_number_chk(vp, NULL);
12114
12115#ifdef FEAT_WINDOWS
12116 if (nr < 0)
12117 return NULL;
12118 if (nr == 0)
12119 return curwin;
12120
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012121 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12122 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012123 if (--nr <= 0)
12124 break;
12125 return wp;
12126#else
12127 if (nr == 0 || nr == 1)
12128 return curwin;
12129 return NULL;
12130#endif
12131}
12132
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133/*
12134 * "getwinvar()" function
12135 */
12136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 typval_T *argvars;
12139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012141 getwinvar(argvars, rettv, 0);
12142}
12143
12144/*
12145 * getwinvar() and gettabwinvar()
12146 */
12147 static void
12148getwinvar(argvars, rettv, off)
12149 typval_T *argvars;
12150 typval_T *rettv;
12151 int off; /* 1 for gettabwinvar() */
12152{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 win_T *win, *oldcurwin;
12154 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012156 tabpage_T *tp = NULL;
12157 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012158 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012160#ifdef FEAT_WINDOWS
12161 if (off == 1)
12162 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12163 else
12164 tp = curtab;
12165#endif
12166 win = find_win_by_nr(&argvars[off], tp);
12167 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012168 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012170 rettv->v_type = VAR_STRING;
12171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172
12173 if (win != NULL && varname != NULL)
12174 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012175 /* Set curwin to be our win, temporarily. Also set the tabpage,
12176 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012177 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012178
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012180 {
12181 if (get_option_tv(&varname, rettv, 1) == OK)
12182 done = TRUE;
12183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 else
12185 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012186 /* Look up the variable. */
12187 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12188 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012190 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012192 done = TRUE;
12193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012195
12196 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012197 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 }
12199
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012200 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12201 /* use the default return value */
12202 copy_tv(&argvars[off + 2], rettv);
12203
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 --emsg_off;
12205}
12206
12207/*
12208 * "glob()" function
12209 */
12210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012212 typval_T *argvars;
12213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012215 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012217 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012219 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012220 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012222 if (argvars[1].v_type != VAR_UNKNOWN)
12223 {
12224 if (get_tv_number_chk(&argvars[1], &error))
12225 options |= WILD_KEEP_ALL;
12226 if (argvars[2].v_type != VAR_UNKNOWN
12227 && get_tv_number_chk(&argvars[2], &error))
12228 {
12229 rettv->v_type = VAR_LIST;
12230 rettv->vval.v_list = NULL;
12231 }
12232 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012233 if (!error)
12234 {
12235 ExpandInit(&xpc);
12236 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012237 if (p_wic)
12238 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012239 if (rettv->v_type == VAR_STRING)
12240 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012241 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012242 else if (rettv_list_alloc(rettv) != FAIL)
12243 {
12244 int i;
12245
12246 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12247 NULL, options, WILD_ALL_KEEP);
12248 for (i = 0; i < xpc.xp_numfiles; i++)
12249 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12250
12251 ExpandCleanup(&xpc);
12252 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012253 }
12254 else
12255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256}
12257
12258/*
12259 * "globpath()" function
12260 */
12261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012263 typval_T *argvars;
12264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012266 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012269 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012270 garray_T ga;
12271 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012273 /* When the optional second argument is non-zero, don't remove matches
12274 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012276 if (argvars[2].v_type != VAR_UNKNOWN)
12277 {
12278 if (get_tv_number_chk(&argvars[2], &error))
12279 flags |= WILD_KEEP_ALL;
12280 if (argvars[3].v_type != VAR_UNKNOWN
12281 && get_tv_number_chk(&argvars[3], &error))
12282 {
12283 rettv->v_type = VAR_LIST;
12284 rettv->vval.v_list = NULL;
12285 }
12286 }
12287 if (file != NULL && !error)
12288 {
12289 ga_init2(&ga, (int)sizeof(char_u *), 10);
12290 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12291 if (rettv->v_type == VAR_STRING)
12292 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12293 else if (rettv_list_alloc(rettv) != FAIL)
12294 for (i = 0; i < ga.ga_len; ++i)
12295 list_append_string(rettv->vval.v_list,
12296 ((char_u **)(ga.ga_data))[i], -1);
12297 ga_clear_strings(&ga);
12298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012299 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012300 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301}
12302
12303/*
12304 * "has()" function
12305 */
12306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012308 typval_T *argvars;
12309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310{
12311 int i;
12312 char_u *name;
12313 int n = FALSE;
12314 static char *(has_list[]) =
12315 {
12316#ifdef AMIGA
12317 "amiga",
12318# ifdef FEAT_ARP
12319 "arp",
12320# endif
12321#endif
12322#ifdef __BEOS__
12323 "beos",
12324#endif
12325#ifdef MSDOS
12326# ifdef DJGPP
12327 "dos32",
12328# else
12329 "dos16",
12330# endif
12331#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012332#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 "mac",
12334#endif
12335#if defined(MACOS_X_UNIX)
12336 "macunix",
12337#endif
12338#ifdef OS2
12339 "os2",
12340#endif
12341#ifdef __QNX__
12342 "qnx",
12343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344#ifdef UNIX
12345 "unix",
12346#endif
12347#ifdef VMS
12348 "vms",
12349#endif
12350#ifdef WIN16
12351 "win16",
12352#endif
12353#ifdef WIN32
12354 "win32",
12355#endif
12356#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12357 "win32unix",
12358#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012359#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 "win64",
12361#endif
12362#ifdef EBCDIC
12363 "ebcdic",
12364#endif
12365#ifndef CASE_INSENSITIVE_FILENAME
12366 "fname_case",
12367#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012368#ifdef HAVE_ACL
12369 "acl",
12370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371#ifdef FEAT_ARABIC
12372 "arabic",
12373#endif
12374#ifdef FEAT_AUTOCMD
12375 "autocmd",
12376#endif
12377#ifdef FEAT_BEVAL
12378 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012379# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12380 "balloon_multiline",
12381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#endif
12383#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12384 "builtin_terms",
12385# ifdef ALL_BUILTIN_TCAPS
12386 "all_builtin_terms",
12387# endif
12388#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012389#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12390 || defined(FEAT_GUI_W32) \
12391 || defined(FEAT_GUI_MOTIF))
12392 "browsefilter",
12393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#ifdef FEAT_BYTEOFF
12395 "byte_offset",
12396#endif
12397#ifdef FEAT_CINDENT
12398 "cindent",
12399#endif
12400#ifdef FEAT_CLIENTSERVER
12401 "clientserver",
12402#endif
12403#ifdef FEAT_CLIPBOARD
12404 "clipboard",
12405#endif
12406#ifdef FEAT_CMDL_COMPL
12407 "cmdline_compl",
12408#endif
12409#ifdef FEAT_CMDHIST
12410 "cmdline_hist",
12411#endif
12412#ifdef FEAT_COMMENTS
12413 "comments",
12414#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012415#ifdef FEAT_CONCEAL
12416 "conceal",
12417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418#ifdef FEAT_CRYPT
12419 "cryptv",
12420#endif
12421#ifdef FEAT_CSCOPE
12422 "cscope",
12423#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012424#ifdef FEAT_CURSORBIND
12425 "cursorbind",
12426#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012427#ifdef CURSOR_SHAPE
12428 "cursorshape",
12429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430#ifdef DEBUG
12431 "debug",
12432#endif
12433#ifdef FEAT_CON_DIALOG
12434 "dialog_con",
12435#endif
12436#ifdef FEAT_GUI_DIALOG
12437 "dialog_gui",
12438#endif
12439#ifdef FEAT_DIFF
12440 "diff",
12441#endif
12442#ifdef FEAT_DIGRAPHS
12443 "digraphs",
12444#endif
12445#ifdef FEAT_DND
12446 "dnd",
12447#endif
12448#ifdef FEAT_EMACS_TAGS
12449 "emacs_tags",
12450#endif
12451 "eval", /* always present, of course! */
12452#ifdef FEAT_EX_EXTRA
12453 "ex_extra",
12454#endif
12455#ifdef FEAT_SEARCH_EXTRA
12456 "extra_search",
12457#endif
12458#ifdef FEAT_FKMAP
12459 "farsi",
12460#endif
12461#ifdef FEAT_SEARCHPATH
12462 "file_in_path",
12463#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012464#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012465 "filterpipe",
12466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#ifdef FEAT_FIND_ID
12468 "find_in_path",
12469#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012470#ifdef FEAT_FLOAT
12471 "float",
12472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473#ifdef FEAT_FOLDING
12474 "folding",
12475#endif
12476#ifdef FEAT_FOOTER
12477 "footer",
12478#endif
12479#if !defined(USE_SYSTEM) && defined(UNIX)
12480 "fork",
12481#endif
12482#ifdef FEAT_GETTEXT
12483 "gettext",
12484#endif
12485#ifdef FEAT_GUI
12486 "gui",
12487#endif
12488#ifdef FEAT_GUI_ATHENA
12489# ifdef FEAT_GUI_NEXTAW
12490 "gui_neXtaw",
12491# else
12492 "gui_athena",
12493# endif
12494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495#ifdef FEAT_GUI_GTK
12496 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012499#ifdef FEAT_GUI_GNOME
12500 "gui_gnome",
12501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502#ifdef FEAT_GUI_MAC
12503 "gui_mac",
12504#endif
12505#ifdef FEAT_GUI_MOTIF
12506 "gui_motif",
12507#endif
12508#ifdef FEAT_GUI_PHOTON
12509 "gui_photon",
12510#endif
12511#ifdef FEAT_GUI_W16
12512 "gui_win16",
12513#endif
12514#ifdef FEAT_GUI_W32
12515 "gui_win32",
12516#endif
12517#ifdef FEAT_HANGULIN
12518 "hangul_input",
12519#endif
12520#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12521 "iconv",
12522#endif
12523#ifdef FEAT_INS_EXPAND
12524 "insert_expand",
12525#endif
12526#ifdef FEAT_JUMPLIST
12527 "jumplist",
12528#endif
12529#ifdef FEAT_KEYMAP
12530 "keymap",
12531#endif
12532#ifdef FEAT_LANGMAP
12533 "langmap",
12534#endif
12535#ifdef FEAT_LIBCALL
12536 "libcall",
12537#endif
12538#ifdef FEAT_LINEBREAK
12539 "linebreak",
12540#endif
12541#ifdef FEAT_LISP
12542 "lispindent",
12543#endif
12544#ifdef FEAT_LISTCMDS
12545 "listcmds",
12546#endif
12547#ifdef FEAT_LOCALMAP
12548 "localmap",
12549#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012550#ifdef FEAT_LUA
12551# ifndef DYNAMIC_LUA
12552 "lua",
12553# endif
12554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555#ifdef FEAT_MENU
12556 "menu",
12557#endif
12558#ifdef FEAT_SESSION
12559 "mksession",
12560#endif
12561#ifdef FEAT_MODIFY_FNAME
12562 "modify_fname",
12563#endif
12564#ifdef FEAT_MOUSE
12565 "mouse",
12566#endif
12567#ifdef FEAT_MOUSESHAPE
12568 "mouseshape",
12569#endif
12570#if defined(UNIX) || defined(VMS)
12571# ifdef FEAT_MOUSE_DEC
12572 "mouse_dec",
12573# endif
12574# ifdef FEAT_MOUSE_GPM
12575 "mouse_gpm",
12576# endif
12577# ifdef FEAT_MOUSE_JSB
12578 "mouse_jsbterm",
12579# endif
12580# ifdef FEAT_MOUSE_NET
12581 "mouse_netterm",
12582# endif
12583# ifdef FEAT_MOUSE_PTERM
12584 "mouse_pterm",
12585# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012586# ifdef FEAT_MOUSE_SGR
12587 "mouse_sgr",
12588# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012589# ifdef FEAT_SYSMOUSE
12590 "mouse_sysmouse",
12591# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012592# ifdef FEAT_MOUSE_URXVT
12593 "mouse_urxvt",
12594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595# ifdef FEAT_MOUSE_XTERM
12596 "mouse_xterm",
12597# endif
12598#endif
12599#ifdef FEAT_MBYTE
12600 "multi_byte",
12601#endif
12602#ifdef FEAT_MBYTE_IME
12603 "multi_byte_ime",
12604#endif
12605#ifdef FEAT_MULTI_LANG
12606 "multi_lang",
12607#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012608#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012609#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012610 "mzscheme",
12611#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613#ifdef FEAT_OLE
12614 "ole",
12615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616#ifdef FEAT_PATH_EXTRA
12617 "path_extra",
12618#endif
12619#ifdef FEAT_PERL
12620#ifndef DYNAMIC_PERL
12621 "perl",
12622#endif
12623#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012624#ifdef FEAT_PERSISTENT_UNDO
12625 "persistent_undo",
12626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627#ifdef FEAT_PYTHON
12628#ifndef DYNAMIC_PYTHON
12629 "python",
12630#endif
12631#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012632#ifdef FEAT_PYTHON3
12633#ifndef DYNAMIC_PYTHON3
12634 "python3",
12635#endif
12636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#ifdef FEAT_POSTSCRIPT
12638 "postscript",
12639#endif
12640#ifdef FEAT_PRINTER
12641 "printer",
12642#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012643#ifdef FEAT_PROFILE
12644 "profile",
12645#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012646#ifdef FEAT_RELTIME
12647 "reltime",
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef FEAT_QUICKFIX
12650 "quickfix",
12651#endif
12652#ifdef FEAT_RIGHTLEFT
12653 "rightleft",
12654#endif
12655#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12656 "ruby",
12657#endif
12658#ifdef FEAT_SCROLLBIND
12659 "scrollbind",
12660#endif
12661#ifdef FEAT_CMDL_INFO
12662 "showcmd",
12663 "cmdline_info",
12664#endif
12665#ifdef FEAT_SIGNS
12666 "signs",
12667#endif
12668#ifdef FEAT_SMARTINDENT
12669 "smartindent",
12670#endif
12671#ifdef FEAT_SNIFF
12672 "sniff",
12673#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012674#ifdef STARTUPTIME
12675 "startuptime",
12676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677#ifdef FEAT_STL_OPT
12678 "statusline",
12679#endif
12680#ifdef FEAT_SUN_WORKSHOP
12681 "sun_workshop",
12682#endif
12683#ifdef FEAT_NETBEANS_INTG
12684 "netbeans_intg",
12685#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012686#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012687 "spell",
12688#endif
12689#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 "syntax",
12691#endif
12692#if defined(USE_SYSTEM) || !defined(UNIX)
12693 "system",
12694#endif
12695#ifdef FEAT_TAG_BINS
12696 "tag_binary",
12697#endif
12698#ifdef FEAT_TAG_OLDSTATIC
12699 "tag_old_static",
12700#endif
12701#ifdef FEAT_TAG_ANYWHITE
12702 "tag_any_white",
12703#endif
12704#ifdef FEAT_TCL
12705# ifndef DYNAMIC_TCL
12706 "tcl",
12707# endif
12708#endif
12709#ifdef TERMINFO
12710 "terminfo",
12711#endif
12712#ifdef FEAT_TERMRESPONSE
12713 "termresponse",
12714#endif
12715#ifdef FEAT_TEXTOBJ
12716 "textobjects",
12717#endif
12718#ifdef HAVE_TGETENT
12719 "tgetent",
12720#endif
12721#ifdef FEAT_TITLE
12722 "title",
12723#endif
12724#ifdef FEAT_TOOLBAR
12725 "toolbar",
12726#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012727#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12728 "unnamedplus",
12729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730#ifdef FEAT_USR_CMDS
12731 "user-commands", /* was accidentally included in 5.4 */
12732 "user_commands",
12733#endif
12734#ifdef FEAT_VIMINFO
12735 "viminfo",
12736#endif
12737#ifdef FEAT_VERTSPLIT
12738 "vertsplit",
12739#endif
12740#ifdef FEAT_VIRTUALEDIT
12741 "virtualedit",
12742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef FEAT_VISUALEXTRA
12745 "visualextra",
12746#endif
12747#ifdef FEAT_VREPLACE
12748 "vreplace",
12749#endif
12750#ifdef FEAT_WILDIGN
12751 "wildignore",
12752#endif
12753#ifdef FEAT_WILDMENU
12754 "wildmenu",
12755#endif
12756#ifdef FEAT_WINDOWS
12757 "windows",
12758#endif
12759#ifdef FEAT_WAK
12760 "winaltkeys",
12761#endif
12762#ifdef FEAT_WRITEBACKUP
12763 "writebackup",
12764#endif
12765#ifdef FEAT_XIM
12766 "xim",
12767#endif
12768#ifdef FEAT_XFONTSET
12769 "xfontset",
12770#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012771#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012772 "xpm",
12773 "xpm_w32", /* for backward compatibility */
12774#else
12775# if defined(HAVE_XPM)
12776 "xpm",
12777# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779#ifdef USE_XSMP
12780 "xsmp",
12781#endif
12782#ifdef USE_XSMP_INTERACT
12783 "xsmp_interact",
12784#endif
12785#ifdef FEAT_XCLIPBOARD
12786 "xterm_clipboard",
12787#endif
12788#ifdef FEAT_XTERM_SAVE
12789 "xterm_save",
12790#endif
12791#if defined(UNIX) && defined(FEAT_X11)
12792 "X11",
12793#endif
12794 NULL
12795 };
12796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 for (i = 0; has_list[i] != NULL; ++i)
12799 if (STRICMP(name, has_list[i]) == 0)
12800 {
12801 n = TRUE;
12802 break;
12803 }
12804
12805 if (n == FALSE)
12806 {
12807 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012808 {
12809 if (name[5] == '-'
12810 && STRLEN(name) > 11
12811 && vim_isdigit(name[6])
12812 && vim_isdigit(name[8])
12813 && vim_isdigit(name[10]))
12814 {
12815 int major = atoi((char *)name + 6);
12816 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012817
12818 /* Expect "patch-9.9.01234". */
12819 n = (major < VIM_VERSION_MAJOR
12820 || (major == VIM_VERSION_MAJOR
12821 && (minor < VIM_VERSION_MINOR
12822 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012823 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012824 }
12825 else
12826 n = has_patch(atoi((char *)name + 5));
12827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 else if (STRICMP(name, "vim_starting") == 0)
12829 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012830#ifdef FEAT_MBYTE
12831 else if (STRICMP(name, "multi_byte_encoding") == 0)
12832 n = has_mbyte;
12833#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012834#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12835 else if (STRICMP(name, "balloon_multiline") == 0)
12836 n = multiline_balloon_available();
12837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838#ifdef DYNAMIC_TCL
12839 else if (STRICMP(name, "tcl") == 0)
12840 n = tcl_enabled(FALSE);
12841#endif
12842#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12843 else if (STRICMP(name, "iconv") == 0)
12844 n = iconv_enabled(FALSE);
12845#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012846#ifdef DYNAMIC_LUA
12847 else if (STRICMP(name, "lua") == 0)
12848 n = lua_enabled(FALSE);
12849#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012850#ifdef DYNAMIC_MZSCHEME
12851 else if (STRICMP(name, "mzscheme") == 0)
12852 n = mzscheme_enabled(FALSE);
12853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854#ifdef DYNAMIC_RUBY
12855 else if (STRICMP(name, "ruby") == 0)
12856 n = ruby_enabled(FALSE);
12857#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012858#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859#ifdef DYNAMIC_PYTHON
12860 else if (STRICMP(name, "python") == 0)
12861 n = python_enabled(FALSE);
12862#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012863#endif
12864#ifdef FEAT_PYTHON3
12865#ifdef DYNAMIC_PYTHON3
12866 else if (STRICMP(name, "python3") == 0)
12867 n = python3_enabled(FALSE);
12868#endif
12869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870#ifdef DYNAMIC_PERL
12871 else if (STRICMP(name, "perl") == 0)
12872 n = perl_enabled(FALSE);
12873#endif
12874#ifdef FEAT_GUI
12875 else if (STRICMP(name, "gui_running") == 0)
12876 n = (gui.in_use || gui.starting);
12877# ifdef FEAT_GUI_W32
12878 else if (STRICMP(name, "gui_win32s") == 0)
12879 n = gui_is_win32s();
12880# endif
12881# ifdef FEAT_BROWSE
12882 else if (STRICMP(name, "browse") == 0)
12883 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12884# endif
12885#endif
12886#ifdef FEAT_SYN_HL
12887 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012888 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#endif
12890#if defined(WIN3264)
12891 else if (STRICMP(name, "win95") == 0)
12892 n = mch_windows95();
12893#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012894#ifdef FEAT_NETBEANS_INTG
12895 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012896 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 }
12899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901}
12902
12903/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012904 * "has_key()" function
12905 */
12906 static void
12907f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 typval_T *argvars;
12909 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012910{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012911 if (argvars[0].v_type != VAR_DICT)
12912 {
12913 EMSG(_(e_dictreq));
12914 return;
12915 }
12916 if (argvars[0].vval.v_dict == NULL)
12917 return;
12918
12919 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012920 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012921}
12922
12923/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012924 * "haslocaldir()" function
12925 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012926 static void
12927f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012928 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012929 typval_T *rettv;
12930{
12931 rettv->vval.v_number = (curwin->w_localdir != NULL);
12932}
12933
12934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 * "hasmapto()" function
12936 */
12937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012939 typval_T *argvars;
12940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941{
12942 char_u *name;
12943 char_u *mode;
12944 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012945 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012948 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 mode = (char_u *)"nvo";
12950 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012953 if (argvars[2].v_type != VAR_UNKNOWN)
12954 abbr = get_tv_number(&argvars[2]);
12955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956
Bram Moolenaar2c932302006-03-18 21:42:09 +000012957 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961}
12962
12963/*
12964 * "histadd()" function
12965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970{
12971#ifdef FEAT_CMDHIST
12972 int histype;
12973 char_u *str;
12974 char_u buf[NUMBUFLEN];
12975#endif
12976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 if (check_restricted() || check_secure())
12979 return;
12980#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012981 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12982 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 if (histype >= 0)
12984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 if (*str != NUL)
12987 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012988 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 return;
12992 }
12993 }
12994#endif
12995}
12996
12997/*
12998 * "histdel()" function
12999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013002 typval_T *argvars UNUSED;
13003 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004{
13005#ifdef FEAT_CMDHIST
13006 int n;
13007 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13011 if (str == NULL)
13012 n = 0;
13013 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013016 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 else
13021 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 get_tv_string_buf(&argvars[1], buf));
13024 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#endif
13026}
13027
13028/*
13029 * "histget()" function
13030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035{
13036#ifdef FEAT_CMDHIST
13037 int type;
13038 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013039 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13042 if (str == NULL)
13043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013045 {
13046 type = get_histtype(str);
13047 if (argvars[1].v_type == VAR_UNKNOWN)
13048 idx = get_history_idx(type);
13049 else
13050 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13051 /* -1 on type error */
13052 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058}
13059
13060/*
13061 * "histnr()" function
13062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013065 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067{
13068 int i;
13069
13070#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013071 char_u *history = get_tv_string_chk(&argvars[0]);
13072
13073 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 if (i >= HIST_CMD && i < HIST_COUNT)
13075 i = get_history_idx(i);
13076 else
13077#endif
13078 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080}
13081
13082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 * "highlightID(name)" function
13084 */
13085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091}
13092
13093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013094 * "highlight_exists()" function
13095 */
13096 static void
13097f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013098 typval_T *argvars;
13099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100{
13101 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13102}
13103
13104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 * "hostname()" function
13106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111{
13112 char_u hostname[256];
13113
13114 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->v_type = VAR_STRING;
13116 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117}
13118
13119/*
13120 * iconv() function
13121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126{
13127#ifdef FEAT_MBYTE
13128 char_u buf1[NUMBUFLEN];
13129 char_u buf2[NUMBUFLEN];
13130 char_u *from, *to, *str;
13131 vimconv_T vimconv;
13132#endif
13133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->v_type = VAR_STRING;
13135 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
13137#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 str = get_tv_string(&argvars[0]);
13139 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13140 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 vimconv.vc_type = CONV_NONE;
13142 convert_setup(&vimconv, from, to);
13143
13144 /* If the encodings are equal, no conversion needed. */
13145 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149
13150 convert_setup(&vimconv, NULL, NULL);
13151 vim_free(from);
13152 vim_free(to);
13153#endif
13154}
13155
13156/*
13157 * "indent()" function
13158 */
13159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163{
13164 linenr_T lnum;
13165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171}
13172
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013173/*
13174 * "index()" function
13175 */
13176 static void
13177f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *argvars;
13179 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013180{
Bram Moolenaar33570922005-01-25 22:26:29 +000013181 list_T *l;
13182 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013183 long idx = 0;
13184 int ic = FALSE;
13185
13186 rettv->vval.v_number = -1;
13187 if (argvars[0].v_type != VAR_LIST)
13188 {
13189 EMSG(_(e_listreq));
13190 return;
13191 }
13192 l = argvars[0].vval.v_list;
13193 if (l != NULL)
13194 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013195 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013196 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 int error = FALSE;
13199
Bram Moolenaar758711c2005-02-02 23:11:38 +000013200 /* Start at specified item. Use the cached index that list_find()
13201 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013203 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013204 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 ic = get_tv_number_chk(&argvars[3], &error);
13206 if (error)
13207 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013208 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013209
Bram Moolenaar758711c2005-02-02 23:11:38 +000013210 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013211 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013212 {
13213 rettv->vval.v_number = idx;
13214 break;
13215 }
13216 }
13217}
13218
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219static int inputsecret_flag = 0;
13220
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013221static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13222
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013224 * This function is used by f_input() and f_inputdialog() functions. The third
13225 * argument to f_input() specifies the type of completion to use at the
13226 * prompt. The third argument to f_inputdialog() specifies the value to return
13227 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 */
13229 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013230get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013233 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 char_u *p = NULL;
13237 int c;
13238 char_u buf[NUMBUFLEN];
13239 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013241 int xp_type = EXPAND_NOTHING;
13242 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246
13247#ifdef NO_CONSOLE_INPUT
13248 /* While starting up, there is no place to enter text. */
13249 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251#endif
13252
13253 cmd_silent = FALSE; /* Want to see the prompt. */
13254 if (prompt != NULL)
13255 {
13256 /* Only the part of the message after the last NL is considered as
13257 * prompt for the command line */
13258 p = vim_strrchr(prompt, '\n');
13259 if (p == NULL)
13260 p = prompt;
13261 else
13262 {
13263 ++p;
13264 c = *p;
13265 *p = NUL;
13266 msg_start();
13267 msg_clr_eos();
13268 msg_puts_attr(prompt, echo_attr);
13269 msg_didout = FALSE;
13270 msg_starthere();
13271 *p = c;
13272 }
13273 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 if (argvars[1].v_type != VAR_UNKNOWN)
13276 {
13277 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13278 if (defstr != NULL)
13279 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013281 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013282 {
13283 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013284 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013285 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013286
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013287 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013288 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013289
Bram Moolenaar4463f292005-09-25 22:20:24 +000013290 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13291 if (xp_name == NULL)
13292 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013293
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013294 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013295
Bram Moolenaar4463f292005-09-25 22:20:24 +000013296 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13297 &xp_arg) == FAIL)
13298 return;
13299 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013300 }
13301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013303 {
13304# ifdef FEAT_EX_EXTRA
13305 int save_ex_normal_busy = ex_normal_busy;
13306 ex_normal_busy = 0;
13307# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013309 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13310 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013311# ifdef FEAT_EX_EXTRA
13312 ex_normal_busy = save_ex_normal_busy;
13313# endif
13314 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013315 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013316 && argvars[1].v_type != VAR_UNKNOWN
13317 && argvars[2].v_type != VAR_UNKNOWN)
13318 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13319 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013320
13321 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 /* since the user typed this, no need to wait for return */
13324 need_wait_return = FALSE;
13325 msg_didout = FALSE;
13326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 cmd_silent = cmd_silent_save;
13328}
13329
13330/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013331 * "input()" function
13332 * Also handles inputsecret() when inputsecret is set.
13333 */
13334 static void
13335f_input(argvars, rettv)
13336 typval_T *argvars;
13337 typval_T *rettv;
13338{
13339 get_user_input(argvars, rettv, FALSE);
13340}
13341
13342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 * "inputdialog()" function
13344 */
13345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349{
13350#if defined(FEAT_GUI_TEXTDIALOG)
13351 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13352 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13353 {
13354 char_u *message;
13355 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013356 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013358 message = get_tv_string_chk(&argvars[0]);
13359 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013360 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013361 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else
13363 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 if (message != NULL && defstr != NULL
13365 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013366 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 else
13369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 if (message != NULL && defstr != NULL
13371 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013372 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 rettv->vval.v_string = vim_strsave(
13374 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 }
13380 else
13381#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013382 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383}
13384
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013385/*
13386 * "inputlist()" function
13387 */
13388 static void
13389f_inputlist(argvars, rettv)
13390 typval_T *argvars;
13391 typval_T *rettv;
13392{
13393 listitem_T *li;
13394 int selected;
13395 int mouse_used;
13396
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013397#ifdef NO_CONSOLE_INPUT
13398 /* While starting up, there is no place to enter text. */
13399 if (no_console_input())
13400 return;
13401#endif
13402 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13403 {
13404 EMSG2(_(e_listarg), "inputlist()");
13405 return;
13406 }
13407
13408 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013409 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013410 lines_left = Rows; /* avoid more prompt */
13411 msg_scroll = TRUE;
13412 msg_clr_eos();
13413
13414 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13415 {
13416 msg_puts(get_tv_string(&li->li_tv));
13417 msg_putchar('\n');
13418 }
13419
13420 /* Ask for choice. */
13421 selected = prompt_for_number(&mouse_used);
13422 if (mouse_used)
13423 selected -= lines_left;
13424
13425 rettv->vval.v_number = selected;
13426}
13427
13428
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13430
13431/*
13432 * "inputrestore()" function
13433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
13439 if (ga_userinput.ga_len > 0)
13440 {
13441 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13443 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013444 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 }
13446 else if (p_verbose > 1)
13447 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013448 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 }
13451}
13452
13453/*
13454 * "inputsave()" function
13455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013461 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 if (ga_grow(&ga_userinput, 1) == OK)
13463 {
13464 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13465 + ga_userinput.ga_len);
13466 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013467 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 }
13469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471}
13472
13473/*
13474 * "inputsecret()" function
13475 */
13476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
13481 ++cmdline_star;
13482 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 --cmdline_star;
13485 --inputsecret_flag;
13486}
13487
13488/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013489 * "insert()" function
13490 */
13491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013495{
13496 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 listitem_T *item;
13498 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500
13501 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013503 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013504 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505 {
13506 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013507 before = get_tv_number_chk(&argvars[2], &error);
13508 if (error)
13509 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013510
Bram Moolenaar758711c2005-02-02 23:11:38 +000013511 if (before == l->lv_len)
13512 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513 else
13514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013515 item = list_find(l, before);
13516 if (item == NULL)
13517 {
13518 EMSGN(_(e_listidx), before);
13519 l = NULL;
13520 }
13521 }
13522 if (l != NULL)
13523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013524 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013525 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013526 }
13527 }
13528}
13529
13530/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013531 * "invert(expr)" function
13532 */
13533 static void
13534f_invert(argvars, rettv)
13535 typval_T *argvars;
13536 typval_T *rettv;
13537{
13538 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13539}
13540
13541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 * "isdirectory()" function
13543 */
13544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 typval_T *argvars;
13547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550}
13551
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013552/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013553 * "islocked()" function
13554 */
13555 static void
13556f_islocked(argvars, rettv)
13557 typval_T *argvars;
13558 typval_T *rettv;
13559{
13560 lval_T lv;
13561 char_u *end;
13562 dictitem_T *di;
13563
13564 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013565 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13566 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013567 if (end != NULL && lv.ll_name != NULL)
13568 {
13569 if (*end != NUL)
13570 EMSG(_(e_trailing));
13571 else
13572 {
13573 if (lv.ll_tv == NULL)
13574 {
13575 if (check_changedtick(lv.ll_name))
13576 rettv->vval.v_number = 1; /* always locked */
13577 else
13578 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013579 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013580 if (di != NULL)
13581 {
13582 /* Consider a variable locked when:
13583 * 1. the variable itself is locked
13584 * 2. the value of the variable is locked.
13585 * 3. the List or Dict value is locked.
13586 */
13587 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13588 || tv_islocked(&di->di_tv));
13589 }
13590 }
13591 }
13592 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013593 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013594 else if (lv.ll_newkey != NULL)
13595 EMSG2(_(e_dictkey), lv.ll_newkey);
13596 else if (lv.ll_list != NULL)
13597 /* List item. */
13598 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13599 else
13600 /* Dictionary item. */
13601 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13602 }
13603 }
13604
13605 clear_lval(&lv);
13606}
13607
Bram Moolenaar33570922005-01-25 22:26:29 +000013608static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013609
13610/*
13611 * Turn a dict into a list:
13612 * "what" == 0: list of keys
13613 * "what" == 1: list of values
13614 * "what" == 2: list of items
13615 */
13616 static void
13617dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *argvars;
13619 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013620 int what;
13621{
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 list_T *l2;
13623 dictitem_T *di;
13624 hashitem_T *hi;
13625 listitem_T *li;
13626 listitem_T *li2;
13627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013629
Bram Moolenaar8c711452005-01-14 21:53:12 +000013630 if (argvars[0].v_type != VAR_DICT)
13631 {
13632 EMSG(_(e_dictreq));
13633 return;
13634 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013635 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013636 return;
13637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013638 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013639 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013640
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013641 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013643 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013646 --todo;
13647 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013649 li = listitem_alloc();
13650 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013651 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013652 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013653
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013654 if (what == 0)
13655 {
13656 /* keys() */
13657 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013658 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013659 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13660 }
13661 else if (what == 1)
13662 {
13663 /* values() */
13664 copy_tv(&di->di_tv, &li->li_tv);
13665 }
13666 else
13667 {
13668 /* items() */
13669 l2 = list_alloc();
13670 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013671 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013672 li->li_tv.vval.v_list = l2;
13673 if (l2 == NULL)
13674 break;
13675 ++l2->lv_refcount;
13676
13677 li2 = listitem_alloc();
13678 if (li2 == NULL)
13679 break;
13680 list_append(l2, li2);
13681 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013682 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013683 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13684
13685 li2 = listitem_alloc();
13686 if (li2 == NULL)
13687 break;
13688 list_append(l2, li2);
13689 copy_tv(&di->di_tv, &li2->li_tv);
13690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013691 }
13692 }
13693}
13694
13695/*
13696 * "items(dict)" function
13697 */
13698 static void
13699f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013702{
13703 dict_list(argvars, rettv, 2);
13704}
13705
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013707 * "join()" function
13708 */
13709 static void
13710f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 typval_T *argvars;
13712 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013713{
13714 garray_T ga;
13715 char_u *sep;
13716
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013717 if (argvars[0].v_type != VAR_LIST)
13718 {
13719 EMSG(_(e_listreq));
13720 return;
13721 }
13722 if (argvars[0].vval.v_list == NULL)
13723 return;
13724 if (argvars[1].v_type == VAR_UNKNOWN)
13725 sep = (char_u *)" ";
13726 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013728
13729 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730
13731 if (sep != NULL)
13732 {
13733 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013734 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 ga_append(&ga, NUL);
13736 rettv->vval.v_string = (char_u *)ga.ga_data;
13737 }
13738 else
13739 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013740}
13741
13742/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013743 * "keys()" function
13744 */
13745 static void
13746f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013747 typval_T *argvars;
13748 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013749{
13750 dict_list(argvars, rettv, 0);
13751}
13752
13753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 * "last_buffer_nr()" function.
13755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760{
13761 int n = 0;
13762 buf_T *buf;
13763
13764 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13765 if (n < buf->b_fnum)
13766 n = buf->b_fnum;
13767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013769}
13770
13771/*
13772 * "len()" function
13773 */
13774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *argvars;
13777 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013778{
13779 switch (argvars[0].v_type)
13780 {
13781 case VAR_STRING:
13782 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = (varnumber_T)STRLEN(
13784 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013785 break;
13786 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013788 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013789 case VAR_DICT:
13790 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13791 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013792 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013793 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013794 break;
13795 }
13796}
13797
Bram Moolenaar33570922005-01-25 22:26:29 +000013798static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799
13800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *argvars;
13803 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013804 int type;
13805{
13806#ifdef FEAT_LIBCALL
13807 char_u *string_in;
13808 char_u **string_result;
13809 int nr_result;
13810#endif
13811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013813 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013815
13816 if (check_restricted() || check_secure())
13817 return;
13818
13819#ifdef FEAT_LIBCALL
13820 /* The first two args must be strings, otherwise its meaningless */
13821 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13822 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013823 string_in = NULL;
13824 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013825 string_in = argvars[2].vval.v_string;
13826 if (type == VAR_NUMBER)
13827 string_result = NULL;
13828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013830 if (mch_libcall(argvars[0].vval.v_string,
13831 argvars[1].vval.v_string,
13832 string_in,
13833 argvars[2].vval.v_number,
13834 string_result,
13835 &nr_result) == OK
13836 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013838 }
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
13842/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013843 * "libcall()" function
13844 */
13845 static void
13846f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013847 typval_T *argvars;
13848 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013849{
13850 libcall_common(argvars, rettv, VAR_STRING);
13851}
13852
13853/*
13854 * "libcallnr()" function
13855 */
13856 static void
13857f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 typval_T *argvars;
13859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013860{
13861 libcall_common(argvars, rettv, VAR_NUMBER);
13862}
13863
13864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 * "line(string)" function
13866 */
13867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013869 typval_T *argvars;
13870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871{
13872 linenr_T lnum = 0;
13873 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013874 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013876 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 if (fp != NULL)
13878 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880}
13881
13882/*
13883 * "line2byte(lnum)" function
13884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889{
13890#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892#else
13893 linenr_T lnum;
13894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013895 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13900 if (rettv->vval.v_number >= 0)
13901 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902#endif
13903}
13904
13905/*
13906 * "lispindent(lnum)" function
13907 */
13908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013910 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912{
13913#ifdef FEAT_LISP
13914 pos_T pos;
13915 linenr_T lnum;
13916
13917 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013918 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13920 {
13921 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 curwin->w_cursor = pos;
13924 }
13925 else
13926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928}
13929
13930/*
13931 * "localtime()" function
13932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939}
13940
Bram Moolenaar33570922005-01-25 22:26:29 +000013941static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
13943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 typval_T *argvars;
13946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 int exact;
13948{
13949 char_u *keys;
13950 char_u *which;
13951 char_u buf[NUMBUFLEN];
13952 char_u *keys_buf = NULL;
13953 char_u *rhs;
13954 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013955 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013956 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013957 mapblock_T *mp;
13958 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
13960 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->v_type = VAR_STRING;
13962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 if (*keys == NUL)
13966 return;
13967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013971 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013972 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013973 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013974 if (argvars[3].v_type != VAR_UNKNOWN)
13975 get_dict = get_tv_number(&argvars[3]);
13976 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 else
13979 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 if (which == NULL)
13981 return;
13982
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 mode = get_map_mode(&which, 0);
13984
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013985 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013986 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013988
13989 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013991 /* Return a string. */
13992 if (rhs != NULL)
13993 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994
Bram Moolenaarbd743252010-10-20 21:23:33 +020013995 }
13996 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13997 {
13998 /* Return a dictionary. */
13999 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14000 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14001 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
Bram Moolenaarbd743252010-10-20 21:23:33 +020014003 dict_add_nr_str(dict, "lhs", 0L, lhs);
14004 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14005 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14006 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14007 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14008 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14009 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014010 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014011 dict_add_nr_str(dict, "mode", 0L, mapmode);
14012
14013 vim_free(lhs);
14014 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 }
14016}
14017
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014018#ifdef FEAT_FLOAT
14019/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014020 * "log()" function
14021 */
14022 static void
14023f_log(argvars, rettv)
14024 typval_T *argvars;
14025 typval_T *rettv;
14026{
14027 float_T f;
14028
14029 rettv->v_type = VAR_FLOAT;
14030 if (get_float_arg(argvars, &f) == OK)
14031 rettv->vval.v_float = log(f);
14032 else
14033 rettv->vval.v_float = 0.0;
14034}
14035
14036/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014037 * "log10()" function
14038 */
14039 static void
14040f_log10(argvars, rettv)
14041 typval_T *argvars;
14042 typval_T *rettv;
14043{
14044 float_T f;
14045
14046 rettv->v_type = VAR_FLOAT;
14047 if (get_float_arg(argvars, &f) == OK)
14048 rettv->vval.v_float = log10(f);
14049 else
14050 rettv->vval.v_float = 0.0;
14051}
14052#endif
14053
Bram Moolenaar1dced572012-04-05 16:54:08 +020014054#ifdef FEAT_LUA
14055/*
14056 * "luaeval()" function
14057 */
14058 static void
14059f_luaeval(argvars, rettv)
14060 typval_T *argvars;
14061 typval_T *rettv;
14062{
14063 char_u *str;
14064 char_u buf[NUMBUFLEN];
14065
14066 str = get_tv_string_buf(&argvars[0], buf);
14067 do_luaeval(str, argvars + 1, rettv);
14068}
14069#endif
14070
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014072 * "map()" function
14073 */
14074 static void
14075f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078{
14079 filter_map(argvars, rettv, TRUE);
14080}
14081
14082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014083 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 */
14085 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014086f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091}
14092
14093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 */
14096 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102}
14103
Bram Moolenaar33570922005-01-25 22:26:29 +000014104static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105
14106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014108 typval_T *argvars;
14109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 int type;
14111{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014112 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014113 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014114 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 char_u *pat;
14116 regmatch_T regmatch;
14117 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014118 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 char_u *save_cpo;
14120 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014121 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014122 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014123 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 list_T *l = NULL;
14125 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014126 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014127 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128
14129 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14130 save_cpo = p_cpo;
14131 p_cpo = (char_u *)"";
14132
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014133 rettv->vval.v_number = -1;
14134 if (type == 3)
14135 {
14136 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014137 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014138 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014139 }
14140 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->v_type = VAR_STRING;
14143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014146 if (argvars[0].v_type == VAR_LIST)
14147 {
14148 if ((l = argvars[0].vval.v_list) == NULL)
14149 goto theend;
14150 li = l->lv_first;
14151 }
14152 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014153 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014154 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014155 len = (long)STRLEN(str);
14156 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14159 if (pat == NULL)
14160 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 int error = FALSE;
14165
14166 start = get_tv_number_chk(&argvars[2], &error);
14167 if (error)
14168 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014169 if (l != NULL)
14170 {
14171 li = list_find(l, start);
14172 if (li == NULL)
14173 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014174 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014175 }
14176 else
14177 {
14178 if (start < 0)
14179 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014180 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014181 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014182 /* When "count" argument is there ignore matches before "start",
14183 * otherwise skip part of the string. Differs when pattern is "^"
14184 * or "\<". */
14185 if (argvars[3].v_type != VAR_UNKNOWN)
14186 startcol = start;
14187 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014188 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014189 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014190 len -= start;
14191 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014192 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014193
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014194 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 nth = get_tv_number_chk(&argvars[3], &error);
14196 if (error)
14197 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 }
14199
14200 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14201 if (regmatch.regprog != NULL)
14202 {
14203 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014204
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014205 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014206 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014207 if (l != NULL)
14208 {
14209 if (li == NULL)
14210 {
14211 match = FALSE;
14212 break;
14213 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014214 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014215 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014216 if (str == NULL)
14217 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014218 }
14219
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014220 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014221
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014222 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014223 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014224 if (l == NULL && !match)
14225 break;
14226
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014227 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014228 if (l != NULL)
14229 {
14230 li = li->li_next;
14231 ++idx;
14232 }
14233 else
14234 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014235#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014236 startcol = (colnr_T)(regmatch.startp[0]
14237 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014238#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014239 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014240#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014241 if (startcol > (colnr_T)len
14242 || str + startcol <= regmatch.startp[0])
14243 {
14244 match = FALSE;
14245 break;
14246 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014247 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014248 }
14249
14250 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014252 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014254 int i;
14255
14256 /* return list with matched string and submatches */
14257 for (i = 0; i < NSUBEXP; ++i)
14258 {
14259 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014260 {
14261 if (list_append_string(rettv->vval.v_list,
14262 (char_u *)"", 0) == FAIL)
14263 break;
14264 }
14265 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014266 regmatch.startp[i],
14267 (int)(regmatch.endp[i] - regmatch.startp[i]))
14268 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014269 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014270 }
14271 }
14272 else if (type == 2)
14273 {
14274 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014275 if (l != NULL)
14276 copy_tv(&li->li_tv, rettv);
14277 else
14278 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014280 }
14281 else if (l != NULL)
14282 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 else
14284 {
14285 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 (varnumber_T)(regmatch.startp[0] - str);
14288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014289 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014291 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 }
14293 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014294 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 }
14296
14297theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014298 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 p_cpo = save_cpo;
14300}
14301
14302/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014303 * "match()" function
14304 */
14305 static void
14306f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 typval_T *argvars;
14308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014309{
14310 find_some_match(argvars, rettv, 1);
14311}
14312
14313/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014314 * "matchadd()" function
14315 */
14316 static void
14317f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014318 typval_T *argvars UNUSED;
14319 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014320{
14321#ifdef FEAT_SEARCH_EXTRA
14322 char_u buf[NUMBUFLEN];
14323 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14324 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14325 int prio = 10; /* default priority */
14326 int id = -1;
14327 int error = FALSE;
14328
14329 rettv->vval.v_number = -1;
14330
14331 if (grp == NULL || pat == NULL)
14332 return;
14333 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014334 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014335 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014336 if (argvars[3].v_type != VAR_UNKNOWN)
14337 id = get_tv_number_chk(&argvars[3], &error);
14338 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014339 if (error == TRUE)
14340 return;
14341 if (id >= 1 && id <= 3)
14342 {
14343 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14344 return;
14345 }
14346
Bram Moolenaarb3414592014-06-17 17:48:32 +020014347 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14348#endif
14349}
14350
14351/*
14352 * "matchaddpos()" function
14353 */
14354 static void
14355f_matchaddpos(argvars, rettv)
14356 typval_T *argvars UNUSED;
14357 typval_T *rettv UNUSED;
14358{
14359#ifdef FEAT_SEARCH_EXTRA
14360 char_u buf[NUMBUFLEN];
14361 char_u *group;
14362 int prio = 10;
14363 int id = -1;
14364 int error = FALSE;
14365 list_T *l;
14366
14367 rettv->vval.v_number = -1;
14368
14369 group = get_tv_string_buf_chk(&argvars[0], buf);
14370 if (group == NULL)
14371 return;
14372
14373 if (argvars[1].v_type != VAR_LIST)
14374 {
14375 EMSG2(_(e_listarg), "matchaddpos()");
14376 return;
14377 }
14378 l = argvars[1].vval.v_list;
14379 if (l == NULL)
14380 return;
14381
14382 if (argvars[2].v_type != VAR_UNKNOWN)
14383 {
14384 prio = get_tv_number_chk(&argvars[2], &error);
14385 if (argvars[3].v_type != VAR_UNKNOWN)
14386 id = get_tv_number_chk(&argvars[3], &error);
14387 }
14388 if (error == TRUE)
14389 return;
14390
14391 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14392 if (id == 1 || id == 2)
14393 {
14394 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14395 return;
14396 }
14397
14398 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014399#endif
14400}
14401
14402/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014403 * "matcharg()" function
14404 */
14405 static void
14406f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014407 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014408 typval_T *rettv;
14409{
14410 if (rettv_list_alloc(rettv) == OK)
14411 {
14412#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014413 int id = get_tv_number(&argvars[0]);
14414 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014415
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014416 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014417 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014418 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14419 {
14420 list_append_string(rettv->vval.v_list,
14421 syn_id2name(m->hlg_id), -1);
14422 list_append_string(rettv->vval.v_list, m->pattern, -1);
14423 }
14424 else
14425 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014426 list_append_string(rettv->vval.v_list, NULL, -1);
14427 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014428 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014429 }
14430#endif
14431 }
14432}
14433
14434/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014435 * "matchdelete()" function
14436 */
14437 static void
14438f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014439 typval_T *argvars UNUSED;
14440 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014441{
14442#ifdef FEAT_SEARCH_EXTRA
14443 rettv->vval.v_number = match_delete(curwin,
14444 (int)get_tv_number(&argvars[0]), TRUE);
14445#endif
14446}
14447
14448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014449 * "matchend()" function
14450 */
14451 static void
14452f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455{
14456 find_some_match(argvars, rettv, 0);
14457}
14458
14459/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014460 * "matchlist()" function
14461 */
14462 static void
14463f_matchlist(argvars, rettv)
14464 typval_T *argvars;
14465 typval_T *rettv;
14466{
14467 find_some_match(argvars, rettv, 3);
14468}
14469
14470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471 * "matchstr()" function
14472 */
14473 static void
14474f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477{
14478 find_some_match(argvars, rettv, 2);
14479}
14480
Bram Moolenaar33570922005-01-25 22:26:29 +000014481static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014482
14483 static void
14484max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014485 typval_T *argvars;
14486 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014487 int domax;
14488{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014489 long n = 0;
14490 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014492
14493 if (argvars[0].v_type == VAR_LIST)
14494 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 list_T *l;
14496 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014497
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014498 l = argvars[0].vval.v_list;
14499 if (l != NULL)
14500 {
14501 li = l->lv_first;
14502 if (li != NULL)
14503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014505 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014506 {
14507 li = li->li_next;
14508 if (li == NULL)
14509 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014510 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014511 if (domax ? i > n : i < n)
14512 n = i;
14513 }
14514 }
14515 }
14516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014517 else if (argvars[0].v_type == VAR_DICT)
14518 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014519 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014520 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014522 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014523
14524 d = argvars[0].vval.v_dict;
14525 if (d != NULL)
14526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014527 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014528 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014530 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014532 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014534 if (first)
14535 {
14536 n = i;
14537 first = FALSE;
14538 }
14539 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014540 n = i;
14541 }
14542 }
14543 }
14544 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014545 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014546 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014548}
14549
14550/*
14551 * "max()" function
14552 */
14553 static void
14554f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014555 typval_T *argvars;
14556 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014557{
14558 max_min(argvars, rettv, TRUE);
14559}
14560
14561/*
14562 * "min()" function
14563 */
14564 static void
14565f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 typval_T *argvars;
14567 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014568{
14569 max_min(argvars, rettv, FALSE);
14570}
14571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014572static int mkdir_recurse __ARGS((char_u *dir, int prot));
14573
14574/*
14575 * Create the directory in which "dir" is located, and higher levels when
14576 * needed.
14577 */
14578 static int
14579mkdir_recurse(dir, prot)
14580 char_u *dir;
14581 int prot;
14582{
14583 char_u *p;
14584 char_u *updir;
14585 int r = FAIL;
14586
14587 /* Get end of directory name in "dir".
14588 * We're done when it's "/" or "c:/". */
14589 p = gettail_sep(dir);
14590 if (p <= get_past_head(dir))
14591 return OK;
14592
14593 /* If the directory exists we're done. Otherwise: create it.*/
14594 updir = vim_strnsave(dir, (int)(p - dir));
14595 if (updir == NULL)
14596 return FAIL;
14597 if (mch_isdir(updir))
14598 r = OK;
14599 else if (mkdir_recurse(updir, prot) == OK)
14600 r = vim_mkdir_emsg(updir, prot);
14601 vim_free(updir);
14602 return r;
14603}
14604
14605#ifdef vim_mkdir
14606/*
14607 * "mkdir()" function
14608 */
14609 static void
14610f_mkdir(argvars, rettv)
14611 typval_T *argvars;
14612 typval_T *rettv;
14613{
14614 char_u *dir;
14615 char_u buf[NUMBUFLEN];
14616 int prot = 0755;
14617
14618 rettv->vval.v_number = FAIL;
14619 if (check_restricted() || check_secure())
14620 return;
14621
14622 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014623 if (*dir == NUL)
14624 rettv->vval.v_number = FAIL;
14625 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014626 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014627 if (*gettail(dir) == NUL)
14628 /* remove trailing slashes */
14629 *gettail_sep(dir) = NUL;
14630
14631 if (argvars[1].v_type != VAR_UNKNOWN)
14632 {
14633 if (argvars[2].v_type != VAR_UNKNOWN)
14634 prot = get_tv_number_chk(&argvars[2], NULL);
14635 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14636 mkdir_recurse(dir, prot);
14637 }
14638 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014639 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014640}
14641#endif
14642
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 * "mode()" function
14645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 typval_T *argvars;
14649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014651 char_u buf[3];
14652
14653 buf[1] = NUL;
14654 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 if (VIsual_active)
14657 {
14658 if (VIsual_select)
14659 buf[0] = VIsual_mode + 's' - 'v';
14660 else
14661 buf[0] = VIsual_mode;
14662 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014663 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014664 || State == CONFIRM)
14665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014667 if (State == ASKMORE)
14668 buf[1] = 'm';
14669 else if (State == CONFIRM)
14670 buf[1] = '?';
14671 }
14672 else if (State == EXTERNCMD)
14673 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 else if (State & INSERT)
14675 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014676#ifdef FEAT_VREPLACE
14677 if (State & VREPLACE_FLAG)
14678 {
14679 buf[0] = 'R';
14680 buf[1] = 'v';
14681 }
14682 else
14683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 if (State & REPLACE_FLAG)
14685 buf[0] = 'R';
14686 else
14687 buf[0] = 'i';
14688 }
14689 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014692 if (exmode_active)
14693 buf[1] = 'v';
14694 }
14695 else if (exmode_active)
14696 {
14697 buf[0] = 'c';
14698 buf[1] = 'e';
14699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014703 if (finish_op)
14704 buf[1] = 'o';
14705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014707 /* Clear out the minor mode when the argument is not a non-zero number or
14708 * non-empty string. */
14709 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014710 buf[1] = NUL;
14711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 rettv->vval.v_string = vim_strsave(buf);
14713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714}
14715
Bram Moolenaar429fa852013-04-15 12:27:36 +020014716#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014717/*
14718 * "mzeval()" function
14719 */
14720 static void
14721f_mzeval(argvars, rettv)
14722 typval_T *argvars;
14723 typval_T *rettv;
14724{
14725 char_u *str;
14726 char_u buf[NUMBUFLEN];
14727
14728 str = get_tv_string_buf(&argvars[0], buf);
14729 do_mzeval(str, rettv);
14730}
Bram Moolenaar75676462013-01-30 14:55:42 +010014731
14732 void
14733mzscheme_call_vim(name, args, rettv)
14734 char_u *name;
14735 typval_T *args;
14736 typval_T *rettv;
14737{
14738 typval_T argvars[3];
14739
14740 argvars[0].v_type = VAR_STRING;
14741 argvars[0].vval.v_string = name;
14742 copy_tv(args, &argvars[1]);
14743 argvars[2].v_type = VAR_UNKNOWN;
14744 f_call(argvars, rettv);
14745 clear_tv(&argvars[1]);
14746}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014747#endif
14748
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 * "nextnonblank()" function
14751 */
14752 static void
14753f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 typval_T *argvars;
14755 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014756{
14757 linenr_T lnum;
14758
14759 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014761 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 {
14763 lnum = 0;
14764 break;
14765 }
14766 if (*skipwhite(ml_get(lnum)) != NUL)
14767 break;
14768 }
14769 rettv->vval.v_number = lnum;
14770}
14771
14772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 * "nr2char()" function
14774 */
14775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 typval_T *argvars;
14778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779{
14780 char_u buf[NUMBUFLEN];
14781
14782#ifdef FEAT_MBYTE
14783 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014784 {
14785 int utf8 = 0;
14786
14787 if (argvars[1].v_type != VAR_UNKNOWN)
14788 utf8 = get_tv_number_chk(&argvars[1], NULL);
14789 if (utf8)
14790 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14791 else
14792 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 else
14795#endif
14796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 buf[1] = NUL;
14799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800 rettv->v_type = VAR_STRING;
14801 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014802}
14803
14804/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014805 * "or(expr, expr)" function
14806 */
14807 static void
14808f_or(argvars, rettv)
14809 typval_T *argvars;
14810 typval_T *rettv;
14811{
14812 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14813 | get_tv_number_chk(&argvars[1], NULL);
14814}
14815
14816/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014817 * "pathshorten()" function
14818 */
14819 static void
14820f_pathshorten(argvars, rettv)
14821 typval_T *argvars;
14822 typval_T *rettv;
14823{
14824 char_u *p;
14825
14826 rettv->v_type = VAR_STRING;
14827 p = get_tv_string_chk(&argvars[0]);
14828 if (p == NULL)
14829 rettv->vval.v_string = NULL;
14830 else
14831 {
14832 p = vim_strsave(p);
14833 rettv->vval.v_string = p;
14834 if (p != NULL)
14835 shorten_dir(p);
14836 }
14837}
14838
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014839#ifdef FEAT_FLOAT
14840/*
14841 * "pow()" function
14842 */
14843 static void
14844f_pow(argvars, rettv)
14845 typval_T *argvars;
14846 typval_T *rettv;
14847{
14848 float_T fx, fy;
14849
14850 rettv->v_type = VAR_FLOAT;
14851 if (get_float_arg(argvars, &fx) == OK
14852 && get_float_arg(&argvars[1], &fy) == OK)
14853 rettv->vval.v_float = pow(fx, fy);
14854 else
14855 rettv->vval.v_float = 0.0;
14856}
14857#endif
14858
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014860 * "prevnonblank()" function
14861 */
14862 static void
14863f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014864 typval_T *argvars;
14865 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866{
14867 linenr_T lnum;
14868
14869 lnum = get_tv_lnum(argvars);
14870 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14871 lnum = 0;
14872 else
14873 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14874 --lnum;
14875 rettv->vval.v_number = lnum;
14876}
14877
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014878#ifdef HAVE_STDARG_H
14879/* This dummy va_list is here because:
14880 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14881 * - locally in the function results in a "used before set" warning
14882 * - using va_start() to initialize it gives "function with fixed args" error */
14883static va_list ap;
14884#endif
14885
Bram Moolenaar8c711452005-01-14 21:53:12 +000014886/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014887 * "printf()" function
14888 */
14889 static void
14890f_printf(argvars, rettv)
14891 typval_T *argvars;
14892 typval_T *rettv;
14893{
14894 rettv->v_type = VAR_STRING;
14895 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014896#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014897 {
14898 char_u buf[NUMBUFLEN];
14899 int len;
14900 char_u *s;
14901 int saved_did_emsg = did_emsg;
14902 char *fmt;
14903
14904 /* Get the required length, allocate the buffer and do it for real. */
14905 did_emsg = FALSE;
14906 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014907 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014908 if (!did_emsg)
14909 {
14910 s = alloc(len + 1);
14911 if (s != NULL)
14912 {
14913 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014914 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014915 }
14916 }
14917 did_emsg |= saved_did_emsg;
14918 }
14919#endif
14920}
14921
14922/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014923 * "pumvisible()" function
14924 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014925 static void
14926f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014927 typval_T *argvars UNUSED;
14928 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014929{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014930#ifdef FEAT_INS_EXPAND
14931 if (pum_visible())
14932 rettv->vval.v_number = 1;
14933#endif
14934}
14935
Bram Moolenaardb913952012-06-29 12:54:53 +020014936#ifdef FEAT_PYTHON3
14937/*
14938 * "py3eval()" function
14939 */
14940 static void
14941f_py3eval(argvars, rettv)
14942 typval_T *argvars;
14943 typval_T *rettv;
14944{
14945 char_u *str;
14946 char_u buf[NUMBUFLEN];
14947
14948 str = get_tv_string_buf(&argvars[0], buf);
14949 do_py3eval(str, rettv);
14950}
14951#endif
14952
14953#ifdef FEAT_PYTHON
14954/*
14955 * "pyeval()" function
14956 */
14957 static void
14958f_pyeval(argvars, rettv)
14959 typval_T *argvars;
14960 typval_T *rettv;
14961{
14962 char_u *str;
14963 char_u buf[NUMBUFLEN];
14964
14965 str = get_tv_string_buf(&argvars[0], buf);
14966 do_pyeval(str, rettv);
14967}
14968#endif
14969
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014970/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014971 * "range()" function
14972 */
14973 static void
14974f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 typval_T *argvars;
14976 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014977{
14978 long start;
14979 long end;
14980 long stride = 1;
14981 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014982 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014984 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014985 if (argvars[1].v_type == VAR_UNKNOWN)
14986 {
14987 end = start - 1;
14988 start = 0;
14989 }
14990 else
14991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014994 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014995 }
14996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014997 if (error)
14998 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014999 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015000 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015001 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015002 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015003 else
15004 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015005 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015006 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015007 if (list_append_number(rettv->vval.v_list,
15008 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015009 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010 }
15011}
15012
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015013/*
15014 * "readfile()" function
15015 */
15016 static void
15017f_readfile(argvars, rettv)
15018 typval_T *argvars;
15019 typval_T *rettv;
15020{
15021 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015022 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015023 char_u *fname;
15024 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015025 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15026 int io_size = sizeof(buf);
15027 int readlen; /* size of last fread() */
15028 char_u *prev = NULL; /* previously read bytes, if any */
15029 long prevlen = 0; /* length of data in prev */
15030 long prevsize = 0; /* size of prev buffer */
15031 long maxline = MAXLNUM;
15032 long cnt = 0;
15033 char_u *p; /* position in buf */
15034 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015036 if (argvars[1].v_type != VAR_UNKNOWN)
15037 {
15038 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15039 binary = TRUE;
15040 if (argvars[2].v_type != VAR_UNKNOWN)
15041 maxline = get_tv_number(&argvars[2]);
15042 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015043
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015044 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015045 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015046
15047 /* Always open the file in binary mode, library functions have a mind of
15048 * their own about CR-LF conversion. */
15049 fname = get_tv_string(&argvars[0]);
15050 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15051 {
15052 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15053 return;
15054 }
15055
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015056 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015057 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015058 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015059
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015060 /* This for loop processes what was read, but is also entered at end
15061 * of file so that either:
15062 * - an incomplete line gets written
15063 * - a "binary" file gets an empty line at the end if it ends in a
15064 * newline. */
15065 for (p = buf, start = buf;
15066 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15067 ++p)
15068 {
15069 if (*p == '\n' || readlen <= 0)
15070 {
15071 listitem_T *li;
15072 char_u *s = NULL;
15073 long_u len = p - start;
15074
15075 /* Finished a line. Remove CRs before NL. */
15076 if (readlen > 0 && !binary)
15077 {
15078 while (len > 0 && start[len - 1] == '\r')
15079 --len;
15080 /* removal may cross back to the "prev" string */
15081 if (len == 0)
15082 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15083 --prevlen;
15084 }
15085 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015086 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015087 else
15088 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015089 /* Change "prev" buffer to be the right size. This way
15090 * the bytes are only copied once, and very long lines are
15091 * allocated only once. */
15092 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015093 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015094 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015095 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015096 prev = NULL; /* the list will own the string */
15097 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015098 }
15099 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015100 if (s == NULL)
15101 {
15102 do_outofmem_msg((long_u) prevlen + len + 1);
15103 failed = TRUE;
15104 break;
15105 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015106
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015107 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015108 {
15109 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015110 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015111 break;
15112 }
15113 li->li_tv.v_type = VAR_STRING;
15114 li->li_tv.v_lock = 0;
15115 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015116 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015117
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015118 start = p + 1; /* step over newline */
15119 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015120 break;
15121 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015122 else if (*p == NUL)
15123 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015124#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015125 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15126 * when finding the BF and check the previous two bytes. */
15127 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015128 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015129 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15130 * + 1, these may be in the "prev" string. */
15131 char_u back1 = p >= buf + 1 ? p[-1]
15132 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15133 char_u back2 = p >= buf + 2 ? p[-2]
15134 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15135 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015136
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015137 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015139 char_u *dest = p - 2;
15140
15141 /* Usually a BOM is at the beginning of a file, and so at
15142 * the beginning of a line; then we can just step over it.
15143 */
15144 if (start == dest)
15145 start = p + 1;
15146 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015147 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015148 /* have to shuffle buf to close gap */
15149 int adjust_prevlen = 0;
15150
15151 if (dest < buf)
15152 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015153 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015154 dest = buf;
15155 }
15156 if (readlen > p - buf + 1)
15157 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15158 readlen -= 3 - adjust_prevlen;
15159 prevlen -= adjust_prevlen;
15160 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015161 }
15162 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015163 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015164#endif
15165 } /* for */
15166
15167 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15168 break;
15169 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015170 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015171 /* There's part of a line in buf, store it in "prev". */
15172 if (p - start + prevlen >= prevsize)
15173 {
15174 /* need bigger "prev" buffer */
15175 char_u *newprev;
15176
15177 /* A common use case is ordinary text files and "prev" gets a
15178 * fragment of a line, so the first allocation is made
15179 * small, to avoid repeatedly 'allocing' large and
15180 * 'reallocing' small. */
15181 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015182 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015183 else
15184 {
15185 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015186 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015187 prevsize = grow50pc > growmin ? grow50pc : growmin;
15188 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015189 newprev = prev == NULL ? alloc(prevsize)
15190 : vim_realloc(prev, prevsize);
15191 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015192 {
15193 do_outofmem_msg((long_u)prevsize);
15194 failed = TRUE;
15195 break;
15196 }
15197 prev = newprev;
15198 }
15199 /* Add the line part to end of "prev". */
15200 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015201 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015202 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015203 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015204
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015205 /*
15206 * For a negative line count use only the lines at the end of the file,
15207 * free the rest.
15208 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015209 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015210 while (cnt > -maxline)
15211 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015212 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015213 --cnt;
15214 }
15215
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015216 if (failed)
15217 {
15218 list_free(rettv->vval.v_list, TRUE);
15219 /* readfile doc says an empty list is returned on error */
15220 rettv->vval.v_list = list_alloc();
15221 }
15222
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015223 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015224 fclose(fd);
15225}
15226
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015227#if defined(FEAT_RELTIME)
15228static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15229
15230/*
15231 * Convert a List to proftime_T.
15232 * Return FAIL when there is something wrong.
15233 */
15234 static int
15235list2proftime(arg, tm)
15236 typval_T *arg;
15237 proftime_T *tm;
15238{
15239 long n1, n2;
15240 int error = FALSE;
15241
15242 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15243 || arg->vval.v_list->lv_len != 2)
15244 return FAIL;
15245 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15246 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15247# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015248 tm->HighPart = n1;
15249 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015250# else
15251 tm->tv_sec = n1;
15252 tm->tv_usec = n2;
15253# endif
15254 return error ? FAIL : OK;
15255}
15256#endif /* FEAT_RELTIME */
15257
15258/*
15259 * "reltime()" function
15260 */
15261 static void
15262f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015263 typval_T *argvars UNUSED;
15264 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015265{
15266#ifdef FEAT_RELTIME
15267 proftime_T res;
15268 proftime_T start;
15269
15270 if (argvars[0].v_type == VAR_UNKNOWN)
15271 {
15272 /* No arguments: get current time. */
15273 profile_start(&res);
15274 }
15275 else if (argvars[1].v_type == VAR_UNKNOWN)
15276 {
15277 if (list2proftime(&argvars[0], &res) == FAIL)
15278 return;
15279 profile_end(&res);
15280 }
15281 else
15282 {
15283 /* Two arguments: compute the difference. */
15284 if (list2proftime(&argvars[0], &start) == FAIL
15285 || list2proftime(&argvars[1], &res) == FAIL)
15286 return;
15287 profile_sub(&res, &start);
15288 }
15289
15290 if (rettv_list_alloc(rettv) == OK)
15291 {
15292 long n1, n2;
15293
15294# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015295 n1 = res.HighPart;
15296 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015297# else
15298 n1 = res.tv_sec;
15299 n2 = res.tv_usec;
15300# endif
15301 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15302 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15303 }
15304#endif
15305}
15306
15307/*
15308 * "reltimestr()" function
15309 */
15310 static void
15311f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015312 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015313 typval_T *rettv;
15314{
15315#ifdef FEAT_RELTIME
15316 proftime_T tm;
15317#endif
15318
15319 rettv->v_type = VAR_STRING;
15320 rettv->vval.v_string = NULL;
15321#ifdef FEAT_RELTIME
15322 if (list2proftime(&argvars[0], &tm) == OK)
15323 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15324#endif
15325}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015326
Bram Moolenaar0d660222005-01-07 21:51:51 +000015327#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15328static void make_connection __ARGS((void));
15329static int check_connection __ARGS((void));
15330
15331 static void
15332make_connection()
15333{
15334 if (X_DISPLAY == NULL
15335# ifdef FEAT_GUI
15336 && !gui.in_use
15337# endif
15338 )
15339 {
15340 x_force_connect = TRUE;
15341 setup_term_clip();
15342 x_force_connect = FALSE;
15343 }
15344}
15345
15346 static int
15347check_connection()
15348{
15349 make_connection();
15350 if (X_DISPLAY == NULL)
15351 {
15352 EMSG(_("E240: No connection to Vim server"));
15353 return FAIL;
15354 }
15355 return OK;
15356}
15357#endif
15358
15359#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015360static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361
15362 static void
15363remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015364 typval_T *argvars;
15365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366 int expr;
15367{
15368 char_u *server_name;
15369 char_u *keys;
15370 char_u *r = NULL;
15371 char_u buf[NUMBUFLEN];
15372# ifdef WIN32
15373 HWND w;
15374# else
15375 Window w;
15376# endif
15377
15378 if (check_restricted() || check_secure())
15379 return;
15380
15381# ifdef FEAT_X11
15382 if (check_connection() == FAIL)
15383 return;
15384# endif
15385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015386 server_name = get_tv_string_chk(&argvars[0]);
15387 if (server_name == NULL)
15388 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015389 keys = get_tv_string_buf(&argvars[1], buf);
15390# ifdef WIN32
15391 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15392# else
15393 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15394 < 0)
15395# endif
15396 {
15397 if (r != NULL)
15398 EMSG(r); /* sending worked but evaluation failed */
15399 else
15400 EMSG2(_("E241: Unable to send to %s"), server_name);
15401 return;
15402 }
15403
15404 rettv->vval.v_string = r;
15405
15406 if (argvars[2].v_type != VAR_UNKNOWN)
15407 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015408 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015409 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015410 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015412 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015413 v.di_tv.v_type = VAR_STRING;
15414 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 idvar = get_tv_string_chk(&argvars[2]);
15416 if (idvar != NULL)
15417 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015418 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419 }
15420}
15421#endif
15422
15423/*
15424 * "remote_expr()" function
15425 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426 static void
15427f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015430{
15431 rettv->v_type = VAR_STRING;
15432 rettv->vval.v_string = NULL;
15433#ifdef FEAT_CLIENTSERVER
15434 remote_common(argvars, rettv, TRUE);
15435#endif
15436}
15437
15438/*
15439 * "remote_foreground()" function
15440 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441 static void
15442f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015443 typval_T *argvars UNUSED;
15444 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015445{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015446#ifdef FEAT_CLIENTSERVER
15447# ifdef WIN32
15448 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015449 {
15450 char_u *server_name = get_tv_string_chk(&argvars[0]);
15451
15452 if (server_name != NULL)
15453 serverForeground(server_name);
15454 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015455# else
15456 /* Send a foreground() expression to the server. */
15457 argvars[1].v_type = VAR_STRING;
15458 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15459 argvars[2].v_type = VAR_UNKNOWN;
15460 remote_common(argvars, rettv, TRUE);
15461 vim_free(argvars[1].vval.v_string);
15462# endif
15463#endif
15464}
15465
Bram Moolenaar0d660222005-01-07 21:51:51 +000015466 static void
15467f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015469 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015470{
15471#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015473 char_u *s = NULL;
15474# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015475 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015476# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015477 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478
15479 if (check_restricted() || check_secure())
15480 {
15481 rettv->vval.v_number = -1;
15482 return;
15483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015484 serverid = get_tv_string_chk(&argvars[0]);
15485 if (serverid == NULL)
15486 {
15487 rettv->vval.v_number = -1;
15488 return; /* type error; errmsg already given */
15489 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015490# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015491 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015492 if (n == 0)
15493 rettv->vval.v_number = -1;
15494 else
15495 {
15496 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15497 rettv->vval.v_number = (s != NULL);
15498 }
15499# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015500 if (check_connection() == FAIL)
15501 return;
15502
15503 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015504 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505# endif
15506
15507 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015509 char_u *retvar;
15510
Bram Moolenaar33570922005-01-25 22:26:29 +000015511 v.di_tv.v_type = VAR_STRING;
15512 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015513 retvar = get_tv_string_chk(&argvars[1]);
15514 if (retvar != NULL)
15515 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015516 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015517 }
15518#else
15519 rettv->vval.v_number = -1;
15520#endif
15521}
15522
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 static void
15524f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015525 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527{
15528 char_u *r = NULL;
15529
15530#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 char_u *serverid = get_tv_string_chk(&argvars[0]);
15532
15533 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534 {
15535# ifdef WIN32
15536 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015537 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015538
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015539 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015540 if (n != 0)
15541 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15542 if (r == NULL)
15543# else
15544 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015546# endif
15547 EMSG(_("E277: Unable to read a server reply"));
15548 }
15549#endif
15550 rettv->v_type = VAR_STRING;
15551 rettv->vval.v_string = r;
15552}
15553
15554/*
15555 * "remote_send()" function
15556 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557 static void
15558f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561{
15562 rettv->v_type = VAR_STRING;
15563 rettv->vval.v_string = NULL;
15564#ifdef FEAT_CLIENTSERVER
15565 remote_common(argvars, rettv, FALSE);
15566#endif
15567}
15568
15569/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015570 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015571 */
15572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 typval_T *argvars;
15575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015576{
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 list_T *l;
15578 listitem_T *item, *item2;
15579 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015580 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015581 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015582 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015583 dict_T *d;
15584 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015585 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586
Bram Moolenaar8c711452005-01-14 21:53:12 +000015587 if (argvars[0].v_type == VAR_DICT)
15588 {
15589 if (argvars[2].v_type != VAR_UNKNOWN)
15590 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015591 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015592 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 key = get_tv_string_chk(&argvars[1]);
15595 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015597 di = dict_find(d, key, -1);
15598 if (di == NULL)
15599 EMSG2(_(e_dictkey), key);
15600 else
15601 {
15602 *rettv = di->di_tv;
15603 init_tv(&di->di_tv);
15604 dictitem_remove(d, di);
15605 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015607 }
15608 }
15609 else if (argvars[0].v_type != VAR_LIST)
15610 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015611 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015612 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 int error = FALSE;
15615
15616 idx = get_tv_number_chk(&argvars[1], &error);
15617 if (error)
15618 ; /* type error: do nothing, errmsg already given */
15619 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015620 EMSGN(_(e_listidx), idx);
15621 else
15622 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015623 if (argvars[2].v_type == VAR_UNKNOWN)
15624 {
15625 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015626 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015627 *rettv = item->li_tv;
15628 vim_free(item);
15629 }
15630 else
15631 {
15632 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015633 end = get_tv_number_chk(&argvars[2], &error);
15634 if (error)
15635 ; /* type error: do nothing */
15636 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015637 EMSGN(_(e_listidx), end);
15638 else
15639 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015640 int cnt = 0;
15641
15642 for (li = item; li != NULL; li = li->li_next)
15643 {
15644 ++cnt;
15645 if (li == item2)
15646 break;
15647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015648 if (li == NULL) /* didn't find "item2" after "item" */
15649 EMSG(_(e_invrange));
15650 else
15651 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015652 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015653 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015654 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015655 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015656 l->lv_first = item;
15657 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015658 item->li_prev = NULL;
15659 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015660 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015661 }
15662 }
15663 }
15664 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015665 }
15666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667}
15668
15669/*
15670 * "rename({from}, {to})" function
15671 */
15672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 typval_T *argvars;
15675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676{
15677 char_u buf[NUMBUFLEN];
15678
15679 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15683 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684}
15685
15686/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015687 * "repeat()" function
15688 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015689 static void
15690f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *argvars;
15692 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015693{
15694 char_u *p;
15695 int n;
15696 int slen;
15697 int len;
15698 char_u *r;
15699 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015700
15701 n = get_tv_number(&argvars[1]);
15702 if (argvars[0].v_type == VAR_LIST)
15703 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015705 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 if (list_extend(rettv->vval.v_list,
15707 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015708 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015709 }
15710 else
15711 {
15712 p = get_tv_string(&argvars[0]);
15713 rettv->v_type = VAR_STRING;
15714 rettv->vval.v_string = NULL;
15715
15716 slen = (int)STRLEN(p);
15717 len = slen * n;
15718 if (len <= 0)
15719 return;
15720
15721 r = alloc(len + 1);
15722 if (r != NULL)
15723 {
15724 for (i = 0; i < n; i++)
15725 mch_memmove(r + i * slen, p, (size_t)slen);
15726 r[len] = NUL;
15727 }
15728
15729 rettv->vval.v_string = r;
15730 }
15731}
15732
15733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 * "resolve()" function
15735 */
15736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 typval_T *argvars;
15739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740{
15741 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015742#ifdef HAVE_READLINK
15743 char_u *buf = NULL;
15744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747#ifdef FEAT_SHORTCUT
15748 {
15749 char_u *v = NULL;
15750
15751 v = mch_resolve_shortcut(p);
15752 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015755 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 }
15757#else
15758# ifdef HAVE_READLINK
15759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 char_u *cpy;
15761 int len;
15762 char_u *remain = NULL;
15763 char_u *q;
15764 int is_relative_to_current = FALSE;
15765 int has_trailing_pathsep = FALSE;
15766 int limit = 100;
15767
15768 p = vim_strsave(p);
15769
15770 if (p[0] == '.' && (vim_ispathsep(p[1])
15771 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15772 is_relative_to_current = TRUE;
15773
15774 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015775 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015778 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780
15781 q = getnextcomp(p);
15782 if (*q != NUL)
15783 {
15784 /* Separate the first path component in "p", and keep the
15785 * remainder (beginning with the path separator). */
15786 remain = vim_strsave(q - 1);
15787 q[-1] = NUL;
15788 }
15789
Bram Moolenaard9462e32011-04-11 21:35:11 +020015790 buf = alloc(MAXPATHL + 1);
15791 if (buf == NULL)
15792 goto fail;
15793
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 for (;;)
15795 {
15796 for (;;)
15797 {
15798 len = readlink((char *)p, (char *)buf, MAXPATHL);
15799 if (len <= 0)
15800 break;
15801 buf[len] = NUL;
15802
15803 if (limit-- == 0)
15804 {
15805 vim_free(p);
15806 vim_free(remain);
15807 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 goto fail;
15810 }
15811
15812 /* Ensure that the result will have a trailing path separator
15813 * if the argument has one. */
15814 if (remain == NULL && has_trailing_pathsep)
15815 add_pathsep(buf);
15816
15817 /* Separate the first path component in the link value and
15818 * concatenate the remainders. */
15819 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15820 if (*q != NUL)
15821 {
15822 if (remain == NULL)
15823 remain = vim_strsave(q - 1);
15824 else
15825 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015826 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 if (cpy != NULL)
15828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 vim_free(remain);
15830 remain = cpy;
15831 }
15832 }
15833 q[-1] = NUL;
15834 }
15835
15836 q = gettail(p);
15837 if (q > p && *q == NUL)
15838 {
15839 /* Ignore trailing path separator. */
15840 q[-1] = NUL;
15841 q = gettail(p);
15842 }
15843 if (q > p && !mch_isFullName(buf))
15844 {
15845 /* symlink is relative to directory of argument */
15846 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15847 if (cpy != NULL)
15848 {
15849 STRCPY(cpy, p);
15850 STRCPY(gettail(cpy), buf);
15851 vim_free(p);
15852 p = cpy;
15853 }
15854 }
15855 else
15856 {
15857 vim_free(p);
15858 p = vim_strsave(buf);
15859 }
15860 }
15861
15862 if (remain == NULL)
15863 break;
15864
15865 /* Append the first path component of "remain" to "p". */
15866 q = getnextcomp(remain + 1);
15867 len = q - remain - (*q != NUL);
15868 cpy = vim_strnsave(p, STRLEN(p) + len);
15869 if (cpy != NULL)
15870 {
15871 STRNCAT(cpy, remain, len);
15872 vim_free(p);
15873 p = cpy;
15874 }
15875 /* Shorten "remain". */
15876 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015877 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 else
15879 {
15880 vim_free(remain);
15881 remain = NULL;
15882 }
15883 }
15884
15885 /* If the result is a relative path name, make it explicitly relative to
15886 * the current directory if and only if the argument had this form. */
15887 if (!vim_ispathsep(*p))
15888 {
15889 if (is_relative_to_current
15890 && *p != NUL
15891 && !(p[0] == '.'
15892 && (p[1] == NUL
15893 || vim_ispathsep(p[1])
15894 || (p[1] == '.'
15895 && (p[2] == NUL
15896 || vim_ispathsep(p[2]))))))
15897 {
15898 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015899 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 if (cpy != NULL)
15901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 vim_free(p);
15903 p = cpy;
15904 }
15905 }
15906 else if (!is_relative_to_current)
15907 {
15908 /* Strip leading "./". */
15909 q = p;
15910 while (q[0] == '.' && vim_ispathsep(q[1]))
15911 q += 2;
15912 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015913 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 }
15915 }
15916
15917 /* Ensure that the result will have no trailing path separator
15918 * if the argument had none. But keep "/" or "//". */
15919 if (!has_trailing_pathsep)
15920 {
15921 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015922 if (after_pathsep(p, q))
15923 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 }
15925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015926 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 }
15928# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015929 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930# endif
15931#endif
15932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015933 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934
15935#ifdef HAVE_READLINK
15936fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015937 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940}
15941
15942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 */
15945 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015947 typval_T *argvars;
15948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949{
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 list_T *l;
15951 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952
Bram Moolenaar0d660222005-01-07 21:51:51 +000015953 if (argvars[0].v_type != VAR_LIST)
15954 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015955 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015956 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015957 {
15958 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015959 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015960 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 while (li != NULL)
15962 {
15963 ni = li->li_prev;
15964 list_append(l, li);
15965 li = ni;
15966 }
15967 rettv->vval.v_list = l;
15968 rettv->v_type = VAR_LIST;
15969 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015970 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972}
15973
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015974#define SP_NOMOVE 0x01 /* don't move cursor */
15975#define SP_REPEAT 0x02 /* repeat to find outer pair */
15976#define SP_RETCOUNT 0x04 /* return matchcount */
15977#define SP_SETPCMARK 0x08 /* set previous context mark */
15978#define SP_START 0x10 /* accept match at start position */
15979#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15980#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015981
Bram Moolenaar33570922005-01-25 22:26:29 +000015982static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015983
15984/*
15985 * Get flags for a search function.
15986 * Possibly sets "p_ws".
15987 * Returns BACKWARD, FORWARD or zero (for an error).
15988 */
15989 static int
15990get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015992 int *flagsp;
15993{
15994 int dir = FORWARD;
15995 char_u *flags;
15996 char_u nbuf[NUMBUFLEN];
15997 int mask;
15998
15999 if (varp->v_type != VAR_UNKNOWN)
16000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 flags = get_tv_string_buf_chk(varp, nbuf);
16002 if (flags == NULL)
16003 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016004 while (*flags != NUL)
16005 {
16006 switch (*flags)
16007 {
16008 case 'b': dir = BACKWARD; break;
16009 case 'w': p_ws = TRUE; break;
16010 case 'W': p_ws = FALSE; break;
16011 default: mask = 0;
16012 if (flagsp != NULL)
16013 switch (*flags)
16014 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016015 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016016 case 'e': mask = SP_END; break;
16017 case 'm': mask = SP_RETCOUNT; break;
16018 case 'n': mask = SP_NOMOVE; break;
16019 case 'p': mask = SP_SUBPAT; break;
16020 case 'r': mask = SP_REPEAT; break;
16021 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016022 }
16023 if (mask == 0)
16024 {
16025 EMSG2(_(e_invarg2), flags);
16026 dir = 0;
16027 }
16028 else
16029 *flagsp |= mask;
16030 }
16031 if (dir == 0)
16032 break;
16033 ++flags;
16034 }
16035 }
16036 return dir;
16037}
16038
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016040 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016042 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016043search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016046 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016048 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 char_u *pat;
16050 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016051 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 int save_p_ws = p_ws;
16053 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016054 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016055 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016056 proftime_T tm;
16057#ifdef FEAT_RELTIME
16058 long time_limit = 0;
16059#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016060 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016061 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016064 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016065 if (dir == 0)
16066 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016067 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016068 if (flags & SP_START)
16069 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016070 if (flags & SP_END)
16071 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016072
Bram Moolenaar76929292008-01-06 19:07:36 +000016073 /* Optional arguments: line number to stop searching and timeout. */
16074 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 {
16076 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16077 if (lnum_stop < 0)
16078 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016079#ifdef FEAT_RELTIME
16080 if (argvars[3].v_type != VAR_UNKNOWN)
16081 {
16082 time_limit = get_tv_number_chk(&argvars[3], NULL);
16083 if (time_limit < 0)
16084 goto theend;
16085 }
16086#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 }
16088
Bram Moolenaar76929292008-01-06 19:07:36 +000016089#ifdef FEAT_RELTIME
16090 /* Set the time limit, if there is one. */
16091 profile_setlimit(time_limit, &tm);
16092#endif
16093
Bram Moolenaar231334e2005-07-25 20:46:57 +000016094 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016095 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016096 * Check to make sure only those flags are set.
16097 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16098 * flags cannot be set. Check for that condition also.
16099 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016100 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016101 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016103 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016104 goto theend;
16105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016107 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016108 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016109 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016110 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016112 if (flags & SP_SUBPAT)
16113 retval = subpatnum;
16114 else
16115 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016116 if (flags & SP_SETPCMARK)
16117 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016119 if (match_pos != NULL)
16120 {
16121 /* Store the match cursor position */
16122 match_pos->lnum = pos.lnum;
16123 match_pos->col = pos.col + 1;
16124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 /* "/$" will put the cursor after the end of the line, may need to
16126 * correct that here */
16127 check_cursor();
16128 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016129
16130 /* If 'n' flag is used: restore cursor position. */
16131 if (flags & SP_NOMOVE)
16132 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016133 else
16134 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016135theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137
16138 return retval;
16139}
16140
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016141#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016142
16143/*
16144 * round() is not in C90, use ceil() or floor() instead.
16145 */
16146 float_T
16147vim_round(f)
16148 float_T f;
16149{
16150 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16151}
16152
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016153/*
16154 * "round({float})" function
16155 */
16156 static void
16157f_round(argvars, rettv)
16158 typval_T *argvars;
16159 typval_T *rettv;
16160{
16161 float_T f;
16162
16163 rettv->v_type = VAR_FLOAT;
16164 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016165 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166 else
16167 rettv->vval.v_float = 0.0;
16168}
16169#endif
16170
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016171/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016172 * "screenattr()" function
16173 */
16174 static void
16175f_screenattr(argvars, rettv)
16176 typval_T *argvars UNUSED;
16177 typval_T *rettv;
16178{
16179 int row;
16180 int col;
16181 int c;
16182
16183 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16184 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16185 if (row < 0 || row >= screen_Rows
16186 || col < 0 || col >= screen_Columns)
16187 c = -1;
16188 else
16189 c = ScreenAttrs[LineOffset[row] + col];
16190 rettv->vval.v_number = c;
16191}
16192
16193/*
16194 * "screenchar()" function
16195 */
16196 static void
16197f_screenchar(argvars, rettv)
16198 typval_T *argvars UNUSED;
16199 typval_T *rettv;
16200{
16201 int row;
16202 int col;
16203 int off;
16204 int c;
16205
16206 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16207 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16208 if (row < 0 || row >= screen_Rows
16209 || col < 0 || col >= screen_Columns)
16210 c = -1;
16211 else
16212 {
16213 off = LineOffset[row] + col;
16214#ifdef FEAT_MBYTE
16215 if (enc_utf8 && ScreenLinesUC[off] != 0)
16216 c = ScreenLinesUC[off];
16217 else
16218#endif
16219 c = ScreenLines[off];
16220 }
16221 rettv->vval.v_number = c;
16222}
16223
16224/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016225 * "screencol()" function
16226 *
16227 * First column is 1 to be consistent with virtcol().
16228 */
16229 static void
16230f_screencol(argvars, rettv)
16231 typval_T *argvars UNUSED;
16232 typval_T *rettv;
16233{
16234 rettv->vval.v_number = screen_screencol() + 1;
16235}
16236
16237/*
16238 * "screenrow()" function
16239 */
16240 static void
16241f_screenrow(argvars, rettv)
16242 typval_T *argvars UNUSED;
16243 typval_T *rettv;
16244{
16245 rettv->vval.v_number = screen_screenrow() + 1;
16246}
16247
16248/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016249 * "search()" function
16250 */
16251 static void
16252f_search(argvars, rettv)
16253 typval_T *argvars;
16254 typval_T *rettv;
16255{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016256 int flags = 0;
16257
16258 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259}
16260
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016262 * "searchdecl()" function
16263 */
16264 static void
16265f_searchdecl(argvars, rettv)
16266 typval_T *argvars;
16267 typval_T *rettv;
16268{
16269 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016270 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016271 int error = FALSE;
16272 char_u *name;
16273
16274 rettv->vval.v_number = 1; /* default: FAIL */
16275
16276 name = get_tv_string_chk(&argvars[0]);
16277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016278 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016279 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016280 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16281 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16282 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016283 if (!error && name != NULL)
16284 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016285 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016286}
16287
16288/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016289 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016291 static int
16292searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016294 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295{
16296 char_u *spat, *mpat, *epat;
16297 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 int dir;
16300 int flags = 0;
16301 char_u nbuf1[NUMBUFLEN];
16302 char_u nbuf2[NUMBUFLEN];
16303 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016304 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016305 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016306 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 spat = get_tv_string_chk(&argvars[0]);
16310 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16311 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16312 if (spat == NULL || mpat == NULL || epat == NULL)
16313 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 /* Handle the optional fourth argument: flags */
16316 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016317 if (dir == 0)
16318 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016319
16320 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016321 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16322 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016323 if ((flags & (SP_END | SP_SUBPAT)) != 0
16324 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016325 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016326 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016327 goto theend;
16328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016330 /* Using 'r' implies 'W', otherwise it doesn't work. */
16331 if (flags & SP_REPEAT)
16332 p_ws = FALSE;
16333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016334 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016335 if (argvars[3].v_type == VAR_UNKNOWN
16336 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 skip = (char_u *)"";
16338 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016340 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016341 if (argvars[5].v_type != VAR_UNKNOWN)
16342 {
16343 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16344 if (lnum_stop < 0)
16345 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016346#ifdef FEAT_RELTIME
16347 if (argvars[6].v_type != VAR_UNKNOWN)
16348 {
16349 time_limit = get_tv_number_chk(&argvars[6], NULL);
16350 if (time_limit < 0)
16351 goto theend;
16352 }
16353#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016354 }
16355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016356 if (skip == NULL)
16357 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016359 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016360 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016361
16362theend:
16363 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016364
16365 return retval;
16366}
16367
16368/*
16369 * "searchpair()" function
16370 */
16371 static void
16372f_searchpair(argvars, rettv)
16373 typval_T *argvars;
16374 typval_T *rettv;
16375{
16376 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16377}
16378
16379/*
16380 * "searchpairpos()" function
16381 */
16382 static void
16383f_searchpairpos(argvars, rettv)
16384 typval_T *argvars;
16385 typval_T *rettv;
16386{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 pos_T match_pos;
16388 int lnum = 0;
16389 int col = 0;
16390
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016391 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016393
16394 if (searchpair_cmn(argvars, &match_pos) > 0)
16395 {
16396 lnum = match_pos.lnum;
16397 col = match_pos.col;
16398 }
16399
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016400 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16401 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016402}
16403
16404/*
16405 * Search for a start/middle/end thing.
16406 * Used by searchpair(), see its documentation for the details.
16407 * Returns 0 or -1 for no match,
16408 */
16409 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016410do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16411 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016412 char_u *spat; /* start pattern */
16413 char_u *mpat; /* middle pattern */
16414 char_u *epat; /* end pattern */
16415 int dir; /* BACKWARD or FORWARD */
16416 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016417 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016418 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016419 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016420 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016421{
16422 char_u *save_cpo;
16423 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16424 long retval = 0;
16425 pos_T pos;
16426 pos_T firstpos;
16427 pos_T foundpos;
16428 pos_T save_cursor;
16429 pos_T save_pos;
16430 int n;
16431 int r;
16432 int nest = 1;
16433 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016434 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016435 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016436
16437 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16438 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016439 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016440
Bram Moolenaar76929292008-01-06 19:07:36 +000016441#ifdef FEAT_RELTIME
16442 /* Set the time limit, if there is one. */
16443 profile_setlimit(time_limit, &tm);
16444#endif
16445
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016446 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16447 * start/middle/end (pat3, for the top pair). */
16448 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16449 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16450 if (pat2 == NULL || pat3 == NULL)
16451 goto theend;
16452 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16453 if (*mpat == NUL)
16454 STRCPY(pat3, pat2);
16455 else
16456 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16457 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016458 if (flags & SP_START)
16459 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016460
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 save_cursor = curwin->w_cursor;
16462 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016463 clearpos(&firstpos);
16464 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 pat = pat3;
16466 for (;;)
16467 {
16468 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016469 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16471 /* didn't find it or found the first match again: FAIL */
16472 break;
16473
16474 if (firstpos.lnum == 0)
16475 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016476 if (equalpos(pos, foundpos))
16477 {
16478 /* Found the same position again. Can happen with a pattern that
16479 * has "\zs" at the end and searching backwards. Advance one
16480 * character and try again. */
16481 if (dir == BACKWARD)
16482 decl(&pos);
16483 else
16484 incl(&pos);
16485 }
16486 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016488 /* clear the start flag to avoid getting stuck here */
16489 options &= ~SEARCH_START;
16490
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 /* If the skip pattern matches, ignore this match. */
16492 if (*skip != NUL)
16493 {
16494 save_pos = curwin->w_cursor;
16495 curwin->w_cursor = pos;
16496 r = eval_to_bool(skip, &err, NULL, FALSE);
16497 curwin->w_cursor = save_pos;
16498 if (err)
16499 {
16500 /* Evaluating {skip} caused an error, break here. */
16501 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016502 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 break;
16504 }
16505 if (r)
16506 continue;
16507 }
16508
16509 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16510 {
16511 /* Found end when searching backwards or start when searching
16512 * forward: nested pair. */
16513 ++nest;
16514 pat = pat2; /* nested, don't search for middle */
16515 }
16516 else
16517 {
16518 /* Found end when searching forward or start when searching
16519 * backward: end of (nested) pair; or found middle in outer pair. */
16520 if (--nest == 1)
16521 pat = pat3; /* outer level, search for middle */
16522 }
16523
16524 if (nest == 0)
16525 {
16526 /* Found the match: return matchcount or line number. */
16527 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016528 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016530 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016531 if (flags & SP_SETPCMARK)
16532 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 curwin->w_cursor = pos;
16534 if (!(flags & SP_REPEAT))
16535 break;
16536 nest = 1; /* search for next unmatched */
16537 }
16538 }
16539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016540 if (match_pos != NULL)
16541 {
16542 /* Store the match cursor position */
16543 match_pos->lnum = curwin->w_cursor.lnum;
16544 match_pos->col = curwin->w_cursor.col + 1;
16545 }
16546
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016548 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 curwin->w_cursor = save_cursor;
16550
16551theend:
16552 vim_free(pat2);
16553 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016554 if (p_cpo == empty_option)
16555 p_cpo = save_cpo;
16556 else
16557 /* Darn, evaluating the {skip} expression changed the value. */
16558 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016559
16560 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561}
16562
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016563/*
16564 * "searchpos()" function
16565 */
16566 static void
16567f_searchpos(argvars, rettv)
16568 typval_T *argvars;
16569 typval_T *rettv;
16570{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016571 pos_T match_pos;
16572 int lnum = 0;
16573 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016574 int n;
16575 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016579
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016580 n = search_cmn(argvars, &match_pos, &flags);
16581 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016582 {
16583 lnum = match_pos.lnum;
16584 col = match_pos.col;
16585 }
16586
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016587 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16588 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016589 if (flags & SP_SUBPAT)
16590 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016591}
16592
16593
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594 static void
16595f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016596 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016599#ifdef FEAT_CLIENTSERVER
16600 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016601 char_u *server = get_tv_string_chk(&argvars[0]);
16602 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 if (server == NULL || reply == NULL)
16606 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 if (check_restricted() || check_secure())
16608 return;
16609# ifdef FEAT_X11
16610 if (check_connection() == FAIL)
16611 return;
16612# endif
16613
16614 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016616 EMSG(_("E258: Unable to send to client"));
16617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016619 rettv->vval.v_number = 0;
16620#else
16621 rettv->vval.v_number = -1;
16622#endif
16623}
16624
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625 static void
16626f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629{
16630 char_u *r = NULL;
16631
16632#ifdef FEAT_CLIENTSERVER
16633# ifdef WIN32
16634 r = serverGetVimNames();
16635# else
16636 make_connection();
16637 if (X_DISPLAY != NULL)
16638 r = serverGetVimNames(X_DISPLAY);
16639# endif
16640#endif
16641 rettv->v_type = VAR_STRING;
16642 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643}
16644
16645/*
16646 * "setbufvar()" function
16647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016649f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016650 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016651 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652{
16653 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016656 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 char_u nbuf[NUMBUFLEN];
16658
16659 if (check_restricted() || check_secure())
16660 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16662 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016663 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 varp = &argvars[2];
16665
16666 if (buf != NULL && varname != NULL && varp != NULL)
16667 {
16668 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
16671 if (*varname == '&')
16672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016673 long numval;
16674 char_u *strval;
16675 int error = FALSE;
16676
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016678 numval = get_tv_number_chk(varp, &error);
16679 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 if (!error && strval != NULL)
16681 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 }
16683 else
16684 {
16685 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16686 if (bufvarname != NULL)
16687 {
16688 STRCPY(bufvarname, "b:");
16689 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016690 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 vim_free(bufvarname);
16692 }
16693 }
16694
16695 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698}
16699
16700/*
16701 * "setcmdpos()" function
16702 */
16703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 typval_T *argvars;
16706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 int pos = (int)get_tv_number(&argvars[0]) - 1;
16709
16710 if (pos >= 0)
16711 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712}
16713
16714/*
16715 * "setline()" function
16716 */
16717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016719 typval_T *argvars;
16720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721{
16722 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016723 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016724 list_T *l = NULL;
16725 listitem_T *li = NULL;
16726 long added = 0;
16727 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016729 lnum = get_tv_lnum(&argvars[0]);
16730 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016732 l = argvars[1].vval.v_list;
16733 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016735 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016737
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016738 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016739 for (;;)
16740 {
16741 if (l != NULL)
16742 {
16743 /* list argument, get next string */
16744 if (li == NULL)
16745 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016746 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016747 li = li->li_next;
16748 }
16749
16750 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016751 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016752 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016753
16754 /* When coming here from Insert mode, sync undo, so that this can be
16755 * undone separately from what was previously inserted. */
16756 if (u_sync_once == 2)
16757 {
16758 u_sync_once = 1; /* notify that u_sync() was called */
16759 u_sync(TRUE);
16760 }
16761
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016762 if (lnum <= curbuf->b_ml.ml_line_count)
16763 {
16764 /* existing line, replace it */
16765 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16766 {
16767 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016768 if (lnum == curwin->w_cursor.lnum)
16769 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016770 rettv->vval.v_number = 0; /* OK */
16771 }
16772 }
16773 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16774 {
16775 /* lnum is one past the last line, append the line */
16776 ++added;
16777 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16778 rettv->vval.v_number = 0; /* OK */
16779 }
16780
16781 if (l == NULL) /* only one string argument */
16782 break;
16783 ++lnum;
16784 }
16785
16786 if (added > 0)
16787 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788}
16789
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016790static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16791
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016793 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016794 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016795 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016796set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016797 win_T *wp UNUSED;
16798 typval_T *list_arg UNUSED;
16799 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016800 typval_T *rettv;
16801{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016802#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016803 char_u *act;
16804 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016805#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016806
Bram Moolenaar2641f772005-03-25 21:58:17 +000016807 rettv->vval.v_number = -1;
16808
16809#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016810 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016811 EMSG(_(e_listreq));
16812 else
16813 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016814 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016815
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016816 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016817 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016818 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016819 if (act == NULL)
16820 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016821 if (*act == 'a' || *act == 'r')
16822 action = *act;
16823 }
16824
Bram Moolenaar81484f42012-12-05 15:16:47 +010016825 if (l != NULL && set_errorlist(wp, l, action,
16826 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016827 rettv->vval.v_number = 0;
16828 }
16829#endif
16830}
16831
16832/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016833 * "setloclist()" function
16834 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016835 static void
16836f_setloclist(argvars, rettv)
16837 typval_T *argvars;
16838 typval_T *rettv;
16839{
16840 win_T *win;
16841
16842 rettv->vval.v_number = -1;
16843
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016844 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016845 if (win != NULL)
16846 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16847}
16848
16849/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016850 * "setmatches()" function
16851 */
16852 static void
16853f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016854 typval_T *argvars UNUSED;
16855 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016856{
16857#ifdef FEAT_SEARCH_EXTRA
16858 list_T *l;
16859 listitem_T *li;
16860 dict_T *d;
16861
16862 rettv->vval.v_number = -1;
16863 if (argvars[0].v_type != VAR_LIST)
16864 {
16865 EMSG(_(e_listreq));
16866 return;
16867 }
16868 if ((l = argvars[0].vval.v_list) != NULL)
16869 {
16870
16871 /* To some extent make sure that we are dealing with a list from
16872 * "getmatches()". */
16873 li = l->lv_first;
16874 while (li != NULL)
16875 {
16876 if (li->li_tv.v_type != VAR_DICT
16877 || (d = li->li_tv.vval.v_dict) == NULL)
16878 {
16879 EMSG(_(e_invarg));
16880 return;
16881 }
16882 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16883 && dict_find(d, (char_u *)"pattern", -1) != NULL
16884 && dict_find(d, (char_u *)"priority", -1) != NULL
16885 && dict_find(d, (char_u *)"id", -1) != NULL))
16886 {
16887 EMSG(_(e_invarg));
16888 return;
16889 }
16890 li = li->li_next;
16891 }
16892
16893 clear_matches(curwin);
16894 li = l->lv_first;
16895 while (li != NULL)
16896 {
16897 d = li->li_tv.vval.v_dict;
16898 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16899 get_dict_string(d, (char_u *)"pattern", FALSE),
16900 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020016901 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016902 li = li->li_next;
16903 }
16904 rettv->vval.v_number = 0;
16905 }
16906#endif
16907}
16908
16909/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016910 * "setpos()" function
16911 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016912 static void
16913f_setpos(argvars, rettv)
16914 typval_T *argvars;
16915 typval_T *rettv;
16916{
16917 pos_T pos;
16918 int fnum;
16919 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016920 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016921
Bram Moolenaar08250432008-02-13 11:42:46 +000016922 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016923 name = get_tv_string_chk(argvars);
16924 if (name != NULL)
16925 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016926 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016927 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016928 if (--pos.col < 0)
16929 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016930 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016931 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016932 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016933 if (fnum == curbuf->b_fnum)
16934 {
16935 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016936 if (curswant >= 0)
16937 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016938 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016939 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016940 }
16941 else
16942 EMSG(_(e_invarg));
16943 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016944 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16945 {
16946 /* set mark */
16947 if (setmark_pos(name[1], &pos, fnum) == OK)
16948 rettv->vval.v_number = 0;
16949 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016950 else
16951 EMSG(_(e_invarg));
16952 }
16953 }
16954}
16955
16956/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016957 * "setqflist()" function
16958 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016959 static void
16960f_setqflist(argvars, rettv)
16961 typval_T *argvars;
16962 typval_T *rettv;
16963{
16964 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16965}
16966
16967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 * "setreg()" function
16969 */
16970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016971f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 typval_T *argvars;
16973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974{
16975 int regname;
16976 char_u *strregname;
16977 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016978 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 int append;
16980 char_u yank_type;
16981 long block_len;
16982
16983 block_len = -1;
16984 yank_type = MAUTO;
16985 append = FALSE;
16986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016987 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 if (strregname == NULL)
16991 return; /* type error; errmsg already given */
16992 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 if (regname == 0 || regname == '@')
16994 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 stropt = get_tv_string_chk(&argvars[2]);
16999 if (stropt == NULL)
17000 return; /* type error */
17001 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 switch (*stropt)
17003 {
17004 case 'a': case 'A': /* append */
17005 append = TRUE;
17006 break;
17007 case 'v': case 'c': /* character-wise selection */
17008 yank_type = MCHAR;
17009 break;
17010 case 'V': case 'l': /* line-wise selection */
17011 yank_type = MLINE;
17012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 case 'b': case Ctrl_V: /* block-wise selection */
17014 yank_type = MBLOCK;
17015 if (VIM_ISDIGIT(stropt[1]))
17016 {
17017 ++stropt;
17018 block_len = getdigits(&stropt) - 1;
17019 --stropt;
17020 }
17021 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 }
17023 }
17024
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017025 if (argvars[1].v_type == VAR_LIST)
17026 {
17027 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017028 char_u **allocval;
17029 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017030 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017031 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017032 int len = argvars[1].vval.v_list->lv_len;
17033 listitem_T *li;
17034
Bram Moolenaar7d647822014-04-05 21:28:56 +020017035 /* First half: use for pointers to result lines; second half: use for
17036 * pointers to allocated copies. */
17037 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017038 if (lstval == NULL)
17039 return;
17040 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017041 allocval = lstval + len + 2;
17042 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017043
17044 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17045 li = li->li_next)
17046 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017047 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017048 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017049 goto free_lstval;
17050 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017051 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017052 /* Need to make a copy, next get_tv_string_buf_chk() will
17053 * overwrite the string. */
17054 strval = vim_strsave(buf);
17055 if (strval == NULL)
17056 goto free_lstval;
17057 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017058 }
17059 *curval++ = strval;
17060 }
17061 *curval++ = NULL;
17062
17063 write_reg_contents_lst(regname, lstval, -1,
17064 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017065free_lstval:
17066 while (curallocval > allocval)
17067 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017068 vim_free(lstval);
17069 }
17070 else
17071 {
17072 strval = get_tv_string_chk(&argvars[1]);
17073 if (strval == NULL)
17074 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079}
17080
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017081/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017082 * "settabvar()" function
17083 */
17084 static void
17085f_settabvar(argvars, rettv)
17086 typval_T *argvars;
17087 typval_T *rettv;
17088{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017089#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017090 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017091 tabpage_T *tp;
17092#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017093 char_u *varname, *tabvarname;
17094 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017095
17096 rettv->vval.v_number = 0;
17097
17098 if (check_restricted() || check_secure())
17099 return;
17100
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017101#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017102 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017103#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017104 varname = get_tv_string_chk(&argvars[1]);
17105 varp = &argvars[2];
17106
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017107 if (varname != NULL && varp != NULL
17108#ifdef FEAT_WINDOWS
17109 && tp != NULL
17110#endif
17111 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017112 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017113#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017114 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017115 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017116#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017117
17118 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17119 if (tabvarname != NULL)
17120 {
17121 STRCPY(tabvarname, "t:");
17122 STRCPY(tabvarname + 2, varname);
17123 set_var(tabvarname, varp, TRUE);
17124 vim_free(tabvarname);
17125 }
17126
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017127#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017128 /* Restore current tabpage */
17129 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017130 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017131#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017132 }
17133}
17134
17135/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017136 * "settabwinvar()" function
17137 */
17138 static void
17139f_settabwinvar(argvars, rettv)
17140 typval_T *argvars;
17141 typval_T *rettv;
17142{
17143 setwinvar(argvars, rettv, 1);
17144}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145
17146/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017147 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 typval_T *argvars;
17152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017154 setwinvar(argvars, rettv, 0);
17155}
17156
17157/*
17158 * "setwinvar()" and "settabwinvar()" functions
17159 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017160
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017161 static void
17162setwinvar(argvars, rettv, off)
17163 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017164 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017165 int off;
17166{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 win_T *win;
17168#ifdef FEAT_WINDOWS
17169 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017170 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171#endif
17172 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017175 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176
17177 if (check_restricted() || check_secure())
17178 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017179
17180#ifdef FEAT_WINDOWS
17181 if (off == 1)
17182 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17183 else
17184 tp = curtab;
17185#endif
17186 win = find_win_by_nr(&argvars[off], tp);
17187 varname = get_tv_string_chk(&argvars[off + 1]);
17188 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
17190 if (win != NULL && varname != NULL && varp != NULL)
17191 {
17192#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017193 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195#endif
17196
17197 if (*varname == '&')
17198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017199 long numval;
17200 char_u *strval;
17201 int error = FALSE;
17202
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 numval = get_tv_number_chk(varp, &error);
17205 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017206 if (!error && strval != NULL)
17207 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 }
17209 else
17210 {
17211 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17212 if (winvarname != NULL)
17213 {
17214 STRCPY(winvarname, "w:");
17215 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017216 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 vim_free(winvarname);
17218 }
17219 }
17220
17221#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017222 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223#endif
17224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225}
17226
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017227#ifdef FEAT_CRYPT
17228/*
17229 * "sha256({string})" function
17230 */
17231 static void
17232f_sha256(argvars, rettv)
17233 typval_T *argvars;
17234 typval_T *rettv;
17235{
17236 char_u *p;
17237
17238 p = get_tv_string(&argvars[0]);
17239 rettv->vval.v_string = vim_strsave(
17240 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17241 rettv->v_type = VAR_STRING;
17242}
17243#endif /* FEAT_CRYPT */
17244
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017246 * "shellescape({string})" function
17247 */
17248 static void
17249f_shellescape(argvars, rettv)
17250 typval_T *argvars;
17251 typval_T *rettv;
17252{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017253 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017254 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017255 rettv->v_type = VAR_STRING;
17256}
17257
17258/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017259 * shiftwidth() function
17260 */
17261 static void
17262f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017263 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017264 typval_T *rettv;
17265{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017266 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017267}
17268
17269/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017270 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 */
17272 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017273f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 typval_T *argvars;
17275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017277 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278
Bram Moolenaar0d660222005-01-07 21:51:51 +000017279 p = get_tv_string(&argvars[0]);
17280 rettv->vval.v_string = vim_strsave(p);
17281 simplify_filename(rettv->vval.v_string); /* simplify in place */
17282 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283}
17284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017285#ifdef FEAT_FLOAT
17286/*
17287 * "sin()" function
17288 */
17289 static void
17290f_sin(argvars, rettv)
17291 typval_T *argvars;
17292 typval_T *rettv;
17293{
17294 float_T f;
17295
17296 rettv->v_type = VAR_FLOAT;
17297 if (get_float_arg(argvars, &f) == OK)
17298 rettv->vval.v_float = sin(f);
17299 else
17300 rettv->vval.v_float = 0.0;
17301}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017302
17303/*
17304 * "sinh()" function
17305 */
17306 static void
17307f_sinh(argvars, rettv)
17308 typval_T *argvars;
17309 typval_T *rettv;
17310{
17311 float_T f;
17312
17313 rettv->v_type = VAR_FLOAT;
17314 if (get_float_arg(argvars, &f) == OK)
17315 rettv->vval.v_float = sinh(f);
17316 else
17317 rettv->vval.v_float = 0.0;
17318}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017319#endif
17320
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321static int
17322#ifdef __BORLANDC__
17323 _RTLENTRYF
17324#endif
17325 item_compare __ARGS((const void *s1, const void *s2));
17326static int
17327#ifdef __BORLANDC__
17328 _RTLENTRYF
17329#endif
17330 item_compare2 __ARGS((const void *s1, const void *s2));
17331
17332static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017333static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017335static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017337static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338#define ITEM_COMPARE_FAIL 999
17339
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017341 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343 static int
17344#ifdef __BORLANDC__
17345_RTLENTRYF
17346#endif
17347item_compare(s1, s2)
17348 const void *s1;
17349 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351 char_u *p1, *p2;
17352 char_u *tofree1, *tofree2;
17353 int res;
17354 char_u numbuf1[NUMBUFLEN];
17355 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017357 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17358 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017359 if (p1 == NULL)
17360 p1 = (char_u *)"";
17361 if (p2 == NULL)
17362 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017363 if (!item_compare_numeric)
17364 {
17365 if (item_compare_ic)
17366 res = STRICMP(p1, p2);
17367 else
17368 res = STRCMP(p1, p2);
17369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017371 {
17372 double n1, n2;
17373 n1 = strtod((char *)p1, (char **)&p1);
17374 n2 = strtod((char *)p2, (char **)&p2);
17375 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17376 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017377 vim_free(tofree1);
17378 vim_free(tofree2);
17379 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380}
17381
17382 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383#ifdef __BORLANDC__
17384_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017386item_compare2(s1, s2)
17387 const void *s1;
17388 const void *s2;
17389{
17390 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017392 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017393 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017395 /* shortcut after failure in previous call; compare all items equal */
17396 if (item_compare_func_err)
17397 return 0;
17398
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017399 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17400 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17402 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017403
17404 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017405 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017406 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17407 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408 clear_tv(&argv[0]);
17409 clear_tv(&argv[1]);
17410
17411 if (res == FAIL)
17412 res = ITEM_COMPARE_FAIL;
17413 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17415 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017416 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017417 clear_tv(&rettv);
17418 return res;
17419}
17420
17421/*
17422 * "sort({list})" function
17423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017425do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 typval_T *argvars;
17427 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017428 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429{
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 list_T *l;
17431 listitem_T *li;
17432 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 long len;
17434 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017437 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 else
17439 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017441 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017442 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017443 return;
17444 rettv->vval.v_list = l;
17445 rettv->v_type = VAR_LIST;
17446 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 len = list_len(l);
17449 if (len <= 1)
17450 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451
Bram Moolenaar0d660222005-01-07 21:51:51 +000017452 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017453 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017455 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017456 if (argvars[1].v_type != VAR_UNKNOWN)
17457 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017458 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017461 else
17462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017463 int error = FALSE;
17464
17465 i = get_tv_number_chk(&argvars[1], &error);
17466 if (error)
17467 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017468 if (i == 1)
17469 item_compare_ic = TRUE;
17470 else
17471 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017472 if (item_compare_func != NULL)
17473 {
17474 if (STRCMP(item_compare_func, "n") == 0)
17475 {
17476 item_compare_func = NULL;
17477 item_compare_numeric = TRUE;
17478 }
17479 else if (STRCMP(item_compare_func, "i") == 0)
17480 {
17481 item_compare_func = NULL;
17482 item_compare_ic = TRUE;
17483 }
17484 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017486
17487 if (argvars[2].v_type != VAR_UNKNOWN)
17488 {
17489 /* optional third argument: {dict} */
17490 if (argvars[2].v_type != VAR_DICT)
17491 {
17492 EMSG(_(e_dictreq));
17493 return;
17494 }
17495 item_compare_selfdict = argvars[2].vval.v_dict;
17496 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498
Bram Moolenaar0d660222005-01-07 21:51:51 +000017499 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501 if (ptrs == NULL)
17502 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503
Bram Moolenaar327aa022014-03-25 18:24:23 +010017504 i = 0;
17505 if (sort)
17506 {
17507 /* sort(): ptrs will be the list to sort */
17508 for (li = l->lv_first; li != NULL; li = li->li_next)
17509 ptrs[i++] = li;
17510
17511 item_compare_func_err = FALSE;
17512 /* test the compare function */
17513 if (item_compare_func != NULL
17514 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017516 EMSG(_("E702: Sort compare function failed"));
17517 else
17518 {
17519 /* Sort the array with item pointers. */
17520 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17521 item_compare_func == NULL ? item_compare : item_compare2);
17522
17523 if (!item_compare_func_err)
17524 {
17525 /* Clear the List and append the items in sorted order. */
17526 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17527 l->lv_len = 0;
17528 for (i = 0; i < len; ++i)
17529 list_append(l, ptrs[i]);
17530 }
17531 }
17532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017535 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17536
17537 /* f_uniq(): ptrs will be a stack of items to remove */
17538 item_compare_func_err = FALSE;
17539 item_compare_func_ptr = item_compare_func
17540 ? item_compare2 : item_compare;
17541
17542 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17543 li = li->li_next)
17544 {
17545 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17546 == 0)
17547 ptrs[i++] = li;
17548 if (item_compare_func_err)
17549 {
17550 EMSG(_("E882: Uniq compare function failed"));
17551 break;
17552 }
17553 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017555 if (!item_compare_func_err)
17556 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017557 while (--i >= 0)
17558 {
17559 li = ptrs[i]->li_next;
17560 ptrs[i]->li_next = li->li_next;
17561 if (li->li_next != NULL)
17562 li->li_next->li_prev = ptrs[i];
17563 else
17564 l->lv_last = ptrs[i];
17565 list_fix_watch(l, li);
17566 listitem_free(li);
17567 l->lv_len--;
17568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570 }
17571
17572 vim_free(ptrs);
17573 }
17574}
17575
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017576/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017577 * "sort({list})" function
17578 */
17579 static void
17580f_sort(argvars, rettv)
17581 typval_T *argvars;
17582 typval_T *rettv;
17583{
17584 do_sort_uniq(argvars, rettv, TRUE);
17585}
17586
17587/*
17588 * "uniq({list})" function
17589 */
17590 static void
17591f_uniq(argvars, rettv)
17592 typval_T *argvars;
17593 typval_T *rettv;
17594{
17595 do_sort_uniq(argvars, rettv, FALSE);
17596}
17597
17598/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017599 * "soundfold({word})" function
17600 */
17601 static void
17602f_soundfold(argvars, rettv)
17603 typval_T *argvars;
17604 typval_T *rettv;
17605{
17606 char_u *s;
17607
17608 rettv->v_type = VAR_STRING;
17609 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017610#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017611 rettv->vval.v_string = eval_soundfold(s);
17612#else
17613 rettv->vval.v_string = vim_strsave(s);
17614#endif
17615}
17616
17617/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017618 * "spellbadword()" function
17619 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017620 static void
17621f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017622 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017623 typval_T *rettv;
17624{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017625 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017626 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017627 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017628
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017629 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017630 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017631
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017632#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017633 if (argvars[0].v_type == VAR_UNKNOWN)
17634 {
17635 /* Find the start and length of the badly spelled word. */
17636 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17637 if (len != 0)
17638 word = ml_get_cursor();
17639 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017640 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017641 {
17642 char_u *str = get_tv_string_chk(&argvars[0]);
17643 int capcol = -1;
17644
17645 if (str != NULL)
17646 {
17647 /* Check the argument for spelling. */
17648 while (*str != NUL)
17649 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017650 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017651 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017652 {
17653 word = str;
17654 break;
17655 }
17656 str += len;
17657 }
17658 }
17659 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017660#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017661
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017662 list_append_string(rettv->vval.v_list, word, len);
17663 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017664 attr == HLF_SPB ? "bad" :
17665 attr == HLF_SPR ? "rare" :
17666 attr == HLF_SPL ? "local" :
17667 attr == HLF_SPC ? "caps" :
17668 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017669}
17670
17671/*
17672 * "spellsuggest()" function
17673 */
17674 static void
17675f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017676 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017677 typval_T *rettv;
17678{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017679#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017680 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017681 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017682 int maxcount;
17683 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017684 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017685 listitem_T *li;
17686 int need_capital = FALSE;
17687#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017689 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017690 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017691
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017692#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017693 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017694 {
17695 str = get_tv_string(&argvars[0]);
17696 if (argvars[1].v_type != VAR_UNKNOWN)
17697 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017698 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017699 if (maxcount <= 0)
17700 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017701 if (argvars[2].v_type != VAR_UNKNOWN)
17702 {
17703 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17704 if (typeerr)
17705 return;
17706 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017707 }
17708 else
17709 maxcount = 25;
17710
Bram Moolenaar4770d092006-01-12 23:22:24 +000017711 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017712
17713 for (i = 0; i < ga.ga_len; ++i)
17714 {
17715 str = ((char_u **)ga.ga_data)[i];
17716
17717 li = listitem_alloc();
17718 if (li == NULL)
17719 vim_free(str);
17720 else
17721 {
17722 li->li_tv.v_type = VAR_STRING;
17723 li->li_tv.v_lock = 0;
17724 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017725 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017726 }
17727 }
17728 ga_clear(&ga);
17729 }
17730#endif
17731}
17732
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017734f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 typval_T *argvars;
17736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737{
17738 char_u *str;
17739 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017740 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017741 regmatch_T regmatch;
17742 char_u patbuf[NUMBUFLEN];
17743 char_u *save_cpo;
17744 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017746 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017747 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017748
17749 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17750 save_cpo = p_cpo;
17751 p_cpo = (char_u *)"";
17752
17753 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017754 if (argvars[1].v_type != VAR_UNKNOWN)
17755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17757 if (pat == NULL)
17758 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017759 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017761 }
17762 if (pat == NULL || *pat == NUL)
17763 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017765 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017767 if (typeerr)
17768 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17771 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017774 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017775 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017776 if (*str == NUL)
17777 match = FALSE; /* empty item at the end */
17778 else
17779 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017780 if (match)
17781 end = regmatch.startp[0];
17782 else
17783 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017784 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17785 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017787 if (list_append_string(rettv->vval.v_list, str,
17788 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017789 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 }
17791 if (!match)
17792 break;
17793 /* Advance to just after the match. */
17794 if (regmatch.endp[0] > str)
17795 col = 0;
17796 else
17797 {
17798 /* Don't get stuck at the same match. */
17799#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017800 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801#else
17802 col = 1;
17803#endif
17804 }
17805 str = regmatch.endp[0];
17806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807
Bram Moolenaar473de612013-06-08 18:19:48 +020017808 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaar0d660222005-01-07 21:51:51 +000017811 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812}
17813
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017814#ifdef FEAT_FLOAT
17815/*
17816 * "sqrt()" function
17817 */
17818 static void
17819f_sqrt(argvars, rettv)
17820 typval_T *argvars;
17821 typval_T *rettv;
17822{
17823 float_T f;
17824
17825 rettv->v_type = VAR_FLOAT;
17826 if (get_float_arg(argvars, &f) == OK)
17827 rettv->vval.v_float = sqrt(f);
17828 else
17829 rettv->vval.v_float = 0.0;
17830}
17831
17832/*
17833 * "str2float()" function
17834 */
17835 static void
17836f_str2float(argvars, rettv)
17837 typval_T *argvars;
17838 typval_T *rettv;
17839{
17840 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17841
17842 if (*p == '+')
17843 p = skipwhite(p + 1);
17844 (void)string2float(p, &rettv->vval.v_float);
17845 rettv->v_type = VAR_FLOAT;
17846}
17847#endif
17848
Bram Moolenaar2c932302006-03-18 21:42:09 +000017849/*
17850 * "str2nr()" function
17851 */
17852 static void
17853f_str2nr(argvars, rettv)
17854 typval_T *argvars;
17855 typval_T *rettv;
17856{
17857 int base = 10;
17858 char_u *p;
17859 long n;
17860
17861 if (argvars[1].v_type != VAR_UNKNOWN)
17862 {
17863 base = get_tv_number(&argvars[1]);
17864 if (base != 8 && base != 10 && base != 16)
17865 {
17866 EMSG(_(e_invarg));
17867 return;
17868 }
17869 }
17870
17871 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017872 if (*p == '+')
17873 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017874 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17875 rettv->vval.v_number = n;
17876}
17877
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878#ifdef HAVE_STRFTIME
17879/*
17880 * "strftime({format}[, {time}])" function
17881 */
17882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017883f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 typval_T *argvars;
17885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886{
17887 char_u result_buf[256];
17888 struct tm *curtime;
17889 time_t seconds;
17890 char_u *p;
17891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017894 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017895 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 seconds = time(NULL);
17897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017898 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 curtime = localtime(&seconds);
17900 /* MSVC returns NULL for an invalid value of seconds. */
17901 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017902 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 else
17904 {
17905# ifdef FEAT_MBYTE
17906 vimconv_T conv;
17907 char_u *enc;
17908
17909 conv.vc_type = CONV_NONE;
17910 enc = enc_locale();
17911 convert_setup(&conv, p_enc, enc);
17912 if (conv.vc_type != CONV_NONE)
17913 p = string_convert(&conv, p, NULL);
17914# endif
17915 if (p != NULL)
17916 (void)strftime((char *)result_buf, sizeof(result_buf),
17917 (char *)p, curtime);
17918 else
17919 result_buf[0] = NUL;
17920
17921# ifdef FEAT_MBYTE
17922 if (conv.vc_type != CONV_NONE)
17923 vim_free(p);
17924 convert_setup(&conv, enc, p_enc);
17925 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017926 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 else
17928# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017929 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
17931# ifdef FEAT_MBYTE
17932 /* Release conversion descriptors */
17933 convert_setup(&conv, NULL, NULL);
17934 vim_free(enc);
17935# endif
17936 }
17937}
17938#endif
17939
17940/*
17941 * "stridx()" function
17942 */
17943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017944f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017945 typval_T *argvars;
17946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947{
17948 char_u buf[NUMBUFLEN];
17949 char_u *needle;
17950 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017955 needle = get_tv_string_chk(&argvars[1]);
17956 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017958 if (needle == NULL || haystack == NULL)
17959 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960
Bram Moolenaar33570922005-01-25 22:26:29 +000017961 if (argvars[2].v_type != VAR_UNKNOWN)
17962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017963 int error = FALSE;
17964
17965 start_idx = get_tv_number_chk(&argvars[2], &error);
17966 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017968 if (start_idx >= 0)
17969 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017970 }
17971
17972 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17973 if (pos != NULL)
17974 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975}
17976
17977/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017978 * "string()" function
17979 */
17980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 typval_T *argvars;
17983 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017984{
17985 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017986 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017988 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017989 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017990 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017991 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993}
17994
17995/*
17996 * "strlen()" function
17997 */
17998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017999f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 typval_T *argvars;
18001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003 rettv->vval.v_number = (varnumber_T)(STRLEN(
18004 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005}
18006
18007/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018008 * "strchars()" function
18009 */
18010 static void
18011f_strchars(argvars, rettv)
18012 typval_T *argvars;
18013 typval_T *rettv;
18014{
18015 char_u *s = get_tv_string(&argvars[0]);
18016#ifdef FEAT_MBYTE
18017 varnumber_T len = 0;
18018
18019 while (*s != NUL)
18020 {
18021 mb_cptr2char_adv(&s);
18022 ++len;
18023 }
18024 rettv->vval.v_number = len;
18025#else
18026 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18027#endif
18028}
18029
18030/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018031 * "strdisplaywidth()" function
18032 */
18033 static void
18034f_strdisplaywidth(argvars, rettv)
18035 typval_T *argvars;
18036 typval_T *rettv;
18037{
18038 char_u *s = get_tv_string(&argvars[0]);
18039 int col = 0;
18040
18041 if (argvars[1].v_type != VAR_UNKNOWN)
18042 col = get_tv_number(&argvars[1]);
18043
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018044 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018045}
18046
18047/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018048 * "strwidth()" function
18049 */
18050 static void
18051f_strwidth(argvars, rettv)
18052 typval_T *argvars;
18053 typval_T *rettv;
18054{
18055 char_u *s = get_tv_string(&argvars[0]);
18056
18057 rettv->vval.v_number = (varnumber_T)(
18058#ifdef FEAT_MBYTE
18059 mb_string2cells(s, -1)
18060#else
18061 STRLEN(s)
18062#endif
18063 );
18064}
18065
18066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 * "strpart()" function
18068 */
18069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018070f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018071 typval_T *argvars;
18072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073{
18074 char_u *p;
18075 int n;
18076 int len;
18077 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018080 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 slen = (int)STRLEN(p);
18082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 n = get_tv_number_chk(&argvars[1], &error);
18084 if (error)
18085 len = 0;
18086 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 else
18089 len = slen - n; /* default len: all bytes that are available. */
18090
18091 /*
18092 * Only return the overlap between the specified part and the actual
18093 * string.
18094 */
18095 if (n < 0)
18096 {
18097 len += n;
18098 n = 0;
18099 }
18100 else if (n > slen)
18101 n = slen;
18102 if (len < 0)
18103 len = 0;
18104 else if (n + len > slen)
18105 len = slen - n;
18106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 rettv->v_type = VAR_STRING;
18108 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109}
18110
18111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018112 * "strridx()" function
18113 */
18114 static void
18115f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018116 typval_T *argvars;
18117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018118{
18119 char_u buf[NUMBUFLEN];
18120 char_u *needle;
18121 char_u *haystack;
18122 char_u *rest;
18123 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018124 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018126 needle = get_tv_string_chk(&argvars[1]);
18127 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018128
18129 rettv->vval.v_number = -1;
18130 if (needle == NULL || haystack == NULL)
18131 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018132
18133 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018134 if (argvars[2].v_type != VAR_UNKNOWN)
18135 {
18136 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018137 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018138 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018139 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018140 }
18141 else
18142 end_idx = haystack_len;
18143
Bram Moolenaar0d660222005-01-07 21:51:51 +000018144 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018145 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018147 lastmatch = haystack + end_idx;
18148 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018149 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 for (rest = haystack; *rest != '\0'; ++rest)
18152 {
18153 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018154 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 break;
18156 lastmatch = rest;
18157 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018159
18160 if (lastmatch == NULL)
18161 rettv->vval.v_number = -1;
18162 else
18163 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18164}
18165
18166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 * "strtrans()" function
18168 */
18169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 typval_T *argvars;
18172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174 rettv->v_type = VAR_STRING;
18175 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176}
18177
18178/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 * "submatch()" function
18180 */
18181 static void
18182f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018183 typval_T *argvars;
18184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018185{
Bram Moolenaar41571762014-04-02 19:00:58 +020018186 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018187 int no;
18188 int retList = 0;
18189
18190 no = (int)get_tv_number_chk(&argvars[0], &error);
18191 if (error)
18192 return;
18193 error = FALSE;
18194 if (argvars[1].v_type != VAR_UNKNOWN)
18195 retList = get_tv_number_chk(&argvars[1], &error);
18196 if (error)
18197 return;
18198
18199 if (retList == 0)
18200 {
18201 rettv->v_type = VAR_STRING;
18202 rettv->vval.v_string = reg_submatch(no);
18203 }
18204 else
18205 {
18206 rettv->v_type = VAR_LIST;
18207 rettv->vval.v_list = reg_submatch_list(no);
18208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209}
18210
18211/*
18212 * "substitute()" function
18213 */
18214 static void
18215f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018216 typval_T *argvars;
18217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018218{
18219 char_u patbuf[NUMBUFLEN];
18220 char_u subbuf[NUMBUFLEN];
18221 char_u flagsbuf[NUMBUFLEN];
18222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 char_u *str = get_tv_string_chk(&argvars[0]);
18224 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18225 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18226 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18227
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018229 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18230 rettv->vval.v_string = NULL;
18231 else
18232 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018233}
18234
18235/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018236 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018239f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018240 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242{
18243 int id = 0;
18244#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018245 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 long col;
18247 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018248 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 lnum = get_tv_lnum(argvars); /* -1 on type error */
18251 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18252 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018254 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018255 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018256 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257#endif
18258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018259 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260}
18261
18262/*
18263 * "synIDattr(id, what [, mode])" function
18264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018266f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269{
18270 char_u *p = NULL;
18271#ifdef FEAT_SYN_HL
18272 int id;
18273 char_u *what;
18274 char_u *mode;
18275 char_u modebuf[NUMBUFLEN];
18276 int modec;
18277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018278 id = get_tv_number(&argvars[0]);
18279 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018280 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018282 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018284 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 modec = 0; /* replace invalid with current */
18286 }
18287 else
18288 {
18289#ifdef FEAT_GUI
18290 if (gui.in_use)
18291 modec = 'g';
18292 else
18293#endif
18294 if (t_colors > 1)
18295 modec = 'c';
18296 else
18297 modec = 't';
18298 }
18299
18300
18301 switch (TOLOWER_ASC(what[0]))
18302 {
18303 case 'b':
18304 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18305 p = highlight_color(id, what, modec);
18306 else /* bold */
18307 p = highlight_has_attr(id, HL_BOLD, modec);
18308 break;
18309
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018310 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 p = highlight_color(id, what, modec);
18312 break;
18313
18314 case 'i':
18315 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18316 p = highlight_has_attr(id, HL_INVERSE, modec);
18317 else /* italic */
18318 p = highlight_has_attr(id, HL_ITALIC, modec);
18319 break;
18320
18321 case 'n': /* name */
18322 p = get_highlight_name(NULL, id - 1);
18323 break;
18324
18325 case 'r': /* reverse */
18326 p = highlight_has_attr(id, HL_INVERSE, modec);
18327 break;
18328
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018329 case 's':
18330 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18331 p = highlight_color(id, what, modec);
18332 else /* standout */
18333 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 break;
18335
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018336 case 'u':
18337 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18338 /* underline */
18339 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18340 else
18341 /* undercurl */
18342 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 break;
18344 }
18345
18346 if (p != NULL)
18347 p = vim_strsave(p);
18348#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018349 rettv->v_type = VAR_STRING;
18350 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351}
18352
18353/*
18354 * "synIDtrans(id)" function
18355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360{
18361 int id;
18362
18363#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
18366 if (id > 0)
18367 id = syn_get_final_id(id);
18368 else
18369#endif
18370 id = 0;
18371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373}
18374
18375/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018376 * "synconcealed(lnum, col)" function
18377 */
18378 static void
18379f_synconcealed(argvars, rettv)
18380 typval_T *argvars UNUSED;
18381 typval_T *rettv;
18382{
18383#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18384 long lnum;
18385 long col;
18386 int syntax_flags = 0;
18387 int cchar;
18388 int matchid = 0;
18389 char_u str[NUMBUFLEN];
18390#endif
18391
18392 rettv->v_type = VAR_LIST;
18393 rettv->vval.v_list = NULL;
18394
18395#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18396 lnum = get_tv_lnum(argvars); /* -1 on type error */
18397 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18398
18399 vim_memset(str, NUL, sizeof(str));
18400
18401 if (rettv_list_alloc(rettv) != FAIL)
18402 {
18403 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18404 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18405 && curwin->w_p_cole > 0)
18406 {
18407 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18408 syntax_flags = get_syntax_info(&matchid);
18409
18410 /* get the conceal character */
18411 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18412 {
18413 cchar = syn_get_sub_char();
18414 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18415 cchar = lcs_conceal;
18416 if (cchar != NUL)
18417 {
18418# ifdef FEAT_MBYTE
18419 if (has_mbyte)
18420 (*mb_char2bytes)(cchar, str);
18421 else
18422# endif
18423 str[0] = cchar;
18424 }
18425 }
18426 }
18427
18428 list_append_number(rettv->vval.v_list,
18429 (syntax_flags & HL_CONCEAL) != 0);
18430 /* -1 to auto-determine strlen */
18431 list_append_string(rettv->vval.v_list, str, -1);
18432 list_append_number(rettv->vval.v_list, matchid);
18433 }
18434#endif
18435}
18436
18437/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018438 * "synstack(lnum, col)" function
18439 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018440 static void
18441f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018442 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018443 typval_T *rettv;
18444{
18445#ifdef FEAT_SYN_HL
18446 long lnum;
18447 long col;
18448 int i;
18449 int id;
18450#endif
18451
18452 rettv->v_type = VAR_LIST;
18453 rettv->vval.v_list = NULL;
18454
18455#ifdef FEAT_SYN_HL
18456 lnum = get_tv_lnum(argvars); /* -1 on type error */
18457 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18458
18459 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018460 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018461 && rettv_list_alloc(rettv) != FAIL)
18462 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018463 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018464 for (i = 0; ; ++i)
18465 {
18466 id = syn_get_stack_item(i);
18467 if (id < 0)
18468 break;
18469 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18470 break;
18471 }
18472 }
18473#endif
18474}
18475
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018477get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018478 typval_T *argvars;
18479 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018480 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018482 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018484 char_u *infile = NULL;
18485 char_u buf[NUMBUFLEN];
18486 int err = FALSE;
18487 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018488 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018490 rettv->v_type = VAR_STRING;
18491 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018492 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018493 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018495 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018496 {
18497 /*
18498 * Write the string to a temp file, to be used for input of the shell
18499 * command.
18500 */
18501 if ((infile = vim_tempname('i')) == NULL)
18502 {
18503 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018504 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018505 }
18506
18507 fd = mch_fopen((char *)infile, WRITEBIN);
18508 if (fd == NULL)
18509 {
18510 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018511 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018512 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018513 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018514 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018515 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18516 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018517 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018518 else
18519 {
18520 p = get_tv_string_buf_chk(&argvars[1], buf);
18521 if (p == NULL)
18522 {
18523 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018524 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018525 }
18526 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18527 err = TRUE;
18528 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018529 if (fclose(fd) != 0)
18530 err = TRUE;
18531 if (err)
18532 {
18533 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018534 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018535 }
18536 }
18537
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018538 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018540 int len;
18541 listitem_T *li;
18542 char_u *s = NULL;
18543 char_u *start;
18544 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018545 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018547 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18548 SHELL_SILENT | SHELL_COOKED, &len);
18549 if (res == NULL)
18550 goto errret;
18551
18552 list = list_alloc();
18553 if (list == NULL)
18554 goto errret;
18555
18556 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018558 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018559 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018560 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018561 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018562
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018563 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018564 if (s == NULL)
18565 goto errret;
18566
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018567 for (p = s; start < end; ++p, ++start)
18568 *p = *start == NUL ? NL : *start;
18569 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018570
18571 li = listitem_alloc();
18572 if (li == NULL)
18573 {
18574 vim_free(s);
18575 goto errret;
18576 }
18577 li->li_tv.v_type = VAR_STRING;
18578 li->li_tv.vval.v_string = s;
18579 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018581
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018582 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018583 rettv->v_type = VAR_LIST;
18584 rettv->vval.v_list = list;
18585 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018587 else
18588 {
18589 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18590 SHELL_SILENT | SHELL_COOKED, NULL);
18591#ifdef USE_CR
18592 /* translate <CR> into <NL> */
18593 if (res != NULL)
18594 {
18595 char_u *s;
18596
18597 for (s = res; *s; ++s)
18598 {
18599 if (*s == CAR)
18600 *s = NL;
18601 }
18602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603#else
18604# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018605 /* translate <CR><NL> into <NL> */
18606 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018608 char_u *s, *d;
18609
18610 d = res;
18611 for (s = res; *s; ++s)
18612 {
18613 if (s[0] == CAR && s[1] == NL)
18614 ++s;
18615 *d++ = *s;
18616 }
18617 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619# endif
18620#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018621 rettv->vval.v_string = res;
18622 res = NULL;
18623 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018624
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018625errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018626 if (infile != NULL)
18627 {
18628 mch_remove(infile);
18629 vim_free(infile);
18630 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018631 if (res != NULL)
18632 vim_free(res);
18633 if (list != NULL)
18634 list_free(list, TRUE);
18635}
18636
18637/*
18638 * "system()" function
18639 */
18640 static void
18641f_system(argvars, rettv)
18642 typval_T *argvars;
18643 typval_T *rettv;
18644{
18645 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18646}
18647
18648/*
18649 * "systemlist()" function
18650 */
18651 static void
18652f_systemlist(argvars, rettv)
18653 typval_T *argvars;
18654 typval_T *rettv;
18655{
18656 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018660 * "tabpagebuflist()" function
18661 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018662 static void
18663f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018664 typval_T *argvars UNUSED;
18665 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018666{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018667#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018668 tabpage_T *tp;
18669 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018670
18671 if (argvars[0].v_type == VAR_UNKNOWN)
18672 wp = firstwin;
18673 else
18674 {
18675 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18676 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018677 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018678 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018679 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018680 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018681 for (; wp != NULL; wp = wp->w_next)
18682 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018683 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018684 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018685 }
18686#endif
18687}
18688
18689
18690/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018691 * "tabpagenr()" function
18692 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018693 static void
18694f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018695 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018696 typval_T *rettv;
18697{
18698 int nr = 1;
18699#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018700 char_u *arg;
18701
18702 if (argvars[0].v_type != VAR_UNKNOWN)
18703 {
18704 arg = get_tv_string_chk(&argvars[0]);
18705 nr = 0;
18706 if (arg != NULL)
18707 {
18708 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018709 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018710 else
18711 EMSG2(_(e_invexpr2), arg);
18712 }
18713 }
18714 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018715 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018716#endif
18717 rettv->vval.v_number = nr;
18718}
18719
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018720
18721#ifdef FEAT_WINDOWS
18722static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18723
18724/*
18725 * Common code for tabpagewinnr() and winnr().
18726 */
18727 static int
18728get_winnr(tp, argvar)
18729 tabpage_T *tp;
18730 typval_T *argvar;
18731{
18732 win_T *twin;
18733 int nr = 1;
18734 win_T *wp;
18735 char_u *arg;
18736
18737 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18738 if (argvar->v_type != VAR_UNKNOWN)
18739 {
18740 arg = get_tv_string_chk(argvar);
18741 if (arg == NULL)
18742 nr = 0; /* type error; errmsg already given */
18743 else if (STRCMP(arg, "$") == 0)
18744 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18745 else if (STRCMP(arg, "#") == 0)
18746 {
18747 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18748 if (twin == NULL)
18749 nr = 0;
18750 }
18751 else
18752 {
18753 EMSG2(_(e_invexpr2), arg);
18754 nr = 0;
18755 }
18756 }
18757
18758 if (nr > 0)
18759 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18760 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018761 {
18762 if (wp == NULL)
18763 {
18764 /* didn't find it in this tabpage */
18765 nr = 0;
18766 break;
18767 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018768 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018769 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018770 return nr;
18771}
18772#endif
18773
18774/*
18775 * "tabpagewinnr()" function
18776 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018777 static void
18778f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018779 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018780 typval_T *rettv;
18781{
18782 int nr = 1;
18783#ifdef FEAT_WINDOWS
18784 tabpage_T *tp;
18785
18786 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18787 if (tp == NULL)
18788 nr = 0;
18789 else
18790 nr = get_winnr(tp, &argvars[1]);
18791#endif
18792 rettv->vval.v_number = nr;
18793}
18794
18795
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018796/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018797 * "tagfiles()" function
18798 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018799 static void
18800f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018801 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018802 typval_T *rettv;
18803{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018804 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018805 tagname_T tn;
18806 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018808 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018809 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018810 fname = alloc(MAXPATHL);
18811 if (fname == NULL)
18812 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018813
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018814 for (first = TRUE; ; first = FALSE)
18815 if (get_tagfname(&tn, first, fname) == FAIL
18816 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018817 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018818 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018819 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018820}
18821
18822/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018823 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018824 */
18825 static void
18826f_taglist(argvars, rettv)
18827 typval_T *argvars;
18828 typval_T *rettv;
18829{
18830 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018831
18832 tag_pattern = get_tv_string(&argvars[0]);
18833
18834 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018835 if (*tag_pattern == NUL)
18836 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018837
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018838 if (rettv_list_alloc(rettv) == OK)
18839 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018840}
18841
18842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 * "tempname()" function
18844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849{
18850 static int x = 'A';
18851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018852 rettv->v_type = VAR_STRING;
18853 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854
18855 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18856 * names. Skip 'I' and 'O', they are used for shell redirection. */
18857 do
18858 {
18859 if (x == 'Z')
18860 x = '0';
18861 else if (x == '9')
18862 x = 'A';
18863 else
18864 {
18865#ifdef EBCDIC
18866 if (x == 'I')
18867 x = 'J';
18868 else if (x == 'R')
18869 x = 'S';
18870 else
18871#endif
18872 ++x;
18873 }
18874 } while (x == 'I' || x == 'O');
18875}
18876
18877/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018878 * "test(list)" function: Just checking the walls...
18879 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018880 static void
18881f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018882 typval_T *argvars UNUSED;
18883 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018884{
18885 /* Used for unit testing. Change the code below to your liking. */
18886#if 0
18887 listitem_T *li;
18888 list_T *l;
18889 char_u *bad, *good;
18890
18891 if (argvars[0].v_type != VAR_LIST)
18892 return;
18893 l = argvars[0].vval.v_list;
18894 if (l == NULL)
18895 return;
18896 li = l->lv_first;
18897 if (li == NULL)
18898 return;
18899 bad = get_tv_string(&li->li_tv);
18900 li = li->li_next;
18901 if (li == NULL)
18902 return;
18903 good = get_tv_string(&li->li_tv);
18904 rettv->vval.v_number = test_edit_score(bad, good);
18905#endif
18906}
18907
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018908#ifdef FEAT_FLOAT
18909/*
18910 * "tan()" function
18911 */
18912 static void
18913f_tan(argvars, rettv)
18914 typval_T *argvars;
18915 typval_T *rettv;
18916{
18917 float_T f;
18918
18919 rettv->v_type = VAR_FLOAT;
18920 if (get_float_arg(argvars, &f) == OK)
18921 rettv->vval.v_float = tan(f);
18922 else
18923 rettv->vval.v_float = 0.0;
18924}
18925
18926/*
18927 * "tanh()" function
18928 */
18929 static void
18930f_tanh(argvars, rettv)
18931 typval_T *argvars;
18932 typval_T *rettv;
18933{
18934 float_T f;
18935
18936 rettv->v_type = VAR_FLOAT;
18937 if (get_float_arg(argvars, &f) == OK)
18938 rettv->vval.v_float = tanh(f);
18939 else
18940 rettv->vval.v_float = 0.0;
18941}
18942#endif
18943
Bram Moolenaard52d9742005-08-21 22:20:28 +000018944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 * "tolower(string)" function
18946 */
18947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018949 typval_T *argvars;
18950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951{
18952 char_u *p;
18953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954 p = vim_strsave(get_tv_string(&argvars[0]));
18955 rettv->v_type = VAR_STRING;
18956 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957
18958 if (p != NULL)
18959 while (*p != NUL)
18960 {
18961#ifdef FEAT_MBYTE
18962 int l;
18963
18964 if (enc_utf8)
18965 {
18966 int c, lc;
18967
18968 c = utf_ptr2char(p);
18969 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018970 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 /* TODO: reallocate string when byte count changes. */
18972 if (utf_char2len(lc) == l)
18973 utf_char2bytes(lc, p);
18974 p += l;
18975 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018976 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 p += l; /* skip multi-byte character */
18978 else
18979#endif
18980 {
18981 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18982 ++p;
18983 }
18984 }
18985}
18986
18987/*
18988 * "toupper(string)" function
18989 */
18990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018991f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018992 typval_T *argvars;
18993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018995 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018996 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997}
18998
18999/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019000 * "tr(string, fromstr, tostr)" function
19001 */
19002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 typval_T *argvars;
19005 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019006{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019007 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019008 char_u *fromstr;
19009 char_u *tostr;
19010 char_u *p;
19011#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019012 int inlen;
19013 int fromlen;
19014 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019015 int idx;
19016 char_u *cpstr;
19017 int cplen;
19018 int first = TRUE;
19019#endif
19020 char_u buf[NUMBUFLEN];
19021 char_u buf2[NUMBUFLEN];
19022 garray_T ga;
19023
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019024 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019025 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19026 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019027
19028 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 rettv->v_type = VAR_STRING;
19030 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019031 if (fromstr == NULL || tostr == NULL)
19032 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019033 ga_init2(&ga, (int)sizeof(char), 80);
19034
19035#ifdef FEAT_MBYTE
19036 if (!has_mbyte)
19037#endif
19038 /* not multi-byte: fromstr and tostr must be the same length */
19039 if (STRLEN(fromstr) != STRLEN(tostr))
19040 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019041#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019042error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019043#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019044 EMSG2(_(e_invarg2), fromstr);
19045 ga_clear(&ga);
19046 return;
19047 }
19048
19049 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019050 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019051 {
19052#ifdef FEAT_MBYTE
19053 if (has_mbyte)
19054 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019055 inlen = (*mb_ptr2len)(in_str);
19056 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019057 cplen = inlen;
19058 idx = 0;
19059 for (p = fromstr; *p != NUL; p += fromlen)
19060 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019061 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019062 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019063 {
19064 for (p = tostr; *p != NUL; p += tolen)
19065 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019066 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019067 if (idx-- == 0)
19068 {
19069 cplen = tolen;
19070 cpstr = p;
19071 break;
19072 }
19073 }
19074 if (*p == NUL) /* tostr is shorter than fromstr */
19075 goto error;
19076 break;
19077 }
19078 ++idx;
19079 }
19080
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019081 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019082 {
19083 /* Check that fromstr and tostr have the same number of
19084 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019085 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019086 first = FALSE;
19087 for (p = tostr; *p != NUL; p += tolen)
19088 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019089 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019090 --idx;
19091 }
19092 if (idx != 0)
19093 goto error;
19094 }
19095
19096 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019097 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019098 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019099
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019100 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019101 }
19102 else
19103#endif
19104 {
19105 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019106 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019107 if (p != NULL)
19108 ga_append(&ga, tostr[p - fromstr]);
19109 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019110 ga_append(&ga, *in_str);
19111 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019112 }
19113 }
19114
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019115 /* add a terminating NUL */
19116 ga_grow(&ga, 1);
19117 ga_append(&ga, NUL);
19118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019120}
19121
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019122#ifdef FEAT_FLOAT
19123/*
19124 * "trunc({float})" function
19125 */
19126 static void
19127f_trunc(argvars, rettv)
19128 typval_T *argvars;
19129 typval_T *rettv;
19130{
19131 float_T f;
19132
19133 rettv->v_type = VAR_FLOAT;
19134 if (get_float_arg(argvars, &f) == OK)
19135 /* trunc() is not in C90, use floor() or ceil() instead. */
19136 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19137 else
19138 rettv->vval.v_float = 0.0;
19139}
19140#endif
19141
Bram Moolenaar8299df92004-07-10 09:47:34 +000019142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 * "type(expr)" function
19144 */
19145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019147 typval_T *argvars;
19148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019150 int n;
19151
19152 switch (argvars[0].v_type)
19153 {
19154 case VAR_NUMBER: n = 0; break;
19155 case VAR_STRING: n = 1; break;
19156 case VAR_FUNC: n = 2; break;
19157 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019158 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019159#ifdef FEAT_FLOAT
19160 case VAR_FLOAT: n = 5; break;
19161#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019162 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19163 }
19164 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165}
19166
19167/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019168 * "undofile(name)" function
19169 */
19170 static void
19171f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019172 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019173 typval_T *rettv;
19174{
19175 rettv->v_type = VAR_STRING;
19176#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019177 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019178 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019179
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019180 if (*fname == NUL)
19181 {
19182 /* If there is no file name there will be no undo file. */
19183 rettv->vval.v_string = NULL;
19184 }
19185 else
19186 {
19187 char_u *ffname = FullName_save(fname, FALSE);
19188
19189 if (ffname != NULL)
19190 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19191 vim_free(ffname);
19192 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019193 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019194#else
19195 rettv->vval.v_string = NULL;
19196#endif
19197}
19198
19199/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019200 * "undotree()" function
19201 */
19202 static void
19203f_undotree(argvars, rettv)
19204 typval_T *argvars UNUSED;
19205 typval_T *rettv;
19206{
19207 if (rettv_dict_alloc(rettv) == OK)
19208 {
19209 dict_T *dict = rettv->vval.v_dict;
19210 list_T *list;
19211
Bram Moolenaar730cde92010-06-27 05:18:54 +020019212 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019213 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019214 dict_add_nr_str(dict, "save_last",
19215 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019216 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19217 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019218 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019219
19220 list = list_alloc();
19221 if (list != NULL)
19222 {
19223 u_eval_tree(curbuf->b_u_oldhead, list);
19224 dict_add_list(dict, "entries", list);
19225 }
19226 }
19227}
19228
19229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019230 * "values(dict)" function
19231 */
19232 static void
19233f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 typval_T *argvars;
19235 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019236{
19237 dict_list(argvars, rettv, 1);
19238}
19239
19240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 * "virtcol(string)" function
19242 */
19243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019245 typval_T *argvars;
19246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247{
19248 colnr_T vcol = 0;
19249 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019250 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019252 fp = var2fpos(&argvars[0], FALSE, &fnum);
19253 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19254 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 {
19256 getvvcol(curwin, fp, NULL, NULL, &vcol);
19257 ++vcol;
19258 }
19259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261}
19262
19263/*
19264 * "visualmode()" function
19265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019268 typval_T *argvars UNUSED;
19269 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 char_u str[2];
19272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 str[0] = curbuf->b_visual_mode_eval;
19275 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019276 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277
19278 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019279 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281}
19282
19283/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019284 * "wildmenumode()" function
19285 */
19286 static void
19287f_wildmenumode(argvars, rettv)
19288 typval_T *argvars UNUSED;
19289 typval_T *rettv UNUSED;
19290{
19291#ifdef FEAT_WILDMENU
19292 if (wild_menu_showing)
19293 rettv->vval.v_number = 1;
19294#endif
19295}
19296
19297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 * "winbufnr(nr)" function
19299 */
19300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019301f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 typval_T *argvars;
19303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304{
19305 win_T *wp;
19306
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019307 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312}
19313
19314/*
19315 * "wincol()" function
19316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019319 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321{
19322 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324}
19325
19326/*
19327 * "winheight(nr)" function
19328 */
19329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019330f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 typval_T *argvars;
19332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333{
19334 win_T *wp;
19335
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019336 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341}
19342
19343/*
19344 * "winline()" function
19345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350{
19351 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
19355/*
19356 * "winnr()" function
19357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362{
19363 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019364
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019366 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019368 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369}
19370
19371/*
19372 * "winrestcmd()" function
19373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019376 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378{
19379#ifdef FEAT_WINDOWS
19380 win_T *wp;
19381 int winnr = 1;
19382 garray_T ga;
19383 char_u buf[50];
19384
19385 ga_init2(&ga, (int)sizeof(char), 70);
19386 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19387 {
19388 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19389 ga_concat(&ga, buf);
19390# ifdef FEAT_VERTSPLIT
19391 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19392 ga_concat(&ga, buf);
19393# endif
19394 ++winnr;
19395 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019396 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403}
19404
19405/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019406 * "winrestview()" function
19407 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019408 static void
19409f_winrestview(argvars, rettv)
19410 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019411 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019412{
19413 dict_T *dict;
19414
19415 if (argvars[0].v_type != VAR_DICT
19416 || (dict = argvars[0].vval.v_dict) == NULL)
19417 EMSG(_(e_invarg));
19418 else
19419 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019420 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19421 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19422 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19423 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019424#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019425 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19426 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019427#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019428 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19429 {
19430 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19431 curwin->w_set_curswant = FALSE;
19432 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019433
Bram Moolenaar82c25852014-05-28 16:47:16 +020019434 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19435 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019436#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019437 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19438 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019439#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019440 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19441 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19442 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19443 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019444
19445 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019446 win_new_height(curwin, curwin->w_height);
19447# ifdef FEAT_VERTSPLIT
19448 win_new_width(curwin, W_WIDTH(curwin));
19449# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019450 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019451
19452 if (curwin->w_topline == 0)
19453 curwin->w_topline = 1;
19454 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19455 curwin->w_topline = curbuf->b_ml.ml_line_count;
19456#ifdef FEAT_DIFF
19457 check_topfill(curwin, TRUE);
19458#endif
19459 }
19460}
19461
19462/*
19463 * "winsaveview()" function
19464 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019465 static void
19466f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019467 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019468 typval_T *rettv;
19469{
19470 dict_T *dict;
19471
Bram Moolenaara800b422010-06-27 01:15:55 +020019472 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019473 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019474 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019475
19476 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19477 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19478#ifdef FEAT_VIRTUALEDIT
19479 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19480#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019481 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019482 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19483
19484 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19485#ifdef FEAT_DIFF
19486 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19487#endif
19488 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19489 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19490}
19491
19492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 * "winwidth(nr)" function
19494 */
19495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019497 typval_T *argvars;
19498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499{
19500 win_T *wp;
19501
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019502 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 else
19506#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510#endif
19511}
19512
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019514 * Write list of strings to file
19515 */
19516 static int
19517write_list(fd, list, binary)
19518 FILE *fd;
19519 list_T *list;
19520 int binary;
19521{
19522 listitem_T *li;
19523 int c;
19524 int ret = OK;
19525 char_u *s;
19526
19527 for (li = list->lv_first; li != NULL; li = li->li_next)
19528 {
19529 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19530 {
19531 if (*s == '\n')
19532 c = putc(NUL, fd);
19533 else
19534 c = putc(*s, fd);
19535 if (c == EOF)
19536 {
19537 ret = FAIL;
19538 break;
19539 }
19540 }
19541 if (!binary || li->li_next != NULL)
19542 if (putc('\n', fd) == EOF)
19543 {
19544 ret = FAIL;
19545 break;
19546 }
19547 if (ret == FAIL)
19548 {
19549 EMSG(_(e_write));
19550 break;
19551 }
19552 }
19553 return ret;
19554}
19555
19556/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019557 * "writefile()" function
19558 */
19559 static void
19560f_writefile(argvars, rettv)
19561 typval_T *argvars;
19562 typval_T *rettv;
19563{
19564 int binary = FALSE;
19565 char_u *fname;
19566 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019567 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019568
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019569 if (check_restricted() || check_secure())
19570 return;
19571
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019572 if (argvars[0].v_type != VAR_LIST)
19573 {
19574 EMSG2(_(e_listarg), "writefile()");
19575 return;
19576 }
19577 if (argvars[0].vval.v_list == NULL)
19578 return;
19579
19580 if (argvars[2].v_type != VAR_UNKNOWN
19581 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19582 binary = TRUE;
19583
19584 /* Always open the file in binary mode, library functions have a mind of
19585 * their own about CR-LF conversion. */
19586 fname = get_tv_string(&argvars[1]);
19587 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19588 {
19589 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19590 ret = -1;
19591 }
19592 else
19593 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019594 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19595 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019596 fclose(fd);
19597 }
19598
19599 rettv->vval.v_number = ret;
19600}
19601
19602/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019603 * "xor(expr, expr)" function
19604 */
19605 static void
19606f_xor(argvars, rettv)
19607 typval_T *argvars;
19608 typval_T *rettv;
19609{
19610 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19611 ^ get_tv_number_chk(&argvars[1], NULL);
19612}
19613
19614
19615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019617 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 */
19619 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019620var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019622 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019623 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019625 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019627 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628
Bram Moolenaara5525202006-03-02 22:52:09 +000019629 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019630 if (varp->v_type == VAR_LIST)
19631 {
19632 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019633 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019634 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019635 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019636
19637 l = varp->vval.v_list;
19638 if (l == NULL)
19639 return NULL;
19640
19641 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019642 pos.lnum = list_find_nr(l, 0L, &error);
19643 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019644 return NULL; /* invalid line number */
19645
19646 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019647 pos.col = list_find_nr(l, 1L, &error);
19648 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019649 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019650 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019651
19652 /* We accept "$" for the column number: last column. */
19653 li = list_find(l, 1L);
19654 if (li != NULL && li->li_tv.v_type == VAR_STRING
19655 && li->li_tv.vval.v_string != NULL
19656 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19657 pos.col = len + 1;
19658
Bram Moolenaara5525202006-03-02 22:52:09 +000019659 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019660 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019661 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019662 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019663
Bram Moolenaara5525202006-03-02 22:52:09 +000019664#ifdef FEAT_VIRTUALEDIT
19665 /* Get the virtual offset. Defaults to zero. */
19666 pos.coladd = list_find_nr(l, 2L, &error);
19667 if (error)
19668 pos.coladd = 0;
19669#endif
19670
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019671 return &pos;
19672 }
19673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019674 name = get_tv_string_chk(varp);
19675 if (name == NULL)
19676 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019677 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019679 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19680 {
19681 if (VIsual_active)
19682 return &VIsual;
19683 return &curwin->w_cursor;
19684 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019685 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019687 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19689 return NULL;
19690 return pp;
19691 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019692
19693#ifdef FEAT_VIRTUALEDIT
19694 pos.coladd = 0;
19695#endif
19696
Bram Moolenaar477933c2007-07-17 14:32:23 +000019697 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019698 {
19699 pos.col = 0;
19700 if (name[1] == '0') /* "w0": first visible line */
19701 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019702 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019703 pos.lnum = curwin->w_topline;
19704 return &pos;
19705 }
19706 else if (name[1] == '$') /* "w$": last visible line */
19707 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019708 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019709 pos.lnum = curwin->w_botline - 1;
19710 return &pos;
19711 }
19712 }
19713 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019715 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 {
19717 pos.lnum = curbuf->b_ml.ml_line_count;
19718 pos.col = 0;
19719 }
19720 else
19721 {
19722 pos.lnum = curwin->w_cursor.lnum;
19723 pos.col = (colnr_T)STRLEN(ml_get_curline());
19724 }
19725 return &pos;
19726 }
19727 return NULL;
19728}
19729
19730/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019731 * Convert list in "arg" into a position and optional file number.
19732 * When "fnump" is NULL there is no file number, only 3 items.
19733 * Note that the column is passed on as-is, the caller may want to decrement
19734 * it to use 1 for the first column.
19735 * Return FAIL when conversion is not possible, doesn't check the position for
19736 * validity.
19737 */
19738 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019739list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019740 typval_T *arg;
19741 pos_T *posp;
19742 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019743 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019744{
19745 list_T *l = arg->vval.v_list;
19746 long i = 0;
19747 long n;
19748
Bram Moolenaar493c1782014-05-28 14:34:46 +020019749 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19750 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019751 if (arg->v_type != VAR_LIST
19752 || l == NULL
19753 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019754 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019755 return FAIL;
19756
19757 if (fnump != NULL)
19758 {
19759 n = list_find_nr(l, i++, NULL); /* fnum */
19760 if (n < 0)
19761 return FAIL;
19762 if (n == 0)
19763 n = curbuf->b_fnum; /* current buffer */
19764 *fnump = n;
19765 }
19766
19767 n = list_find_nr(l, i++, NULL); /* lnum */
19768 if (n < 0)
19769 return FAIL;
19770 posp->lnum = n;
19771
19772 n = list_find_nr(l, i++, NULL); /* col */
19773 if (n < 0)
19774 return FAIL;
19775 posp->col = n;
19776
19777#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019778 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019779 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019780 posp->coladd = 0;
19781 else
19782 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019783#endif
19784
Bram Moolenaar493c1782014-05-28 14:34:46 +020019785 if (curswantp != NULL)
19786 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19787
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019788 return OK;
19789}
19790
19791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 * Get the length of an environment variable name.
19793 * Advance "arg" to the first character after the name.
19794 * Return 0 for error.
19795 */
19796 static int
19797get_env_len(arg)
19798 char_u **arg;
19799{
19800 char_u *p;
19801 int len;
19802
19803 for (p = *arg; vim_isIDc(*p); ++p)
19804 ;
19805 if (p == *arg) /* no name found */
19806 return 0;
19807
19808 len = (int)(p - *arg);
19809 *arg = p;
19810 return len;
19811}
19812
19813/*
19814 * Get the length of the name of a function or internal variable.
19815 * "arg" is advanced to the first non-white character after the name.
19816 * Return 0 if something is wrong.
19817 */
19818 static int
19819get_id_len(arg)
19820 char_u **arg;
19821{
19822 char_u *p;
19823 int len;
19824
19825 /* Find the end of the name. */
19826 for (p = *arg; eval_isnamec(*p); ++p)
19827 ;
19828 if (p == *arg) /* no name found */
19829 return 0;
19830
19831 len = (int)(p - *arg);
19832 *arg = skipwhite(p);
19833
19834 return len;
19835}
19836
19837/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019838 * Get the length of the name of a variable or function.
19839 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019841 * Return -1 if curly braces expansion failed.
19842 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 * If the name contains 'magic' {}'s, expand them and return the
19844 * expanded name in an allocated string via 'alias' - caller must free.
19845 */
19846 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019847get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 char_u **arg;
19849 char_u **alias;
19850 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019851 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852{
19853 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 char_u *p;
19855 char_u *expr_start;
19856 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857
19858 *alias = NULL; /* default to no alias */
19859
19860 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19861 && (*arg)[2] == (int)KE_SNR)
19862 {
19863 /* hard coded <SNR>, already translated */
19864 *arg += 3;
19865 return get_id_len(arg) + 3;
19866 }
19867 len = eval_fname_script(*arg);
19868 if (len > 0)
19869 {
19870 /* literal "<SID>", "s:" or "<SNR>" */
19871 *arg += len;
19872 }
19873
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019877 p = find_name_end(*arg, &expr_start, &expr_end,
19878 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 if (expr_start != NULL)
19880 {
19881 char_u *temp_string;
19882
19883 if (!evaluate)
19884 {
19885 len += (int)(p - *arg);
19886 *arg = skipwhite(p);
19887 return len;
19888 }
19889
19890 /*
19891 * Include any <SID> etc in the expanded string:
19892 * Thus the -len here.
19893 */
19894 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19895 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019896 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 *alias = temp_string;
19898 *arg = skipwhite(p);
19899 return (int)STRLEN(temp_string);
19900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901
19902 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019903 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 EMSG2(_(e_invexpr2), *arg);
19905
19906 return len;
19907}
19908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909/*
19910 * Find the end of a variable or function name, taking care of magic braces.
19911 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19912 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019913 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019914 * Return a pointer to just after the name. Equal to "arg" if there is no
19915 * valid name.
19916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019918find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 char_u *arg;
19920 char_u **expr_start;
19921 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019922 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 int mb_nest = 0;
19925 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 char_u *p;
19927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930 *expr_start = NULL;
19931 *expr_end = NULL;
19932 }
19933
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019934 /* Quick check for valid starting character. */
19935 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19936 return arg;
19937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019938 for (p = arg; *p != NUL
19939 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019940 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019941 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019943 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019945 if (*p == '\'')
19946 {
19947 /* skip over 'string' to avoid counting [ and ] inside it. */
19948 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19949 ;
19950 if (*p == NUL)
19951 break;
19952 }
19953 else if (*p == '"')
19954 {
19955 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19956 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19957 if (*p == '\\' && p[1] != NUL)
19958 ++p;
19959 if (*p == NUL)
19960 break;
19961 }
19962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 if (*p == '[')
19966 ++br_nest;
19967 else if (*p == ']')
19968 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 if (*p == '{')
19974 {
19975 mb_nest++;
19976 if (expr_start != NULL && *expr_start == NULL)
19977 *expr_start = p;
19978 }
19979 else if (*p == '}')
19980 {
19981 mb_nest--;
19982 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19983 *expr_end = p;
19984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 }
19987
19988 return p;
19989}
19990
19991/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019992 * Expands out the 'magic' {}'s in a variable/function name.
19993 * Note that this can call itself recursively, to deal with
19994 * constructs like foo{bar}{baz}{bam}
19995 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19996 * "in_start" ^
19997 * "expr_start" ^
19998 * "expr_end" ^
19999 * "in_end" ^
20000 *
20001 * Returns a new allocated string, which the caller must free.
20002 * Returns NULL for failure.
20003 */
20004 static char_u *
20005make_expanded_name(in_start, expr_start, expr_end, in_end)
20006 char_u *in_start;
20007 char_u *expr_start;
20008 char_u *expr_end;
20009 char_u *in_end;
20010{
20011 char_u c1;
20012 char_u *retval = NULL;
20013 char_u *temp_result;
20014 char_u *nextcmd = NULL;
20015
20016 if (expr_end == NULL || in_end == NULL)
20017 return NULL;
20018 *expr_start = NUL;
20019 *expr_end = NUL;
20020 c1 = *in_end;
20021 *in_end = NUL;
20022
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020023 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020024 if (temp_result != NULL && nextcmd == NULL)
20025 {
20026 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20027 + (in_end - expr_end) + 1));
20028 if (retval != NULL)
20029 {
20030 STRCPY(retval, in_start);
20031 STRCAT(retval, temp_result);
20032 STRCAT(retval, expr_end + 1);
20033 }
20034 }
20035 vim_free(temp_result);
20036
20037 *in_end = c1; /* put char back for error messages */
20038 *expr_start = '{';
20039 *expr_end = '}';
20040
20041 if (retval != NULL)
20042 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020043 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020044 if (expr_start != NULL)
20045 {
20046 /* Further expansion! */
20047 temp_result = make_expanded_name(retval, expr_start,
20048 expr_end, temp_result);
20049 vim_free(retval);
20050 retval = temp_result;
20051 }
20052 }
20053
20054 return retval;
20055}
20056
20057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020059 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 */
20061 static int
20062eval_isnamec(c)
20063 int c;
20064{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020065 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20066}
20067
20068/*
20069 * Return TRUE if character "c" can be used as the first character in a
20070 * variable or function name (excluding '{' and '}').
20071 */
20072 static int
20073eval_isnamec1(c)
20074 int c;
20075{
20076 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 * Set number v: variable to "val".
20081 */
20082 void
20083set_vim_var_nr(idx, val)
20084 int idx;
20085 long val;
20086{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020087 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089
20090/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020091 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 */
20093 long
20094get_vim_var_nr(idx)
20095 int idx;
20096{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020097 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098}
20099
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020100/*
20101 * Get string v: variable value. Uses a static buffer, can only be used once.
20102 */
20103 char_u *
20104get_vim_var_str(idx)
20105 int idx;
20106{
20107 return get_tv_string(&vimvars[idx].vv_tv);
20108}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020109
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020111 * Get List v: variable value. Caller must take care of reference count when
20112 * needed.
20113 */
20114 list_T *
20115get_vim_var_list(idx)
20116 int idx;
20117{
20118 return vimvars[idx].vv_list;
20119}
20120
20121/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020122 * Set v:char to character "c".
20123 */
20124 void
20125set_vim_var_char(c)
20126 int c;
20127{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020128 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020129
20130#ifdef FEAT_MBYTE
20131 if (has_mbyte)
20132 buf[(*mb_char2bytes)(c, buf)] = NUL;
20133 else
20134#endif
20135 {
20136 buf[0] = c;
20137 buf[1] = NUL;
20138 }
20139 set_vim_var_string(VV_CHAR, buf, -1);
20140}
20141
20142/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020143 * Set v:count to "count" and v:count1 to "count1".
20144 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 */
20146 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020147set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 long count;
20149 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020150 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020152 if (set_prevcount)
20153 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020154 vimvars[VV_COUNT].vv_nr = count;
20155 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
20158/*
20159 * Set string v: variable to a copy of "val".
20160 */
20161 void
20162set_vim_var_string(idx, val, len)
20163 int idx;
20164 char_u *val;
20165 int len; /* length of "val" to use or -1 (whole string) */
20166{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020167 /* Need to do this (at least) once, since we can't initialize a union.
20168 * Will always be invoked when "v:progname" is set. */
20169 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20170
Bram Moolenaare9a41262005-01-15 22:18:47 +000020171 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020173 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020175 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020177 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178}
20179
20180/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020181 * Set List v: variable to "val".
20182 */
20183 void
20184set_vim_var_list(idx, val)
20185 int idx;
20186 list_T *val;
20187{
20188 list_unref(vimvars[idx].vv_list);
20189 vimvars[idx].vv_list = val;
20190 if (val != NULL)
20191 ++val->lv_refcount;
20192}
20193
20194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 * Set v:register if needed.
20196 */
20197 void
20198set_reg_var(c)
20199 int c;
20200{
20201 char_u regname;
20202
20203 if (c == 0 || c == ' ')
20204 regname = '"';
20205 else
20206 regname = c;
20207 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020208 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 set_vim_var_string(VV_REG, &regname, 1);
20210}
20211
20212/*
20213 * Get or set v:exception. If "oldval" == NULL, return the current value.
20214 * Otherwise, restore the value to "oldval" and return NULL.
20215 * Must always be called in pairs to save and restore v:exception! Does not
20216 * take care of memory allocations.
20217 */
20218 char_u *
20219v_exception(oldval)
20220 char_u *oldval;
20221{
20222 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020223 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224
Bram Moolenaare9a41262005-01-15 22:18:47 +000020225 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 return NULL;
20227}
20228
20229/*
20230 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20231 * Otherwise, restore the value to "oldval" and return NULL.
20232 * Must always be called in pairs to save and restore v:throwpoint! Does not
20233 * take care of memory allocations.
20234 */
20235 char_u *
20236v_throwpoint(oldval)
20237 char_u *oldval;
20238{
20239 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020240 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241
Bram Moolenaare9a41262005-01-15 22:18:47 +000020242 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 return NULL;
20244}
20245
20246#if defined(FEAT_AUTOCMD) || defined(PROTO)
20247/*
20248 * Set v:cmdarg.
20249 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20250 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20251 * Must always be called in pairs!
20252 */
20253 char_u *
20254set_cmdarg(eap, oldarg)
20255 exarg_T *eap;
20256 char_u *oldarg;
20257{
20258 char_u *oldval;
20259 char_u *newval;
20260 unsigned len;
20261
Bram Moolenaare9a41262005-01-15 22:18:47 +000020262 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020263 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020265 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020266 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020267 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 }
20269
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020270 if (eap->force_bin == FORCE_BIN)
20271 len = 6;
20272 else if (eap->force_bin == FORCE_NOBIN)
20273 len = 8;
20274 else
20275 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020276
20277 if (eap->read_edit)
20278 len += 7;
20279
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020280 if (eap->force_ff != 0)
20281 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20282# ifdef FEAT_MBYTE
20283 if (eap->force_enc != 0)
20284 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020285 if (eap->bad_char != 0)
20286 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020287# endif
20288
20289 newval = alloc(len + 1);
20290 if (newval == NULL)
20291 return NULL;
20292
20293 if (eap->force_bin == FORCE_BIN)
20294 sprintf((char *)newval, " ++bin");
20295 else if (eap->force_bin == FORCE_NOBIN)
20296 sprintf((char *)newval, " ++nobin");
20297 else
20298 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020299
20300 if (eap->read_edit)
20301 STRCAT(newval, " ++edit");
20302
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020303 if (eap->force_ff != 0)
20304 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20305 eap->cmd + eap->force_ff);
20306# ifdef FEAT_MBYTE
20307 if (eap->force_enc != 0)
20308 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20309 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020310 if (eap->bad_char == BAD_KEEP)
20311 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20312 else if (eap->bad_char == BAD_DROP)
20313 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20314 else if (eap->bad_char != 0)
20315 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020316# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020317 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020318 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319}
20320#endif
20321
20322/*
20323 * Get the value of internal variable "name".
20324 * Return OK or FAIL.
20325 */
20326 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020327get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 char_u *name;
20329 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020330 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020331 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020332 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333{
20334 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020335 typval_T *tv = NULL;
20336 typval_T atv;
20337 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339
20340 /* truncate the name, so that we can use strcmp() */
20341 cc = name[len];
20342 name[len] = NUL;
20343
20344 /*
20345 * Check for "b:changedtick".
20346 */
20347 if (STRCMP(name, "b:changedtick") == 0)
20348 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020349 atv.v_type = VAR_NUMBER;
20350 atv.vval.v_number = curbuf->b_changedtick;
20351 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 }
20353
20354 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 * Check for user-defined variables.
20356 */
20357 else
20358 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020359 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 }
20363
Bram Moolenaare9a41262005-01-15 22:18:47 +000020364 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020366 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 ret = FAIL;
20369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020371 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372
20373 name[len] = cc;
20374
20375 return ret;
20376}
20377
20378/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020379 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20380 * Also handle function call with Funcref variable: func(expr)
20381 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20382 */
20383 static int
20384handle_subscript(arg, rettv, evaluate, verbose)
20385 char_u **arg;
20386 typval_T *rettv;
20387 int evaluate; /* do more than finding the end */
20388 int verbose; /* give error messages */
20389{
20390 int ret = OK;
20391 dict_T *selfdict = NULL;
20392 char_u *s;
20393 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020394 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020395
20396 while (ret == OK
20397 && (**arg == '['
20398 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020399 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020400 && !vim_iswhite(*(*arg - 1)))
20401 {
20402 if (**arg == '(')
20403 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020404 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020405 if (evaluate)
20406 {
20407 functv = *rettv;
20408 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020409
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020410 /* Invoke the function. Recursive! */
20411 s = functv.vval.v_string;
20412 }
20413 else
20414 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020415 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020416 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20417 &len, evaluate, selfdict);
20418
20419 /* Clear the funcref afterwards, so that deleting it while
20420 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020421 if (evaluate)
20422 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020423
20424 /* Stop the expression evaluation when immediately aborting on
20425 * error, or when an interrupt occurred or an exception was thrown
20426 * but not caught. */
20427 if (aborting())
20428 {
20429 if (ret == OK)
20430 clear_tv(rettv);
20431 ret = FAIL;
20432 }
20433 dict_unref(selfdict);
20434 selfdict = NULL;
20435 }
20436 else /* **arg == '[' || **arg == '.' */
20437 {
20438 dict_unref(selfdict);
20439 if (rettv->v_type == VAR_DICT)
20440 {
20441 selfdict = rettv->vval.v_dict;
20442 if (selfdict != NULL)
20443 ++selfdict->dv_refcount;
20444 }
20445 else
20446 selfdict = NULL;
20447 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20448 {
20449 clear_tv(rettv);
20450 ret = FAIL;
20451 }
20452 }
20453 }
20454 dict_unref(selfdict);
20455 return ret;
20456}
20457
20458/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020459 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020460 * value).
20461 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020464{
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020466}
20467
20468/*
20469 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 * The string "s" must have been allocated, it is consumed.
20471 * Return NULL for out of memory, the variable otherwise.
20472 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020474alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 char_u *s;
20476{
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020479 rettv = alloc_tv();
20480 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482 rettv->v_type = VAR_STRING;
20483 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 }
20485 else
20486 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488}
20489
20490/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020491 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020493 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020494free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020495 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496{
20497 if (varp != NULL)
20498 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499 switch (varp->v_type)
20500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020501 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020502 func_unref(varp->vval.v_string);
20503 /*FALLTHROUGH*/
20504 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020505 vim_free(varp->vval.v_string);
20506 break;
20507 case VAR_LIST:
20508 list_unref(varp->vval.v_list);
20509 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020510 case VAR_DICT:
20511 dict_unref(varp->vval.v_dict);
20512 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020513 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020514#ifdef FEAT_FLOAT
20515 case VAR_FLOAT:
20516#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020517 case VAR_UNKNOWN:
20518 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020519 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020520 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020521 break;
20522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 vim_free(varp);
20524 }
20525}
20526
20527/*
20528 * Free the memory for a variable value and set the value to NULL or 0.
20529 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020530 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020531clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533{
20534 if (varp != NULL)
20535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020536 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020538 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020539 func_unref(varp->vval.v_string);
20540 /*FALLTHROUGH*/
20541 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020542 vim_free(varp->vval.v_string);
20543 varp->vval.v_string = NULL;
20544 break;
20545 case VAR_LIST:
20546 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020547 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020548 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020549 case VAR_DICT:
20550 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020551 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020552 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020553 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020554 varp->vval.v_number = 0;
20555 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020556#ifdef FEAT_FLOAT
20557 case VAR_FLOAT:
20558 varp->vval.v_float = 0.0;
20559 break;
20560#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020561 case VAR_UNKNOWN:
20562 break;
20563 default:
20564 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020566 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 }
20568}
20569
20570/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020571 * Set the value of a variable to NULL without freeing items.
20572 */
20573 static void
20574init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020575 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020576{
20577 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020579}
20580
20581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 * Get the number value of a variable.
20583 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020584 * For incompatible types, return 0.
20585 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20586 * caller of incompatible types: it sets *denote to TRUE if "denote"
20587 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 */
20589 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020593 int error = FALSE;
20594
20595 return get_tv_number_chk(varp, &error); /* return 0L on error */
20596}
20597
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020598 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020599get_tv_number_chk(varp, denote)
20600 typval_T *varp;
20601 int *denote;
20602{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020608 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020609#ifdef FEAT_FLOAT
20610 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020611 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020612 break;
20613#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020614 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020615 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020616 break;
20617 case VAR_STRING:
20618 if (varp->vval.v_string != NULL)
20619 vim_str2nr(varp->vval.v_string, NULL, NULL,
20620 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020621 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020622 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020623 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020624 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020625 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020626 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020627 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020628 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020629 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020630 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020632 if (denote == NULL) /* useful for values that must be unsigned */
20633 n = -1;
20634 else
20635 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637}
20638
20639/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020640 * Get the lnum from the first argument.
20641 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020642 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 */
20644 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020645get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020646 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647{
Bram Moolenaar33570922005-01-25 22:26:29 +000020648 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 linenr_T lnum;
20650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020651 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 if (lnum == 0) /* no valid number, try using line() */
20653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020654 rettv.v_type = VAR_NUMBER;
20655 f_line(argvars, &rettv);
20656 lnum = rettv.vval.v_number;
20657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 }
20659 return lnum;
20660}
20661
20662/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020663 * Get the lnum from the first argument.
20664 * Also accepts "$", then "buf" is used.
20665 * Returns 0 on error.
20666 */
20667 static linenr_T
20668get_tv_lnum_buf(argvars, buf)
20669 typval_T *argvars;
20670 buf_T *buf;
20671{
20672 if (argvars[0].v_type == VAR_STRING
20673 && argvars[0].vval.v_string != NULL
20674 && argvars[0].vval.v_string[0] == '$'
20675 && buf != NULL)
20676 return buf->b_ml.ml_line_count;
20677 return get_tv_number_chk(&argvars[0], NULL);
20678}
20679
20680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 * Get the string value of a variable.
20682 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020683 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20684 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 * If the String variable has never been set, return an empty string.
20686 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020687 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20688 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 */
20690 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693{
20694 static char_u mybuf[NUMBUFLEN];
20695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020696 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697}
20698
20699 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020700get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020701 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020702 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020704 char_u *res = get_tv_string_buf_chk(varp, buf);
20705
20706 return res != NULL ? res : (char_u *)"";
20707}
20708
Bram Moolenaar7d647822014-04-05 21:28:56 +020020709/*
20710 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20711 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020712 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020713get_tv_string_chk(varp)
20714 typval_T *varp;
20715{
20716 static char_u mybuf[NUMBUFLEN];
20717
20718 return get_tv_string_buf_chk(varp, mybuf);
20719}
20720
20721 static char_u *
20722get_tv_string_buf_chk(varp, buf)
20723 typval_T *varp;
20724 char_u *buf;
20725{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020726 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020728 case VAR_NUMBER:
20729 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20730 return buf;
20731 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020732 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020733 break;
20734 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020735 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020736 break;
20737 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020738 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020739 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020740#ifdef FEAT_FLOAT
20741 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020742 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020743 break;
20744#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020745 case VAR_STRING:
20746 if (varp->vval.v_string != NULL)
20747 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020748 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020749 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020750 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020753 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754}
20755
20756/*
20757 * Find variable "name" in the list of variables.
20758 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020760 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020761 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020763 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020764find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020766 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020767 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020770 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771
Bram Moolenaara7043832005-01-21 11:56:39 +000020772 ht = find_var_ht(name, &varname);
20773 if (htp != NULL)
20774 *htp = ht;
20775 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020777 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778}
20779
20780/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020781 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020782 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020784 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020785find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020786 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020787 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020788 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020789 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020790{
Bram Moolenaar33570922005-01-25 22:26:29 +000020791 hashitem_T *hi;
20792
20793 if (*varname == NUL)
20794 {
20795 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020796 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020797 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020798 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 case 'g': return &globvars_var;
20800 case 'v': return &vimvars_var;
20801 case 'b': return &curbuf->b_bufvar;
20802 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020803#ifdef FEAT_WINDOWS
20804 case 't': return &curtab->tp_winvar;
20805#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020806 case 'l': return current_funccal == NULL
20807 ? NULL : &current_funccal->l_vars_var;
20808 case 'a': return current_funccal == NULL
20809 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020810 }
20811 return NULL;
20812 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020813
20814 hi = hash_find(ht, varname);
20815 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020816 {
20817 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020818 * worked find the variable again. Don't auto-load a script if it was
20819 * loaded already, otherwise it would be loaded every time when
20820 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020821 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020822 {
20823 /* Note: script_autoload() may make "hi" invalid. It must either
20824 * be obtained again or not used. */
20825 if (!script_autoload(varname, FALSE) || aborting())
20826 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020827 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020828 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020829 if (HASHITEM_EMPTY(hi))
20830 return NULL;
20831 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020832 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020833}
20834
20835/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020837 * Set "varname" to the start of name without ':'.
20838 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020840find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 char_u *name;
20842 char_u **varname;
20843{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020844 hashitem_T *hi;
20845
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 if (name[1] != ':')
20847 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020848 /* The name must not start with a colon or #. */
20849 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 return NULL;
20851 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020852
20853 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020854 hi = hash_find(&compat_hashtab, name);
20855 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020856 return &compat_hashtab;
20857
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 return &globvarht; /* global variable */
20860 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 }
20862 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020863 if (*name == 'g') /* global variable */
20864 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020865 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20866 */
20867 if (vim_strchr(name + 2, ':') != NULL
20868 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020869 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020871 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020873 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020874#ifdef FEAT_WINDOWS
20875 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020876 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020877#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020878 if (*name == 'v') /* v: variable */
20879 return &vimvarht;
20880 if (*name == 'a' && current_funccal != NULL) /* function argument */
20881 return &current_funccal->l_avars.dv_hashtab;
20882 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20883 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 if (*name == 's' /* script variable */
20885 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20886 return &SCRIPT_VARS(current_SID);
20887 return NULL;
20888}
20889
20890/*
20891 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020892 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 * Returns NULL when it doesn't exist.
20894 */
20895 char_u *
20896get_var_value(name)
20897 char_u *name;
20898{
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020901 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 if (v == NULL)
20903 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020904 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905}
20906
20907/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020908 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 * sourcing this script and when executing functions defined in the script.
20910 */
20911 void
20912new_script_vars(id)
20913 scid_T id;
20914{
Bram Moolenaara7043832005-01-21 11:56:39 +000020915 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020916 hashtab_T *ht;
20917 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020918
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020921 /* Re-allocating ga_data means that an ht_array pointing to
20922 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020923 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020924 for (i = 1; i <= ga_scripts.ga_len; ++i)
20925 {
20926 ht = &SCRIPT_VARS(i);
20927 if (ht->ht_mask == HT_INIT_SIZE - 1)
20928 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020929 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020930 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020931 }
20932
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 while (ga_scripts.ga_len < id)
20934 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020935 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020936 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020937 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 }
20940 }
20941}
20942
20943/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020944 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20945 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 */
20947 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020948init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 dict_T *dict;
20950 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020951 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952{
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020954 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020955 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020956 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020957 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020958 dict_var->di_tv.vval.v_dict = dict;
20959 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020960 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020961 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20962 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963}
20964
20965/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020966 * Unreference a dictionary initialized by init_var_dict().
20967 */
20968 void
20969unref_var_dict(dict)
20970 dict_T *dict;
20971{
20972 /* Now the dict needs to be freed if no one else is using it, go back to
20973 * normal reference counting. */
20974 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20975 dict_unref(dict);
20976}
20977
20978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 * Frees all allocated variables and the value they contain.
20981 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 */
20983 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020984vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020985 hashtab_T *ht;
20986{
20987 vars_clear_ext(ht, TRUE);
20988}
20989
20990/*
20991 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20992 */
20993 static void
20994vars_clear_ext(ht, free_val)
20995 hashtab_T *ht;
20996 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997{
Bram Moolenaara7043832005-01-21 11:56:39 +000020998 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 hashitem_T *hi;
21000 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021003 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021004 for (hi = ht->ht_array; todo > 0; ++hi)
21005 {
21006 if (!HASHITEM_EMPTY(hi))
21007 {
21008 --todo;
21009
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021011 * ht_array might change then. hash_clear() takes care of it
21012 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 v = HI2DI(hi);
21014 if (free_val)
21015 clear_tv(&v->di_tv);
21016 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21017 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021018 }
21019 }
21020 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021021 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022}
21023
Bram Moolenaara7043832005-01-21 11:56:39 +000021024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021025 * Delete a variable from hashtab "ht" at item "hi".
21026 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021029delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021030 hashtab_T *ht;
21031 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032{
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021034
21035 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021036 clear_tv(&di->di_tv);
21037 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038}
21039
21040/*
21041 * List the value of one internal variable.
21042 */
21043 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021044list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021045 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021047 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021049 char_u *tofree;
21050 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021052
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021053 current_copyID += COPYID_INC;
21054 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021056 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021057 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058}
21059
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021061list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 char_u *prefix;
21063 char_u *name;
21064 int type;
21065 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021066 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067{
Bram Moolenaar31859182007-08-14 20:41:13 +000021068 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21069 msg_start();
21070 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 if (name != NULL) /* "a:" vars don't have a name stored */
21072 msg_puts(name);
21073 msg_putchar(' ');
21074 msg_advance(22);
21075 if (type == VAR_NUMBER)
21076 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 else if (type == VAR_FUNC)
21078 msg_putchar('*');
21079 else if (type == VAR_LIST)
21080 {
21081 msg_putchar('[');
21082 if (*string == '[')
21083 ++string;
21084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021085 else if (type == VAR_DICT)
21086 {
21087 msg_putchar('{');
21088 if (*string == '{')
21089 ++string;
21090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 else
21092 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021093
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021095
21096 if (type == VAR_FUNC)
21097 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021098 if (*first)
21099 {
21100 msg_clr_eos();
21101 *first = FALSE;
21102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103}
21104
21105/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021106 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 * If the variable already exists, the value is updated.
21108 * Otherwise the variable is created.
21109 */
21110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021118 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021120 ht = find_var_ht(name, &varname);
21121 if (ht == NULL || *varname == NUL)
21122 {
21123 EMSG2(_(e_illvar), name);
21124 return;
21125 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021126 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021127
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021128 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21129 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021130
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021133 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021134 if (var_check_ro(v->di_flags, name)
21135 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021136 return;
21137 if (v->di_tv.v_type != tv->v_type
21138 && !((v->di_tv.v_type == VAR_STRING
21139 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021141 || tv->v_type == VAR_NUMBER))
21142#ifdef FEAT_FLOAT
21143 && !((v->di_tv.v_type == VAR_NUMBER
21144 || v->di_tv.v_type == VAR_FLOAT)
21145 && (tv->v_type == VAR_NUMBER
21146 || tv->v_type == VAR_FLOAT))
21147#endif
21148 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021149 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021150 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 return;
21152 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021153
21154 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021155 * Handle setting internal v: variables separately: we don't change
21156 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021157 */
21158 if (ht == &vimvarht)
21159 {
21160 if (v->di_tv.v_type == VAR_STRING)
21161 {
21162 vim_free(v->di_tv.vval.v_string);
21163 if (copy || tv->v_type != VAR_STRING)
21164 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21165 else
21166 {
21167 /* Take over the string to avoid an extra alloc/free. */
21168 v->di_tv.vval.v_string = tv->vval.v_string;
21169 tv->vval.v_string = NULL;
21170 }
21171 }
21172 else if (v->di_tv.v_type != VAR_NUMBER)
21173 EMSG2(_(e_intern2), "set_var()");
21174 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021175 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021176 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021177 if (STRCMP(varname, "searchforward") == 0)
21178 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021179#ifdef FEAT_SEARCH_EXTRA
21180 else if (STRCMP(varname, "hlsearch") == 0)
21181 {
21182 no_hlsearch = !v->di_tv.vval.v_number;
21183 redraw_all_later(SOME_VALID);
21184 }
21185#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021186 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021187 return;
21188 }
21189
21190 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 }
21192 else /* add a new variable */
21193 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021194 /* Can't add "v:" variable. */
21195 if (ht == &vimvarht)
21196 {
21197 EMSG2(_(e_illvar), name);
21198 return;
21199 }
21200
Bram Moolenaar92124a32005-06-17 22:03:40 +000021201 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021202 if (!valid_varname(varname))
21203 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021204
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021205 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21206 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021207 if (v == NULL)
21208 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021209 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021210 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021212 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 return;
21214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021215 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021217
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021218 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021219 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021220 else
21221 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021222 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021223 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226}
21227
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021228/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021229 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021230 * Also give an error message.
21231 */
21232 static int
21233var_check_ro(flags, name)
21234 int flags;
21235 char_u *name;
21236{
21237 if (flags & DI_FLAGS_RO)
21238 {
21239 EMSG2(_(e_readonlyvar), name);
21240 return TRUE;
21241 }
21242 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21243 {
21244 EMSG2(_(e_readonlysbx), name);
21245 return TRUE;
21246 }
21247 return FALSE;
21248}
21249
21250/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021251 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21252 * Also give an error message.
21253 */
21254 static int
21255var_check_fixed(flags, name)
21256 int flags;
21257 char_u *name;
21258{
21259 if (flags & DI_FLAGS_FIX)
21260 {
21261 EMSG2(_("E795: Cannot delete variable %s"), name);
21262 return TRUE;
21263 }
21264 return FALSE;
21265}
21266
21267/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021268 * Check if a funcref is assigned to a valid variable name.
21269 * Return TRUE and give an error if not.
21270 */
21271 static int
21272var_check_func_name(name, new_var)
21273 char_u *name; /* points to start of variable name */
21274 int new_var; /* TRUE when creating the variable */
21275{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021276 /* Allow for w: b: s: and t:. */
21277 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021278 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21279 ? name[2] : name[0]))
21280 {
21281 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21282 name);
21283 return TRUE;
21284 }
21285 /* Don't allow hiding a function. When "v" is not NULL we might be
21286 * assigning another function to the same var, the type is checked
21287 * below. */
21288 if (new_var && function_exists(name))
21289 {
21290 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21291 name);
21292 return TRUE;
21293 }
21294 return FALSE;
21295}
21296
21297/*
21298 * Check if a variable name is valid.
21299 * Return FALSE and give an error if not.
21300 */
21301 static int
21302valid_varname(varname)
21303 char_u *varname;
21304{
21305 char_u *p;
21306
21307 for (p = varname; *p != NUL; ++p)
21308 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21309 && *p != AUTOLOAD_CHAR)
21310 {
21311 EMSG2(_(e_illvar), varname);
21312 return FALSE;
21313 }
21314 return TRUE;
21315}
21316
21317/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021318 * Return TRUE if typeval "tv" is set to be locked (immutable).
21319 * Also give an error message, using "name".
21320 */
21321 static int
21322tv_check_lock(lock, name)
21323 int lock;
21324 char_u *name;
21325{
21326 if (lock & VAR_LOCKED)
21327 {
21328 EMSG2(_("E741: Value is locked: %s"),
21329 name == NULL ? (char_u *)_("Unknown") : name);
21330 return TRUE;
21331 }
21332 if (lock & VAR_FIXED)
21333 {
21334 EMSG2(_("E742: Cannot change value of %s"),
21335 name == NULL ? (char_u *)_("Unknown") : name);
21336 return TRUE;
21337 }
21338 return FALSE;
21339}
21340
21341/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021343 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021344 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021345 * It is OK for "from" and "to" to point to the same item. This is used to
21346 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021347 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021348 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 typval_T *from;
21351 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021353 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021354 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021355 switch (from->v_type)
21356 {
21357 case VAR_NUMBER:
21358 to->vval.v_number = from->vval.v_number;
21359 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021360#ifdef FEAT_FLOAT
21361 case VAR_FLOAT:
21362 to->vval.v_float = from->vval.v_float;
21363 break;
21364#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021365 case VAR_STRING:
21366 case VAR_FUNC:
21367 if (from->vval.v_string == NULL)
21368 to->vval.v_string = NULL;
21369 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021372 if (from->v_type == VAR_FUNC)
21373 func_ref(to->vval.v_string);
21374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375 break;
21376 case VAR_LIST:
21377 if (from->vval.v_list == NULL)
21378 to->vval.v_list = NULL;
21379 else
21380 {
21381 to->vval.v_list = from->vval.v_list;
21382 ++to->vval.v_list->lv_refcount;
21383 }
21384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021385 case VAR_DICT:
21386 if (from->vval.v_dict == NULL)
21387 to->vval.v_dict = NULL;
21388 else
21389 {
21390 to->vval.v_dict = from->vval.v_dict;
21391 ++to->vval.v_dict->dv_refcount;
21392 }
21393 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021395 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021396 break;
21397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398}
21399
21400/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021401 * Make a copy of an item.
21402 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021403 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21404 * reference to an already copied list/dict can be used.
21405 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021406 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021407 static int
21408item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021409 typval_T *from;
21410 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021411 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021412 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021413{
21414 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021415 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021416
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021418 {
21419 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021420 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021421 }
21422 ++recurse;
21423
21424 switch (from->v_type)
21425 {
21426 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021427#ifdef FEAT_FLOAT
21428 case VAR_FLOAT:
21429#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021430 case VAR_STRING:
21431 case VAR_FUNC:
21432 copy_tv(from, to);
21433 break;
21434 case VAR_LIST:
21435 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021436 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021437 if (from->vval.v_list == NULL)
21438 to->vval.v_list = NULL;
21439 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21440 {
21441 /* use the copy made earlier */
21442 to->vval.v_list = from->vval.v_list->lv_copylist;
21443 ++to->vval.v_list->lv_refcount;
21444 }
21445 else
21446 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21447 if (to->vval.v_list == NULL)
21448 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021449 break;
21450 case VAR_DICT:
21451 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021452 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021453 if (from->vval.v_dict == NULL)
21454 to->vval.v_dict = NULL;
21455 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21456 {
21457 /* use the copy made earlier */
21458 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21459 ++to->vval.v_dict->dv_refcount;
21460 }
21461 else
21462 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21463 if (to->vval.v_dict == NULL)
21464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021465 break;
21466 default:
21467 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021468 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021469 }
21470 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021471 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021472}
21473
21474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 * ":echo expr1 ..." print each argument separated with a space, add a
21476 * newline at the end.
21477 * ":echon expr1 ..." print each argument plain.
21478 */
21479 void
21480ex_echo(eap)
21481 exarg_T *eap;
21482{
21483 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021484 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021485 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 char_u *p;
21487 int needclr = TRUE;
21488 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490
21491 if (eap->skip)
21492 ++emsg_skip;
21493 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21494 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021495 /* If eval1() causes an error message the text from the command may
21496 * still need to be cleared. E.g., "echo 22,44". */
21497 need_clr_eos = needclr;
21498
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 {
21502 /*
21503 * Report the invalid expression unless the expression evaluation
21504 * has been cancelled due to an aborting error, an interrupt, or an
21505 * exception.
21506 */
21507 if (!aborting())
21508 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021509 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 break;
21511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021512 need_clr_eos = FALSE;
21513
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 if (!eap->skip)
21515 {
21516 if (atstart)
21517 {
21518 atstart = FALSE;
21519 /* Call msg_start() after eval1(), evaluating the expression
21520 * may cause a message to appear. */
21521 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021522 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021523 /* Mark the saved text as finishing the line, so that what
21524 * follows is displayed on a new line when scrolling back
21525 * at the more prompt. */
21526 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 }
21530 else if (eap->cmdidx == CMD_echo)
21531 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021532 current_copyID += COPYID_INC;
21533 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021534 if (p != NULL)
21535 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021537 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021539 if (*p != TAB && needclr)
21540 {
21541 /* remove any text still there from the command */
21542 msg_clr_eos();
21543 needclr = FALSE;
21544 }
21545 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 }
21547 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021548 {
21549#ifdef FEAT_MBYTE
21550 if (has_mbyte)
21551 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021552 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021553
21554 (void)msg_outtrans_len_attr(p, i, echo_attr);
21555 p += i - 1;
21556 }
21557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021559 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 arg = skipwhite(arg);
21566 }
21567 eap->nextcmd = check_nextcmd(arg);
21568
21569 if (eap->skip)
21570 --emsg_skip;
21571 else
21572 {
21573 /* remove text that may still be there from the command */
21574 if (needclr)
21575 msg_clr_eos();
21576 if (eap->cmdidx == CMD_echo)
21577 msg_end();
21578 }
21579}
21580
21581/*
21582 * ":echohl {name}".
21583 */
21584 void
21585ex_echohl(eap)
21586 exarg_T *eap;
21587{
21588 int id;
21589
21590 id = syn_name2id(eap->arg);
21591 if (id == 0)
21592 echo_attr = 0;
21593 else
21594 echo_attr = syn_id2attr(id);
21595}
21596
21597/*
21598 * ":execute expr1 ..." execute the result of an expression.
21599 * ":echomsg expr1 ..." Print a message
21600 * ":echoerr expr1 ..." Print an error
21601 * Each gets spaces around each argument and a newline at the end for
21602 * echo commands
21603 */
21604 void
21605ex_execute(eap)
21606 exarg_T *eap;
21607{
21608 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021609 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 int ret = OK;
21611 char_u *p;
21612 garray_T ga;
21613 int len;
21614 int save_did_emsg;
21615
21616 ga_init2(&ga, 1, 80);
21617
21618 if (eap->skip)
21619 ++emsg_skip;
21620 while (*arg != NUL && *arg != '|' && *arg != '\n')
21621 {
21622 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021623 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 {
21625 /*
21626 * Report the invalid expression unless the expression evaluation
21627 * has been cancelled due to an aborting error, an interrupt, or an
21628 * exception.
21629 */
21630 if (!aborting())
21631 EMSG2(_(e_invexpr2), p);
21632 ret = FAIL;
21633 break;
21634 }
21635
21636 if (!eap->skip)
21637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 len = (int)STRLEN(p);
21640 if (ga_grow(&ga, len + 2) == FAIL)
21641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 ret = FAIL;
21644 break;
21645 }
21646 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 ga.ga_len += len;
21650 }
21651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 arg = skipwhite(arg);
21654 }
21655
21656 if (ret != FAIL && ga.ga_data != NULL)
21657 {
21658 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021661 out_flush();
21662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 else if (eap->cmdidx == CMD_echoerr)
21664 {
21665 /* We don't want to abort following commands, restore did_emsg. */
21666 save_did_emsg = did_emsg;
21667 EMSG((char_u *)ga.ga_data);
21668 if (!force_abort)
21669 did_emsg = save_did_emsg;
21670 }
21671 else if (eap->cmdidx == CMD_execute)
21672 do_cmdline((char_u *)ga.ga_data,
21673 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21674 }
21675
21676 ga_clear(&ga);
21677
21678 if (eap->skip)
21679 --emsg_skip;
21680
21681 eap->nextcmd = check_nextcmd(arg);
21682}
21683
21684/*
21685 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21686 * "arg" points to the "&" or '+' when called, to "option" when returning.
21687 * Returns NULL when no option name found. Otherwise pointer to the char
21688 * after the option name.
21689 */
21690 static char_u *
21691find_option_end(arg, opt_flags)
21692 char_u **arg;
21693 int *opt_flags;
21694{
21695 char_u *p = *arg;
21696
21697 ++p;
21698 if (*p == 'g' && p[1] == ':')
21699 {
21700 *opt_flags = OPT_GLOBAL;
21701 p += 2;
21702 }
21703 else if (*p == 'l' && p[1] == ':')
21704 {
21705 *opt_flags = OPT_LOCAL;
21706 p += 2;
21707 }
21708 else
21709 *opt_flags = 0;
21710
21711 if (!ASCII_ISALPHA(*p))
21712 return NULL;
21713 *arg = p;
21714
21715 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21716 p += 4; /* termcap option */
21717 else
21718 while (ASCII_ISALPHA(*p))
21719 ++p;
21720 return p;
21721}
21722
21723/*
21724 * ":function"
21725 */
21726 void
21727ex_function(eap)
21728 exarg_T *eap;
21729{
21730 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021731 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 int j;
21733 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021735 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 char_u *name = NULL;
21737 char_u *p;
21738 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021739 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 garray_T newargs;
21741 garray_T newlines;
21742 int varargs = FALSE;
21743 int mustend = FALSE;
21744 int flags = 0;
21745 ufunc_T *fp;
21746 int indent;
21747 int nesting;
21748 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 dictitem_T *v;
21750 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751 static int func_nr = 0; /* number for nameless function */
21752 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021753 hashtab_T *ht;
21754 int todo;
21755 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021756 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757
21758 /*
21759 * ":function" without argument: list functions.
21760 */
21761 if (ends_excmd(*eap->arg))
21762 {
21763 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021765 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021766 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021767 {
21768 if (!HASHITEM_EMPTY(hi))
21769 {
21770 --todo;
21771 fp = HI2UF(hi);
21772 if (!isdigit(*fp->uf_name))
21773 list_func_head(fp, FALSE);
21774 }
21775 }
21776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 eap->nextcmd = check_nextcmd(eap->arg);
21778 return;
21779 }
21780
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021781 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021782 * ":function /pat": list functions matching pattern.
21783 */
21784 if (*eap->arg == '/')
21785 {
21786 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21787 if (!eap->skip)
21788 {
21789 regmatch_T regmatch;
21790
21791 c = *p;
21792 *p = NUL;
21793 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21794 *p = c;
21795 if (regmatch.regprog != NULL)
21796 {
21797 regmatch.rm_ic = p_ic;
21798
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021799 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021800 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21801 {
21802 if (!HASHITEM_EMPTY(hi))
21803 {
21804 --todo;
21805 fp = HI2UF(hi);
21806 if (!isdigit(*fp->uf_name)
21807 && vim_regexec(&regmatch, fp->uf_name, 0))
21808 list_func_head(fp, FALSE);
21809 }
21810 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021811 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021812 }
21813 }
21814 if (*p == '/')
21815 ++p;
21816 eap->nextcmd = check_nextcmd(p);
21817 return;
21818 }
21819
21820 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021821 * Get the function name. There are these situations:
21822 * func normal function name
21823 * "name" == func, "fudi.fd_dict" == NULL
21824 * dict.func new dictionary entry
21825 * "name" == NULL, "fudi.fd_dict" set,
21826 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21827 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021828 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021829 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21830 * dict.func existing dict entry that's not a Funcref
21831 * "name" == NULL, "fudi.fd_dict" set,
21832 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021833 * s:func script-local function name
21834 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 name = trans_function_name(&p, eap->skip, 0, &fudi);
21838 paren = (vim_strchr(p, '(') != NULL);
21839 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 {
21841 /*
21842 * Return on an invalid expression in braces, unless the expression
21843 * evaluation has been cancelled due to an aborting error, an
21844 * interrupt, or an exception.
21845 */
21846 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021848 if (!eap->skip && fudi.fd_newkey != NULL)
21849 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021850 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 else
21854 eap->skip = TRUE;
21855 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021856
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 /* An error in a function call during evaluation of an expression in magic
21858 * braces should not cause the function not to be defined. */
21859 saved_did_emsg = did_emsg;
21860 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861
21862 /*
21863 * ":function func" with only function name: list function.
21864 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021865 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 {
21867 if (!ends_excmd(*skipwhite(p)))
21868 {
21869 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021870 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 }
21872 eap->nextcmd = check_nextcmd(p);
21873 if (eap->nextcmd != NULL)
21874 *p = NUL;
21875 if (!eap->skip && !got_int)
21876 {
21877 fp = find_func(name);
21878 if (fp != NULL)
21879 {
21880 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021881 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021883 if (FUNCLINE(fp, j) == NULL)
21884 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 msg_putchar('\n');
21886 msg_outnum((long)(j + 1));
21887 if (j < 9)
21888 msg_putchar(' ');
21889 if (j < 99)
21890 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021891 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 out_flush(); /* show a line at a time */
21893 ui_breakcheck();
21894 }
21895 if (!got_int)
21896 {
21897 msg_putchar('\n');
21898 msg_puts((char_u *)" endfunction");
21899 }
21900 }
21901 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021902 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021904 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 }
21906
21907 /*
21908 * ":function name(arg1, arg2)" Define function.
21909 */
21910 p = skipwhite(p);
21911 if (*p != '(')
21912 {
21913 if (!eap->skip)
21914 {
21915 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021916 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 }
21918 /* attempt to continue by skipping some text */
21919 if (vim_strchr(p, '(') != NULL)
21920 p = vim_strchr(p, '(');
21921 }
21922 p = skipwhite(p + 1);
21923
21924 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21925 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21926
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021927 if (!eap->skip)
21928 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021929 /* Check the name of the function. Unless it's a dictionary function
21930 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021931 if (name != NULL)
21932 arg = name;
21933 else
21934 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021935 if (arg != NULL && (fudi.fd_di == NULL
21936 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021937 {
21938 if (*arg == K_SPECIAL)
21939 j = 3;
21940 else
21941 j = 0;
21942 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21943 : eval_isnamec(arg[j])))
21944 ++j;
21945 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021946 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021947 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021948 /* Disallow using the g: dict. */
21949 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21950 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021951 }
21952
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 /*
21954 * Isolate the arguments: "arg1, arg2, ...)"
21955 */
21956 while (*p != ')')
21957 {
21958 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21959 {
21960 varargs = TRUE;
21961 p += 3;
21962 mustend = TRUE;
21963 }
21964 else
21965 {
21966 arg = p;
21967 while (ASCII_ISALNUM(*p) || *p == '_')
21968 ++p;
21969 if (arg == p || isdigit(*arg)
21970 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21971 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21972 {
21973 if (!eap->skip)
21974 EMSG2(_("E125: Illegal argument: %s"), arg);
21975 break;
21976 }
21977 if (ga_grow(&newargs, 1) == FAIL)
21978 goto erret;
21979 c = *p;
21980 *p = NUL;
21981 arg = vim_strsave(arg);
21982 if (arg == NULL)
21983 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021984
21985 /* Check for duplicate argument name. */
21986 for (i = 0; i < newargs.ga_len; ++i)
21987 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21988 {
21989 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021990 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021991 goto erret;
21992 }
21993
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21995 *p = c;
21996 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 if (*p == ',')
21998 ++p;
21999 else
22000 mustend = TRUE;
22001 }
22002 p = skipwhite(p);
22003 if (mustend && *p != ')')
22004 {
22005 if (!eap->skip)
22006 EMSG2(_(e_invarg2), eap->arg);
22007 break;
22008 }
22009 }
22010 ++p; /* skip the ')' */
22011
Bram Moolenaare9a41262005-01-15 22:18:47 +000022012 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 for (;;)
22014 {
22015 p = skipwhite(p);
22016 if (STRNCMP(p, "range", 5) == 0)
22017 {
22018 flags |= FC_RANGE;
22019 p += 5;
22020 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022021 else if (STRNCMP(p, "dict", 4) == 0)
22022 {
22023 flags |= FC_DICT;
22024 p += 4;
22025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 else if (STRNCMP(p, "abort", 5) == 0)
22027 {
22028 flags |= FC_ABORT;
22029 p += 5;
22030 }
22031 else
22032 break;
22033 }
22034
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022035 /* When there is a line break use what follows for the function body.
22036 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22037 if (*p == '\n')
22038 line_arg = p + 1;
22039 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 EMSG(_(e_trailing));
22041
22042 /*
22043 * Read the body of the function, until ":endfunction" is found.
22044 */
22045 if (KeyTyped)
22046 {
22047 /* Check if the function already exists, don't let the user type the
22048 * whole function before telling him it doesn't work! For a script we
22049 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022050 if (!eap->skip && !eap->forceit)
22051 {
22052 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22053 EMSG(_(e_funcdict));
22054 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022055 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022058 if (!eap->skip && did_emsg)
22059 goto erret;
22060
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 msg_putchar('\n'); /* don't overwrite the function name */
22062 cmdline_row = msg_row;
22063 }
22064
22065 indent = 2;
22066 nesting = 0;
22067 for (;;)
22068 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022069 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022070 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022071 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022072 saved_wait_return = FALSE;
22073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022075 sourcing_lnum_off = sourcing_lnum;
22076
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022077 if (line_arg != NULL)
22078 {
22079 /* Use eap->arg, split up in parts by line breaks. */
22080 theline = line_arg;
22081 p = vim_strchr(theline, '\n');
22082 if (p == NULL)
22083 line_arg += STRLEN(line_arg);
22084 else
22085 {
22086 *p = NUL;
22087 line_arg = p + 1;
22088 }
22089 }
22090 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 theline = getcmdline(':', 0L, indent);
22092 else
22093 theline = eap->getline(':', eap->cookie, indent);
22094 if (KeyTyped)
22095 lines_left = Rows - 1;
22096 if (theline == NULL)
22097 {
22098 EMSG(_("E126: Missing :endfunction"));
22099 goto erret;
22100 }
22101
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022102 /* Detect line continuation: sourcing_lnum increased more than one. */
22103 if (sourcing_lnum > sourcing_lnum_off + 1)
22104 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22105 else
22106 sourcing_lnum_off = 0;
22107
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 if (skip_until != NULL)
22109 {
22110 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22111 * don't check for ":endfunc". */
22112 if (STRCMP(theline, skip_until) == 0)
22113 {
22114 vim_free(skip_until);
22115 skip_until = NULL;
22116 }
22117 }
22118 else
22119 {
22120 /* skip ':' and blanks*/
22121 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22122 ;
22123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022124 /* Check for "endfunction". */
22125 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022127 if (line_arg == NULL)
22128 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 break;
22130 }
22131
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022132 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 * at "end". */
22134 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22135 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022136 else if (STRNCMP(p, "if", 2) == 0
22137 || STRNCMP(p, "wh", 2) == 0
22138 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 || STRNCMP(p, "try", 3) == 0)
22140 indent += 2;
22141
22142 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022143 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022145 if (*p == '!')
22146 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 p += eval_fname_script(p);
22148 if (ASCII_ISALPHA(*p))
22149 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022150 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 if (*skipwhite(p) == '(')
22152 {
22153 ++nesting;
22154 indent += 2;
22155 }
22156 }
22157 }
22158
22159 /* Check for ":append" or ":insert". */
22160 p = skip_range(p, NULL);
22161 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22162 || (p[0] == 'i'
22163 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22164 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22165 skip_until = vim_strsave((char_u *)".");
22166
22167 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22168 arg = skipwhite(skiptowhite(p));
22169 if (arg[0] == '<' && arg[1] =='<'
22170 && ((p[0] == 'p' && p[1] == 'y'
22171 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22172 || (p[0] == 'p' && p[1] == 'e'
22173 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22174 || (p[0] == 't' && p[1] == 'c'
22175 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022176 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22177 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22179 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022180 || (p[0] == 'm' && p[1] == 'z'
22181 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 ))
22183 {
22184 /* ":python <<" continues until a dot, like ":append" */
22185 p = skipwhite(arg + 2);
22186 if (*p == NUL)
22187 skip_until = vim_strsave((char_u *)".");
22188 else
22189 skip_until = vim_strsave(p);
22190 }
22191 }
22192
22193 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022194 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022195 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022196 if (line_arg == NULL)
22197 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022199 }
22200
22201 /* Copy the line to newly allocated memory. get_one_sourceline()
22202 * allocates 250 bytes per line, this saves 80% on average. The cost
22203 * is an extra alloc/free. */
22204 p = vim_strsave(theline);
22205 if (p != NULL)
22206 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022207 if (line_arg == NULL)
22208 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022209 theline = p;
22210 }
22211
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022212 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22213
22214 /* Add NULL lines for continuation lines, so that the line count is
22215 * equal to the index in the growarray. */
22216 while (sourcing_lnum_off-- > 0)
22217 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022218
22219 /* Check for end of eap->arg. */
22220 if (line_arg != NULL && *line_arg == NUL)
22221 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 }
22223
22224 /* Don't define the function when skipping commands or when an error was
22225 * detected. */
22226 if (eap->skip || did_emsg)
22227 goto erret;
22228
22229 /*
22230 * If there are no errors, add the function
22231 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022232 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022233 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022234 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022236 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022237 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022238 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022239 goto erret;
22240 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022242 fp = find_func(name);
22243 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022245 if (!eap->forceit)
22246 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022247 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022248 goto erret;
22249 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022251 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022252 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022253 name);
22254 goto erret;
22255 }
22256 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022257 ga_clear_strings(&(fp->uf_args));
22258 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022259 vim_free(name);
22260 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 }
22263 else
22264 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022265 char numbuf[20];
22266
22267 fp = NULL;
22268 if (fudi.fd_newkey == NULL && !eap->forceit)
22269 {
22270 EMSG(_(e_funcdict));
22271 goto erret;
22272 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022273 if (fudi.fd_di == NULL)
22274 {
22275 /* Can't add a function to a locked dictionary */
22276 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22277 goto erret;
22278 }
22279 /* Can't change an existing function if it is locked */
22280 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22281 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022282
22283 /* Give the function a sequential number. Can only be used with a
22284 * Funcref! */
22285 vim_free(name);
22286 sprintf(numbuf, "%d", ++func_nr);
22287 name = vim_strsave((char_u *)numbuf);
22288 if (name == NULL)
22289 goto erret;
22290 }
22291
22292 if (fp == NULL)
22293 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022294 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022295 {
22296 int slen, plen;
22297 char_u *scriptname;
22298
22299 /* Check that the autoload name matches the script name. */
22300 j = FAIL;
22301 if (sourcing_name != NULL)
22302 {
22303 scriptname = autoload_name(name);
22304 if (scriptname != NULL)
22305 {
22306 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022307 plen = (int)STRLEN(p);
22308 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022309 if (slen > plen && fnamecmp(p,
22310 sourcing_name + slen - plen) == 0)
22311 j = OK;
22312 vim_free(scriptname);
22313 }
22314 }
22315 if (j == FAIL)
22316 {
22317 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22318 goto erret;
22319 }
22320 }
22321
22322 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 if (fp == NULL)
22324 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325
22326 if (fudi.fd_dict != NULL)
22327 {
22328 if (fudi.fd_di == NULL)
22329 {
22330 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022331 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 if (fudi.fd_di == NULL)
22333 {
22334 vim_free(fp);
22335 goto erret;
22336 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022337 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22338 {
22339 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022340 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022341 goto erret;
22342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022343 }
22344 else
22345 /* overwrite existing dict entry */
22346 clear_tv(&fudi.fd_di->di_tv);
22347 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022348 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022350 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022351
22352 /* behave like "dict" was used */
22353 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 }
22355
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022357 STRCPY(fp->uf_name, name);
22358 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022360 fp->uf_args = newargs;
22361 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022362#ifdef FEAT_PROFILE
22363 fp->uf_tml_count = NULL;
22364 fp->uf_tml_total = NULL;
22365 fp->uf_tml_self = NULL;
22366 fp->uf_profiling = FALSE;
22367 if (prof_def_func())
22368 func_do_profile(fp);
22369#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022370 fp->uf_varargs = varargs;
22371 fp->uf_flags = flags;
22372 fp->uf_calls = 0;
22373 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022374 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375
22376erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 ga_clear_strings(&newargs);
22378 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379ret_free:
22380 vim_free(skip_until);
22381 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022384 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385}
22386
22387/*
22388 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022389 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022391 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022392 * TFN_INT: internal function name OK
22393 * TFN_QUIET: be quiet
22394 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 * Advances "pp" to just after the function name (if no error).
22396 */
22397 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022398trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 char_u **pp;
22400 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022401 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022402 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022404 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 char_u *start;
22406 char_u *end;
22407 int lead;
22408 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022411
22412 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022415
22416 /* Check for hard coded <SNR>: already translated function ID (from a user
22417 * command). */
22418 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22419 && (*pp)[2] == (int)KE_SNR)
22420 {
22421 *pp += 3;
22422 len = get_id_len(pp) + 3;
22423 return vim_strnsave(start, len);
22424 }
22425
22426 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22427 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022429 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022431
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022432 /* Note that TFN_ flags use the same values as GLV_ flags. */
22433 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022434 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022435 if (end == start)
22436 {
22437 if (!skip)
22438 EMSG(_("E129: Function name required"));
22439 goto theend;
22440 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022441 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022442 {
22443 /*
22444 * Report an invalid expression in braces, unless the expression
22445 * evaluation has been cancelled due to an aborting error, an
22446 * interrupt, or an exception.
22447 */
22448 if (!aborting())
22449 {
22450 if (end != NULL)
22451 EMSG2(_(e_invarg2), start);
22452 }
22453 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022454 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022455 goto theend;
22456 }
22457
22458 if (lv.ll_tv != NULL)
22459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022460 if (fdp != NULL)
22461 {
22462 fdp->fd_dict = lv.ll_dict;
22463 fdp->fd_newkey = lv.ll_newkey;
22464 lv.ll_newkey = NULL;
22465 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022467 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22468 {
22469 name = vim_strsave(lv.ll_tv->vval.v_string);
22470 *pp = end;
22471 }
22472 else
22473 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022474 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22475 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022476 EMSG(_(e_funcref));
22477 else
22478 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022479 name = NULL;
22480 }
22481 goto theend;
22482 }
22483
22484 if (lv.ll_name == NULL)
22485 {
22486 /* Error found, but continue after the function name. */
22487 *pp = end;
22488 goto theend;
22489 }
22490
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022491 /* Check if the name is a Funcref. If so, use the value. */
22492 if (lv.ll_exp_name != NULL)
22493 {
22494 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022495 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022496 if (name == lv.ll_exp_name)
22497 name = NULL;
22498 }
22499 else
22500 {
22501 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022502 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022503 if (name == *pp)
22504 name = NULL;
22505 }
22506 if (name != NULL)
22507 {
22508 name = vim_strsave(name);
22509 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022510 if (STRNCMP(name, "<SNR>", 5) == 0)
22511 {
22512 /* Change "<SNR>" to the byte sequence. */
22513 name[0] = K_SPECIAL;
22514 name[1] = KS_EXTRA;
22515 name[2] = (int)KE_SNR;
22516 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22517 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022518 goto theend;
22519 }
22520
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022521 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022522 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022523 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022524 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22525 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22526 {
22527 /* When there was "s:" already or the name expanded to get a
22528 * leading "s:" then remove it. */
22529 lv.ll_name += 2;
22530 len -= 2;
22531 lead = 2;
22532 }
22533 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022534 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022535 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022536 /* skip over "s:" and "g:" */
22537 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022538 lv.ll_name += 2;
22539 len = (int)(end - lv.ll_name);
22540 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022541
22542 /*
22543 * Copy the function name to allocated memory.
22544 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22545 * Accept <SNR>123_name() outside a script.
22546 */
22547 if (skip)
22548 lead = 0; /* do nothing */
22549 else if (lead > 0)
22550 {
22551 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022552 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22553 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022554 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022555 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022556 if (current_SID <= 0)
22557 {
22558 EMSG(_(e_usingsid));
22559 goto theend;
22560 }
22561 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22562 lead += (int)STRLEN(sid_buf);
22563 }
22564 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022565 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022566 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022567 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022568 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022569 goto theend;
22570 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022571 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022572 {
22573 char_u *cp = vim_strchr(lv.ll_name, ':');
22574
22575 if (cp != NULL && cp < end)
22576 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022577 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022578 goto theend;
22579 }
22580 }
22581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022582 name = alloc((unsigned)(len + lead + 1));
22583 if (name != NULL)
22584 {
22585 if (lead > 0)
22586 {
22587 name[0] = K_SPECIAL;
22588 name[1] = KS_EXTRA;
22589 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022590 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022591 STRCPY(name + 3, sid_buf);
22592 }
22593 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022594 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022595 }
22596 *pp = end;
22597
22598theend:
22599 clear_lval(&lv);
22600 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601}
22602
22603/*
22604 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22605 * Return 2 if "p" starts with "s:".
22606 * Return 0 otherwise.
22607 */
22608 static int
22609eval_fname_script(p)
22610 char_u *p;
22611{
22612 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22613 || STRNICMP(p + 1, "SNR>", 4) == 0))
22614 return 5;
22615 if (p[0] == 's' && p[1] == ':')
22616 return 2;
22617 return 0;
22618}
22619
22620/*
22621 * Return TRUE if "p" starts with "<SID>" or "s:".
22622 * Only works if eval_fname_script() returned non-zero for "p"!
22623 */
22624 static int
22625eval_fname_sid(p)
22626 char_u *p;
22627{
22628 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22629}
22630
22631/*
22632 * List the head of the function: "name(arg1, arg2)".
22633 */
22634 static void
22635list_func_head(fp, indent)
22636 ufunc_T *fp;
22637 int indent;
22638{
22639 int j;
22640
22641 msg_start();
22642 if (indent)
22643 MSG_PUTS(" ");
22644 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022645 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 {
22647 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022648 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 }
22650 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022651 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022653 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
22655 if (j)
22656 MSG_PUTS(", ");
22657 msg_puts(FUNCARG(fp, j));
22658 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022659 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 {
22661 if (j)
22662 MSG_PUTS(", ");
22663 MSG_PUTS("...");
22664 }
22665 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022666 if (fp->uf_flags & FC_ABORT)
22667 MSG_PUTS(" abort");
22668 if (fp->uf_flags & FC_RANGE)
22669 MSG_PUTS(" range");
22670 if (fp->uf_flags & FC_DICT)
22671 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022672 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022673 if (p_verbose > 0)
22674 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675}
22676
22677/*
22678 * Find a function by name, return pointer to it in ufuncs.
22679 * Return NULL for unknown function.
22680 */
22681 static ufunc_T *
22682find_func(name)
22683 char_u *name;
22684{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022685 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022687 hi = hash_find(&func_hashtab, name);
22688 if (!HASHITEM_EMPTY(hi))
22689 return HI2UF(hi);
22690 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691}
22692
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022693#if defined(EXITFREE) || defined(PROTO)
22694 void
22695free_all_functions()
22696{
22697 hashitem_T *hi;
22698
22699 /* Need to start all over every time, because func_free() may change the
22700 * hash table. */
22701 while (func_hashtab.ht_used > 0)
22702 for (hi = func_hashtab.ht_array; ; ++hi)
22703 if (!HASHITEM_EMPTY(hi))
22704 {
22705 func_free(HI2UF(hi));
22706 break;
22707 }
22708}
22709#endif
22710
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022711 int
22712translated_function_exists(name)
22713 char_u *name;
22714{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022715 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022716 return find_internal_func(name) >= 0;
22717 return find_func(name) != NULL;
22718}
22719
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720/*
22721 * Return TRUE if a function "name" exists.
22722 */
22723 static int
22724function_exists(name)
22725 char_u *name;
22726{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022727 char_u *nm = name;
22728 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022729 int n = FALSE;
22730
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022731 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22732 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022733 nm = skipwhite(nm);
22734
22735 /* Only accept "funcname", "funcname ", "funcname (..." and
22736 * "funcname(...", not "funcname!...". */
22737 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022738 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022739 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022740 return n;
22741}
22742
Bram Moolenaara1544c02013-05-30 12:35:52 +020022743 char_u *
22744get_expanded_name(name, check)
22745 char_u *name;
22746 int check;
22747{
22748 char_u *nm = name;
22749 char_u *p;
22750
22751 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22752
22753 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022754 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022755 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022756
Bram Moolenaara1544c02013-05-30 12:35:52 +020022757 vim_free(p);
22758 return NULL;
22759}
22760
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022761/*
22762 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022763 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22764 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022765 */
22766 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022767builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022768 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022769 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022770{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022771 char_u *p;
22772
22773 if (!ASCII_ISLOWER(name[0]))
22774 return FALSE;
22775 p = vim_strchr(name, AUTOLOAD_CHAR);
22776 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022777}
22778
Bram Moolenaar05159a02005-02-26 23:04:13 +000022779#if defined(FEAT_PROFILE) || defined(PROTO)
22780/*
22781 * Start profiling function "fp".
22782 */
22783 static void
22784func_do_profile(fp)
22785 ufunc_T *fp;
22786{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022787 int len = fp->uf_lines.ga_len;
22788
22789 if (len == 0)
22790 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022791 fp->uf_tm_count = 0;
22792 profile_zero(&fp->uf_tm_self);
22793 profile_zero(&fp->uf_tm_total);
22794 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022795 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022796 if (fp->uf_tml_total == NULL)
22797 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022798 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022799 if (fp->uf_tml_self == NULL)
22800 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022801 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022802 fp->uf_tml_idx = -1;
22803 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22804 || fp->uf_tml_self == NULL)
22805 return; /* out of memory */
22806
22807 fp->uf_profiling = TRUE;
22808}
22809
22810/*
22811 * Dump the profiling results for all functions in file "fd".
22812 */
22813 void
22814func_dump_profile(fd)
22815 FILE *fd;
22816{
22817 hashitem_T *hi;
22818 int todo;
22819 ufunc_T *fp;
22820 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022821 ufunc_T **sorttab;
22822 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022823
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022824 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022825 if (todo == 0)
22826 return; /* nothing to dump */
22827
Bram Moolenaar73830342005-02-28 22:48:19 +000022828 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22829
Bram Moolenaar05159a02005-02-26 23:04:13 +000022830 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22831 {
22832 if (!HASHITEM_EMPTY(hi))
22833 {
22834 --todo;
22835 fp = HI2UF(hi);
22836 if (fp->uf_profiling)
22837 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022838 if (sorttab != NULL)
22839 sorttab[st_len++] = fp;
22840
Bram Moolenaar05159a02005-02-26 23:04:13 +000022841 if (fp->uf_name[0] == K_SPECIAL)
22842 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22843 else
22844 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22845 if (fp->uf_tm_count == 1)
22846 fprintf(fd, "Called 1 time\n");
22847 else
22848 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22849 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22850 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22851 fprintf(fd, "\n");
22852 fprintf(fd, "count total (s) self (s)\n");
22853
22854 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22855 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022856 if (FUNCLINE(fp, i) == NULL)
22857 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022858 prof_func_line(fd, fp->uf_tml_count[i],
22859 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022860 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22861 }
22862 fprintf(fd, "\n");
22863 }
22864 }
22865 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022866
22867 if (sorttab != NULL && st_len > 0)
22868 {
22869 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22870 prof_total_cmp);
22871 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22872 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22873 prof_self_cmp);
22874 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22875 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022876
22877 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022878}
Bram Moolenaar73830342005-02-28 22:48:19 +000022879
22880 static void
22881prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22882 FILE *fd;
22883 ufunc_T **sorttab;
22884 int st_len;
22885 char *title;
22886 int prefer_self; /* when equal print only self time */
22887{
22888 int i;
22889 ufunc_T *fp;
22890
22891 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22892 fprintf(fd, "count total (s) self (s) function\n");
22893 for (i = 0; i < 20 && i < st_len; ++i)
22894 {
22895 fp = sorttab[i];
22896 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22897 prefer_self);
22898 if (fp->uf_name[0] == K_SPECIAL)
22899 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22900 else
22901 fprintf(fd, " %s()\n", fp->uf_name);
22902 }
22903 fprintf(fd, "\n");
22904}
22905
22906/*
22907 * Print the count and times for one function or function line.
22908 */
22909 static void
22910prof_func_line(fd, count, total, self, prefer_self)
22911 FILE *fd;
22912 int count;
22913 proftime_T *total;
22914 proftime_T *self;
22915 int prefer_self; /* when equal print only self time */
22916{
22917 if (count > 0)
22918 {
22919 fprintf(fd, "%5d ", count);
22920 if (prefer_self && profile_equal(total, self))
22921 fprintf(fd, " ");
22922 else
22923 fprintf(fd, "%s ", profile_msg(total));
22924 if (!prefer_self && profile_equal(total, self))
22925 fprintf(fd, " ");
22926 else
22927 fprintf(fd, "%s ", profile_msg(self));
22928 }
22929 else
22930 fprintf(fd, " ");
22931}
22932
22933/*
22934 * Compare function for total time sorting.
22935 */
22936 static int
22937#ifdef __BORLANDC__
22938_RTLENTRYF
22939#endif
22940prof_total_cmp(s1, s2)
22941 const void *s1;
22942 const void *s2;
22943{
22944 ufunc_T *p1, *p2;
22945
22946 p1 = *(ufunc_T **)s1;
22947 p2 = *(ufunc_T **)s2;
22948 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22949}
22950
22951/*
22952 * Compare function for self time sorting.
22953 */
22954 static int
22955#ifdef __BORLANDC__
22956_RTLENTRYF
22957#endif
22958prof_self_cmp(s1, s2)
22959 const void *s1;
22960 const void *s2;
22961{
22962 ufunc_T *p1, *p2;
22963
22964 p1 = *(ufunc_T **)s1;
22965 p2 = *(ufunc_T **)s2;
22966 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22967}
22968
Bram Moolenaar05159a02005-02-26 23:04:13 +000022969#endif
22970
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022971/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022972 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022973 * Return TRUE if a package was loaded.
22974 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022975 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022976script_autoload(name, reload)
22977 char_u *name;
22978 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022979{
22980 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022981 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022982 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022983 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022984
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022985 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022986 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022987 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022988 return FALSE;
22989
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022990 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022992 /* Find the name in the list of previously loaded package names. Skip
22993 * "autoload/", it's always the same. */
22994 for (i = 0; i < ga_loaded.ga_len; ++i)
22995 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22996 break;
22997 if (!reload && i < ga_loaded.ga_len)
22998 ret = FALSE; /* was loaded already */
22999 else
23000 {
23001 /* Remember the name if it wasn't loaded already. */
23002 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23003 {
23004 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23005 tofree = NULL;
23006 }
23007
23008 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023009 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023010 ret = TRUE;
23011 }
23012
23013 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023014 return ret;
23015}
23016
23017/*
23018 * Return the autoload script name for a function or variable name.
23019 * Returns NULL when out of memory.
23020 */
23021 static char_u *
23022autoload_name(name)
23023 char_u *name;
23024{
23025 char_u *p;
23026 char_u *scriptname;
23027
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023028 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023029 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23030 if (scriptname == NULL)
23031 return FALSE;
23032 STRCPY(scriptname, "autoload/");
23033 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023034 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023035 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023036 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023037 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023038 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023039}
23040
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23042
23043/*
23044 * Function given to ExpandGeneric() to obtain the list of user defined
23045 * function names.
23046 */
23047 char_u *
23048get_user_func_name(xp, idx)
23049 expand_T *xp;
23050 int idx;
23051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023052 static long_u done;
23053 static hashitem_T *hi;
23054 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055
23056 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023058 done = 0;
23059 hi = func_hashtab.ht_array;
23060 }
23061 if (done < func_hashtab.ht_used)
23062 {
23063 if (done++ > 0)
23064 ++hi;
23065 while (HASHITEM_EMPTY(hi))
23066 ++hi;
23067 fp = HI2UF(hi);
23068
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023069 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023070 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23073 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074
23075 cat_func_name(IObuff, fp);
23076 if (xp->xp_context != EXPAND_USER_FUNC)
23077 {
23078 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023079 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 STRCAT(IObuff, ")");
23081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 return IObuff;
23083 }
23084 return NULL;
23085}
23086
23087#endif /* FEAT_CMDL_COMPL */
23088
23089/*
23090 * Copy the function name of "fp" to buffer "buf".
23091 * "buf" must be able to hold the function name plus three bytes.
23092 * Takes care of script-local function names.
23093 */
23094 static void
23095cat_func_name(buf, fp)
23096 char_u *buf;
23097 ufunc_T *fp;
23098{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023099 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 {
23101 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023102 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 }
23104 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023105 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106}
23107
23108/*
23109 * ":delfunction {name}"
23110 */
23111 void
23112ex_delfunction(eap)
23113 exarg_T *eap;
23114{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023115 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 char_u *p;
23117 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119
23120 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023121 name = trans_function_name(&p, eap->skip, 0, &fudi);
23122 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023124 {
23125 if (fudi.fd_dict != NULL && !eap->skip)
23126 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 if (!ends_excmd(*skipwhite(p)))
23130 {
23131 vim_free(name);
23132 EMSG(_(e_trailing));
23133 return;
23134 }
23135 eap->nextcmd = check_nextcmd(p);
23136 if (eap->nextcmd != NULL)
23137 *p = NUL;
23138
23139 if (!eap->skip)
23140 fp = find_func(name);
23141 vim_free(name);
23142
23143 if (!eap->skip)
23144 {
23145 if (fp == NULL)
23146 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023147 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 return;
23149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 {
23152 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23153 return;
23154 }
23155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023156 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023158 /* Delete the dict item that refers to the function, it will
23159 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023160 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023162 else
23163 func_free(fp);
23164 }
23165}
23166
23167/*
23168 * Free a function and remove it from the list of functions.
23169 */
23170 static void
23171func_free(fp)
23172 ufunc_T *fp;
23173{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023174 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175
23176 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023177 ga_clear_strings(&(fp->uf_args));
23178 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023179#ifdef FEAT_PROFILE
23180 vim_free(fp->uf_tml_count);
23181 vim_free(fp->uf_tml_total);
23182 vim_free(fp->uf_tml_self);
23183#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023184
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023185 /* remove the function from the function hashtable */
23186 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23187 if (HASHITEM_EMPTY(hi))
23188 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023189 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023190 hash_remove(&func_hashtab, hi);
23191
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023192 vim_free(fp);
23193}
23194
23195/*
23196 * Unreference a Function: decrement the reference count and free it when it
23197 * becomes zero. Only for numbered functions.
23198 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023199 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023200func_unref(name)
23201 char_u *name;
23202{
23203 ufunc_T *fp;
23204
23205 if (name != NULL && isdigit(*name))
23206 {
23207 fp = find_func(name);
23208 if (fp == NULL)
23209 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023210 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023211 {
23212 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023213 * when "uf_calls" becomes zero. */
23214 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023215 func_free(fp);
23216 }
23217 }
23218}
23219
23220/*
23221 * Count a reference to a Function.
23222 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023223 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023224func_ref(name)
23225 char_u *name;
23226{
23227 ufunc_T *fp;
23228
23229 if (name != NULL && isdigit(*name))
23230 {
23231 fp = find_func(name);
23232 if (fp == NULL)
23233 EMSG2(_(e_intern2), "func_ref()");
23234 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023235 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 }
23237}
23238
23239/*
23240 * Call a user function.
23241 */
23242 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023243call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 ufunc_T *fp; /* pointer to function */
23245 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 typval_T *argvars; /* arguments */
23247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 linenr_T firstline; /* first line of range */
23249 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251{
Bram Moolenaar33570922005-01-25 22:26:29 +000023252 char_u *save_sourcing_name;
23253 linenr_T save_sourcing_lnum;
23254 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023255 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023256 int save_did_emsg;
23257 static int depth = 0;
23258 dictitem_T *v;
23259 int fixvar_idx = 0; /* index in fixvar[] */
23260 int i;
23261 int ai;
23262 char_u numbuf[NUMBUFLEN];
23263 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023264#ifdef FEAT_PROFILE
23265 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023266 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268
23269 /* If depth of calling is getting too high, don't execute the function */
23270 if (depth >= p_mfd)
23271 {
23272 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023273 rettv->v_type = VAR_NUMBER;
23274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 return;
23276 }
23277 ++depth;
23278
23279 line_breakcheck(); /* check for CTRL-C hit */
23280
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023281 fc = (funccall_T *)alloc(sizeof(funccall_T));
23282 fc->caller = current_funccal;
23283 current_funccal = fc;
23284 fc->func = fp;
23285 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023286 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023287 fc->linenr = 0;
23288 fc->returned = FALSE;
23289 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023291 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23292 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293
Bram Moolenaar33570922005-01-25 22:26:29 +000023294 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023295 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23297 * each argument variable and saves a lot of time.
23298 */
23299 /*
23300 * Init l: variables.
23301 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023302 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023303 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023304 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023305 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23306 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023307 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023308 name = v->di_key;
23309 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023311 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023312 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023313 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 v->di_tv.vval.v_dict = selfdict;
23315 ++selfdict->dv_refcount;
23316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023317
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 /*
23319 * Init a: variables.
23320 * Set a:0 to "argcount".
23321 * Set a:000 to a list with room for the "..." arguments.
23322 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023323 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023324 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023325 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023326 /* Use "name" to avoid a warning from some compiler that checks the
23327 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023328 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023329 name = v->di_key;
23330 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023331 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023332 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023333 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023334 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023335 v->di_tv.vval.v_list = &fc->l_varlist;
23336 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23337 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23338 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023339
23340 /*
23341 * Set a:firstline to "firstline" and a:lastline to "lastline".
23342 * Set a:name to named arguments.
23343 * Set a:N to the "..." arguments.
23344 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023345 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023347 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023348 (varnumber_T)lastline);
23349 for (i = 0; i < argcount; ++i)
23350 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023351 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023352 if (ai < 0)
23353 /* named argument a:name */
23354 name = FUNCARG(fp, i);
23355 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023356 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023357 /* "..." argument a:1, a:2, etc. */
23358 sprintf((char *)numbuf, "%d", ai + 1);
23359 name = numbuf;
23360 }
23361 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23362 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023363 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023364 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23365 }
23366 else
23367 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023368 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23369 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023370 if (v == NULL)
23371 break;
23372 v->di_flags = DI_FLAGS_RO;
23373 }
23374 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023375 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023376
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023377 /* Note: the values are copied directly to avoid alloc/free.
23378 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023379 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023380 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023381
23382 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23383 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023384 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23385 fc->l_listitems[ai].li_tv = argvars[i];
23386 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023387 }
23388 }
23389
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 /* Don't redraw while executing the function. */
23391 ++RedrawingDisabled;
23392 save_sourcing_name = sourcing_name;
23393 save_sourcing_lnum = sourcing_lnum;
23394 sourcing_lnum = 1;
23395 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023396 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 if (sourcing_name != NULL)
23398 {
23399 if (save_sourcing_name != NULL
23400 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23401 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23402 else
23403 STRCPY(sourcing_name, "function ");
23404 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23405
23406 if (p_verbose >= 12)
23407 {
23408 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023409 verbose_enter_scroll();
23410
Bram Moolenaar555b2802005-05-19 21:08:39 +000023411 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 if (p_verbose >= 14)
23413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023415 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023416 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023417 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418
23419 msg_puts((char_u *)"(");
23420 for (i = 0; i < argcount; ++i)
23421 {
23422 if (i > 0)
23423 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023424 if (argvars[i].v_type == VAR_NUMBER)
23425 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 else
23427 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023428 /* Do not want errors such as E724 here. */
23429 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023430 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023431 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023432 if (s != NULL)
23433 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023434 if (vim_strsize(s) > MSG_BUF_CLEN)
23435 {
23436 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23437 s = buf;
23438 }
23439 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023440 vim_free(tofree);
23441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 }
23443 }
23444 msg_puts((char_u *)")");
23445 }
23446 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023447
23448 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 --no_wait_return;
23450 }
23451 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023452#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023453 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023454 {
23455 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23456 func_do_profile(fp);
23457 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023458 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023459 {
23460 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023461 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023462 profile_zero(&fp->uf_tm_children);
23463 }
23464 script_prof_save(&wait_start);
23465 }
23466#endif
23467
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023469 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 save_did_emsg = did_emsg;
23471 did_emsg = FALSE;
23472
23473 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023474 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23476
23477 --RedrawingDisabled;
23478
23479 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023480 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023482 clear_tv(rettv);
23483 rettv->v_type = VAR_NUMBER;
23484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 }
23486
Bram Moolenaar05159a02005-02-26 23:04:13 +000023487#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023488 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023489 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023490 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023491 profile_end(&call_start);
23492 profile_sub_wait(&wait_start, &call_start);
23493 profile_add(&fp->uf_tm_total, &call_start);
23494 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023495 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023496 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023497 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23498 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023499 }
23500 }
23501#endif
23502
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 /* when being verbose, mention the return value */
23504 if (p_verbose >= 12)
23505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023507 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023510 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023511 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023512 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023513 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023514 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023516 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023517 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023518 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023519 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023520
Bram Moolenaar555b2802005-05-19 21:08:39 +000023521 /* The value may be very long. Skip the middle part, so that we
23522 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023523 * truncate it at the end. Don't want errors such as E724 here. */
23524 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023525 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023526 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023527 if (s != NULL)
23528 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023529 if (vim_strsize(s) > MSG_BUF_CLEN)
23530 {
23531 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23532 s = buf;
23533 }
23534 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023535 vim_free(tofree);
23536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 }
23538 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023539
23540 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 --no_wait_return;
23542 }
23543
23544 vim_free(sourcing_name);
23545 sourcing_name = save_sourcing_name;
23546 sourcing_lnum = save_sourcing_lnum;
23547 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023548#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023549 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023550 script_prof_restore(&wait_start);
23551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552
23553 if (p_verbose >= 12 && sourcing_name != NULL)
23554 {
23555 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023556 verbose_enter_scroll();
23557
Bram Moolenaar555b2802005-05-19 21:08:39 +000023558 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023560
23561 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 --no_wait_return;
23563 }
23564
23565 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023566 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023568
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023569 /* If the a:000 list and the l: and a: dicts are not referenced we can
23570 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023571 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23572 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23573 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23574 {
23575 free_funccal(fc, FALSE);
23576 }
23577 else
23578 {
23579 hashitem_T *hi;
23580 listitem_T *li;
23581 int todo;
23582
23583 /* "fc" is still in use. This can happen when returning "a:000" or
23584 * assigning "l:" to a global variable.
23585 * Link "fc" in the list for garbage collection later. */
23586 fc->caller = previous_funccal;
23587 previous_funccal = fc;
23588
23589 /* Make a copy of the a: variables, since we didn't do that above. */
23590 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23591 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23592 {
23593 if (!HASHITEM_EMPTY(hi))
23594 {
23595 --todo;
23596 v = HI2DI(hi);
23597 copy_tv(&v->di_tv, &v->di_tv);
23598 }
23599 }
23600
23601 /* Make a copy of the a:000 items, since we didn't do that above. */
23602 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23603 copy_tv(&li->li_tv, &li->li_tv);
23604 }
23605}
23606
23607/*
23608 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023609 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023610 */
23611 static int
23612can_free_funccal(fc, copyID)
23613 funccall_T *fc;
23614 int copyID;
23615{
23616 return (fc->l_varlist.lv_copyID != copyID
23617 && fc->l_vars.dv_copyID != copyID
23618 && fc->l_avars.dv_copyID != copyID);
23619}
23620
23621/*
23622 * Free "fc" and what it contains.
23623 */
23624 static void
23625free_funccal(fc, free_val)
23626 funccall_T *fc;
23627 int free_val; /* a: vars were allocated */
23628{
23629 listitem_T *li;
23630
23631 /* The a: variables typevals may not have been allocated, only free the
23632 * allocated variables. */
23633 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23634
23635 /* free all l: variables */
23636 vars_clear(&fc->l_vars.dv_hashtab);
23637
23638 /* Free the a:000 variables if they were allocated. */
23639 if (free_val)
23640 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23641 clear_tv(&li->li_tv);
23642
23643 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644}
23645
23646/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023647 * Add a number variable "name" to dict "dp" with value "nr".
23648 */
23649 static void
23650add_nr_var(dp, v, name, nr)
23651 dict_T *dp;
23652 dictitem_T *v;
23653 char *name;
23654 varnumber_T nr;
23655{
23656 STRCPY(v->di_key, name);
23657 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23658 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23659 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023660 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023661 v->di_tv.vval.v_number = nr;
23662}
23663
23664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 * ":return [expr]"
23666 */
23667 void
23668ex_return(eap)
23669 exarg_T *eap;
23670{
23671 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023672 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 int returning = FALSE;
23674
23675 if (current_funccal == NULL)
23676 {
23677 EMSG(_("E133: :return not inside a function"));
23678 return;
23679 }
23680
23681 if (eap->skip)
23682 ++emsg_skip;
23683
23684 eap->nextcmd = NULL;
23685 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023686 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 {
23688 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023689 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023691 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 }
23693 /* It's safer to return also on error. */
23694 else if (!eap->skip)
23695 {
23696 /*
23697 * Return unless the expression evaluation has been cancelled due to an
23698 * aborting error, an interrupt, or an exception.
23699 */
23700 if (!aborting())
23701 returning = do_return(eap, FALSE, TRUE, NULL);
23702 }
23703
23704 /* When skipping or the return gets pending, advance to the next command
23705 * in this line (!returning). Otherwise, ignore the rest of the line.
23706 * Following lines will be ignored by get_func_line(). */
23707 if (returning)
23708 eap->nextcmd = NULL;
23709 else if (eap->nextcmd == NULL) /* no argument */
23710 eap->nextcmd = check_nextcmd(arg);
23711
23712 if (eap->skip)
23713 --emsg_skip;
23714}
23715
23716/*
23717 * Return from a function. Possibly makes the return pending. Also called
23718 * for a pending return at the ":endtry" or after returning from an extra
23719 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023720 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023721 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 * FALSE when the return gets pending.
23723 */
23724 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023725do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 exarg_T *eap;
23727 int reanimate;
23728 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023729 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730{
23731 int idx;
23732 struct condstack *cstack = eap->cstack;
23733
23734 if (reanimate)
23735 /* Undo the return. */
23736 current_funccal->returned = FALSE;
23737
23738 /*
23739 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23740 * not in its finally clause (which then is to be executed next) is found.
23741 * In this case, make the ":return" pending for execution at the ":endtry".
23742 * Otherwise, return normally.
23743 */
23744 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23745 if (idx >= 0)
23746 {
23747 cstack->cs_pending[idx] = CSTP_RETURN;
23748
23749 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023750 /* A pending return again gets pending. "rettv" points to an
23751 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023753 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 else
23755 {
23756 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023757 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023759 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023761 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 {
23763 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023764 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023765 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 else
23767 EMSG(_(e_outofmem));
23768 }
23769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023770 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771
23772 if (reanimate)
23773 {
23774 /* The pending return value could be overwritten by a ":return"
23775 * without argument in a finally clause; reset the default
23776 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023777 current_funccal->rettv->v_type = VAR_NUMBER;
23778 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 }
23780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023781 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 }
23783 else
23784 {
23785 current_funccal->returned = TRUE;
23786
23787 /* If the return is carried out now, store the return value. For
23788 * a return immediately after reanimation, the value is already
23789 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023790 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023792 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023793 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023795 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 }
23797 }
23798
23799 return idx < 0;
23800}
23801
23802/*
23803 * Free the variable with a pending return value.
23804 */
23805 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023806discard_pending_return(rettv)
23807 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808{
Bram Moolenaar33570922005-01-25 22:26:29 +000023809 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810}
23811
23812/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023813 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 * is an allocated string. Used by report_pending() for verbose messages.
23815 */
23816 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023817get_return_cmd(rettv)
23818 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023820 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023821 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023822 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023824 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023825 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023826 if (s == NULL)
23827 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023828
23829 STRCPY(IObuff, ":return ");
23830 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23831 if (STRLEN(s) + 8 >= IOSIZE)
23832 STRCPY(IObuff + IOSIZE - 4, "...");
23833 vim_free(tofree);
23834 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835}
23836
23837/*
23838 * Get next function line.
23839 * Called by do_cmdline() to get the next line.
23840 * Returns allocated string, or NULL for end of function.
23841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 char_u *
23843get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023844 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023846 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847{
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023849 ufunc_T *fp = fcp->func;
23850 char_u *retval;
23851 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852
23853 /* If breakpoints have been added/deleted need to check for it. */
23854 if (fcp->dbg_tick != debug_tick)
23855 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023856 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 sourcing_lnum);
23858 fcp->dbg_tick = debug_tick;
23859 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023860#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023861 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023862 func_line_end(cookie);
23863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864
Bram Moolenaar05159a02005-02-26 23:04:13 +000023865 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023866 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23867 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 retval = NULL;
23869 else
23870 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023871 /* Skip NULL lines (continuation lines). */
23872 while (fcp->linenr < gap->ga_len
23873 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23874 ++fcp->linenr;
23875 if (fcp->linenr >= gap->ga_len)
23876 retval = NULL;
23877 else
23878 {
23879 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23880 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023882 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023883 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023884#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
23887
23888 /* Did we encounter a breakpoint? */
23889 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23890 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023891 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023893 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 sourcing_lnum);
23895 fcp->dbg_tick = debug_tick;
23896 }
23897
23898 return retval;
23899}
23900
Bram Moolenaar05159a02005-02-26 23:04:13 +000023901#if defined(FEAT_PROFILE) || defined(PROTO)
23902/*
23903 * Called when starting to read a function line.
23904 * "sourcing_lnum" must be correct!
23905 * When skipping lines it may not actually be executed, but we won't find out
23906 * until later and we need to store the time now.
23907 */
23908 void
23909func_line_start(cookie)
23910 void *cookie;
23911{
23912 funccall_T *fcp = (funccall_T *)cookie;
23913 ufunc_T *fp = fcp->func;
23914
23915 if (fp->uf_profiling && sourcing_lnum >= 1
23916 && sourcing_lnum <= fp->uf_lines.ga_len)
23917 {
23918 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023919 /* Skip continuation lines. */
23920 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23921 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023922 fp->uf_tml_execed = FALSE;
23923 profile_start(&fp->uf_tml_start);
23924 profile_zero(&fp->uf_tml_children);
23925 profile_get_wait(&fp->uf_tml_wait);
23926 }
23927}
23928
23929/*
23930 * Called when actually executing a function line.
23931 */
23932 void
23933func_line_exec(cookie)
23934 void *cookie;
23935{
23936 funccall_T *fcp = (funccall_T *)cookie;
23937 ufunc_T *fp = fcp->func;
23938
23939 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23940 fp->uf_tml_execed = TRUE;
23941}
23942
23943/*
23944 * Called when done with a function line.
23945 */
23946 void
23947func_line_end(cookie)
23948 void *cookie;
23949{
23950 funccall_T *fcp = (funccall_T *)cookie;
23951 ufunc_T *fp = fcp->func;
23952
23953 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23954 {
23955 if (fp->uf_tml_execed)
23956 {
23957 ++fp->uf_tml_count[fp->uf_tml_idx];
23958 profile_end(&fp->uf_tml_start);
23959 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023960 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023961 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23962 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023963 }
23964 fp->uf_tml_idx = -1;
23965 }
23966}
23967#endif
23968
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969/*
23970 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023971 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 */
23973 int
23974func_has_ended(cookie)
23975 void *cookie;
23976{
Bram Moolenaar33570922005-01-25 22:26:29 +000023977 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978
23979 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23980 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023981 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 || fcp->returned);
23983}
23984
23985/*
23986 * return TRUE if cookie indicates a function which "abort"s on errors.
23987 */
23988 int
23989func_has_abort(cookie)
23990 void *cookie;
23991{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023992 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993}
23994
23995#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23996typedef enum
23997{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023998 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23999 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24000 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001} var_flavour_T;
24002
24003static var_flavour_T var_flavour __ARGS((char_u *varname));
24004
24005 static var_flavour_T
24006var_flavour(varname)
24007 char_u *varname;
24008{
24009 char_u *p = varname;
24010
24011 if (ASCII_ISUPPER(*p))
24012 {
24013 while (*(++p))
24014 if (ASCII_ISLOWER(*p))
24015 return VAR_FLAVOUR_SESSION;
24016 return VAR_FLAVOUR_VIMINFO;
24017 }
24018 else
24019 return VAR_FLAVOUR_DEFAULT;
24020}
24021#endif
24022
24023#if defined(FEAT_VIMINFO) || defined(PROTO)
24024/*
24025 * Restore global vars that start with a capital from the viminfo file
24026 */
24027 int
24028read_viminfo_varlist(virp, writing)
24029 vir_T *virp;
24030 int writing;
24031{
24032 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024033 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024034 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035
24036 if (!writing && (find_viminfo_parameter('!') != NULL))
24037 {
24038 tab = vim_strchr(virp->vir_line + 1, '\t');
24039 if (tab != NULL)
24040 {
24041 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024042 switch (*tab)
24043 {
24044 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024045#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024046 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024047#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024048 case 'D': type = VAR_DICT; break;
24049 case 'L': type = VAR_LIST; break;
24050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051
24052 tab = vim_strchr(tab, '\t');
24053 if (tab != NULL)
24054 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024055 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024056 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024057 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024059#ifdef FEAT_FLOAT
24060 else if (type == VAR_FLOAT)
24061 (void)string2float(tab + 1, &tv.vval.v_float);
24062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024064 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024065 if (type == VAR_DICT || type == VAR_LIST)
24066 {
24067 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24068
24069 if (etv == NULL)
24070 /* Failed to parse back the dict or list, use it as a
24071 * string. */
24072 tv.v_type = VAR_STRING;
24073 else
24074 {
24075 vim_free(tv.vval.v_string);
24076 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024077 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024078 }
24079 }
24080
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024081 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024082
24083 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024084 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024085 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24086 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 }
24088 }
24089 }
24090
24091 return viminfo_readline(virp);
24092}
24093
24094/*
24095 * Write global vars that start with a capital to the viminfo file
24096 */
24097 void
24098write_viminfo_varlist(fp)
24099 FILE *fp;
24100{
Bram Moolenaar33570922005-01-25 22:26:29 +000024101 hashitem_T *hi;
24102 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024103 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024104 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024105 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024106 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024107 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108
24109 if (find_viminfo_parameter('!') == NULL)
24110 return;
24111
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024112 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024114 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024115 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024117 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024119 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024120 this_var = HI2DI(hi);
24121 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024122 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024123 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024124 {
24125 case VAR_STRING: s = "STR"; break;
24126 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024127#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024128 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024129#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024130 case VAR_DICT: s = "DIC"; break;
24131 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024132 default: continue;
24133 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024134 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024135 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024136 if (p != NULL)
24137 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024138 vim_free(tofree);
24139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 }
24141 }
24142}
24143#endif
24144
24145#if defined(FEAT_SESSION) || defined(PROTO)
24146 int
24147store_session_globals(fd)
24148 FILE *fd;
24149{
Bram Moolenaar33570922005-01-25 22:26:29 +000024150 hashitem_T *hi;
24151 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024152 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 char_u *p, *t;
24154
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024155 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024156 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024158 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024160 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 this_var = HI2DI(hi);
24162 if ((this_var->di_tv.v_type == VAR_NUMBER
24163 || this_var->di_tv.v_type == VAR_STRING)
24164 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024165 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024166 /* Escape special characters with a backslash. Turn a LF and
24167 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024168 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024169 (char_u *)"\\\"\n\r");
24170 if (p == NULL) /* out of memory */
24171 break;
24172 for (t = p; *t != NUL; ++t)
24173 if (*t == '\n')
24174 *t = 'n';
24175 else if (*t == '\r')
24176 *t = 'r';
24177 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 this_var->di_key,
24179 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24180 : ' ',
24181 p,
24182 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24183 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024184 || put_eol(fd) == FAIL)
24185 {
24186 vim_free(p);
24187 return FAIL;
24188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 vim_free(p);
24190 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024191#ifdef FEAT_FLOAT
24192 else if (this_var->di_tv.v_type == VAR_FLOAT
24193 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24194 {
24195 float_T f = this_var->di_tv.vval.v_float;
24196 int sign = ' ';
24197
24198 if (f < 0)
24199 {
24200 f = -f;
24201 sign = '-';
24202 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024203 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024204 this_var->di_key, sign, f) < 0)
24205 || put_eol(fd) == FAIL)
24206 return FAIL;
24207 }
24208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 }
24210 }
24211 return OK;
24212}
24213#endif
24214
Bram Moolenaar661b1822005-07-28 22:36:45 +000024215/*
24216 * Display script name where an item was last set.
24217 * Should only be invoked when 'verbose' is non-zero.
24218 */
24219 void
24220last_set_msg(scriptID)
24221 scid_T scriptID;
24222{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024223 char_u *p;
24224
Bram Moolenaar661b1822005-07-28 22:36:45 +000024225 if (scriptID != 0)
24226 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024227 p = home_replace_save(NULL, get_scriptname(scriptID));
24228 if (p != NULL)
24229 {
24230 verbose_enter();
24231 MSG_PUTS(_("\n\tLast set from "));
24232 MSG_PUTS(p);
24233 vim_free(p);
24234 verbose_leave();
24235 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024236 }
24237}
24238
Bram Moolenaard812df62008-11-09 12:46:09 +000024239/*
24240 * List v:oldfiles in a nice way.
24241 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024242 void
24243ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024244 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024245{
24246 list_T *l = vimvars[VV_OLDFILES].vv_list;
24247 listitem_T *li;
24248 int nr = 0;
24249
24250 if (l == NULL)
24251 msg((char_u *)_("No old files"));
24252 else
24253 {
24254 msg_start();
24255 msg_scroll = TRUE;
24256 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24257 {
24258 msg_outnum((long)++nr);
24259 MSG_PUTS(": ");
24260 msg_outtrans(get_tv_string(&li->li_tv));
24261 msg_putchar('\n');
24262 out_flush(); /* output one line at a time */
24263 ui_breakcheck();
24264 }
24265 /* Assume "got_int" was set to truncate the listing. */
24266 got_int = FALSE;
24267
24268#ifdef FEAT_BROWSE_CMD
24269 if (cmdmod.browse)
24270 {
24271 quit_more = FALSE;
24272 nr = prompt_for_number(FALSE);
24273 msg_starthere();
24274 if (nr > 0)
24275 {
24276 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24277 (long)nr);
24278
24279 if (p != NULL)
24280 {
24281 p = expand_env_save(p);
24282 eap->arg = p;
24283 eap->cmdidx = CMD_edit;
24284 cmdmod.browse = FALSE;
24285 do_exedit(eap, NULL);
24286 vim_free(p);
24287 }
24288 }
24289 }
24290#endif
24291 }
24292}
24293
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294#endif /* FEAT_EVAL */
24295
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024297#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298
24299#ifdef WIN3264
24300/*
24301 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24302 */
24303static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24304static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24305static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24306
24307/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024308 * Get the short path (8.3) for the filename in "fnamep".
24309 * Only works for a valid file name.
24310 * When the path gets longer "fnamep" is changed and the allocated buffer
24311 * is put in "bufp".
24312 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24313 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 */
24315 static int
24316get_short_pathname(fnamep, bufp, fnamelen)
24317 char_u **fnamep;
24318 char_u **bufp;
24319 int *fnamelen;
24320{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024321 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 char_u *newbuf;
24323
24324 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 l = GetShortPathName(*fnamep, *fnamep, len);
24326 if (l > len - 1)
24327 {
24328 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024329 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 newbuf = vim_strnsave(*fnamep, l);
24331 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333
24334 vim_free(*bufp);
24335 *fnamep = *bufp = newbuf;
24336
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024337 /* Really should always succeed, as the buffer is big enough. */
24338 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 }
24340
24341 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024342 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343}
24344
24345/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024346 * Get the short path (8.3) for the filename in "fname". The converted
24347 * path is returned in "bufp".
24348 *
24349 * Some of the directories specified in "fname" may not exist. This function
24350 * will shorten the existing directories at the beginning of the path and then
24351 * append the remaining non-existing path.
24352 *
24353 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024354 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024355 * bufp - Pointer to an allocated buffer for the filename.
24356 * fnamelen - Length of the filename pointed to by fname
24357 *
24358 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 */
24360 static int
24361shortpath_for_invalid_fname(fname, bufp, fnamelen)
24362 char_u **fname;
24363 char_u **bufp;
24364 int *fnamelen;
24365{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024366 char_u *short_fname, *save_fname, *pbuf_unused;
24367 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024369 int old_len, len;
24370 int new_len, sfx_len;
24371 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372
24373 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024374 old_len = *fnamelen;
24375 save_fname = vim_strnsave(*fname, old_len);
24376 pbuf_unused = NULL;
24377 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024379 endp = save_fname + old_len - 1; /* Find the end of the copy */
24380 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024382 /*
24383 * Try shortening the supplied path till it succeeds by removing one
24384 * directory at a time from the tail of the path.
24385 */
24386 len = 0;
24387 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024389 /* go back one path-separator */
24390 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24391 --endp;
24392 if (endp <= save_fname)
24393 break; /* processed the complete path */
24394
24395 /*
24396 * Replace the path separator with a NUL and try to shorten the
24397 * resulting path.
24398 */
24399 ch = *endp;
24400 *endp = 0;
24401 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024402 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024403 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24404 {
24405 retval = FAIL;
24406 goto theend;
24407 }
24408 *endp = ch; /* preserve the string */
24409
24410 if (len > 0)
24411 break; /* successfully shortened the path */
24412
24413 /* failed to shorten the path. Skip the path separator */
24414 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 }
24416
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024417 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024419 /*
24420 * Succeeded in shortening the path. Now concatenate the shortened
24421 * path with the remaining path at the tail.
24422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024424 /* Compute the length of the new path. */
24425 sfx_len = (int)(save_endp - endp) + 1;
24426 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024428 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024430 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024432 /* There is not enough space in the currently allocated string,
24433 * copy it to a buffer big enough. */
24434 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024436 {
24437 retval = FAIL;
24438 goto theend;
24439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 }
24441 else
24442 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024443 /* Transfer short_fname to the main buffer (it's big enough),
24444 * unless get_short_pathname() did its work in-place. */
24445 *fname = *bufp = save_fname;
24446 if (short_fname != save_fname)
24447 vim_strncpy(save_fname, short_fname, len);
24448 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024450
24451 /* concat the not-shortened part of the path */
24452 vim_strncpy(*fname + len, endp, sfx_len);
24453 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024455
24456theend:
24457 vim_free(pbuf_unused);
24458 vim_free(save_fname);
24459
24460 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461}
24462
24463/*
24464 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024465 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 */
24467 static int
24468shortpath_for_partial(fnamep, bufp, fnamelen)
24469 char_u **fnamep;
24470 char_u **bufp;
24471 int *fnamelen;
24472{
24473 int sepcount, len, tflen;
24474 char_u *p;
24475 char_u *pbuf, *tfname;
24476 int hasTilde;
24477
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024478 /* Count up the path separators from the RHS.. so we know which part
24479 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024481 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482 if (vim_ispathsep(*p))
24483 ++sepcount;
24484
24485 /* Need full path first (use expand_env() to remove a "~/") */
24486 hasTilde = (**fnamep == '~');
24487 if (hasTilde)
24488 pbuf = tfname = expand_env_save(*fnamep);
24489 else
24490 pbuf = tfname = FullName_save(*fnamep, FALSE);
24491
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024492 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024494 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496
24497 if (len == 0)
24498 {
24499 /* Don't have a valid filename, so shorten the rest of the
24500 * path if we can. This CAN give us invalid 8.3 filenames, but
24501 * there's not a lot of point in guessing what it might be.
24502 */
24503 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024504 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 }
24507
24508 /* Count the paths backward to find the beginning of the desired string. */
24509 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024510 {
24511#ifdef FEAT_MBYTE
24512 if (has_mbyte)
24513 p -= mb_head_off(tfname, p);
24514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 if (vim_ispathsep(*p))
24516 {
24517 if (sepcount == 0 || (hasTilde && sepcount == 1))
24518 break;
24519 else
24520 sepcount --;
24521 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 if (hasTilde)
24524 {
24525 --p;
24526 if (p >= tfname)
24527 *p = '~';
24528 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 }
24531 else
24532 ++p;
24533
24534 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24535 vim_free(*bufp);
24536 *fnamelen = (int)STRLEN(p);
24537 *bufp = pbuf;
24538 *fnamep = p;
24539
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024540 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541}
24542#endif /* WIN3264 */
24543
24544/*
24545 * Adjust a filename, according to a string of modifiers.
24546 * *fnamep must be NUL terminated when called. When returning, the length is
24547 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024548 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 * When there is an error, *fnamep is set to NULL.
24550 */
24551 int
24552modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24553 char_u *src; /* string with modifiers */
24554 int *usedlen; /* characters after src that are used */
24555 char_u **fnamep; /* file name so far */
24556 char_u **bufp; /* buffer for allocated file name or NULL */
24557 int *fnamelen; /* length of fnamep */
24558{
24559 int valid = 0;
24560 char_u *tail;
24561 char_u *s, *p, *pbuf;
24562 char_u dirname[MAXPATHL];
24563 int c;
24564 int has_fullname = 0;
24565#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024566 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 int has_shortname = 0;
24568#endif
24569
24570repeat:
24571 /* ":p" - full path/file_name */
24572 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24573 {
24574 has_fullname = 1;
24575
24576 valid |= VALID_PATH;
24577 *usedlen += 2;
24578
24579 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24580 if ((*fnamep)[0] == '~'
24581#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24582 && ((*fnamep)[1] == '/'
24583# ifdef BACKSLASH_IN_FILENAME
24584 || (*fnamep)[1] == '\\'
24585# endif
24586 || (*fnamep)[1] == NUL)
24587
24588#endif
24589 )
24590 {
24591 *fnamep = expand_env_save(*fnamep);
24592 vim_free(*bufp); /* free any allocated file name */
24593 *bufp = *fnamep;
24594 if (*fnamep == NULL)
24595 return -1;
24596 }
24597
24598 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024599 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 {
24601 if (vim_ispathsep(*p)
24602 && p[1] == '.'
24603 && (p[2] == NUL
24604 || vim_ispathsep(p[2])
24605 || (p[2] == '.'
24606 && (p[3] == NUL || vim_ispathsep(p[3])))))
24607 break;
24608 }
24609
24610 /* FullName_save() is slow, don't use it when not needed. */
24611 if (*p != NUL || !vim_isAbsName(*fnamep))
24612 {
24613 *fnamep = FullName_save(*fnamep, *p != NUL);
24614 vim_free(*bufp); /* free any allocated file name */
24615 *bufp = *fnamep;
24616 if (*fnamep == NULL)
24617 return -1;
24618 }
24619
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024620#ifdef WIN3264
24621# if _WIN32_WINNT >= 0x0500
24622 if (vim_strchr(*fnamep, '~') != NULL)
24623 {
24624 /* Expand 8.3 filename to full path. Needed to make sure the same
24625 * file does not have two different names.
24626 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24627 p = alloc(_MAX_PATH + 1);
24628 if (p != NULL)
24629 {
24630 if (GetLongPathName(*fnamep, p, MAXPATHL))
24631 {
24632 vim_free(*bufp);
24633 *bufp = *fnamep = p;
24634 }
24635 else
24636 vim_free(p);
24637 }
24638 }
24639# endif
24640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 /* Append a path separator to a directory. */
24642 if (mch_isdir(*fnamep))
24643 {
24644 /* Make room for one or two extra characters. */
24645 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24646 vim_free(*bufp); /* free any allocated file name */
24647 *bufp = *fnamep;
24648 if (*fnamep == NULL)
24649 return -1;
24650 add_pathsep(*fnamep);
24651 }
24652 }
24653
24654 /* ":." - path relative to the current directory */
24655 /* ":~" - path relative to the home directory */
24656 /* ":8" - shortname path - postponed till after */
24657 while (src[*usedlen] == ':'
24658 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24659 {
24660 *usedlen += 2;
24661 if (c == '8')
24662 {
24663#ifdef WIN3264
24664 has_shortname = 1; /* Postpone this. */
24665#endif
24666 continue;
24667 }
24668 pbuf = NULL;
24669 /* Need full path first (use expand_env() to remove a "~/") */
24670 if (!has_fullname)
24671 {
24672 if (c == '.' && **fnamep == '~')
24673 p = pbuf = expand_env_save(*fnamep);
24674 else
24675 p = pbuf = FullName_save(*fnamep, FALSE);
24676 }
24677 else
24678 p = *fnamep;
24679
24680 has_fullname = 0;
24681
24682 if (p != NULL)
24683 {
24684 if (c == '.')
24685 {
24686 mch_dirname(dirname, MAXPATHL);
24687 s = shorten_fname(p, dirname);
24688 if (s != NULL)
24689 {
24690 *fnamep = s;
24691 if (pbuf != NULL)
24692 {
24693 vim_free(*bufp); /* free any allocated file name */
24694 *bufp = pbuf;
24695 pbuf = NULL;
24696 }
24697 }
24698 }
24699 else
24700 {
24701 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24702 /* Only replace it when it starts with '~' */
24703 if (*dirname == '~')
24704 {
24705 s = vim_strsave(dirname);
24706 if (s != NULL)
24707 {
24708 *fnamep = s;
24709 vim_free(*bufp);
24710 *bufp = s;
24711 }
24712 }
24713 }
24714 vim_free(pbuf);
24715 }
24716 }
24717
24718 tail = gettail(*fnamep);
24719 *fnamelen = (int)STRLEN(*fnamep);
24720
24721 /* ":h" - head, remove "/file_name", can be repeated */
24722 /* Don't remove the first "/" or "c:\" */
24723 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24724 {
24725 valid |= VALID_HEAD;
24726 *usedlen += 2;
24727 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024728 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024729 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 *fnamelen = (int)(tail - *fnamep);
24731#ifdef VMS
24732 if (*fnamelen > 0)
24733 *fnamelen += 1; /* the path separator is part of the path */
24734#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024735 if (*fnamelen == 0)
24736 {
24737 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24738 p = vim_strsave((char_u *)".");
24739 if (p == NULL)
24740 return -1;
24741 vim_free(*bufp);
24742 *bufp = *fnamep = tail = p;
24743 *fnamelen = 1;
24744 }
24745 else
24746 {
24747 while (tail > s && !after_pathsep(s, tail))
24748 mb_ptr_back(*fnamep, tail);
24749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 }
24751
24752 /* ":8" - shortname */
24753 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24754 {
24755 *usedlen += 2;
24756#ifdef WIN3264
24757 has_shortname = 1;
24758#endif
24759 }
24760
24761#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024762 /*
24763 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 */
24765 if (has_shortname)
24766 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024767 /* Copy the string if it is shortened by :h and when it wasn't copied
24768 * yet, because we are going to change it in place. Avoids changing
24769 * the buffer name for "%:8". */
24770 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 {
24772 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024773 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 return -1;
24775 vim_free(*bufp);
24776 *bufp = *fnamep = p;
24777 }
24778
24779 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024780 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 if (!has_fullname && !vim_isAbsName(*fnamep))
24782 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024783 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 return -1;
24785 }
24786 else
24787 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024788 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789
Bram Moolenaardc935552011-08-17 15:23:23 +020024790 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024792 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 return -1;
24794
24795 if (l == 0)
24796 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024797 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024799 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 return -1;
24801 }
24802 *fnamelen = l;
24803 }
24804 }
24805#endif /* WIN3264 */
24806
24807 /* ":t" - tail, just the basename */
24808 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24809 {
24810 *usedlen += 2;
24811 *fnamelen -= (int)(tail - *fnamep);
24812 *fnamep = tail;
24813 }
24814
24815 /* ":e" - extension, can be repeated */
24816 /* ":r" - root, without extension, can be repeated */
24817 while (src[*usedlen] == ':'
24818 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24819 {
24820 /* find a '.' in the tail:
24821 * - for second :e: before the current fname
24822 * - otherwise: The last '.'
24823 */
24824 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24825 s = *fnamep - 2;
24826 else
24827 s = *fnamep + *fnamelen - 1;
24828 for ( ; s > tail; --s)
24829 if (s[0] == '.')
24830 break;
24831 if (src[*usedlen + 1] == 'e') /* :e */
24832 {
24833 if (s > tail)
24834 {
24835 *fnamelen += (int)(*fnamep - (s + 1));
24836 *fnamep = s + 1;
24837#ifdef VMS
24838 /* cut version from the extension */
24839 s = *fnamep + *fnamelen - 1;
24840 for ( ; s > *fnamep; --s)
24841 if (s[0] == ';')
24842 break;
24843 if (s > *fnamep)
24844 *fnamelen = s - *fnamep;
24845#endif
24846 }
24847 else if (*fnamep <= tail)
24848 *fnamelen = 0;
24849 }
24850 else /* :r */
24851 {
24852 if (s > tail) /* remove one extension */
24853 *fnamelen = (int)(s - *fnamep);
24854 }
24855 *usedlen += 2;
24856 }
24857
24858 /* ":s?pat?foo?" - substitute */
24859 /* ":gs?pat?foo?" - global substitute */
24860 if (src[*usedlen] == ':'
24861 && (src[*usedlen + 1] == 's'
24862 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24863 {
24864 char_u *str;
24865 char_u *pat;
24866 char_u *sub;
24867 int sep;
24868 char_u *flags;
24869 int didit = FALSE;
24870
24871 flags = (char_u *)"";
24872 s = src + *usedlen + 2;
24873 if (src[*usedlen + 1] == 'g')
24874 {
24875 flags = (char_u *)"g";
24876 ++s;
24877 }
24878
24879 sep = *s++;
24880 if (sep)
24881 {
24882 /* find end of pattern */
24883 p = vim_strchr(s, sep);
24884 if (p != NULL)
24885 {
24886 pat = vim_strnsave(s, (int)(p - s));
24887 if (pat != NULL)
24888 {
24889 s = p + 1;
24890 /* find end of substitution */
24891 p = vim_strchr(s, sep);
24892 if (p != NULL)
24893 {
24894 sub = vim_strnsave(s, (int)(p - s));
24895 str = vim_strnsave(*fnamep, *fnamelen);
24896 if (sub != NULL && str != NULL)
24897 {
24898 *usedlen = (int)(p + 1 - src);
24899 s = do_string_sub(str, pat, sub, flags);
24900 if (s != NULL)
24901 {
24902 *fnamep = s;
24903 *fnamelen = (int)STRLEN(s);
24904 vim_free(*bufp);
24905 *bufp = s;
24906 didit = TRUE;
24907 }
24908 }
24909 vim_free(sub);
24910 vim_free(str);
24911 }
24912 vim_free(pat);
24913 }
24914 }
24915 /* after using ":s", repeat all the modifiers */
24916 if (didit)
24917 goto repeat;
24918 }
24919 }
24920
Bram Moolenaar26df0922014-02-23 23:39:13 +010024921 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24922 {
24923 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24924 if (p == NULL)
24925 return -1;
24926 vim_free(*bufp);
24927 *bufp = *fnamep = p;
24928 *fnamelen = (int)STRLEN(p);
24929 *usedlen += 2;
24930 }
24931
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 return valid;
24933}
24934
24935/*
24936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24937 * "flags" can be "g" to do a global substitute.
24938 * Returns an allocated string, NULL for error.
24939 */
24940 char_u *
24941do_string_sub(str, pat, sub, flags)
24942 char_u *str;
24943 char_u *pat;
24944 char_u *sub;
24945 char_u *flags;
24946{
24947 int sublen;
24948 regmatch_T regmatch;
24949 int i;
24950 int do_all;
24951 char_u *tail;
24952 garray_T ga;
24953 char_u *ret;
24954 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024955 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956
24957 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24958 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024959 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960
24961 ga_init2(&ga, 1, 200);
24962
24963 do_all = (flags[0] == 'g');
24964
24965 regmatch.rm_ic = p_ic;
24966 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24967 if (regmatch.regprog != NULL)
24968 {
24969 tail = str;
24970 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24971 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024972 /* Skip empty match except for first match. */
24973 if (regmatch.startp[0] == regmatch.endp[0])
24974 {
24975 if (zero_width == regmatch.startp[0])
24976 {
24977 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020024978 i = MB_PTR2LEN(tail);
24979 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
24980 (size_t)i);
24981 ga.ga_len += i;
24982 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024983 continue;
24984 }
24985 zero_width = regmatch.startp[0];
24986 }
24987
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 /*
24989 * Get some space for a temporary buffer to do the substitution
24990 * into. It will contain:
24991 * - The text up to where the match is.
24992 * - The substituted text.
24993 * - The text after the match.
24994 */
24995 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24996 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24997 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24998 {
24999 ga_clear(&ga);
25000 break;
25001 }
25002
25003 /* copy the text up to where the match is */
25004 i = (int)(regmatch.startp[0] - tail);
25005 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25006 /* add the substituted text */
25007 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25008 + ga.ga_len + i, TRUE, TRUE, FALSE);
25009 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025010 tail = regmatch.endp[0];
25011 if (*tail == NUL)
25012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 if (!do_all)
25014 break;
25015 }
25016
25017 if (ga.ga_data != NULL)
25018 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25019
Bram Moolenaar473de612013-06-08 18:19:48 +020025020 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 }
25022
25023 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25024 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025025 if (p_cpo == empty_option)
25026 p_cpo = save_cpo;
25027 else
25028 /* Darn, evaluating {sub} expression changed the value. */
25029 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030
25031 return ret;
25032}
25033
25034#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */