blob: 4e512af648580558d9d6aa232d629220ce2f320b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000368};
369
370/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_type vv_di.di_tv.v_type
372#define vv_nr vv_di.di_tv.vval.v_number
373#define vv_float vv_di.di_tv.vval.v_float
374#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000375#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000376#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200378static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000429static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000431static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000435static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100436static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000438static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200439static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
447static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000449#ifdef FEAT_FLOAT
450static int string2float __ARGS((char_u *text, float_T *value));
451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100454static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200456static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000458static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200462static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100465static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200469static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200472static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100485static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100487static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
490static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000492static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000495static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000497#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000498static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000499static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200506static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
511static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200521static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200557static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000565static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000566static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200567static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000568static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000569static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200572static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000573static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
805static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000806static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000809static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
2811 else if (var_check_ro(lp->ll_di->di_flags, name))
2812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
2944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
2948 /*
2949 * Assign the List values to the list items.
2950 */
2951 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 if (op != NULL && *op != '=')
2954 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2955 else
2956 {
2957 clear_tv(&lp->ll_li->li_tv);
2958 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 ri = ri->li_next;
2961 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2962 break;
2963 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002966 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 ri = NULL;
2969 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002970 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 lp->ll_li = lp->ll_li->li_next;
2973 ++lp->ll_n1;
2974 }
2975 if (ri != NULL)
2976 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 else if (lp->ll_empty2
2978 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 : lp->ll_n1 != lp->ll_n2)
2980 EMSG(_("E711: List value has not enough items"));
2981 }
2982 else
2983 {
2984 /*
2985 * Assign to a List or Dictionary item.
2986 */
2987 if (lp->ll_newkey != NULL)
2988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 if (op != NULL && *op != '=')
2990 {
2991 EMSG2(_(e_letwrong), op);
2992 return;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002996 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 if (di == NULL)
2998 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002999 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3000 {
3001 vim_free(di);
3002 return;
3003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 else if (op != NULL && *op != '=')
3007 {
3008 tv_op(lp->ll_tv, rettv, op);
3009 return;
3010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003011 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 /*
3015 * Assign the value to the variable or list item.
3016 */
3017 if (copy)
3018 copy_tv(rettv, lp->ll_tv);
3019 else
3020 {
3021 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003022 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024 }
3025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003026}
3027
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3030 * Returns OK or FAIL.
3031 */
3032 static int
3033tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 typval_T *tv1;
3035 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 char_u *op;
3037{
3038 long n;
3039 char_u numbuf[NUMBUFLEN];
3040 char_u *s;
3041
3042 /* Can't do anything with a Funcref or a Dict on the right. */
3043 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3044 {
3045 switch (tv1->v_type)
3046 {
3047 case VAR_DICT:
3048 case VAR_FUNC:
3049 break;
3050
3051 case VAR_LIST:
3052 if (*op != '+' || tv2->v_type != VAR_LIST)
3053 break;
3054 /* List += List */
3055 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3056 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3057 return OK;
3058
3059 case VAR_NUMBER:
3060 case VAR_STRING:
3061 if (tv2->v_type == VAR_LIST)
3062 break;
3063 if (*op == '+' || *op == '-')
3064 {
3065 /* nr += nr or nr -= nr*/
3066 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#ifdef FEAT_FLOAT
3068 if (tv2->v_type == VAR_FLOAT)
3069 {
3070 float_T f = n;
3071
3072 if (*op == '+')
3073 f += tv2->vval.v_float;
3074 else
3075 f -= tv2->vval.v_float;
3076 clear_tv(tv1);
3077 tv1->v_type = VAR_FLOAT;
3078 tv1->vval.v_float = f;
3079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003081#endif
3082 {
3083 if (*op == '+')
3084 n += get_tv_number(tv2);
3085 else
3086 n -= get_tv_number(tv2);
3087 clear_tv(tv1);
3088 tv1->v_type = VAR_NUMBER;
3089 tv1->vval.v_number = n;
3090 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 }
3092 else
3093 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 if (tv2->v_type == VAR_FLOAT)
3095 break;
3096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 /* str .= str */
3098 s = get_tv_string(tv1);
3099 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3100 clear_tv(tv1);
3101 tv1->v_type = VAR_STRING;
3102 tv1->vval.v_string = s;
3103 }
3104 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105
3106#ifdef FEAT_FLOAT
3107 case VAR_FLOAT:
3108 {
3109 float_T f;
3110
3111 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3112 && tv2->v_type != VAR_NUMBER
3113 && tv2->v_type != VAR_STRING))
3114 break;
3115 if (tv2->v_type == VAR_FLOAT)
3116 f = tv2->vval.v_float;
3117 else
3118 f = get_tv_number(tv2);
3119 if (*op == '+')
3120 tv1->vval.v_float += f;
3121 else
3122 tv1->vval.v_float -= f;
3123 }
3124 return OK;
3125#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 }
3127 }
3128
3129 EMSG2(_(e_letwrong), op);
3130 return FAIL;
3131}
3132
3133/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 * Add a watcher to a list.
3135 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003136 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 list_T *l;
3139 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140{
3141 lw->lw_next = l->lv_watch;
3142 l->lv_watch = lw;
3143}
3144
3145/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003146 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 * No warning when it isn't found...
3148 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003149 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 list_T *l;
3152 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155
3156 lwp = &l->lv_watch;
3157 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3158 {
3159 if (lw == lwrem)
3160 {
3161 *lwp = lw->lw_next;
3162 break;
3163 }
3164 lwp = &lw->lw_next;
3165 }
3166}
3167
3168/*
3169 * Just before removing an item from a list: advance watchers to the next
3170 * item.
3171 */
3172 static void
3173list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 list_T *l;
3175 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176{
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3180 if (lw->lw_item == item)
3181 lw->lw_item = item->li_next;
3182}
3183
3184/*
3185 * Evaluate the expression used in a ":for var in expr" command.
3186 * "arg" points to "var".
3187 * Set "*errp" to TRUE for an error, FALSE otherwise;
3188 * Return a pointer that holds the info. Null when there is an error.
3189 */
3190 void *
3191eval_for_line(arg, errp, nextcmdp, skip)
3192 char_u *arg;
3193 int *errp;
3194 char_u **nextcmdp;
3195 int skip;
3196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 typval_T tv;
3200 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 *errp = TRUE; /* default: there is an error */
3203
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 if (fi == NULL)
3206 return NULL;
3207
3208 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3209 if (expr == NULL)
3210 return fi;
3211
3212 expr = skipwhite(expr);
3213 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3214 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003215 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 return fi;
3217 }
3218
3219 if (skip)
3220 ++emsg_skip;
3221 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3222 {
3223 *errp = FALSE;
3224 if (!skip)
3225 {
3226 l = tv.vval.v_list;
3227 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003230 clear_tv(&tv);
3231 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 else
3233 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003234 /* No need to increment the refcount, it's already set for the
3235 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 fi->fi_list = l;
3237 list_add_watch(l, &fi->fi_lw);
3238 fi->fi_lw.lw_item = l->lv_first;
3239 }
3240 }
3241 }
3242 if (skip)
3243 --emsg_skip;
3244
3245 return fi;
3246}
3247
3248/*
3249 * Use the first item in a ":for" list. Advance to the next.
3250 * Assign the values to the variable (list). "arg" points to the first one.
3251 * Return TRUE when a valid item was found, FALSE when at end of list or
3252 * something wrong.
3253 */
3254 int
3255next_for_item(fi_void, arg)
3256 void *fi_void;
3257 char_u *arg;
3258{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003259 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262
3263 item = fi->fi_lw.lw_item;
3264 if (item == NULL)
3265 result = FALSE;
3266 else
3267 {
3268 fi->fi_lw.lw_item = item->li_next;
3269 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3270 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3271 }
3272 return result;
3273}
3274
3275/*
3276 * Free the structure used to store info used by ":for".
3277 */
3278 void
3279free_for_info(fi_void)
3280 void *fi_void;
3281{
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003284 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 list_unref(fi->fi_list);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 vim_free(fi);
3290}
3291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3293
3294 void
3295set_context_for_expression(xp, arg, cmdidx)
3296 expand_T *xp;
3297 char_u *arg;
3298 cmdidx_T cmdidx;
3299{
3300 int got_eq = FALSE;
3301 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 if (cmdidx == CMD_let)
3305 {
3306 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003307 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 {
3309 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003310 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 {
3312 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003313 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 if (vim_iswhite(*p))
3315 break;
3316 }
3317 return;
3318 }
3319 }
3320 else
3321 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3322 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 while ((xp->xp_pattern = vim_strpbrk(arg,
3324 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3325 {
3326 c = *xp->xp_pattern;
3327 if (c == '&')
3328 {
3329 c = xp->xp_pattern[1];
3330 if (c == '&')
3331 {
3332 ++xp->xp_pattern;
3333 xp->xp_context = cmdidx != CMD_let || got_eq
3334 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3335 }
3336 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003339 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3340 xp->xp_pattern += 2;
3341
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 else if (c == '$')
3345 {
3346 /* environment variable */
3347 xp->xp_context = EXPAND_ENV_VARS;
3348 }
3349 else if (c == '=')
3350 {
3351 got_eq = TRUE;
3352 xp->xp_context = EXPAND_EXPRESSION;
3353 }
3354 else if (c == '<'
3355 && xp->xp_context == EXPAND_FUNCTIONS
3356 && vim_strchr(xp->xp_pattern, '(') == NULL)
3357 {
3358 /* Function name can start with "<SNR>" */
3359 break;
3360 }
3361 else if (cmdidx != CMD_let || got_eq)
3362 {
3363 if (c == '"') /* string */
3364 {
3365 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3366 if (c == '\\' && xp->xp_pattern[1] != NUL)
3367 ++xp->xp_pattern;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '\'') /* literal string */
3371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3374 /* skip */ ;
3375 xp->xp_context = EXPAND_NOTHING;
3376 }
3377 else if (c == '|')
3378 {
3379 if (xp->xp_pattern[1] == '|')
3380 {
3381 ++xp->xp_pattern;
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
3384 else
3385 xp->xp_context = EXPAND_COMMANDS;
3386 }
3387 else
3388 xp->xp_context = EXPAND_EXPRESSION;
3389 }
3390 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 /* Doesn't look like something valid, expand as an expression
3392 * anyway. */
3393 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 arg = xp->xp_pattern;
3395 if (*arg != NUL)
3396 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3397 /* skip */ ;
3398 }
3399 xp->xp_pattern = arg;
3400}
3401
3402#endif /* FEAT_CMDL_COMPL */
3403
3404/*
3405 * ":1,25call func(arg1, arg2)" function call.
3406 */
3407 void
3408ex_call(eap)
3409 exarg_T *eap;
3410{
3411 char_u *arg = eap->arg;
3412 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 linenr_T lnum;
3418 int doesrange;
3419 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003420 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003422 if (eap->skip)
3423 {
3424 /* trans_function_name() doesn't work well when skipping, use eval0()
3425 * instead to skip to any following command, e.g. for:
3426 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003427 ++emsg_skip;
3428 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3429 clear_tv(&rettv);
3430 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003431 return;
3432 }
3433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003435 if (fudi.fd_newkey != NULL)
3436 {
3437 /* Still need to give an error message for missing key. */
3438 EMSG2(_(e_dictkey), fudi.fd_newkey);
3439 vim_free(fudi.fd_newkey);
3440 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003441 if (tofree == NULL)
3442 return;
3443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003444 /* Increase refcount on dictionary, it could get deleted when evaluating
3445 * the arguments. */
3446 if (fudi.fd_dict != NULL)
3447 ++fudi.fd_dict->dv_refcount;
3448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003449 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003450 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003451 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar532c7802005-01-27 14:44:31 +00003453 /* Skip white space to allow ":call func ()". Not good, but required for
3454 * backward compatibility. */
3455 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 if (*startarg != '(')
3459 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003460 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 goto end;
3462 }
3463
3464 /*
3465 * When skipping, evaluate the function once, to find the end of the
3466 * arguments.
3467 * When the function takes a range, this is discovered after the first
3468 * call, and the loop is broken.
3469 */
3470 if (eap->skip)
3471 {
3472 ++emsg_skip;
3473 lnum = eap->line2; /* do it once, also with an invalid range */
3474 }
3475 else
3476 lnum = eap->line1;
3477 for ( ; lnum <= eap->line2; ++lnum)
3478 {
3479 if (!eap->skip && eap->addr_count > 0)
3480 {
3481 curwin->w_cursor.lnum = lnum;
3482 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003483#ifdef FEAT_VIRTUALEDIT
3484 curwin->w_cursor.coladd = 0;
3485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 eap->line1, eap->line2, &doesrange,
3490 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 failed = TRUE;
3493 break;
3494 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003495
3496 /* Handle a function returning a Funcref, Dictionary or List. */
3497 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3498 {
3499 failed = TRUE;
3500 break;
3501 }
3502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (doesrange || eap->skip)
3505 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003510 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (aborting())
3512 break;
3513 }
3514 if (eap->skip)
3515 --emsg_skip;
3516
3517 if (!failed)
3518 {
3519 /* Check for trailing illegal characters and a following command. */
3520 if (!ends_excmd(*arg))
3521 {
3522 emsg_severe = TRUE;
3523 EMSG(_(e_trailing));
3524 }
3525 else
3526 eap->nextcmd = check_nextcmd(arg);
3527 }
3528
3529end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003531 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532}
3533
3534/*
3535 * ":unlet[!] var1 ... " command.
3536 */
3537 void
3538ex_unlet(eap)
3539 exarg_T *eap;
3540{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003541 ex_unletlock(eap, eap->arg, 0);
3542}
3543
3544/*
3545 * ":lockvar" and ":unlockvar" commands
3546 */
3547 void
3548ex_lockvar(eap)
3549 exarg_T *eap;
3550{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 int deep = 2;
3553
3554 if (eap->forceit)
3555 deep = -1;
3556 else if (vim_isdigit(*arg))
3557 {
3558 deep = getdigits(&arg);
3559 arg = skipwhite(arg);
3560 }
3561
3562 ex_unletlock(eap, arg, deep);
3563}
3564
3565/*
3566 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3567 */
3568 static void
3569ex_unletlock(eap, argstart, deep)
3570 exarg_T *eap;
3571 char_u *argstart;
3572 int deep;
3573{
3574 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 do
3580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003582 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003583 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 if (lv.ll_name == NULL)
3585 error = TRUE; /* error but continue parsing */
3586 if (name_end == NULL || (!vim_iswhite(*name_end)
3587 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589 if (name_end != NULL)
3590 {
3591 emsg_severe = TRUE;
3592 EMSG(_(e_trailing));
3593 }
3594 if (!(eap->skip || error))
3595 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 break;
3597 }
3598
3599 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 {
3601 if (eap->cmdidx == CMD_unlet)
3602 {
3603 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3604 error = TRUE;
3605 }
3606 else
3607 {
3608 if (do_lock_var(&lv, name_end, deep,
3609 eap->cmdidx == CMD_lockvar) == FAIL)
3610 error = TRUE;
3611 }
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (!eap->skip)
3615 clear_lval(&lv);
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 arg = skipwhite(name_end);
3618 } while (!ends_excmd(*arg));
3619
3620 eap->nextcmd = check_nextcmd(arg);
3621}
3622
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 int forceit;
3628{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 int ret = OK;
3630 int cc;
3631
3632 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 cc = *name_end;
3635 *name_end = NUL;
3636
3637 /* Normal name or expanded name. */
3638 if (check_changedtick(lp->ll_name))
3639 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3645 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 else if (lp->ll_range)
3647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649
3650 /* Delete a range of List items. */
3651 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3652 {
3653 li = lp->ll_li->li_next;
3654 listitem_remove(lp->ll_list, lp->ll_li);
3655 lp->ll_li = li;
3656 ++lp->ll_n1;
3657 }
3658 }
3659 else
3660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 /* unlet a List item. */
3663 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003666 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 }
3668
3669 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670}
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672/*
3673 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 */
3676 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 hashtab_T *ht;
3682 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003684 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 ht = find_var_ht(name, &varname);
3687 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 hi = hash_find(ht, varname);
3690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003692 di = HI2DI(hi);
3693 if (var_check_fixed(di->di_flags, name)
3694 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 return FAIL;
3696 delete_var(ht, hi);
3697 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003700 if (forceit)
3701 return OK;
3702 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return FAIL;
3704}
3705
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003706/*
3707 * Lock or unlock variable indicated by "lp".
3708 * "deep" is the levels to go (-1 for unlimited);
3709 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3710 */
3711 static int
3712do_lock_var(lp, name_end, deep, lock)
3713 lval_T *lp;
3714 char_u *name_end;
3715 int deep;
3716 int lock;
3717{
3718 int ret = OK;
3719 int cc;
3720 dictitem_T *di;
3721
3722 if (deep == 0) /* nothing to do */
3723 return OK;
3724
3725 if (lp->ll_tv == NULL)
3726 {
3727 cc = *name_end;
3728 *name_end = NUL;
3729
3730 /* Normal name or expanded name. */
3731 if (check_changedtick(lp->ll_name))
3732 ret = FAIL;
3733 else
3734 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003735 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 if (di == NULL)
3737 ret = FAIL;
3738 else
3739 {
3740 if (lock)
3741 di->di_flags |= DI_FLAGS_LOCK;
3742 else
3743 di->di_flags &= ~DI_FLAGS_LOCK;
3744 item_lock(&di->di_tv, deep, lock);
3745 }
3746 }
3747 *name_end = cc;
3748 }
3749 else if (lp->ll_range)
3750 {
3751 listitem_T *li = lp->ll_li;
3752
3753 /* (un)lock a range of List items. */
3754 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3755 {
3756 item_lock(&li->li_tv, deep, lock);
3757 li = li->li_next;
3758 ++lp->ll_n1;
3759 }
3760 }
3761 else if (lp->ll_list != NULL)
3762 /* (un)lock a List item. */
3763 item_lock(&lp->ll_li->li_tv, deep, lock);
3764 else
3765 /* un(lock) a Dictionary item. */
3766 item_lock(&lp->ll_di->di_tv, deep, lock);
3767
3768 return ret;
3769}
3770
3771/*
3772 * Lock or unlock an item. "deep" is nr of levels to go.
3773 */
3774 static void
3775item_lock(tv, deep, lock)
3776 typval_T *tv;
3777 int deep;
3778 int lock;
3779{
3780 static int recurse = 0;
3781 list_T *l;
3782 listitem_T *li;
3783 dict_T *d;
3784 hashitem_T *hi;
3785 int todo;
3786
3787 if (recurse >= DICT_MAXNEST)
3788 {
3789 EMSG(_("E743: variable nested too deep for (un)lock"));
3790 return;
3791 }
3792 if (deep == 0)
3793 return;
3794 ++recurse;
3795
3796 /* lock/unlock the item itself */
3797 if (lock)
3798 tv->v_lock |= VAR_LOCKED;
3799 else
3800 tv->v_lock &= ~VAR_LOCKED;
3801
3802 switch (tv->v_type)
3803 {
3804 case VAR_LIST:
3805 if ((l = tv->vval.v_list) != NULL)
3806 {
3807 if (lock)
3808 l->lv_lock |= VAR_LOCKED;
3809 else
3810 l->lv_lock &= ~VAR_LOCKED;
3811 if (deep < 0 || deep > 1)
3812 /* recursive: lock/unlock the items the List contains */
3813 for (li = l->lv_first; li != NULL; li = li->li_next)
3814 item_lock(&li->li_tv, deep - 1, lock);
3815 }
3816 break;
3817 case VAR_DICT:
3818 if ((d = tv->vval.v_dict) != NULL)
3819 {
3820 if (lock)
3821 d->dv_lock |= VAR_LOCKED;
3822 else
3823 d->dv_lock &= ~VAR_LOCKED;
3824 if (deep < 0 || deep > 1)
3825 {
3826 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003827 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3829 {
3830 if (!HASHITEM_EMPTY(hi))
3831 {
3832 --todo;
3833 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3834 }
3835 }
3836 }
3837 }
3838 }
3839 --recurse;
3840}
3841
Bram Moolenaara40058a2005-07-11 22:42:07 +00003842/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003843 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3844 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003845 */
3846 static int
3847tv_islocked(tv)
3848 typval_T *tv;
3849{
3850 return (tv->v_lock & VAR_LOCKED)
3851 || (tv->v_type == VAR_LIST
3852 && tv->vval.v_list != NULL
3853 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3854 || (tv->v_type == VAR_DICT
3855 && tv->vval.v_dict != NULL
3856 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3857}
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3860/*
3861 * Delete all "menutrans_" variables.
3862 */
3863 void
3864del_menutrans_vars()
3865{
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003867 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003872 {
3873 if (!HASHITEM_EMPTY(hi))
3874 {
3875 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3877 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003878 }
3879 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003880 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881}
3882#endif
3883
3884#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3885
3886/*
3887 * Local string buffer for the next two functions to store a variable name
3888 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3889 * get_user_var_name().
3890 */
3891
3892static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3893
3894static char_u *varnamebuf = NULL;
3895static int varnamebuflen = 0;
3896
3897/*
3898 * Function to concatenate a prefix and a variable name.
3899 */
3900 static char_u *
3901cat_prefix_varname(prefix, name)
3902 int prefix;
3903 char_u *name;
3904{
3905 int len;
3906
3907 len = (int)STRLEN(name) + 3;
3908 if (len > varnamebuflen)
3909 {
3910 vim_free(varnamebuf);
3911 len += 10; /* some additional space */
3912 varnamebuf = alloc(len);
3913 if (varnamebuf == NULL)
3914 {
3915 varnamebuflen = 0;
3916 return NULL;
3917 }
3918 varnamebuflen = len;
3919 }
3920 *varnamebuf = prefix;
3921 varnamebuf[1] = ':';
3922 STRCPY(varnamebuf + 2, name);
3923 return varnamebuf;
3924}
3925
3926/*
3927 * Function given to ExpandGeneric() to obtain the list of user defined
3928 * (global/buffer/window/built-in) variable names.
3929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 char_u *
3931get_user_var_name(xp, idx)
3932 expand_T *xp;
3933 int idx;
3934{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003935 static long_u gdone;
3936 static long_u bdone;
3937 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003938#ifdef FEAT_WINDOWS
3939 static long_u tdone;
3940#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 static int vidx;
3942 static hashitem_T *hi;
3943 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003946 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003948#ifdef FEAT_WINDOWS
3949 tdone = 0;
3950#endif
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952
3953 /* Global variables */
3954 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003958 else
3959 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 while (HASHITEM_EMPTY(hi))
3961 ++hi;
3962 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3963 return cat_prefix_varname('g', hi->hi_key);
3964 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
3967 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003968 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003971 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003973 else
3974 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 while (HASHITEM_EMPTY(hi))
3976 ++hi;
3977 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003981 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return (char_u *)"b:changedtick";
3983 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003984
3985 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003986 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003989 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003991 else
3992 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003993 while (HASHITEM_EMPTY(hi))
3994 ++hi;
3995 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998#ifdef FEAT_WINDOWS
3999 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004000 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 if (tdone < ht->ht_used)
4002 {
4003 if (tdone++ == 0)
4004 hi = ht->ht_array;
4005 else
4006 ++hi;
4007 while (HASHITEM_EMPTY(hi))
4008 ++hi;
4009 return cat_prefix_varname('t', hi->hi_key);
4010 }
4011#endif
4012
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 /* v: variables */
4014 if (vidx < VV_LEN)
4015 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 vim_free(varnamebuf);
4018 varnamebuf = NULL;
4019 varnamebuflen = 0;
4020 return NULL;
4021}
4022
4023#endif /* FEAT_CMDL_COMPL */
4024
4025/*
4026 * types for expressions.
4027 */
4028typedef enum
4029{
4030 TYPE_UNKNOWN = 0
4031 , TYPE_EQUAL /* == */
4032 , TYPE_NEQUAL /* != */
4033 , TYPE_GREATER /* > */
4034 , TYPE_GEQUAL /* >= */
4035 , TYPE_SMALLER /* < */
4036 , TYPE_SEQUAL /* <= */
4037 , TYPE_MATCH /* =~ */
4038 , TYPE_NOMATCH /* !~ */
4039} exptype_T;
4040
4041/*
4042 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4045 */
4046
4047/*
4048 * Handle zero level expression.
4049 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004051 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 char_u **nextcmd;
4059 int evaluate;
4060{
4061 int ret;
4062 char_u *p;
4063
4064 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (ret == FAIL || !ends_excmd(*p))
4067 {
4068 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 /*
4071 * Report the invalid expression unless the expression evaluation has
4072 * been cancelled due to an aborting error, an interrupt, or an
4073 * exception.
4074 */
4075 if (!aborting())
4076 EMSG2(_(e_invexpr2), arg);
4077 ret = FAIL;
4078 }
4079 if (nextcmd != NULL)
4080 *nextcmd = check_nextcmd(p);
4081
4082 return ret;
4083}
4084
4085/*
4086 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004087 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 *
4089 * "arg" must point to the first non-white of the expression.
4090 * "arg" is advanced to the next non-white after the recognized expression.
4091 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004092 * Note: "rettv.v_lock" is not set.
4093 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 * Return OK or FAIL.
4095 */
4096 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 int evaluate;
4101{
4102 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 /*
4106 * Get the first variable.
4107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110
4111 if ((*arg)[0] == '?')
4112 {
4113 result = FALSE;
4114 if (evaluate)
4115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116 int error = FALSE;
4117
4118 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 if (error)
4122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124
4125 /*
4126 * Get the second variable.
4127 */
4128 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Check for the ":".
4134 */
4135 if ((*arg)[0] != ':')
4136 {
4137 EMSG(_("E109: Missing ':' after '?'"));
4138 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142
4143 /*
4144 * Get the third variable.
4145 */
4146 *arg = skipwhite(*arg + 1);
4147 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4148 {
4149 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return FAIL;
4152 }
4153 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 return OK;
4158}
4159
4160/*
4161 * Handle first level expression:
4162 * expr2 || expr2 || expr2 logical OR
4163 *
4164 * "arg" must point to the first non-white of the expression.
4165 * "arg" is advanced to the next non-white after the recognized expression.
4166 *
4167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int evaluate;
4174{
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 long result;
4177 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 /*
4181 * Get the first variable.
4182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 /*
4187 * Repeat until there is no following "||".
4188 */
4189 first = TRUE;
4190 result = FALSE;
4191 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4192 {
4193 if (evaluate && first)
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (error)
4199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 first = FALSE;
4201 }
4202
4203 /*
4204 * Get the second variable.
4205 */
4206 *arg = skipwhite(*arg + 2);
4207 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4208 return FAIL;
4209
4210 /*
4211 * Compute the result.
4212 */
4213 if (evaluate && !result)
4214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 if (error)
4219 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 if (evaluate)
4222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 rettv->v_type = VAR_NUMBER;
4224 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227
4228 return OK;
4229}
4230
4231/*
4232 * Handle second level expression:
4233 * expr3 && expr3 && expr3 logical AND
4234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
4238 * Return OK or FAIL.
4239 */
4240 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 int evaluate;
4245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 long result;
4248 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Repeat until there is no following "&&".
4259 */
4260 first = TRUE;
4261 result = TRUE;
4262 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4263 {
4264 if (evaluate && first)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 first = FALSE;
4272 }
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 2);
4278 if (eval4(arg, &var2, evaluate && result) == FAIL)
4279 return FAIL;
4280
4281 /*
4282 * Compute the result.
4283 */
4284 if (evaluate && result)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (evaluate)
4293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 rettv->v_type = VAR_NUMBER;
4295 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298
4299 return OK;
4300}
4301
4302/*
4303 * Handle third level expression:
4304 * var1 == var2
4305 * var1 =~ var2
4306 * var1 != var2
4307 * var1 !~ var2
4308 * var1 > var2
4309 * var1 >= var2
4310 * var1 < var2
4311 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004312 * var1 is var2
4313 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int evaluate;
4325{
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 char_u *p;
4328 int i;
4329 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int len = 2;
4332 long n1, n2;
4333 char_u *s1, *s2;
4334 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4335 regmatch_T regmatch;
4336 int ic;
4337 char_u *save_cpo;
4338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 p = *arg;
4346 switch (p[0])
4347 {
4348 case '=': if (p[1] == '=')
4349 type = TYPE_EQUAL;
4350 else if (p[1] == '~')
4351 type = TYPE_MATCH;
4352 break;
4353 case '!': if (p[1] == '=')
4354 type = TYPE_NEQUAL;
4355 else if (p[1] == '~')
4356 type = TYPE_NOMATCH;
4357 break;
4358 case '>': if (p[1] != '=')
4359 {
4360 type = TYPE_GREATER;
4361 len = 1;
4362 }
4363 else
4364 type = TYPE_GEQUAL;
4365 break;
4366 case '<': if (p[1] != '=')
4367 {
4368 type = TYPE_SMALLER;
4369 len = 1;
4370 }
4371 else
4372 type = TYPE_SEQUAL;
4373 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 case 'i': if (p[1] == 's')
4375 {
4376 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4377 len = 5;
4378 if (!vim_isIDc(p[len]))
4379 {
4380 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4381 type_is = TRUE;
4382 }
4383 }
4384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386
4387 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 if (type != TYPE_UNKNOWN)
4391 {
4392 /* extra question mark appended: ignore case */
4393 if (p[len] == '?')
4394 {
4395 ic = TRUE;
4396 ++len;
4397 }
4398 /* extra '#' appended: match case */
4399 else if (p[len] == '#')
4400 {
4401 ic = FALSE;
4402 ++len;
4403 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004404 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 else
4406 ic = p_ic;
4407
4408 /*
4409 * Get the second variable.
4410 */
4411 *arg = skipwhite(p + len);
4412 if (eval5(arg, &var2, evaluate) == FAIL)
4413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416 }
4417
4418 if (evaluate)
4419 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 if (type_is && rettv->v_type != var2.v_type)
4421 {
4422 /* For "is" a different type always means FALSE, for "notis"
4423 * it means TRUE. */
4424 n1 = (type == TYPE_NEQUAL);
4425 }
4426 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4427 {
4428 if (type_is)
4429 {
4430 n1 = (rettv->v_type == var2.v_type
4431 && rettv->vval.v_list == var2.vval.v_list);
4432 if (type == TYPE_NEQUAL)
4433 n1 = !n1;
4434 }
4435 else if (rettv->v_type != var2.v_type
4436 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4437 {
4438 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004439 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004441 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 clear_tv(rettv);
4443 clear_tv(&var2);
4444 return FAIL;
4445 }
4446 else
4447 {
4448 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004449 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4450 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 if (type == TYPE_NEQUAL)
4452 n1 = !n1;
4453 }
4454 }
4455
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004456 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4457 {
4458 if (type_is)
4459 {
4460 n1 = (rettv->v_type == var2.v_type
4461 && rettv->vval.v_dict == var2.vval.v_dict);
4462 if (type == TYPE_NEQUAL)
4463 n1 = !n1;
4464 }
4465 else if (rettv->v_type != var2.v_type
4466 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4467 {
4468 if (rettv->v_type != var2.v_type)
4469 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4470 else
4471 EMSG(_("E736: Invalid operation for Dictionary"));
4472 clear_tv(rettv);
4473 clear_tv(&var2);
4474 return FAIL;
4475 }
4476 else
4477 {
4478 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004479 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4480 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004481 if (type == TYPE_NEQUAL)
4482 n1 = !n1;
4483 }
4484 }
4485
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4487 {
4488 if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004494 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Funcrefs for being equal or unequal. */
4502 if (rettv->vval.v_string == NULL
4503 || var2.vval.v_string == NULL)
4504 n1 = FALSE;
4505 else
4506 n1 = STRCMP(rettv->vval.v_string,
4507 var2.vval.v_string) == 0;
4508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004513#ifdef FEAT_FLOAT
4514 /*
4515 * If one of the two variables is a float, compare as a float.
4516 * When using "=~" or "!~", always compare as string.
4517 */
4518 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4519 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 {
4521 float_T f1, f2;
4522
4523 if (rettv->v_type == VAR_FLOAT)
4524 f1 = rettv->vval.v_float;
4525 else
4526 f1 = get_tv_number(rettv);
4527 if (var2.v_type == VAR_FLOAT)
4528 f2 = var2.vval.v_float;
4529 else
4530 f2 = get_tv_number(&var2);
4531 n1 = FALSE;
4532 switch (type)
4533 {
4534 case TYPE_EQUAL: n1 = (f1 == f2); break;
4535 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4536 case TYPE_GREATER: n1 = (f1 > f2); break;
4537 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4538 case TYPE_SMALLER: n1 = (f1 < f2); break;
4539 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4540 case TYPE_UNKNOWN:
4541 case TYPE_MATCH:
4542 case TYPE_NOMATCH: break; /* avoid gcc warning */
4543 }
4544 }
4545#endif
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /*
4548 * If one of the two variables is a number, compare as a number.
4549 * When using "=~" or "!~", always compare as string.
4550 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 n1 = get_tv_number(rettv);
4555 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 switch (type)
4557 {
4558 case TYPE_EQUAL: n1 = (n1 == n2); break;
4559 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4560 case TYPE_GREATER: n1 = (n1 > n2); break;
4561 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4562 case TYPE_SMALLER: n1 = (n1 < n2); break;
4563 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4564 case TYPE_UNKNOWN:
4565 case TYPE_MATCH:
4566 case TYPE_NOMATCH: break; /* avoid gcc warning */
4567 }
4568 }
4569 else
4570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 s1 = get_tv_string_buf(rettv, buf1);
4572 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4574 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4575 else
4576 i = 0;
4577 n1 = FALSE;
4578 switch (type)
4579 {
4580 case TYPE_EQUAL: n1 = (i == 0); break;
4581 case TYPE_NEQUAL: n1 = (i != 0); break;
4582 case TYPE_GREATER: n1 = (i > 0); break;
4583 case TYPE_GEQUAL: n1 = (i >= 0); break;
4584 case TYPE_SMALLER: n1 = (i < 0); break;
4585 case TYPE_SEQUAL: n1 = (i <= 0); break;
4586
4587 case TYPE_MATCH:
4588 case TYPE_NOMATCH:
4589 /* avoid 'l' flag in 'cpoptions' */
4590 save_cpo = p_cpo;
4591 p_cpo = (char_u *)"";
4592 regmatch.regprog = vim_regcomp(s2,
4593 RE_MAGIC + RE_STRING);
4594 regmatch.rm_ic = ic;
4595 if (regmatch.regprog != NULL)
4596 {
4597 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004598 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (type == TYPE_NOMATCH)
4600 n1 = !n1;
4601 }
4602 p_cpo = save_cpo;
4603 break;
4604
4605 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4606 }
4607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 rettv->v_type = VAR_NUMBER;
4611 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 }
4614
4615 return OK;
4616}
4617
4618/*
4619 * Handle fourth level expression:
4620 * + number addition
4621 * - number subtraction
4622 * . string concatenation
4623 *
4624 * "arg" must point to the first non-white of the expression.
4625 * "arg" is advanced to the next non-white after the recognized expression.
4626 *
4627 * Return OK or FAIL.
4628 */
4629 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 int evaluate;
4634{
Bram Moolenaar33570922005-01-25 22:26:29 +00004635 typval_T var2;
4636 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 int op;
4638 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639#ifdef FEAT_FLOAT
4640 float_T f1 = 0, f2 = 0;
4641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 char_u *s1, *s2;
4643 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4644 char_u *p;
4645
4646 /*
4647 * Get the first variable.
4648 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004649 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return FAIL;
4651
4652 /*
4653 * Repeat computing, until no '+', '-' or '.' is following.
4654 */
4655 for (;;)
4656 {
4657 op = **arg;
4658 if (op != '+' && op != '-' && op != '.')
4659 break;
4660
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661 if ((op != '+' || rettv->v_type != VAR_LIST)
4662#ifdef FEAT_FLOAT
4663 && (op == '.' || rettv->v_type != VAR_FLOAT)
4664#endif
4665 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 {
4667 /* For "list + ...", an illegal use of the first operand as
4668 * a number cannot be determined before evaluating the 2nd
4669 * operand: if this is also a list, all is ok.
4670 * For "something . ...", "something - ..." or "non-list + ...",
4671 * we know that the first operand needs to be a string or number
4672 * without evaluating the 2nd operand. So check before to avoid
4673 * side effects after an error. */
4674 if (evaluate && get_tv_string_chk(rettv) == NULL)
4675 {
4676 clear_tv(rettv);
4677 return FAIL;
4678 }
4679 }
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 /*
4682 * Get the second variable.
4683 */
4684 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004685 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
4689 }
4690
4691 if (evaluate)
4692 {
4693 /*
4694 * Compute the result.
4695 */
4696 if (op == '.')
4697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4699 s2 = get_tv_string_buf_chk(&var2, buf2);
4700 if (s2 == NULL) /* type error ? */
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004706 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(rettv);
4708 rettv->v_type = VAR_STRING;
4709 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004711 else if (op == '+' && rettv->v_type == VAR_LIST
4712 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004713 {
4714 /* concatenate Lists */
4715 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4716 &var3) == FAIL)
4717 {
4718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 return FAIL;
4721 }
4722 clear_tv(rettv);
4723 *rettv = var3;
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
4726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 int error = FALSE;
4728
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729#ifdef FEAT_FLOAT
4730 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732 f1 = rettv->vval.v_float;
4733 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735 else
4736#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004738 n1 = get_tv_number_chk(rettv, &error);
4739 if (error)
4740 {
4741 /* This can only happen for "list + non-list". For
4742 * "non-list + ..." or "something - ...", we returned
4743 * before evaluating the 2nd operand. */
4744 clear_tv(rettv);
4745 return FAIL;
4746 }
4747#ifdef FEAT_FLOAT
4748 if (var2.v_type == VAR_FLOAT)
4749 f1 = n1;
4750#endif
4751 }
4752#ifdef FEAT_FLOAT
4753 if (var2.v_type == VAR_FLOAT)
4754 {
4755 f2 = var2.vval.v_float;
4756 n2 = 0;
4757 }
4758 else
4759#endif
4760 {
4761 n2 = get_tv_number_chk(&var2, &error);
4762 if (error)
4763 {
4764 clear_tv(rettv);
4765 clear_tv(&var2);
4766 return FAIL;
4767 }
4768#ifdef FEAT_FLOAT
4769 if (rettv->v_type == VAR_FLOAT)
4770 f2 = n2;
4771#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774
4775#ifdef FEAT_FLOAT
4776 /* If there is a float on either side the result is a float. */
4777 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4778 {
4779 if (op == '+')
4780 f1 = f1 + f2;
4781 else
4782 f1 = f1 - f2;
4783 rettv->v_type = VAR_FLOAT;
4784 rettv->vval.v_float = f1;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787#endif
4788 {
4789 if (op == '+')
4790 n1 = n1 + n2;
4791 else
4792 n1 = n1 - n2;
4793 rettv->v_type = VAR_NUMBER;
4794 rettv->vval.v_number = n1;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
4800 return OK;
4801}
4802
4803/*
4804 * Handle fifth level expression:
4805 * * number multiplication
4806 * / number division
4807 * % number modulo
4808 *
4809 * "arg" must point to the first non-white of the expression.
4810 * "arg" is advanced to the next non-white after the recognized expression.
4811 *
4812 * Return OK or FAIL.
4813 */
4814 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004815eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004819 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
Bram Moolenaar33570922005-01-25 22:26:29 +00004821 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 int op;
4823 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824#ifdef FEAT_FLOAT
4825 int use_float = FALSE;
4826 float_T f1 = 0, f2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 /*
4831 * Get the first variable.
4832 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004833 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return FAIL;
4835
4836 /*
4837 * Repeat computing, until no '*', '/' or '%' is following.
4838 */
4839 for (;;)
4840 {
4841 op = **arg;
4842 if (op != '*' && op != '/' && op != '%')
4843 break;
4844
4845 if (evaluate)
4846 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847#ifdef FEAT_FLOAT
4848 if (rettv->v_type == VAR_FLOAT)
4849 {
4850 f1 = rettv->vval.v_float;
4851 use_float = TRUE;
4852 n1 = 0;
4853 }
4854 else
4855#endif
4856 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858 if (error)
4859 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 else
4862 n1 = 0;
4863
4864 /*
4865 * Get the second variable.
4866 */
4867 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004868 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return FAIL;
4870
4871 if (evaluate)
4872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (var2.v_type == VAR_FLOAT)
4875 {
4876 if (!use_float)
4877 {
4878 f1 = n1;
4879 use_float = TRUE;
4880 }
4881 f2 = var2.vval.v_float;
4882 n2 = 0;
4883 }
4884 else
4885#endif
4886 {
4887 n2 = get_tv_number_chk(&var2, &error);
4888 clear_tv(&var2);
4889 if (error)
4890 return FAIL;
4891#ifdef FEAT_FLOAT
4892 if (use_float)
4893 f2 = n2;
4894#endif
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904 if (op == '*')
4905 f1 = f1 * f2;
4906 else if (op == '/')
4907 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# ifdef VMS
4909 /* VMS crashes on divide by zero, work around it */
4910 if (f2 == 0.0)
4911 {
4912 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004913 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004914 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004915 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004916 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004917 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918 }
4919 else
4920 f1 = f1 / f2;
4921# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 /* We rely on the floating point library to handle divide
4923 * by zero to result in "inf" and not a crash. */
4924 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004925# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004929 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930 return FAIL;
4931 }
4932 rettv->v_type = VAR_FLOAT;
4933 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 if (op == '*')
4939 n1 = n1 * n2;
4940 else if (op == '/')
4941 {
4942 if (n2 == 0) /* give an error message? */
4943 {
4944 if (n1 == 0)
4945 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4946 else if (n1 < 0)
4947 n1 = -0x7fffffffL;
4948 else
4949 n1 = 0x7fffffffL;
4950 }
4951 else
4952 n1 = n1 / n2;
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 {
4956 if (n2 == 0) /* give an error message? */
4957 n1 = 0;
4958 else
4959 n1 = n1 % n2;
4960 }
4961 rettv->v_type = VAR_NUMBER;
4962 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 }
4966
4967 return OK;
4968}
4969
4970/*
4971 * Handle sixth level expression:
4972 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004973 * "string" string constant
4974 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 * &option-name option value
4976 * @r register contents
4977 * identifier variable value
4978 * function() function call
4979 * $VAR environment variable
4980 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004981 * [expr, expr] List
4982 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * Also handle:
4985 * ! in front logical NOT
4986 * - in front unary minus
4987 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 * trailing [] subscript in String or List
4989 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 *
4991 * "arg" must point to the first non-white of the expression.
4992 * "arg" is advanced to the next non-white after the recognized expression.
4993 *
4994 * Return OK or FAIL.
4995 */
4996 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004997eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005001 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 long n;
5004 int len;
5005 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 char_u *start_leader, *end_leader;
5007 int ret = OK;
5008 char_u *alias;
5009
5010 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 /*
5017 * Skip '!' and '-' characters. They are handled later.
5018 */
5019 start_leader = *arg;
5020 while (**arg == '!' || **arg == '-' || **arg == '+')
5021 *arg = skipwhite(*arg + 1);
5022 end_leader = *arg;
5023
5024 switch (**arg)
5025 {
5026 /*
5027 * Number constant.
5028 */
5029 case '0':
5030 case '1':
5031 case '2':
5032 case '3':
5033 case '4':
5034 case '5':
5035 case '6':
5036 case '7':
5037 case '8':
5038 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 {
5040#ifdef FEAT_FLOAT
5041 char_u *p = skipdigits(*arg + 1);
5042 int get_float = FALSE;
5043
5044 /* We accept a float when the format matches
5045 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005046 * strict to avoid backwards compatibility problems.
5047 * Don't look for a float after the "." operator, so that
5048 * ":let vers = 1.2.3" doesn't fail. */
5049 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005051 get_float = TRUE;
5052 p = skipdigits(p + 2);
5053 if (*p == 'e' || *p == 'E')
5054 {
5055 ++p;
5056 if (*p == '-' || *p == '+')
5057 ++p;
5058 if (!vim_isdigit(*p))
5059 get_float = FALSE;
5060 else
5061 p = skipdigits(p + 1);
5062 }
5063 if (ASCII_ISALPHA(*p) || *p == '.')
5064 get_float = FALSE;
5065 }
5066 if (get_float)
5067 {
5068 float_T f;
5069
5070 *arg += string2float(*arg, &f);
5071 if (evaluate)
5072 {
5073 rettv->v_type = VAR_FLOAT;
5074 rettv->vval.v_float = f;
5075 }
5076 }
5077 else
5078#endif
5079 {
5080 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5081 *arg += len;
5082 if (evaluate)
5083 {
5084 rettv->v_type = VAR_NUMBER;
5085 rettv->vval.v_number = n;
5086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 /*
5092 * String constant: "string".
5093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 break;
5102
5103 /*
5104 * List: [expr, expr]
5105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108
5109 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 * Dictionary: {key: val, key: val}
5111 */
5112 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5113 break;
5114
5115 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005116 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 break;
5120
5121 /*
5122 * Environment variable: $VAR.
5123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005124 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126
5127 /*
5128 * Register contents: @r.
5129 */
5130 case '@': ++*arg;
5131 if (evaluate)
5132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005134 rettv->vval.v_string = get_reg_contents(**arg,
5135 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137 if (**arg != NUL)
5138 ++*arg;
5139 break;
5140
5141 /*
5142 * nested expression: (expression).
5143 */
5144 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (**arg == ')')
5147 ++*arg;
5148 else if (ret == OK)
5149 {
5150 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 ret = FAIL;
5153 }
5154 break;
5155
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159
5160 if (ret == NOTDONE)
5161 {
5162 /*
5163 * Must be a variable or function name.
5164 * Can also be a curly-braces kind of name: {expr}.
5165 */
5166 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005167 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 if (alias != NULL)
5169 s = alias;
5170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005171 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 ret = FAIL;
5173 else
5174 {
5175 if (**arg == '(') /* recursive! */
5176 {
5177 /* If "s" is the name of a variable of type VAR_FUNC
5178 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005179 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180
5181 /* Invoke the function. */
5182 ret = get_func_tv(s, len, rettv, arg,
5183 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185
5186 /* If evaluate is FALSE rettv->v_type was not set in
5187 * get_func_tv, but it's needed in handle_subscript() to parse
5188 * what follows. So set it here. */
5189 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5190 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005191 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005192 rettv->v_type = VAR_FUNC;
5193 }
5194
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 /* Stop the expression evaluation when immediately
5196 * aborting on error, or when an interrupt occurred or
5197 * an exception was thrown but not caught. */
5198 if (aborting())
5199 {
5200 if (ret == OK)
5201 clear_tv(rettv);
5202 ret = FAIL;
5203 }
5204 }
5205 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005206 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005207 else
5208 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005210 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 }
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 *arg = skipwhite(*arg);
5214
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5216 * expr(expr). */
5217 if (ret == OK)
5218 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 /*
5221 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5222 */
5223 if (ret == OK && evaluate && end_leader > start_leader)
5224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 int val = 0;
5227#ifdef FEAT_FLOAT
5228 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230 if (rettv->v_type == VAR_FLOAT)
5231 f = rettv->vval.v_float;
5232 else
5233#endif
5234 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 clear_tv(rettv);
5238 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005240 else
5241 {
5242 while (end_leader > start_leader)
5243 {
5244 --end_leader;
5245 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 {
5247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 f = !f;
5250 else
5251#endif
5252 val = !val;
5253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005255 {
5256#ifdef FEAT_FLOAT
5257 if (rettv->v_type == VAR_FLOAT)
5258 f = -f;
5259 else
5260#endif
5261 val = -val;
5262 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005264#ifdef FEAT_FLOAT
5265 if (rettv->v_type == VAR_FLOAT)
5266 {
5267 clear_tv(rettv);
5268 rettv->vval.v_float = f;
5269 }
5270 else
5271#endif
5272 {
5273 clear_tv(rettv);
5274 rettv->v_type = VAR_NUMBER;
5275 rettv->vval.v_number = val;
5276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279
5280 return ret;
5281}
5282
5283/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005284 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5285 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5287 */
5288 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005293 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294{
5295 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 long len = -1;
5299 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005303 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005305 if (verbose)
5306 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 return FAIL;
5308 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005309#ifdef FEAT_FLOAT
5310 else if (rettv->v_type == VAR_FLOAT)
5311 {
5312 if (verbose)
5313 EMSG(_(e_float_as_string));
5314 return FAIL;
5315 }
5316#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 /*
5321 * dict.name
5322 */
5323 key = *arg + 1;
5324 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5325 ;
5326 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 }
5330 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 /*
5333 * something[idx]
5334 *
5335 * Get the (first) variable from inside the [].
5336 */
5337 *arg = skipwhite(*arg + 1);
5338 if (**arg == ':')
5339 empty1 = TRUE;
5340 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5341 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5343 {
5344 /* not a number or string */
5345 clear_tv(&var1);
5346 return FAIL;
5347 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348
5349 /*
5350 * Get the second variable from inside the [:].
5351 */
5352 if (**arg == ':')
5353 {
5354 range = TRUE;
5355 *arg = skipwhite(*arg + 1);
5356 if (**arg == ']')
5357 empty2 = TRUE;
5358 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360 if (!empty1)
5361 clear_tv(&var1);
5362 return FAIL;
5363 }
5364 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5365 {
5366 /* not a number or string */
5367 if (!empty1)
5368 clear_tv(&var1);
5369 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 return FAIL;
5371 }
5372 }
5373
5374 /* Check for the ']'. */
5375 if (**arg != ']')
5376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005377 if (verbose)
5378 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 clear_tv(&var1);
5380 if (range)
5381 clear_tv(&var2);
5382 return FAIL;
5383 }
5384 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386
5387 if (evaluate)
5388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 n1 = 0;
5390 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 n1 = get_tv_number(&var1);
5393 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 }
5395 if (range)
5396 {
5397 if (empty2)
5398 n2 = -1;
5399 else
5400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 n2 = get_tv_number(&var2);
5402 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 }
5405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
5408 case VAR_NUMBER:
5409 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 len = (long)STRLEN(s);
5412 if (range)
5413 {
5414 /* The resulting variable is a substring. If the indexes
5415 * are out of range the result is empty. */
5416 if (n1 < 0)
5417 {
5418 n1 = len + n1;
5419 if (n1 < 0)
5420 n1 = 0;
5421 }
5422 if (n2 < 0)
5423 n2 = len + n2;
5424 else if (n2 >= len)
5425 n2 = len;
5426 if (n1 >= len || n2 < 0 || n1 > n2)
5427 s = NULL;
5428 else
5429 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5430 }
5431 else
5432 {
5433 /* The resulting variable is a string of a single
5434 * character. If the index is too big or negative the
5435 * result is empty. */
5436 if (n1 >= len || n1 < 0)
5437 s = NULL;
5438 else
5439 s = vim_strnsave(s + n1, 1);
5440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 clear_tv(rettv);
5442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 break;
5445
5446 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 if (n1 < 0)
5449 n1 = len + n1;
5450 if (!empty1 && (n1 < 0 || n1 >= len))
5451 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005452 /* For a range we allow invalid values and return an empty
5453 * list. A list index out of range is an error. */
5454 if (!range)
5455 {
5456 if (verbose)
5457 EMSGN(_(e_listidx), n1);
5458 return FAIL;
5459 }
5460 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 if (range)
5463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 list_T *l;
5465 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466
5467 if (n2 < 0)
5468 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005469 else if (n2 >= len)
5470 n2 = len - 1;
5471 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005472 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 l = list_alloc();
5474 if (l == NULL)
5475 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 n1 <= n2; ++n1)
5478 {
5479 if (list_append_tv(l, &item->li_tv) == FAIL)
5480 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005481 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 return FAIL;
5483 }
5484 item = item->li_next;
5485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 clear_tv(rettv);
5487 rettv->v_type = VAR_LIST;
5488 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005489 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 }
5491 else
5492 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005493 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 clear_tv(rettv);
5495 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
5499 case VAR_DICT:
5500 if (range)
5501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005502 if (verbose)
5503 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 if (len == -1)
5505 clear_tv(&var1);
5506 return FAIL;
5507 }
5508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510
5511 if (len == -1)
5512 {
5513 key = get_tv_string(&var1);
5514 if (*key == NUL)
5515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005516 if (verbose)
5517 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 clear_tv(&var1);
5519 return FAIL;
5520 }
5521 }
5522
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005523 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005526 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 if (len == -1)
5528 clear_tv(&var1);
5529 if (item == NULL)
5530 return FAIL;
5531
5532 copy_tv(&item->di_tv, &var1);
5533 clear_tv(rettv);
5534 *rettv = var1;
5535 }
5536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 }
5538 }
5539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 return OK;
5541}
5542
5543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 * Get an option value.
5545 * "arg" points to the '&' or '+' before the option name.
5546 * "arg" is advanced to character after the option name.
5547 * Return OK or FAIL.
5548 */
5549 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 int evaluate;
5554{
5555 char_u *option_end;
5556 long numval;
5557 char_u *stringval;
5558 int opt_type;
5559 int c;
5560 int working = (**arg == '+'); /* has("+option") */
5561 int ret = OK;
5562 int opt_flags;
5563
5564 /*
5565 * Isolate the option name and find its value.
5566 */
5567 option_end = find_option_end(arg, &opt_flags);
5568 if (option_end == NULL)
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 EMSG2(_("E112: Option name missing: %s"), *arg);
5572 return FAIL;
5573 }
5574
5575 if (!evaluate)
5576 {
5577 *arg = option_end;
5578 return OK;
5579 }
5580
5581 c = *option_end;
5582 *option_end = NUL;
5583 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 if (opt_type == -3) /* invalid name */
5587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005588 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 EMSG2(_("E113: Unknown option: %s"), *arg);
5590 ret = FAIL;
5591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
5594 if (opt_type == -2) /* hidden string option */
5595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 rettv->v_type = VAR_STRING;
5597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else if (opt_type == -1) /* hidden number option */
5600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 rettv->v_type = VAR_NUMBER;
5602 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604 else if (opt_type == 1) /* number option */
5605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606 rettv->v_type = VAR_NUMBER;
5607 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609 else /* string option */
5610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 rettv->v_type = VAR_STRING;
5612 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 }
5615 else if (working && (opt_type == -2 || opt_type == -1))
5616 ret = FAIL;
5617
5618 *option_end = c; /* put back for error messages */
5619 *arg = option_end;
5620
5621 return ret;
5622}
5623
5624/*
5625 * Allocate a variable for a string constant.
5626 * Return OK or FAIL.
5627 */
5628 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 int evaluate;
5633{
5634 char_u *p;
5635 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 int extra = 0;
5637
5638 /*
5639 * Find the end of the string, skipping backslashed characters.
5640 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 if (*p == '\\' && p[1] != NUL)
5644 {
5645 ++p;
5646 /* A "\<x>" form occupies at least 4 characters, and produces up
5647 * to 6 characters: reserve space for 2 extra */
5648 if (*p == '<')
5649 extra += 2;
5650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652
5653 if (*p != '"')
5654 {
5655 EMSG2(_("E114: Missing quote: %s"), *arg);
5656 return FAIL;
5657 }
5658
5659 /* If only parsing, set *arg and return here */
5660 if (!evaluate)
5661 {
5662 *arg = p + 1;
5663 return OK;
5664 }
5665
5666 /*
5667 * Copy the string into allocated memory, handling backslashed
5668 * characters.
5669 */
5670 name = alloc((unsigned)(p - *arg + extra));
5671 if (name == NULL)
5672 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 rettv->v_type = VAR_STRING;
5674 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 if (*p == '\\')
5679 {
5680 switch (*++p)
5681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 case 'b': *name++ = BS; ++p; break;
5683 case 'e': *name++ = ESC; ++p; break;
5684 case 'f': *name++ = FF; ++p; break;
5685 case 'n': *name++ = NL; ++p; break;
5686 case 'r': *name++ = CAR; ++p; break;
5687 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689 case 'X': /* hex: "\x1", "\x12" */
5690 case 'x':
5691 case 'u': /* Unicode: "\u0023" */
5692 case 'U':
5693 if (vim_isxdigit(p[1]))
5694 {
5695 int n, nr;
5696 int c = toupper(*p);
5697
5698 if (c == 'X')
5699 n = 2;
5700 else
5701 n = 4;
5702 nr = 0;
5703 while (--n >= 0 && vim_isxdigit(p[1]))
5704 {
5705 ++p;
5706 nr = (nr << 4) + hex2nr(*p);
5707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#ifdef FEAT_MBYTE
5710 /* For "\u" store the number according to
5711 * 'encoding'. */
5712 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 else
5715#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 break;
5719
5720 /* octal: "\1", "\12", "\123" */
5721 case '0':
5722 case '1':
5723 case '2':
5724 case '3':
5725 case '4':
5726 case '5':
5727 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 case '7': *name = *p++ - '0';
5729 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 *name = (*name << 3) + *p++ - '0';
5732 if (*p >= '0' && *p <= '7')
5733 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737
5738 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (extra != 0)
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 break;
5744 }
5745 /* FALLTHROUGH */
5746
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 break;
5749 }
5750 }
5751 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 *arg = p + 1;
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 return OK;
5759}
5760
5761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 * Return OK or FAIL.
5764 */
5765 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005766get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 int evaluate;
5770{
5771 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 int reduce = 0;
5774
5775 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 */
5778 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 break;
5784 ++reduce;
5785 ++p;
5786 }
5787 }
5788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 return FAIL;
5793 }
5794
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 if (!evaluate)
5797 {
5798 *arg = p + 1;
5799 return OK;
5800 }
5801
5802 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 */
5805 str = alloc((unsigned)((p - *arg) - reduce));
5806 if (str == NULL)
5807 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 rettv->v_type = VAR_STRING;
5809 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 break;
5817 ++p;
5818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005822 *arg = p + 1;
5823
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 return OK;
5825}
5826
5827/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 * Allocate a variable for a List and fill it from "*arg".
5829 * Return OK or FAIL.
5830 */
5831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 int evaluate;
5836{
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l = NULL;
5838 typval_T tv;
5839 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840
5841 if (evaluate)
5842 {
5843 l = list_alloc();
5844 if (l == NULL)
5845 return FAIL;
5846 }
5847
5848 *arg = skipwhite(*arg + 1);
5849 while (**arg != ']' && **arg != NUL)
5850 {
5851 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5852 goto failret;
5853 if (evaluate)
5854 {
5855 item = listitem_alloc();
5856 if (item != NULL)
5857 {
5858 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005859 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 list_append(l, item);
5861 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005862 else
5863 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 }
5865
5866 if (**arg == ']')
5867 break;
5868 if (**arg != ',')
5869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 goto failret;
5872 }
5873 *arg = skipwhite(*arg + 1);
5874 }
5875
5876 if (**arg != ']')
5877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879failret:
5880 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005881 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 return FAIL;
5883 }
5884
5885 *arg = skipwhite(*arg + 1);
5886 if (evaluate)
5887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888 rettv->v_type = VAR_LIST;
5889 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 ++l->lv_refcount;
5891 }
5892
5893 return OK;
5894}
5895
5896/*
5897 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005898 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005900 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901list_alloc()
5902{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005903 list_T *l;
5904
5905 l = (list_T *)alloc_clear(sizeof(list_T));
5906 if (l != NULL)
5907 {
5908 /* Prepend the list to the list of lists for garbage collection. */
5909 if (first_list != NULL)
5910 first_list->lv_used_prev = l;
5911 l->lv_used_prev = NULL;
5912 l->lv_used_next = first_list;
5913 first_list = l;
5914 }
5915 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916}
5917
5918/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005919 * Allocate an empty list for a return value.
5920 * Returns OK or FAIL.
5921 */
5922 static int
5923rettv_list_alloc(rettv)
5924 typval_T *rettv;
5925{
5926 list_T *l = list_alloc();
5927
5928 if (l == NULL)
5929 return FAIL;
5930
5931 rettv->vval.v_list = l;
5932 rettv->v_type = VAR_LIST;
5933 ++l->lv_refcount;
5934 return OK;
5935}
5936
5937/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 * Unreference a list: decrement the reference count and free it when it
5939 * becomes zero.
5940 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005941 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005945 if (l != NULL && --l->lv_refcount <= 0)
5946 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947}
5948
5949/*
5950 * Free a list, including all items it points to.
5951 * Ignores the reference count.
5952 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005953 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005954list_free(l, recurse)
5955 list_T *l;
5956 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 /* Remove the list from the list of lists for garbage collection. */
5961 if (l->lv_used_prev == NULL)
5962 first_list = l->lv_used_next;
5963 else
5964 l->lv_used_prev->lv_used_next = l->lv_used_next;
5965 if (l->lv_used_next != NULL)
5966 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5967
Bram Moolenaard9fba312005-06-26 22:34:35 +00005968 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005970 /* Remove the item before deleting it. */
5971 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005972 if (recurse || (item->li_tv.v_type != VAR_LIST
5973 && item->li_tv.v_type != VAR_DICT))
5974 clear_tv(&item->li_tv);
5975 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 }
5977 vim_free(l);
5978}
5979
5980/*
5981 * Allocate a list item.
5982 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005983 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984listitem_alloc()
5985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987}
5988
5989/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005990 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005992 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005996 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 vim_free(item);
5998}
5999
6000/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006001 * Remove a list item from a List and free it. Also clears the value.
6002 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006003 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006004listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 list_T *l;
6006 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006007{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006008 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006009 listitem_free(item);
6010}
6011
6012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 * Get the number of items in a list.
6014 */
6015 static long
6016list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 if (l == NULL)
6020 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006021 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022}
6023
6024/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 * Return TRUE when two lists have exactly the same values.
6026 */
6027 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006028list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 list_T *l1;
6030 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006031 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033{
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006036 if (l1 == NULL || l2 == NULL)
6037 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006038 if (l1 == l2)
6039 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040 if (list_len(l1) != list_len(l2))
6041 return FALSE;
6042
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006043 for (item1 = l1->lv_first, item2 = l2->lv_first;
6044 item1 != NULL && item2 != NULL;
6045 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047 return FALSE;
6048 return item1 == NULL && item2 == NULL;
6049}
6050
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006051#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6052 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006053/*
6054 * Return the dictitem that an entry in a hashtable points to.
6055 */
6056 dictitem_T *
6057dict_lookup(hi)
6058 hashitem_T *hi;
6059{
6060 return HI2DI(hi);
6061}
6062#endif
6063
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006064/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 * Return TRUE when two dictionaries have exactly the same key/values.
6066 */
6067 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006068dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 dict_T *d1;
6070 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006073{
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 hashitem_T *hi;
6075 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006077
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006078 if (d1 == NULL || d2 == NULL)
6079 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006080 if (d1 == d2)
6081 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006082 if (dict_len(d1) != dict_len(d2))
6083 return FALSE;
6084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006085 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088 if (!HASHITEM_EMPTY(hi))
6089 {
6090 item2 = dict_find(d2, hi->hi_key, -1);
6091 if (item2 == NULL)
6092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 return FALSE;
6095 --todo;
6096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 }
6098 return TRUE;
6099}
6100
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101static int tv_equal_recurse_limit;
6102
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006103/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006106 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107 */
6108 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 typval_T *tv1;
6111 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 int ic; /* ignore case */
6113 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114{
6115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006116 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006118 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006120 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006123 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 * recursiveness to a limit. We guess they are equal then.
6125 * A fixed limit has the problem of still taking an awful long time.
6126 * Reduce the limit every time running into it. That should work fine for
6127 * deeply linked structures that are not recursively linked and catch
6128 * recursiveness quickly. */
6129 if (!recursive)
6130 tv_equal_recurse_limit = 1000;
6131 if (recursive_cnt >= tv_equal_recurse_limit)
6132 {
6133 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136
6137 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006139 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 ++recursive_cnt;
6141 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6142 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006143 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006144
6145 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 ++recursive_cnt;
6147 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6148 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006149 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150
6151 case VAR_FUNC:
6152 return (tv1->vval.v_string != NULL
6153 && tv2->vval.v_string != NULL
6154 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6155
6156 case VAR_NUMBER:
6157 return tv1->vval.v_number == tv2->vval.v_number;
6158
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006159#ifdef FEAT_FLOAT
6160 case VAR_FLOAT:
6161 return tv1->vval.v_float == tv2->vval.v_float;
6162#endif
6163
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 case VAR_STRING:
6165 s1 = get_tv_string_buf(tv1, buf1);
6166 s2 = get_tv_string_buf(tv2, buf2);
6167 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169
6170 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 return TRUE;
6172}
6173
6174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 * Locate item with index "n" in list "l" and return it.
6176 * A negative index is counted from the end; -1 is the last item.
6177 * Returns NULL when "n" is out of range.
6178 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006179 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 long n;
6183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 long idx;
6186
6187 if (l == NULL)
6188 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006189
6190 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006192 n = l->lv_len + n;
6193
6194 /* Check for index out of range. */
6195 if (n < 0 || n >= l->lv_len)
6196 return NULL;
6197
6198 /* When there is a cached index may start search from there. */
6199 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201 if (n < l->lv_idx / 2)
6202 {
6203 /* closest to the start of the list */
6204 item = l->lv_first;
6205 idx = 0;
6206 }
6207 else if (n > (l->lv_idx + l->lv_len) / 2)
6208 {
6209 /* closest to the end of the list */
6210 item = l->lv_last;
6211 idx = l->lv_len - 1;
6212 }
6213 else
6214 {
6215 /* closest to the cached index */
6216 item = l->lv_idx_item;
6217 idx = l->lv_idx;
6218 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 }
6220 else
6221 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006222 if (n < l->lv_len / 2)
6223 {
6224 /* closest to the start of the list */
6225 item = l->lv_first;
6226 idx = 0;
6227 }
6228 else
6229 {
6230 /* closest to the end of the list */
6231 item = l->lv_last;
6232 idx = l->lv_len - 1;
6233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006235
6236 while (n > idx)
6237 {
6238 /* search forward */
6239 item = item->li_next;
6240 ++idx;
6241 }
6242 while (n < idx)
6243 {
6244 /* search backward */
6245 item = item->li_prev;
6246 --idx;
6247 }
6248
6249 /* cache the used index */
6250 l->lv_idx = idx;
6251 l->lv_idx_item = item;
6252
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 return item;
6254}
6255
6256/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006257 * Get list item "l[idx]" as a number.
6258 */
6259 static long
6260list_find_nr(l, idx, errorp)
6261 list_T *l;
6262 long idx;
6263 int *errorp; /* set to TRUE when something wrong */
6264{
6265 listitem_T *li;
6266
6267 li = list_find(l, idx);
6268 if (li == NULL)
6269 {
6270 if (errorp != NULL)
6271 *errorp = TRUE;
6272 return -1L;
6273 }
6274 return get_tv_number_chk(&li->li_tv, errorp);
6275}
6276
6277/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006278 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6279 */
6280 char_u *
6281list_find_str(l, idx)
6282 list_T *l;
6283 long idx;
6284{
6285 listitem_T *li;
6286
6287 li = list_find(l, idx - 1);
6288 if (li == NULL)
6289 {
6290 EMSGN(_(e_listidx), idx);
6291 return NULL;
6292 }
6293 return get_tv_string(&li->li_tv);
6294}
6295
6296/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297 * Locate "item" list "l" and return its index.
6298 * Returns -1 when "item" is not in the list.
6299 */
6300 static long
6301list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 list_T *l;
6303 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006304{
6305 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006307
6308 if (l == NULL)
6309 return -1;
6310 idx = 0;
6311 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6312 ++idx;
6313 if (li == NULL)
6314 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006315 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006316}
6317
6318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 * Append item "item" to the end of list "l".
6320 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006321 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 list_T *l;
6324 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325{
6326 if (l->lv_last == NULL)
6327 {
6328 /* empty list */
6329 l->lv_first = item;
6330 l->lv_last = item;
6331 item->li_prev = NULL;
6332 }
6333 else
6334 {
6335 l->lv_last->li_next = item;
6336 item->li_prev = l->lv_last;
6337 l->lv_last = item;
6338 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006339 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 item->li_next = NULL;
6341}
6342
6343/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006345 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006347 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 list_T *l;
6350 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 copy_tv(tv, &li->li_tv);
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Return FAIL when out of memory.
6364 */
6365 int
6366list_append_dict(list, dict)
6367 list_T *list;
6368 dict_T *dict;
6369{
6370 listitem_T *li = listitem_alloc();
6371
6372 if (li == NULL)
6373 return FAIL;
6374 li->li_tv.v_type = VAR_DICT;
6375 li->li_tv.v_lock = 0;
6376 li->li_tv.vval.v_dict = dict;
6377 list_append(list, li);
6378 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 return OK;
6380}
6381
6382/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385 * Returns FAIL when out of memory.
6386 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006387 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006388list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006389 list_T *l;
6390 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006391 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006392{
6393 listitem_T *li = listitem_alloc();
6394
6395 if (li == NULL)
6396 return FAIL;
6397 list_append(l, li);
6398 li->li_tv.v_type = VAR_STRING;
6399 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006400 if (str == NULL)
6401 li->li_tv.vval.v_string = NULL;
6402 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006403 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006404 return FAIL;
6405 return OK;
6406}
6407
6408/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409 * Append "n" to list "l".
6410 * Returns FAIL when out of memory.
6411 */
6412 static int
6413list_append_number(l, n)
6414 list_T *l;
6415 varnumber_T n;
6416{
6417 listitem_T *li;
6418
6419 li = listitem_alloc();
6420 if (li == NULL)
6421 return FAIL;
6422 li->li_tv.v_type = VAR_NUMBER;
6423 li->li_tv.v_lock = 0;
6424 li->li_tv.vval.v_number = n;
6425 list_append(l, li);
6426 return OK;
6427}
6428
6429/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 * If "item" is NULL append at the end.
6432 * Return FAIL when out of memory.
6433 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006434 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 list_T *l;
6437 typval_T *tv;
6438 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441
6442 if (ni == NULL)
6443 return FAIL;
6444 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006445 list_insert(l, ni, item);
6446 return OK;
6447}
6448
6449 void
6450list_insert(l, ni, item)
6451 list_T *l;
6452 listitem_T *ni;
6453 listitem_T *item;
6454{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 if (item == NULL)
6456 /* Append new item at end of list. */
6457 list_append(l, ni);
6458 else
6459 {
6460 /* Insert new item before existing item. */
6461 ni->li_prev = item->li_prev;
6462 ni->li_next = item;
6463 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 ++l->lv_idx;
6467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006469 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006471 l->lv_idx_item = NULL;
6472 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006474 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476}
6477
6478/*
6479 * Extend "l1" with "l2".
6480 * If "bef" is NULL append at the end, otherwise insert before this item.
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
6484list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *l1;
6486 list_T *l2;
6487 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488{
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006490 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006492 /* We also quit the loop when we have inserted the original item count of
6493 * the list, avoid a hang when we extend a list with itself. */
6494 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6496 return FAIL;
6497 return OK;
6498}
6499
6500/*
6501 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6502 * Return FAIL when out of memory.
6503 */
6504 static int
6505list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 list_T *l1;
6507 list_T *l2;
6508 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509{
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006512 if (l1 == NULL || l2 == NULL)
6513 return FAIL;
6514
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 if (l == NULL)
6518 return FAIL;
6519 tv->v_type = VAR_LIST;
6520 tv->vval.v_list = l;
6521
6522 /* append all items from the second list */
6523 return list_extend(l, l2, NULL);
6524}
6525
6526/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 * Returns NULL when out of memory.
6531 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 list_T *copy;
6539 listitem_T *item;
6540 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541
6542 if (orig == NULL)
6543 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544
6545 copy = list_alloc();
6546 if (copy != NULL)
6547 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 if (copyID != 0)
6549 {
6550 /* Do this before adding the items, because one of the items may
6551 * refer back to this list. */
6552 orig->lv_copyID = copyID;
6553 orig->lv_copylist = copy;
6554 }
6555 for (item = orig->lv_first; item != NULL && !got_int;
6556 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 {
6558 ni = listitem_alloc();
6559 if (ni == NULL)
6560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006561 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 {
6563 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6564 {
6565 vim_free(ni);
6566 break;
6567 }
6568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 list_append(copy, ni);
6572 }
6573 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (item != NULL)
6575 {
6576 list_unref(copy);
6577 copy = NULL;
6578 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 }
6580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 return copy;
6582}
6583
6584/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006586 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006587 * This used to be called list_remove, but that conflicts with a Sun header
6588 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006590 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006591vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *l;
6593 listitem_T *item;
6594 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595{
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 /* notify watchers */
6599 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006601 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 list_fix_watch(l, ip);
6603 if (ip == item2)
6604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606
6607 if (item2->li_next == NULL)
6608 l->lv_last = item->li_prev;
6609 else
6610 item2->li_next->li_prev = item->li_prev;
6611 if (item->li_prev == NULL)
6612 l->lv_first = item2->li_next;
6613 else
6614 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006615 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616}
6617
6618/*
6619 * Return an allocated string with the string representation of a list.
6620 * May return NULL.
6621 */
6622 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006625 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626{
6627 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628
6629 if (tv->vval.v_list == NULL)
6630 return NULL;
6631 ga_init2(&ga, (int)sizeof(char), 80);
6632 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006633 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 {
6635 vim_free(ga.ga_data);
6636 return NULL;
6637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 ga_append(&ga, ']');
6639 ga_append(&ga, NUL);
6640 return (char_u *)ga.ga_data;
6641}
6642
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006643typedef struct join_S {
6644 char_u *s;
6645 char_u *tofree;
6646} join_T;
6647
6648 static int
6649list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6650 garray_T *gap; /* to store the result in */
6651 list_T *l;
6652 char_u *sep;
6653 int echo_style;
6654 int copyID;
6655 garray_T *join_gap; /* to keep each list item string */
6656{
6657 int i;
6658 join_T *p;
6659 int len;
6660 int sumlen = 0;
6661 int first = TRUE;
6662 char_u *tofree;
6663 char_u numbuf[NUMBUFLEN];
6664 listitem_T *item;
6665 char_u *s;
6666
6667 /* Stringify each item in the list. */
6668 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6669 {
6670 if (echo_style)
6671 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6672 else
6673 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6674 if (s == NULL)
6675 return FAIL;
6676
6677 len = (int)STRLEN(s);
6678 sumlen += len;
6679
6680 ga_grow(join_gap, 1);
6681 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6682 if (tofree != NULL || s != numbuf)
6683 {
6684 p->s = s;
6685 p->tofree = tofree;
6686 }
6687 else
6688 {
6689 p->s = vim_strnsave(s, len);
6690 p->tofree = p->s;
6691 }
6692
6693 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006694 if (did_echo_string_emsg) /* recursion error, bail out */
6695 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 }
6697
6698 /* Allocate result buffer with its total size, avoid re-allocation and
6699 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6700 if (join_gap->ga_len >= 2)
6701 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6702 if (ga_grow(gap, sumlen + 2) == FAIL)
6703 return FAIL;
6704
6705 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6706 {
6707 if (first)
6708 first = FALSE;
6709 else
6710 ga_concat(gap, sep);
6711 p = ((join_T *)join_gap->ga_data) + i;
6712
6713 if (p->s != NULL)
6714 ga_concat(gap, p->s);
6715 line_breakcheck();
6716 }
6717
6718 return OK;
6719}
6720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006723 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006726 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006727list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006731 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006732 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 garray_T join_ga;
6735 int retval;
6736 join_T *p;
6737 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6740 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6741
6742 /* Dispose each item in join_ga. */
6743 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745 p = (join_T *)join_ga.ga_data;
6746 for (i = 0; i < join_ga.ga_len; ++i)
6747 {
6748 vim_free(p->tofree);
6749 ++p;
6750 }
6751 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006752 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753
6754 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006755}
6756
6757/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 * Garbage collection for lists and dictionaries.
6759 *
6760 * We use reference counts to be able to free most items right away when they
6761 * are no longer used. But for composite items it's possible that it becomes
6762 * unused while the reference count is > 0: When there is a recursive
6763 * reference. Example:
6764 * :let l = [1, 2, 3]
6765 * :let d = {9: l}
6766 * :let l[1] = d
6767 *
6768 * Since this is quite unusual we handle this with garbage collection: every
6769 * once in a while find out which lists and dicts are not referenced from any
6770 * variable.
6771 *
6772 * Here is a good reference text about garbage collection (refers to Python
6773 * but it applies to all reference-counting mechanisms):
6774 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006776
6777/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 * Do garbage collection for lists and dicts.
6779 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006780 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 int
6782garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006783{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006784 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785 buf_T *buf;
6786 win_T *wp;
6787 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006788 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006789 int did_free;
6790 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006791#ifdef FEAT_WINDOWS
6792 tabpage_T *tp;
6793#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006795 /* Only do this once. */
6796 want_garbage_collect = FALSE;
6797 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006798 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006799
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800 /* We advance by two because we add one for items referenced through
6801 * previous_funccal. */
6802 current_copyID += COPYID_INC;
6803 copyID = current_copyID;
6804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 /*
6806 * 1. Go through all accessible variables and mark all lists and dicts
6807 * with copyID.
6808 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006809
6810 /* Don't free variables in the previous_funccal list unless they are only
6811 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006812 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6814 {
6815 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6816 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6817 }
6818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 /* script-local variables */
6820 for (i = 1; i <= ga_scripts.ga_len; ++i)
6821 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6822
6823 /* buffer-local variables */
6824 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006825 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826
6827 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006829 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006830#ifdef FEAT_AUTOCMD
6831 if (aucmd_win != NULL)
6832 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6833#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006835#ifdef FEAT_WINDOWS
6836 /* tabpage-local variables */
6837 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006838 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006839#endif
6840
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 /* global variables */
6842 set_ref_in_ht(&globvarht, copyID);
6843
6844 /* function-local variables */
6845 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6846 {
6847 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6848 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6849 }
6850
Bram Moolenaard812df62008-11-09 12:46:09 +00006851 /* v: vars */
6852 set_ref_in_ht(&vimvarht, copyID);
6853
Bram Moolenaar1dced572012-04-05 16:54:08 +02006854#ifdef FEAT_LUA
6855 set_ref_in_lua(copyID);
6856#endif
6857
Bram Moolenaardb913952012-06-29 12:54:53 +02006858#ifdef FEAT_PYTHON
6859 set_ref_in_python(copyID);
6860#endif
6861
6862#ifdef FEAT_PYTHON3
6863 set_ref_in_python3(copyID);
6864#endif
6865
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006866 /*
6867 * 2. Free lists and dictionaries that are not referenced.
6868 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 did_free = free_unref_items(copyID);
6870
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006871 /*
6872 * 3. Check if any funccal can be freed now.
6873 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 for (pfc = &previous_funccal; *pfc != NULL; )
6875 {
6876 if (can_free_funccal(*pfc, copyID))
6877 {
6878 fc = *pfc;
6879 *pfc = fc->caller;
6880 free_funccal(fc, TRUE);
6881 did_free = TRUE;
6882 did_free_funccal = TRUE;
6883 }
6884 else
6885 pfc = &(*pfc)->caller;
6886 }
6887 if (did_free_funccal)
6888 /* When a funccal was freed some more items might be garbage
6889 * collected, so run again. */
6890 (void)garbage_collect();
6891
6892 return did_free;
6893}
6894
6895/*
6896 * Free lists and dictionaries that are no longer referenced.
6897 */
6898 static int
6899free_unref_items(copyID)
6900 int copyID;
6901{
6902 dict_T *dd;
6903 list_T *ll;
6904 int did_free = FALSE;
6905
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006907 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 */
6909 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the Dictionary and ordinary items it contains, but don't
6913 * recurse into Lists and Dictionaries, they will be in the list
6914 * of dicts or list of lists. */
6915 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
6918 /* restart, next dict may also have been freed */
6919 dd = first_dict;
6920 }
6921 else
6922 dd = dd->dv_used_next;
6923
6924 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 * Go through the list of lists and free items without the copyID.
6926 * But don't free a list that has a watcher (used in a for loop), these
6927 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 */
6929 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6931 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006933 /* Free the List and ordinary items it contains, but don't recurse
6934 * into Lists and Dictionaries, they will be in the list of dicts
6935 * or list of lists. */
6936 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 did_free = TRUE;
6938
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006939 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 ll = first_list;
6941 }
6942 else
6943 ll = ll->lv_used_next;
6944
6945 return did_free;
6946}
6947
6948/*
6949 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6950 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006951 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952set_ref_in_ht(ht, copyID)
6953 hashtab_T *ht;
6954 int copyID;
6955{
6956 int todo;
6957 hashitem_T *hi;
6958
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006959 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 for (hi = ht->ht_array; todo > 0; ++hi)
6961 if (!HASHITEM_EMPTY(hi))
6962 {
6963 --todo;
6964 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6965 }
6966}
6967
6968/*
6969 * Mark all lists and dicts referenced through list "l" with "copyID".
6970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006971 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972set_ref_in_list(l, copyID)
6973 list_T *l;
6974 int copyID;
6975{
6976 listitem_T *li;
6977
6978 for (li = l->lv_first; li != NULL; li = li->li_next)
6979 set_ref_in_item(&li->li_tv, copyID);
6980}
6981
6982/*
6983 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6984 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006985 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986set_ref_in_item(tv, copyID)
6987 typval_T *tv;
6988 int copyID;
6989{
6990 dict_T *dd;
6991 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006992
6993 switch (tv->v_type)
6994 {
6995 case VAR_DICT:
6996 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006997 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006998 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 /* Didn't see this dict yet. */
7000 dd->dv_copyID = copyID;
7001 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007002 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007004
7005 case VAR_LIST:
7006 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007007 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /* Didn't see this list yet. */
7010 ll->lv_copyID = copyID;
7011 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007014 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007016}
7017
7018/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019 * Allocate an empty header for a dictionary.
7020 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007021 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022dict_alloc()
7023{
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007025
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007029 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 if (first_dict != NULL)
7031 first_dict->dv_used_prev = d;
7032 d->dv_used_next = first_dict;
7033 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007034 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007037 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007038 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007039 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007041 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007042 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043}
7044
7045/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007046 * Allocate an empty dict for a return value.
7047 * Returns OK or FAIL.
7048 */
7049 static int
7050rettv_dict_alloc(rettv)
7051 typval_T *rettv;
7052{
7053 dict_T *d = dict_alloc();
7054
7055 if (d == NULL)
7056 return FAIL;
7057
7058 rettv->vval.v_dict = d;
7059 rettv->v_type = VAR_DICT;
7060 ++d->dv_refcount;
7061 return OK;
7062}
7063
7064
7065/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 * Unreference a Dictionary: decrement the reference count and free it when it
7067 * becomes zero.
7068 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007069 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073 if (d != NULL && --d->dv_refcount <= 0)
7074 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075}
7076
7077/*
7078 * Free a Dictionary, including all items it contains.
7079 * Ignores the reference count.
7080 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007081 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007082dict_free(d, recurse)
7083 dict_T *d;
7084 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007087 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007088 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 /* Remove the dict from the list of dicts for garbage collection. */
7091 if (d->dv_used_prev == NULL)
7092 first_dict = d->dv_used_next;
7093 else
7094 d->dv_used_prev->dv_used_next = d->dv_used_next;
7095 if (d->dv_used_next != NULL)
7096 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7097
7098 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007099 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007100 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 if (!HASHITEM_EMPTY(hi))
7104 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007105 /* Remove the item before deleting it, just in case there is
7106 * something recursive causing trouble. */
7107 di = HI2DI(hi);
7108 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007109 if (recurse || (di->di_tv.v_type != VAR_LIST
7110 && di->di_tv.v_type != VAR_DICT))
7111 clear_tv(&di->di_tv);
7112 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 --todo;
7114 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 vim_free(d);
7118}
7119
7120/*
7121 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 * The "key" is copied to the new item.
7123 * Note that the value of the item "di_tv" still needs to be initialized!
7124 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007126 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127dictitem_alloc(key)
7128 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129{
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007132 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007136 di->di_flags = 0;
7137 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139}
7140
7141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 * Make a copy of a Dictionary item.
7143 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007150 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7151 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 if (di != NULL)
7153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007155 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156 copy_tv(&org->di_tv, &di->di_tv);
7157 }
7158 return di;
7159}
7160
7161/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 * Remove item "item" from Dictionary "dict" and free it.
7163 */
7164 static void
7165dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 dict_T *dict;
7167 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168{
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 if (HASHITEM_EMPTY(hi))
7173 EMSG2(_(e_intern2), "dictitem_remove()");
7174 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176 dictitem_free(item);
7177}
7178
7179/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 * Free a dict item. Also clears the value.
7181 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007182 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 clear_tv(&item->di_tv);
7187 vim_free(item);
7188}
7189
7190/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7192 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007193 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 * Returns NULL when out of memory.
7195 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201{
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 dict_T *copy;
7203 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206
7207 if (orig == NULL)
7208 return NULL;
7209
7210 copy = dict_alloc();
7211 if (copy != NULL)
7212 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 if (copyID != 0)
7214 {
7215 orig->dv_copyID = copyID;
7216 orig->dv_copydict = copy;
7217 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 --todo;
7224
7225 di = dictitem_alloc(hi->hi_key);
7226 if (di == NULL)
7227 break;
7228 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007229 {
7230 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7231 copyID) == FAIL)
7232 {
7233 vim_free(di);
7234 break;
7235 }
7236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 else
7238 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7239 if (dict_add(copy, di) == FAIL)
7240 {
7241 dictitem_free(di);
7242 break;
7243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007245 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 if (todo > 0)
7249 {
7250 dict_unref(copy);
7251 copy = NULL;
7252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 }
7254
7255 return copy;
7256}
7257
7258/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007260 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007262 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 dict_T *d;
7265 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266{
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268}
7269
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007271 * Add a number or string entry to dictionary "d".
7272 * When "str" is NULL use number "nr", otherwise use "str".
7273 * Returns FAIL when out of memory and when key already exists.
7274 */
7275 int
7276dict_add_nr_str(d, key, nr, str)
7277 dict_T *d;
7278 char *key;
7279 long nr;
7280 char_u *str;
7281{
7282 dictitem_T *item;
7283
7284 item = dictitem_alloc((char_u *)key);
7285 if (item == NULL)
7286 return FAIL;
7287 item->di_tv.v_lock = 0;
7288 if (str == NULL)
7289 {
7290 item->di_tv.v_type = VAR_NUMBER;
7291 item->di_tv.vval.v_number = nr;
7292 }
7293 else
7294 {
7295 item->di_tv.v_type = VAR_STRING;
7296 item->di_tv.vval.v_string = vim_strsave(str);
7297 }
7298 if (dict_add(d, item) == FAIL)
7299 {
7300 dictitem_free(item);
7301 return FAIL;
7302 }
7303 return OK;
7304}
7305
7306/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007307 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007308 * Returns FAIL when out of memory and when key already exists.
7309 */
7310 int
7311dict_add_list(d, key, list)
7312 dict_T *d;
7313 char *key;
7314 list_T *list;
7315{
7316 dictitem_T *item;
7317
7318 item = dictitem_alloc((char_u *)key);
7319 if (item == NULL)
7320 return FAIL;
7321 item->di_tv.v_lock = 0;
7322 item->di_tv.v_type = VAR_LIST;
7323 item->di_tv.vval.v_list = list;
7324 if (dict_add(d, item) == FAIL)
7325 {
7326 dictitem_free(item);
7327 return FAIL;
7328 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007329 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007330 return OK;
7331}
7332
7333/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 * Get the number of items in a Dictionary.
7335 */
7336 static long
7337dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 if (d == NULL)
7341 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007342 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343}
7344
7345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 * Find item "key[len]" in Dictionary "d".
7347 * If "len" is negative use strlen(key).
7348 * Returns NULL when not found.
7349 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007350 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 char_u *key;
7354 int len;
7355{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356#define AKEYLEN 200
7357 char_u buf[AKEYLEN];
7358 char_u *akey;
7359 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (len < 0)
7363 akey = key;
7364 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 tofree = akey = vim_strnsave(key, len);
7367 if (akey == NULL)
7368 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007369 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 else
7371 {
7372 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007373 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 akey = buf;
7375 }
7376
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 vim_free(tofree);
7379 if (HASHITEM_EMPTY(hi))
7380 return NULL;
7381 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382}
7383
7384/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 * Get a string item from a dictionary.
7386 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007387 * Returns NULL if the entry doesn't exist or out of memory.
7388 */
7389 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007390get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007391 dict_T *d;
7392 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007393 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007394{
7395 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007396 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007397
7398 di = dict_find(d, key, -1);
7399 if (di == NULL)
7400 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007401 s = get_tv_string(&di->di_tv);
7402 if (save && s != NULL)
7403 s = vim_strsave(s);
7404 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007405}
7406
7407/*
7408 * Get a number item from a dictionary.
7409 * Returns 0 if the entry doesn't exist or out of memory.
7410 */
7411 long
7412get_dict_number(d, key)
7413 dict_T *d;
7414 char_u *key;
7415{
7416 dictitem_T *di;
7417
7418 di = dict_find(d, key, -1);
7419 if (di == NULL)
7420 return 0;
7421 return get_tv_number(&di->di_tv);
7422}
7423
7424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 * Return an allocated string with the string representation of a Dictionary.
7426 * May return NULL.
7427 */
7428 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432{
7433 garray_T ga;
7434 int first = TRUE;
7435 char_u *tofree;
7436 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 return NULL;
7444 ga_init2(&ga, (int)sizeof(char), 80);
7445 ga_append(&ga, '{');
7446
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007447 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007448 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 --todo;
7453
7454 if (first)
7455 first = FALSE;
7456 else
7457 ga_concat(&ga, (char_u *)", ");
7458
7459 tofree = string_quote(hi->hi_key, FALSE);
7460 if (tofree != NULL)
7461 {
7462 ga_concat(&ga, tofree);
7463 vim_free(tofree);
7464 }
7465 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007466 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 if (s != NULL)
7468 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007470 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007471 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007472 line_breakcheck();
7473
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007476 if (todo > 0)
7477 {
7478 vim_free(ga.ga_data);
7479 return NULL;
7480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 ga_append(&ga, '}');
7483 ga_append(&ga, NUL);
7484 return (char_u *)ga.ga_data;
7485}
7486
7487/*
7488 * Allocate a variable for a Dictionary and fill it from "*arg".
7489 * Return OK or FAIL. Returns NOTDONE for {expr}.
7490 */
7491 static int
7492get_dict_tv(arg, rettv, evaluate)
7493 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 int evaluate;
7496{
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 dict_T *d = NULL;
7498 typval_T tvkey;
7499 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007500 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504
7505 /*
7506 * First check if it's not a curly-braces thing: {expr}.
7507 * Must do this without evaluating, otherwise a function may be called
7508 * twice. Unfortunately this means we need to call eval1() twice for the
7509 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (*start != '}')
7513 {
7514 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7515 return FAIL;
7516 if (*start == '}')
7517 return NOTDONE;
7518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519
7520 if (evaluate)
7521 {
7522 d = dict_alloc();
7523 if (d == NULL)
7524 return FAIL;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 tvkey.v_type = VAR_UNKNOWN;
7527 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528
7529 *arg = skipwhite(*arg + 1);
7530 while (**arg != '}' && **arg != NUL)
7531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 goto failret;
7534 if (**arg != ':')
7535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007536 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 goto failret;
7539 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007540 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007542 key = get_tv_string_buf_chk(&tvkey, buf);
7543 if (key == NULL || *key == NUL)
7544 {
7545 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7546 if (key != NULL)
7547 EMSG(_(e_emptykey));
7548 clear_tv(&tvkey);
7549 goto failret;
7550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552
7553 *arg = skipwhite(*arg + 1);
7554 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7555 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007556 if (evaluate)
7557 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 goto failret;
7559 }
7560 if (evaluate)
7561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 if (item != NULL)
7564 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007565 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 clear_tv(&tv);
7568 goto failret;
7569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 item = dictitem_alloc(key);
7571 clear_tv(&tvkey);
7572 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007575 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 if (dict_add(d, item) == FAIL)
7577 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 }
7579 }
7580
7581 if (**arg == '}')
7582 break;
7583 if (**arg != ',')
7584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007585 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 goto failret;
7587 }
7588 *arg = skipwhite(*arg + 1);
7589 }
7590
7591 if (**arg != '}')
7592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007593 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594failret:
7595 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007596 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 return FAIL;
7598 }
7599
7600 *arg = skipwhite(*arg + 1);
7601 if (evaluate)
7602 {
7603 rettv->v_type = VAR_DICT;
7604 rettv->vval.v_dict = d;
7605 ++d->dv_refcount;
7606 }
7607
7608 return OK;
7609}
7610
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 * Return a string with the string representation of a variable.
7613 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007614 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007615 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007617 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 */
7619 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007624 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007625{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 static int recurse = 0;
7627 char_u *r = NULL;
7628
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007631 if (!did_echo_string_emsg)
7632 {
7633 /* Only give this message once for a recursive call to avoid
7634 * flooding the user with errors. And stop iterating over lists
7635 * and dicts. */
7636 did_echo_string_emsg = TRUE;
7637 EMSG(_("E724: variable nested too deep for displaying"));
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007640 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 }
7642 ++recurse;
7643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 switch (tv->v_type)
7645 {
7646 case VAR_FUNC:
7647 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 r = tv->vval.v_string;
7649 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652 if (tv->vval.v_list == NULL)
7653 {
7654 *tofree = NULL;
7655 r = NULL;
7656 }
7657 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7658 {
7659 *tofree = NULL;
7660 r = (char_u *)"[...]";
7661 }
7662 else
7663 {
7664 tv->vval.v_list->lv_copyID = copyID;
7665 *tofree = list2string(tv, copyID);
7666 r = *tofree;
7667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007671 if (tv->vval.v_dict == NULL)
7672 {
7673 *tofree = NULL;
7674 r = NULL;
7675 }
7676 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7677 {
7678 *tofree = NULL;
7679 r = (char_u *)"{...}";
7680 }
7681 else
7682 {
7683 tv->vval.v_dict->dv_copyID = copyID;
7684 *tofree = dict2string(tv, copyID);
7685 r = *tofree;
7686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 case VAR_STRING:
7690 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 *tofree = NULL;
7692 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007695#ifdef FEAT_FLOAT
7696 case VAR_FLOAT:
7697 *tofree = NULL;
7698 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7699 r = numbuf;
7700 break;
7701#endif
7702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007707
Bram Moolenaar8502c702014-06-17 12:51:16 +02007708 if (--recurse == 0)
7709 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711}
7712
7713/*
7714 * Return a string with the string representation of a variable.
7715 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7716 * "numbuf" is used for a number.
7717 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007718 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 */
7720 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007721tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 char_u **tofree;
7724 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726{
7727 switch (tv->v_type)
7728 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 case VAR_FUNC:
7730 *tofree = string_quote(tv->vval.v_string, TRUE);
7731 return *tofree;
7732 case VAR_STRING:
7733 *tofree = string_quote(tv->vval.v_string, FALSE);
7734 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007735#ifdef FEAT_FLOAT
7736 case VAR_FLOAT:
7737 *tofree = NULL;
7738 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7739 return numbuf;
7740#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007745 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007748 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749}
7750
7751/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 * Return string "str" in ' quotes, doubling ' characters.
7753 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755 */
7756 static char_u *
7757string_quote(str, function)
7758 char_u *str;
7759 int function;
7760{
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007762 char_u *p, *r, *s;
7763
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 len = (function ? 13 : 3);
7765 if (str != NULL)
7766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007767 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 for (p = str; *p != NUL; mb_ptr_adv(p))
7769 if (*p == '\'')
7770 ++len;
7771 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007772 s = r = alloc(len);
7773 if (r != NULL)
7774 {
7775 if (function)
7776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007778 r += 10;
7779 }
7780 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 if (str != NULL)
7783 for (p = str; *p != NUL; )
7784 {
7785 if (*p == '\'')
7786 *r++ = '\'';
7787 MB_COPY_CHAR(p, r);
7788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007789 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790 if (function)
7791 *r++ = ')';
7792 *r++ = NUL;
7793 }
7794 return s;
7795}
7796
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007797#ifdef FEAT_FLOAT
7798/*
7799 * Convert the string "text" to a floating point number.
7800 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7801 * this always uses a decimal point.
7802 * Returns the length of the text that was consumed.
7803 */
7804 static int
7805string2float(text, value)
7806 char_u *text;
7807 float_T *value; /* result stored here */
7808{
7809 char *s = (char *)text;
7810 float_T f;
7811
7812 f = strtod(s, &s);
7813 *value = f;
7814 return (int)((char_u *)s - text);
7815}
7816#endif
7817
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 * Get the value of an environment variable.
7820 * "arg" is pointing to the '$'. It is advanced to after the name.
7821 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007822 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 */
7824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 int evaluate;
7829{
7830 char_u *string = NULL;
7831 int len;
7832 int cc;
7833 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007834 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 ++*arg;
7837 name = *arg;
7838 len = get_env_len(arg);
7839 if (evaluate)
7840 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007841 if (len == 0)
7842 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007843
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007844 cc = name[len];
7845 name[len] = NUL;
7846 /* first try vim_getenv(), fast for normal environment vars */
7847 string = vim_getenv(name, &mustfree);
7848 if (string != NULL && *string != NUL)
7849 {
7850 if (!mustfree)
7851 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007853 else
7854 {
7855 if (mustfree)
7856 vim_free(string);
7857
7858 /* next try expanding things like $VIM and ${HOME} */
7859 string = expand_env_save(name - 1);
7860 if (string != NULL && *string == '$')
7861 {
7862 vim_free(string);
7863 string = NULL;
7864 }
7865 }
7866 name[len] = cc;
7867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 rettv->v_type = VAR_STRING;
7869 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871
7872 return OK;
7873}
7874
7875/*
7876 * Array with names and number of arguments of all internal functions
7877 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7878 */
7879static struct fst
7880{
7881 char *f_name; /* function name */
7882 char f_min_argc; /* minimal number of arguments */
7883 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007885 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886} functions[] =
7887{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#ifdef FEAT_FLOAT
7889 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007891#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007892 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007893 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"append", 2, 2, f_append},
7895 {"argc", 0, 0, f_argc},
7896 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007897 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007898 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007900 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007902 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007905 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"bufexists", 1, 1, f_bufexists},
7907 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7908 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7909 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7910 {"buflisted", 1, 1, f_buflisted},
7911 {"bufloaded", 1, 1, f_bufloaded},
7912 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007913 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"bufwinnr", 1, 1, f_bufwinnr},
7915 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007916 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007917 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#ifdef FEAT_FLOAT
7920 {"ceil", 1, 1, f_ceil},
7921#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007922 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007923 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007925 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007927#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007928 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007929 {"complete_add", 1, 1, f_complete_add},
7930 {"complete_check", 0, 0, f_complete_check},
7931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007934#ifdef FEAT_FLOAT
7935 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007936 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007937#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007940 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007941 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"delete", 1, 1, f_delete},
7943 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007944 {"diff_filler", 1, 1, f_diff_filler},
7945 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007946 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007948 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"eventhandler", 0, 0, f_eventhandler},
7950 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007951 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007953#ifdef FEAT_FLOAT
7954 {"exp", 1, 1, f_exp},
7955#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007956 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007957 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007958 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7960 {"filereadable", 1, 1, f_filereadable},
7961 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007963 {"finddir", 1, 3, f_finddir},
7964 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965#ifdef FEAT_FLOAT
7966 {"float2nr", 1, 1, f_float2nr},
7967 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007968 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007970 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"fnamemodify", 2, 2, f_fnamemodify},
7972 {"foldclosed", 1, 1, f_foldclosed},
7973 {"foldclosedend", 1, 1, f_foldclosedend},
7974 {"foldlevel", 1, 1, f_foldlevel},
7975 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007976 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007979 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007980 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007981 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007982 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"getchar", 0, 1, f_getchar},
7984 {"getcharmod", 0, 0, f_getcharmod},
7985 {"getcmdline", 0, 0, f_getcmdline},
7986 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007987 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02007988 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02007989 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007991 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007992 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"getfsize", 1, 1, f_getfsize},
7994 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007995 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007996 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007997 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007998 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007999 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008000 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008001 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008002 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008004 {"gettabvar", 2, 3, f_gettabvar},
8005 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"getwinposx", 0, 0, f_getwinposx},
8007 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008008 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008009 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008010 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008013 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008014 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8016 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8017 {"histadd", 2, 2, f_histadd},
8018 {"histdel", 1, 2, f_histdel},
8019 {"histget", 1, 2, f_histget},
8020 {"histnr", 1, 1, f_histnr},
8021 {"hlID", 1, 1, f_hlID},
8022 {"hlexists", 1, 1, f_hlexists},
8023 {"hostname", 0, 0, f_hostname},
8024 {"iconv", 3, 3, f_iconv},
8025 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008027 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008029 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"inputrestore", 0, 0, f_inputrestore},
8031 {"inputsave", 0, 0, f_inputsave},
8032 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008034 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008036 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008039 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"libcall", 3, 3, f_libcall},
8043 {"libcallnr", 3, 3, f_libcallnr},
8044 {"line", 1, 1, f_line},
8045 {"line2byte", 1, 1, f_line2byte},
8046 {"lispindent", 1, 1, f_lispindent},
8047 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008049 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050 {"log10", 1, 1, f_log10},
8051#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008052#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008053 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008054#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008055 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008056 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008057 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008058 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008059 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008060 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008061 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008062 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008063 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008064 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008065 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008066 {"max", 1, 1, f_max},
8067 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008068#ifdef vim_mkdir
8069 {"mkdir", 1, 3, f_mkdir},
8070#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008072#ifdef FEAT_MZSCHEME
8073 {"mzeval", 1, 1, f_mzeval},
8074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008076 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008077 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008078 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"pow", 2, 2, f_pow},
8081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008083 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008084 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008085#ifdef FEAT_PYTHON3
8086 {"py3eval", 1, 1, f_py3eval},
8087#endif
8088#ifdef FEAT_PYTHON
8089 {"pyeval", 1, 1, f_pyeval},
8090#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008091 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008092 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008093 {"reltime", 0, 2, f_reltime},
8094 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"remote_expr", 2, 3, f_remote_expr},
8096 {"remote_foreground", 1, 1, f_remote_foreground},
8097 {"remote_peek", 1, 2, f_remote_peek},
8098 {"remote_read", 1, 1, f_remote_read},
8099 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008100 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008102 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008104 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"round", 1, 1, f_round},
8107#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008108 {"screenattr", 2, 2, f_screenattr},
8109 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008110 {"screencol", 0, 0, f_screencol},
8111 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008112 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008113 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008114 {"searchpair", 3, 7, f_searchpair},
8115 {"searchpairpos", 3, 7, f_searchpairpos},
8116 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"server2client", 2, 2, f_server2client},
8118 {"serverlist", 0, 0, f_serverlist},
8119 {"setbufvar", 3, 3, f_setbufvar},
8120 {"setcmdpos", 1, 1, f_setcmdpos},
8121 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008122 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008123 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008124 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008125 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008127 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008128 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008130#ifdef FEAT_CRYPT
8131 {"sha256", 1, 1, f_sha256},
8132#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008133 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008134 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#ifdef FEAT_FLOAT
8137 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008140 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008141 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008142 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008143 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008144 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#ifdef FEAT_FLOAT
8146 {"sqrt", 1, 1, f_sqrt},
8147 {"str2float", 1, 1, f_str2float},
8148#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008149 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008150 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008151 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#ifdef HAVE_STRFTIME
8153 {"strftime", 1, 2, f_strftime},
8154#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"strlen", 1, 1, f_strlen},
8158 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008159 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008161 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008162 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"substitute", 4, 4, f_substitute},
8164 {"synID", 3, 3, f_synID},
8165 {"synIDattr", 2, 3, f_synIDattr},
8166 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008167 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008168 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008169 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008170 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008171 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008172 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008173 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008174 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008175 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008176#ifdef FEAT_FLOAT
8177 {"tan", 1, 1, f_tan},
8178 {"tanh", 1, 1, f_tanh},
8179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008181 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"tolower", 1, 1, f_tolower},
8183 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008184 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"trunc", 1, 1, f_trunc},
8187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008189 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008190 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008191 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008192 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"virtcol", 1, 1, f_virtcol},
8194 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008195 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"winbufnr", 1, 1, f_winbufnr},
8197 {"wincol", 0, 0, f_wincol},
8198 {"winheight", 1, 1, f_winheight},
8199 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008200 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008202 {"winrestview", 1, 1, f_winrestview},
8203 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008205 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008206 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207};
8208
8209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8210
8211/*
8212 * Function given to ExpandGeneric() to obtain the list of internal
8213 * or user defined function names.
8214 */
8215 char_u *
8216get_function_name(xp, idx)
8217 expand_T *xp;
8218 int idx;
8219{
8220 static int intidx = -1;
8221 char_u *name;
8222
8223 if (idx == 0)
8224 intidx = -1;
8225 if (intidx < 0)
8226 {
8227 name = get_user_func_name(xp, idx);
8228 if (name != NULL)
8229 return name;
8230 }
8231 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8232 {
8233 STRCPY(IObuff, functions[intidx].f_name);
8234 STRCAT(IObuff, "(");
8235 if (functions[intidx].f_max_argc == 0)
8236 STRCAT(IObuff, ")");
8237 return IObuff;
8238 }
8239
8240 return NULL;
8241}
8242
8243/*
8244 * Function given to ExpandGeneric() to obtain the list of internal or
8245 * user defined variable or function names.
8246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 char_u *
8248get_expr_name(xp, idx)
8249 expand_T *xp;
8250 int idx;
8251{
8252 static int intidx = -1;
8253 char_u *name;
8254
8255 if (idx == 0)
8256 intidx = -1;
8257 if (intidx < 0)
8258 {
8259 name = get_function_name(xp, idx);
8260 if (name != NULL)
8261 return name;
8262 }
8263 return get_user_var_name(xp, ++intidx);
8264}
8265
8266#endif /* FEAT_CMDL_COMPL */
8267
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008268#if defined(EBCDIC) || defined(PROTO)
8269/*
8270 * Compare struct fst by function name.
8271 */
8272 static int
8273compare_func_name(s1, s2)
8274 const void *s1;
8275 const void *s2;
8276{
8277 struct fst *p1 = (struct fst *)s1;
8278 struct fst *p2 = (struct fst *)s2;
8279
8280 return STRCMP(p1->f_name, p2->f_name);
8281}
8282
8283/*
8284 * Sort the function table by function name.
8285 * The sorting of the table above is ASCII dependant.
8286 * On machines using EBCDIC we have to sort it.
8287 */
8288 static void
8289sortFunctions()
8290{
8291 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8292
8293 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8294}
8295#endif
8296
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298/*
8299 * Find internal function in table above.
8300 * Return index, or -1 if not found
8301 */
8302 static int
8303find_internal_func(name)
8304 char_u *name; /* name of the function */
8305{
8306 int first = 0;
8307 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8308 int cmp;
8309 int x;
8310
8311 /*
8312 * Find the function name in the table. Binary search.
8313 */
8314 while (first <= last)
8315 {
8316 x = first + ((unsigned)(last - first) >> 1);
8317 cmp = STRCMP(name, functions[x].f_name);
8318 if (cmp < 0)
8319 last = x - 1;
8320 else if (cmp > 0)
8321 first = x + 1;
8322 else
8323 return x;
8324 }
8325 return -1;
8326}
8327
8328/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008329 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8330 * name it contains, otherwise return "name".
8331 */
8332 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008333deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008334 char_u *name;
8335 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008336 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337{
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 int cc;
8340
8341 cc = name[*lenp];
8342 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008343 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348 {
8349 *lenp = 0;
8350 return (char_u *)""; /* just in case */
8351 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008352 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 }
8355
8356 return name;
8357}
8358
8359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 * Allocate a variable for the result of a function.
8361 * Return OK or FAIL.
8362 */
8363 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008364get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8365 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 char_u *name; /* name of the function */
8367 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 char_u **arg; /* argument, pointing to the '(' */
8370 linenr_T firstline; /* first line of range */
8371 linenr_T lastline; /* last line of range */
8372 int *doesrange; /* return: function handled range */
8373 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 char_u *argp;
8377 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008378 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 int argcount = 0; /* number of arguments found */
8380
8381 /*
8382 * Get the arguments.
8383 */
8384 argp = *arg;
8385 while (argcount < MAX_FUNC_ARGS)
8386 {
8387 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8388 if (*argp == ')' || *argp == ',' || *argp == NUL)
8389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8391 {
8392 ret = FAIL;
8393 break;
8394 }
8395 ++argcount;
8396 if (*argp != ',')
8397 break;
8398 }
8399 if (*argp == ')')
8400 ++argp;
8401 else
8402 ret = FAIL;
8403
8404 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008406 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008408 {
8409 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008410 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008412 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008416 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
8418 *arg = skipwhite(argp);
8419 return ret;
8420}
8421
8422
8423/*
8424 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008425 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008426 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 */
8428 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008429call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008431 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008435 typval_T *argvars; /* vars for arguments, must have "argcount"
8436 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 linenr_T firstline; /* first line of range */
8438 linenr_T lastline; /* last line of range */
8439 int *doesrange; /* return: function handled range */
8440 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444#define ERROR_UNKNOWN 0
8445#define ERROR_TOOMANY 1
8446#define ERROR_TOOFEW 2
8447#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448#define ERROR_DICT 4
8449#define ERROR_NONE 5
8450#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 int error = ERROR_NONE;
8452 int i;
8453 int llen;
8454 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455#define FLEN_FIXED 40
8456 char_u fname_buf[FLEN_FIXED + 1];
8457 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008458 char_u *name;
8459
8460 /* Make a copy of the name, if it comes from a funcref variable it could
8461 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008462 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008463 if (name == NULL)
8464 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
8466 /*
8467 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8468 * Change <SNR>123_name() to K_SNR 123_name().
8469 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 llen = eval_fname_script(name);
8472 if (llen > 0)
8473 {
8474 fname_buf[0] = K_SPECIAL;
8475 fname_buf[1] = KS_EXTRA;
8476 fname_buf[2] = (int)KE_SNR;
8477 i = 3;
8478 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8479 {
8480 if (current_SID <= 0)
8481 error = ERROR_SCRIPT;
8482 else
8483 {
8484 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8485 i = (int)STRLEN(fname_buf);
8486 }
8487 }
8488 if (i + STRLEN(name + llen) < FLEN_FIXED)
8489 {
8490 STRCPY(fname_buf + i, name + llen);
8491 fname = fname_buf;
8492 }
8493 else
8494 {
8495 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8496 if (fname == NULL)
8497 error = ERROR_OTHER;
8498 else
8499 {
8500 mch_memmove(fname, fname_buf, (size_t)i);
8501 STRCPY(fname + i, name + llen);
8502 }
8503 }
8504 }
8505 else
8506 fname = name;
8507
8508 *doesrange = FALSE;
8509
8510
8511 /* execute the function if no errors detected and executing */
8512 if (evaluate && error == ERROR_NONE)
8513 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008514 char_u *rfname = fname;
8515
8516 /* Ignore "g:" before a function name. */
8517 if (fname[0] == 'g' && fname[1] == ':')
8518 rfname = fname + 2;
8519
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008520 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8521 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_UNKNOWN;
8523
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008524 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
8526 /*
8527 * User defined function.
8528 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008529 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008532 /* Trigger FuncUndefined event, may load the function. */
8533 if (fp == NULL
8534 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008535 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008536 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008538 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008539 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
8541#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008542 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008543 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008544 {
8545 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008546 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008547 }
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 if (fp != NULL)
8550 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008551 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008553 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008555 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008557 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else
8560 {
8561 /*
8562 * Call the user function.
8563 * Save and restore search patterns, script variables and
8564 * redo buffer.
8565 */
8566 save_search_patterns();
8567 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008568 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008571 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8572 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8573 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008574 /* Function was unreferenced while being used, free it
8575 * now. */
8576 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 restoreRedobuff();
8578 restore_search_patterns();
8579 error = ERROR_NONE;
8580 }
8581 }
8582 }
8583 else
8584 {
8585 /*
8586 * Find the function name in the table, call its implementation.
8587 */
8588 i = find_internal_func(fname);
8589 if (i >= 0)
8590 {
8591 if (argcount < functions[i].f_min_argc)
8592 error = ERROR_TOOFEW;
8593 else if (argcount > functions[i].f_max_argc)
8594 error = ERROR_TOOMANY;
8595 else
8596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 error = ERROR_NONE;
8600 }
8601 }
8602 }
8603 /*
8604 * The function call (or "FuncUndefined" autocommand sequence) might
8605 * have been aborted by an error, an interrupt, or an explicitly thrown
8606 * exception that has not been caught so far. This situation can be
8607 * tested for by calling aborting(). For an error in an internal
8608 * function or for the "E132" error in call_user_func(), however, the
8609 * throw point at which the "force_abort" flag (temporarily reset by
8610 * emsg()) is normally updated has not been reached yet. We need to
8611 * update that flag first to make aborting() reliable.
8612 */
8613 update_force_abort();
8614 }
8615 if (error == ERROR_NONE)
8616 ret = OK;
8617
8618 /*
8619 * Report an error unless the argument evaluation or function call has been
8620 * cancelled due to an aborting error, an interrupt, or an exception.
8621 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008622 if (!aborting())
8623 {
8624 switch (error)
8625 {
8626 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008627 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008628 break;
8629 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008630 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008631 break;
8632 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008633 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008634 name);
8635 break;
8636 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008637 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008638 name);
8639 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008641 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 name);
8643 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008644 }
8645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (fname != name && fname != fname_buf)
8648 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008649 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 return ret;
8652}
8653
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008654/*
8655 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008656 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 */
8658 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008659emsg_funcname(ermsg, name)
8660 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008661 char_u *name;
8662{
8663 char_u *p;
8664
8665 if (*name == K_SPECIAL)
8666 p = concat_str((char_u *)"<SNR>", name + 3);
8667 else
8668 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008669 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008670 if (p != name)
8671 vim_free(p);
8672}
8673
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008674/*
8675 * Return TRUE for a non-zero Number and a non-empty String.
8676 */
8677 static int
8678non_zero_arg(argvars)
8679 typval_T *argvars;
8680{
8681 return ((argvars[0].v_type == VAR_NUMBER
8682 && argvars[0].vval.v_number != 0)
8683 || (argvars[0].v_type == VAR_STRING
8684 && argvars[0].vval.v_string != NULL
8685 && *argvars[0].vval.v_string != NUL));
8686}
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688/*********************************************
8689 * Implementation of the built-in functions
8690 */
8691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008693static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8694
8695/*
8696 * Get the float value of "argvars[0]" into "f".
8697 * Returns FAIL when the argument is not a Number or Float.
8698 */
8699 static int
8700get_float_arg(argvars, f)
8701 typval_T *argvars;
8702 float_T *f;
8703{
8704 if (argvars[0].v_type == VAR_FLOAT)
8705 {
8706 *f = argvars[0].vval.v_float;
8707 return OK;
8708 }
8709 if (argvars[0].v_type == VAR_NUMBER)
8710 {
8711 *f = (float_T)argvars[0].vval.v_number;
8712 return OK;
8713 }
8714 EMSG(_("E808: Number or Float required"));
8715 return FAIL;
8716}
8717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008718/*
8719 * "abs(expr)" function
8720 */
8721 static void
8722f_abs(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 if (argvars[0].v_type == VAR_FLOAT)
8727 {
8728 rettv->v_type = VAR_FLOAT;
8729 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8730 }
8731 else
8732 {
8733 varnumber_T n;
8734 int error = FALSE;
8735
8736 n = get_tv_number_chk(&argvars[0], &error);
8737 if (error)
8738 rettv->vval.v_number = -1;
8739 else if (n > 0)
8740 rettv->vval.v_number = n;
8741 else
8742 rettv->vval.v_number = -n;
8743 }
8744}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008745
8746/*
8747 * "acos()" function
8748 */
8749 static void
8750f_acos(argvars, rettv)
8751 typval_T *argvars;
8752 typval_T *rettv;
8753{
8754 float_T f;
8755
8756 rettv->v_type = VAR_FLOAT;
8757 if (get_float_arg(argvars, &f) == OK)
8758 rettv->vval.v_float = acos(f);
8759 else
8760 rettv->vval.v_float = 0.0;
8761}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008762#endif
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 */
8767 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008768f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *argvars;
8770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008775 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008777 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008778 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008779 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008781 }
8782 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008783 EMSG(_(e_listreq));
8784}
8785
8786/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008787 * "and(expr, expr)" function
8788 */
8789 static void
8790f_and(argvars, rettv)
8791 typval_T *argvars;
8792 typval_T *rettv;
8793{
8794 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8795 & get_tv_number_chk(&argvars[1], NULL);
8796}
8797
8798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008799 * "append(lnum, string/list)" function
8800 */
8801 static void
8802f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805{
8806 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 list_T *l = NULL;
8809 listitem_T *li = NULL;
8810 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008811 long added = 0;
8812
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008813 /* When coming here from Insert mode, sync undo, so that this can be
8814 * undone separately from what was previously inserted. */
8815 if (u_sync_once == 2)
8816 {
8817 u_sync_once = 1; /* notify that u_sync() was called */
8818 u_sync(TRUE);
8819 }
8820
Bram Moolenaar0d660222005-01-07 21:51:51 +00008821 lnum = get_tv_lnum(argvars);
8822 if (lnum >= 0
8823 && lnum <= curbuf->b_ml.ml_line_count
8824 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008826 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008828 l = argvars[1].vval.v_list;
8829 if (l == NULL)
8830 return;
8831 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008833 for (;;)
8834 {
8835 if (l == NULL)
8836 tv = &argvars[1]; /* append a string */
8837 else if (li == NULL)
8838 break; /* end of list */
8839 else
8840 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 line = get_tv_string_chk(tv);
8842 if (line == NULL) /* type error */
8843 {
8844 rettv->vval.v_number = 1; /* Failed */
8845 break;
8846 }
8847 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008848 ++added;
8849 if (l == NULL)
8850 break;
8851 li = li->li_next;
8852 }
8853
8854 appended_lines_mark(lnum, added);
8855 if (curwin->w_cursor.lnum > lnum)
8856 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 else
8859 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860}
8861
8862/*
8863 * "argc()" function
8864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871}
8872
8873/*
8874 * "argidx()" function
8875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008878 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
8884/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008885 * "arglistid()" function
8886 */
8887 static void
8888f_arglistid(argvars, rettv)
8889 typval_T *argvars UNUSED;
8890 typval_T *rettv;
8891{
8892 win_T *wp;
8893 tabpage_T *tp = NULL;
8894 long n;
8895
8896 rettv->vval.v_number = -1;
8897 if (argvars[0].v_type != VAR_UNKNOWN)
8898 {
8899 if (argvars[1].v_type != VAR_UNKNOWN)
8900 {
8901 n = get_tv_number(&argvars[1]);
8902 if (n >= 0)
8903 tp = find_tabpage(n);
8904 }
8905 else
8906 tp = curtab;
8907
8908 if (tp != NULL)
8909 {
8910 wp = find_win_by_nr(&argvars[0], tp);
8911 if (wp != NULL)
8912 rettv->vval.v_number = wp->w_alist->id;
8913 }
8914 }
8915 else
8916 rettv->vval.v_number = curwin->w_alist->id;
8917}
8918
8919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * "argv(nr)" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 int idx;
8928
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008929 if (argvars[0].v_type != VAR_UNKNOWN)
8930 {
8931 idx = get_tv_number_chk(&argvars[0], NULL);
8932 if (idx >= 0 && idx < ARGCOUNT)
8933 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8934 else
8935 rettv->vval.v_string = NULL;
8936 rettv->v_type = VAR_STRING;
8937 }
8938 else if (rettv_list_alloc(rettv) == OK)
8939 for (idx = 0; idx < ARGCOUNT; ++idx)
8940 list_append_string(rettv->vval.v_list,
8941 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008945/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008946 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008947 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008948 static void
8949f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008951 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008952{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008953 float_T f;
8954
8955 rettv->v_type = VAR_FLOAT;
8956 if (get_float_arg(argvars, &f) == OK)
8957 rettv->vval.v_float = asin(f);
8958 else
8959 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008960}
8961
8962/*
8963 * "atan()" function
8964 */
8965 static void
8966f_atan(argvars, rettv)
8967 typval_T *argvars;
8968 typval_T *rettv;
8969{
8970 float_T f;
8971
8972 rettv->v_type = VAR_FLOAT;
8973 if (get_float_arg(argvars, &f) == OK)
8974 rettv->vval.v_float = atan(f);
8975 else
8976 rettv->vval.v_float = 0.0;
8977}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008978
8979/*
8980 * "atan2()" function
8981 */
8982 static void
8983f_atan2(argvars, rettv)
8984 typval_T *argvars;
8985 typval_T *rettv;
8986{
8987 float_T fx, fy;
8988
8989 rettv->v_type = VAR_FLOAT;
8990 if (get_float_arg(argvars, &fx) == OK
8991 && get_float_arg(&argvars[1], &fy) == OK)
8992 rettv->vval.v_float = atan2(fx, fy);
8993 else
8994 rettv->vval.v_float = 0.0;
8995}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996#endif
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998/*
8999 * "browse(save, title, initdir, default)" function
9000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009003 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006#ifdef FEAT_BROWSE
9007 int save;
9008 char_u *title;
9009 char_u *initdir;
9010 char_u *defname;
9011 char_u buf[NUMBUFLEN];
9012 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 save = get_tv_number_chk(&argvars[0], &error);
9016 title = get_tv_string_chk(&argvars[1]);
9017 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9018 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 if (error || title == NULL || initdir == NULL || defname == NULL)
9021 rettv->vval.v_string = NULL;
9022 else
9023 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009024 do_browse(save ? BROWSE_SAVE : 0,
9025 title, defname, NULL, initdir, NULL, curbuf);
9026#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009030}
9031
9032/*
9033 * "browsedir(title, initdir)" function
9034 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009039{
9040#ifdef FEAT_BROWSE
9041 char_u *title;
9042 char_u *initdir;
9043 char_u buf[NUMBUFLEN];
9044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 title = get_tv_string_chk(&argvars[0]);
9046 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 if (title == NULL || initdir == NULL)
9049 rettv->vval.v_string = NULL;
9050 else
9051 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009052 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
9058
Bram Moolenaar33570922005-01-25 22:26:29 +00009059static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009060
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061/*
9062 * Find a buffer by number or exact name.
9063 */
9064 static buf_T *
9065find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 if (avar->v_type == VAR_NUMBER)
9071 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009072 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009074 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009075 if (buf == NULL)
9076 {
9077 /* No full path name match, try a match with a URL or a "nofile"
9078 * buffer, these don't use the full path. */
9079 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9080 if (buf->b_fname != NULL
9081 && (path_with_url(buf->b_fname)
9082#ifdef FEAT_QUICKFIX
9083 || bt_nofile(buf)
9084#endif
9085 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009087 break;
9088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090 return buf;
9091}
9092
9093/*
9094 * "bufexists(expr)" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "buflisted(expr)" function
9106 */
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *argvars;
9110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 buf_T *buf;
9113
9114 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "bufloaded(expr)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 buf_T *buf;
9127
9128 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009132static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134/*
9135 * Get buffer by number or pattern.
9136 */
9137 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009138get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009140 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 int save_magic;
9144 char_u *save_cpo;
9145 buf_T *buf;
9146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 if (tv->v_type == VAR_NUMBER)
9148 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009149 if (tv->v_type != VAR_STRING)
9150 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (name == NULL || *name == NUL)
9152 return curbuf;
9153 if (name[0] == '$' && name[1] == NUL)
9154 return lastbuf;
9155
9156 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9157 save_magic = p_magic;
9158 p_magic = TRUE;
9159 save_cpo = p_cpo;
9160 p_cpo = (char_u *)"";
9161
9162 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009163 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 p_magic = save_magic;
9166 p_cpo = save_cpo;
9167
9168 /* If not found, try expanding the name, like done for bufexists(). */
9169 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171
9172 return buf;
9173}
9174
9175/*
9176 * "bufname(expr)" function
9177 */
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 buf_T *buf;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009187 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 --emsg_off;
9194}
9195
9196/*
9197 * "bufnr(expr)" function
9198 */
9199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *argvars;
9202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009205 int error = FALSE;
9206 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009211 --emsg_off;
9212
9213 /* If the buffer isn't found and the second argument is not zero create a
9214 * new buffer. */
9215 if (buf == NULL
9216 && argvars[1].v_type != VAR_UNKNOWN
9217 && get_tv_number_chk(&argvars[1], &error) != 0
9218 && !error
9219 && (name = get_tv_string_chk(&argvars[0])) != NULL
9220 && !error)
9221 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
9229/*
9230 * "bufwinnr(nr)" function
9231 */
9232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009234 typval_T *argvars;
9235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237#ifdef FEAT_WINDOWS
9238 win_T *wp;
9239 int winnr = 0;
9240#endif
9241 buf_T *buf;
9242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009245 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#ifdef FEAT_WINDOWS
9247 for (wp = firstwin; wp; wp = wp->w_next)
9248 {
9249 ++winnr;
9250 if (wp->w_buffer == buf)
9251 break;
9252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256#endif
9257 --emsg_off;
9258}
9259
9260/*
9261 * "byte2line(byte)" function
9262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#else
9271 long boff = 0;
9272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 (linenr_T)0, &boff);
9279#endif
9280}
9281
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009282 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009283byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009286 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009287{
9288#ifdef FEAT_MBYTE
9289 char_u *t;
9290#endif
9291 char_u *str;
9292 long idx;
9293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 str = get_tv_string_chk(&argvars[0]);
9295 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009298 return;
9299
9300#ifdef FEAT_MBYTE
9301 t = str;
9302 for ( ; idx > 0; idx--)
9303 {
9304 if (*t == NUL) /* EOL reached */
9305 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009306 if (enc_utf8 && comp)
9307 t += utf_ptr2len(t);
9308 else
9309 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009310 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009311 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009312#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009315#endif
9316}
9317
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009318/*
9319 * "byteidx()" function
9320 */
9321 static void
9322f_byteidx(argvars, rettv)
9323 typval_T *argvars;
9324 typval_T *rettv;
9325{
9326 byteidx(argvars, rettv, FALSE);
9327}
9328
9329/*
9330 * "byteidxcomp()" function
9331 */
9332 static void
9333f_byteidxcomp(argvars, rettv)
9334 typval_T *argvars;
9335 typval_T *rettv;
9336{
9337 byteidx(argvars, rettv, TRUE);
9338}
9339
Bram Moolenaardb913952012-06-29 12:54:53 +02009340 int
9341func_call(name, args, selfdict, rettv)
9342 char_u *name;
9343 typval_T *args;
9344 dict_T *selfdict;
9345 typval_T *rettv;
9346{
9347 listitem_T *item;
9348 typval_T argv[MAX_FUNC_ARGS + 1];
9349 int argc = 0;
9350 int dummy;
9351 int r = 0;
9352
9353 for (item = args->vval.v_list->lv_first; item != NULL;
9354 item = item->li_next)
9355 {
9356 if (argc == MAX_FUNC_ARGS)
9357 {
9358 EMSG(_("E699: Too many arguments"));
9359 break;
9360 }
9361 /* Make a copy of each argument. This is needed to be able to set
9362 * v_lock to VAR_FIXED in the copy without changing the original list.
9363 */
9364 copy_tv(&item->li_tv, &argv[argc++]);
9365 }
9366
9367 if (item == NULL)
9368 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9369 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9370 &dummy, TRUE, selfdict);
9371
9372 /* Free the arguments. */
9373 while (argc > 0)
9374 clear_tv(&argv[--argc]);
9375
9376 return r;
9377}
9378
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009379/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009380 * "call(func, arglist)" function
9381 */
9382 static void
9383f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009386{
9387 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009389
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009390 if (argvars[1].v_type != VAR_LIST)
9391 {
9392 EMSG(_(e_listreq));
9393 return;
9394 }
9395 if (argvars[1].vval.v_list == NULL)
9396 return;
9397
9398 if (argvars[0].v_type == VAR_FUNC)
9399 func = argvars[0].vval.v_string;
9400 else
9401 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009402 if (*func == NUL)
9403 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009404
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 if (argvars[2].v_type != VAR_UNKNOWN)
9406 {
9407 if (argvars[2].v_type != VAR_DICT)
9408 {
9409 EMSG(_(e_dictreq));
9410 return;
9411 }
9412 selfdict = argvars[2].vval.v_dict;
9413 }
9414
Bram Moolenaardb913952012-06-29 12:54:53 +02009415 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009416}
9417
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009418#ifdef FEAT_FLOAT
9419/*
9420 * "ceil({float})" function
9421 */
9422 static void
9423f_ceil(argvars, rettv)
9424 typval_T *argvars;
9425 typval_T *rettv;
9426{
9427 float_T f;
9428
9429 rettv->v_type = VAR_FLOAT;
9430 if (get_float_arg(argvars, &f) == OK)
9431 rettv->vval.v_float = ceil(f);
9432 else
9433 rettv->vval.v_float = 0.0;
9434}
9435#endif
9436
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009437/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009438 * "changenr()" function
9439 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009440 static void
9441f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009442 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009443 typval_T *rettv;
9444{
9445 rettv->vval.v_number = curbuf->b_u_seq_cur;
9446}
9447
9448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 * "char2nr(string)" function
9450 */
9451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009453 typval_T *argvars;
9454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_MBYTE
9457 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009458 {
9459 int utf8 = 0;
9460
9461 if (argvars[1].v_type != VAR_UNKNOWN)
9462 utf8 = get_tv_number_chk(&argvars[1], NULL);
9463
9464 if (utf8)
9465 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9466 else
9467 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 else
9470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
9474/*
9475 * "cindent(lnum)" function
9476 */
9477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
9482#ifdef FEAT_CINDENT
9483 pos_T pos;
9484 linenr_T lnum;
9485
9486 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9489 {
9490 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 curwin->w_cursor = pos;
9493 }
9494 else
9495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497}
9498
9499/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009500 * "clearmatches()" function
9501 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009502 static void
9503f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009504 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009505 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009506{
9507#ifdef FEAT_SEARCH_EXTRA
9508 clear_matches(curwin);
9509#endif
9510}
9511
9512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 * "col(string)" function
9514 */
9515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 typval_T *argvars;
9518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 colnr_T col = 0;
9521 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009522 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009524 fp = var2fpos(&argvars[0], FALSE, &fnum);
9525 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
9527 if (fp->col == MAXCOL)
9528 {
9529 /* '> can be MAXCOL, get the length of the line then */
9530 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009531 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 else
9533 col = MAXCOL;
9534 }
9535 else
9536 {
9537 col = fp->col + 1;
9538#ifdef FEAT_VIRTUALEDIT
9539 /* col(".") when the cursor is on the NUL at the end of the line
9540 * because of "coladd" can be seen as an extra column. */
9541 if (virtual_active() && fp == &curwin->w_cursor)
9542 {
9543 char_u *p = ml_get_cursor();
9544
9545 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9546 curwin->w_virtcol - curwin->w_cursor.coladd))
9547 {
9548# ifdef FEAT_MBYTE
9549 int l;
9550
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009551 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 col += l;
9553# else
9554 if (*p != NUL && p[1] == NUL)
9555 ++col;
9556# endif
9557 }
9558 }
9559#endif
9560 }
9561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
Bram Moolenaar572cb562005-08-05 21:35:02 +00009565#if defined(FEAT_INS_EXPAND)
9566/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009567 * "complete()" function
9568 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009569 static void
9570f_complete(argvars, rettv)
9571 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009572 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009573{
9574 int startcol;
9575
9576 if ((State & INSERT) == 0)
9577 {
9578 EMSG(_("E785: complete() can only be used in Insert mode"));
9579 return;
9580 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009581
9582 /* Check for undo allowed here, because if something was already inserted
9583 * the line was already saved for undo and this check isn't done. */
9584 if (!undo_allowed())
9585 return;
9586
Bram Moolenaarade00832006-03-10 21:46:58 +00009587 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9588 {
9589 EMSG(_(e_invarg));
9590 return;
9591 }
9592
9593 startcol = get_tv_number_chk(&argvars[0], NULL);
9594 if (startcol <= 0)
9595 return;
9596
9597 set_completion(startcol - 1, argvars[1].vval.v_list);
9598}
9599
9600/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009601 * "complete_add()" function
9602 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009603 static void
9604f_complete_add(argvars, rettv)
9605 typval_T *argvars;
9606 typval_T *rettv;
9607{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009608 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009609}
9610
9611/*
9612 * "complete_check()" function
9613 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009614 static void
9615f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009616 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009617 typval_T *rettv;
9618{
9619 int saved = RedrawingDisabled;
9620
9621 RedrawingDisabled = 0;
9622 ins_compl_check_keys(0);
9623 rettv->vval.v_number = compl_interrupted;
9624 RedrawingDisabled = saved;
9625}
9626#endif
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628/*
9629 * "confirm(message, buttons[, default [, type]])" function
9630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009633 typval_T *argvars UNUSED;
9634 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9637 char_u *message;
9638 char_u *buttons = NULL;
9639 char_u buf[NUMBUFLEN];
9640 char_u buf2[NUMBUFLEN];
9641 int def = 1;
9642 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 char_u *typestr;
9644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 message = get_tv_string_chk(&argvars[0]);
9647 if (message == NULL)
9648 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009649 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9652 if (buttons == NULL)
9653 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009657 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9660 if (typestr == NULL)
9661 error = TRUE;
9662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 switch (TOUPPER_ASC(*typestr))
9665 {
9666 case 'E': type = VIM_ERROR; break;
9667 case 'Q': type = VIM_QUESTION; break;
9668 case 'I': type = VIM_INFO; break;
9669 case 'W': type = VIM_WARNING; break;
9670 case 'G': type = VIM_GENERIC; break;
9671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 }
9673 }
9674 }
9675 }
9676
9677 if (buttons == NULL || *buttons == NUL)
9678 buttons = (char_u *)_("&Ok");
9679
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009680 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009681 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009682 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#endif
9684}
9685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686/*
9687 * "copy()" function
9688 */
9689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009691 typval_T *argvars;
9692 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009694 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009697#ifdef FEAT_FLOAT
9698/*
9699 * "cos()" function
9700 */
9701 static void
9702f_cos(argvars, rettv)
9703 typval_T *argvars;
9704 typval_T *rettv;
9705{
9706 float_T f;
9707
9708 rettv->v_type = VAR_FLOAT;
9709 if (get_float_arg(argvars, &f) == OK)
9710 rettv->vval.v_float = cos(f);
9711 else
9712 rettv->vval.v_float = 0.0;
9713}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009714
9715/*
9716 * "cosh()" function
9717 */
9718 static void
9719f_cosh(argvars, rettv)
9720 typval_T *argvars;
9721 typval_T *rettv;
9722{
9723 float_T f;
9724
9725 rettv->v_type = VAR_FLOAT;
9726 if (get_float_arg(argvars, &f) == OK)
9727 rettv->vval.v_float = cosh(f);
9728 else
9729 rettv->vval.v_float = 0.0;
9730}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009731#endif
9732
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009734 * "count()" function
9735 */
9736 static void
9737f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009740{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009741 long n = 0;
9742 int ic = FALSE;
9743
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009746 listitem_T *li;
9747 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009748 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009749
Bram Moolenaare9a41262005-01-15 22:18:47 +00009750 if ((l = argvars[0].vval.v_list) != NULL)
9751 {
9752 li = l->lv_first;
9753 if (argvars[2].v_type != VAR_UNKNOWN)
9754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 int error = FALSE;
9756
9757 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009758 if (argvars[3].v_type != VAR_UNKNOWN)
9759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009760 idx = get_tv_number_chk(&argvars[3], &error);
9761 if (!error)
9762 {
9763 li = list_find(l, idx);
9764 if (li == NULL)
9765 EMSGN(_(e_listidx), idx);
9766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 if (error)
9769 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 }
9771
9772 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009773 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009774 ++n;
9775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 else if (argvars[0].v_type == VAR_DICT)
9778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 int todo;
9780 dict_T *d;
9781 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009782
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009783 if ((d = argvars[0].vval.v_dict) != NULL)
9784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 int error = FALSE;
9786
Bram Moolenaare9a41262005-01-15 22:18:47 +00009787 if (argvars[2].v_type != VAR_UNKNOWN)
9788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009790 if (argvars[3].v_type != VAR_UNKNOWN)
9791 EMSG(_(e_invarg));
9792 }
9793
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009794 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009796 {
9797 if (!HASHITEM_EMPTY(hi))
9798 {
9799 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009800 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009801 ++n;
9802 }
9803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009804 }
9805 }
9806 else
9807 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 rettv->vval.v_number = n;
9809}
9810
9811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9813 *
9814 * Checks the existence of a cscope connection.
9815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821#ifdef FEAT_CSCOPE
9822 int num = 0;
9823 char_u *dbpath = NULL;
9824 char_u *prepend = NULL;
9825 char_u buf[NUMBUFLEN];
9826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009827 if (argvars[0].v_type != VAR_UNKNOWN
9828 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 num = (int)get_tv_number(&argvars[0]);
9831 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837#endif
9838}
9839
9840/*
9841 * "cursor(lnum, col)" function
9842 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009843 * Moves the cursor to the specified line and column.
9844 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009852#ifdef FEAT_VIRTUALEDIT
9853 long coladd = 0;
9854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009856 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009857 if (argvars[1].v_type == VAR_UNKNOWN)
9858 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009859 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009860 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009861
Bram Moolenaar493c1782014-05-28 14:34:46 +02009862 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009863 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009864 line = pos.lnum;
9865 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009866#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009867 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009868#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009869 if (curswant >= 0)
9870 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009871 }
9872 else
9873 {
9874 line = get_tv_lnum(argvars);
9875 col = get_tv_number_chk(&argvars[1], NULL);
9876#ifdef FEAT_VIRTUALEDIT
9877 if (argvars[2].v_type != VAR_UNKNOWN)
9878 coladd = get_tv_number_chk(&argvars[2], NULL);
9879#endif
9880 }
9881 if (line < 0 || col < 0
9882#ifdef FEAT_VIRTUALEDIT
9883 || coladd < 0
9884#endif
9885 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (line > 0)
9888 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (col > 0)
9890 curwin->w_cursor.col = col - 1;
9891#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009892 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893#endif
9894
9895 /* Make sure the cursor is in a valid position. */
9896 check_cursor();
9897#ifdef FEAT_MBYTE
9898 /* Correct cursor for multi-byte character. */
9899 if (has_mbyte)
9900 mb_adjust_cursor();
9901#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009902
9903 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009904 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009908 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 */
9910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 typval_T *argvars;
9913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009915 int noref = 0;
9916
9917 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009919 if (noref < 0 || noref > 1)
9920 EMSG(_(e_invarg));
9921 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009922 {
9923 current_copyID += COPYID_INC;
9924 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926}
9927
9928/*
9929 * "delete()" function
9930 */
9931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 typval_T *argvars;
9934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935{
9936 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940}
9941
9942/*
9943 * "did_filetype()" function
9944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009947 typval_T *argvars UNUSED;
9948 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
9950#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952#endif
9953}
9954
9955/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009956 * "diff_filler()" function
9957 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009960 typval_T *argvars UNUSED;
9961 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009962{
9963#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009965#endif
9966}
9967
9968/*
9969 * "diff_hlID()" function
9970 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009973 typval_T *argvars UNUSED;
9974 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009975{
9976#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009978 static linenr_T prev_lnum = 0;
9979 static int changedtick = 0;
9980 static int fnum = 0;
9981 static int change_start = 0;
9982 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009983 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009984 int filler_lines;
9985 int col;
9986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009987 if (lnum < 0) /* ignore type error in {lnum} arg */
9988 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009989 if (lnum != prev_lnum
9990 || changedtick != curbuf->b_changedtick
9991 || fnum != curbuf->b_fnum)
9992 {
9993 /* New line, buffer, change: need to get the values. */
9994 filler_lines = diff_check(curwin, lnum);
9995 if (filler_lines < 0)
9996 {
9997 if (filler_lines == -1)
9998 {
9999 change_start = MAXCOL;
10000 change_end = -1;
10001 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10002 hlID = HLF_ADD; /* added line */
10003 else
10004 hlID = HLF_CHD; /* changed line */
10005 }
10006 else
10007 hlID = HLF_ADD; /* added line */
10008 }
10009 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010010 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010011 prev_lnum = lnum;
10012 changedtick = curbuf->b_changedtick;
10013 fnum = curbuf->b_fnum;
10014 }
10015
10016 if (hlID == HLF_CHD || hlID == HLF_TXD)
10017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010019 if (col >= change_start && col <= change_end)
10020 hlID = HLF_TXD; /* changed text */
10021 else
10022 hlID = HLF_CHD; /* changed line */
10023 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010024 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010025#endif
10026}
10027
10028/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010029 * "empty({expr})" function
10030 */
10031 static void
10032f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010035{
10036 int n;
10037
10038 switch (argvars[0].v_type)
10039 {
10040 case VAR_STRING:
10041 case VAR_FUNC:
10042 n = argvars[0].vval.v_string == NULL
10043 || *argvars[0].vval.v_string == NUL;
10044 break;
10045 case VAR_NUMBER:
10046 n = argvars[0].vval.v_number == 0;
10047 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010048#ifdef FEAT_FLOAT
10049 case VAR_FLOAT:
10050 n = argvars[0].vval.v_float == 0.0;
10051 break;
10052#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010053 case VAR_LIST:
10054 n = argvars[0].vval.v_list == NULL
10055 || argvars[0].vval.v_list->lv_first == NULL;
10056 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010057 case VAR_DICT:
10058 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010059 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010061 default:
10062 EMSG2(_(e_intern2), "f_empty()");
10063 n = 0;
10064 }
10065
10066 rettv->vval.v_number = n;
10067}
10068
10069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 * "escape({string}, {chars})" function
10071 */
10072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010074 typval_T *argvars;
10075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 char_u buf[NUMBUFLEN];
10078
Bram Moolenaar758711c2005-02-02 23:11:38 +000010079 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10080 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082}
10083
10084/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010085 * "eval()" function
10086 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010087 static void
10088f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010091{
10092 char_u *s;
10093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 s = get_tv_string_chk(&argvars[0]);
10095 if (s != NULL)
10096 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10099 {
10100 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010103 else if (*s != NUL)
10104 EMSG(_(e_trailing));
10105}
10106
10107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 * "eventhandler()" function
10109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
10118/*
10119 * "executable()" function
10120 */
10121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010123 typval_T *argvars;
10124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010126 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10127}
10128
10129/*
10130 * "exepath()" function
10131 */
10132 static void
10133f_exepath(argvars, rettv)
10134 typval_T *argvars;
10135 typval_T *rettv;
10136{
10137 char_u *p = NULL;
10138
10139 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10140 rettv->v_type = VAR_STRING;
10141 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142}
10143
10144/*
10145 * "exists()" function
10146 */
10147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010149 typval_T *argvars;
10150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 char_u *p;
10153 char_u *name;
10154 int n = FALSE;
10155 int len = 0;
10156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (*p == '$') /* environment variable */
10159 {
10160 /* first try "normal" environment variables (fast) */
10161 if (mch_getenv(p + 1) != NULL)
10162 n = TRUE;
10163 else
10164 {
10165 /* try expanding things like $VIM and ${HOME} */
10166 p = expand_env_save(p);
10167 if (p != NULL && *p != '$')
10168 n = TRUE;
10169 vim_free(p);
10170 }
10171 }
10172 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010175 if (*skipwhite(p) != NUL)
10176 n = FALSE; /* trailing garbage */
10177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 else if (*p == '*') /* internal or user defined function */
10179 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010180 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 }
10182 else if (*p == ':')
10183 {
10184 n = cmd_exists(p + 1);
10185 }
10186 else if (*p == '#')
10187 {
10188#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010189 if (p[1] == '#')
10190 n = autocmd_supported(p + 2);
10191 else
10192 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193#endif
10194 }
10195 else /* internal variable */
10196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010197 char_u *tofree;
10198 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010200 /* get_name_len() takes care of expanding curly braces */
10201 name = p;
10202 len = get_name_len(&p, &tofree, TRUE, FALSE);
10203 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010205 if (tofree != NULL)
10206 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010207 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010208 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010210 /* handle d.key, l[idx], f(expr) */
10211 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10212 if (n)
10213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010216 if (*p != NUL)
10217 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010219 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 }
10221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223}
10224
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010225#ifdef FEAT_FLOAT
10226/*
10227 * "exp()" function
10228 */
10229 static void
10230f_exp(argvars, rettv)
10231 typval_T *argvars;
10232 typval_T *rettv;
10233{
10234 float_T f;
10235
10236 rettv->v_type = VAR_FLOAT;
10237 if (get_float_arg(argvars, &f) == OK)
10238 rettv->vval.v_float = exp(f);
10239 else
10240 rettv->vval.v_float = 0.0;
10241}
10242#endif
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244/*
10245 * "expand()" function
10246 */
10247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 typval_T *argvars;
10250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251{
10252 char_u *s;
10253 int len;
10254 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010255 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010258 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010261 if (argvars[1].v_type != VAR_UNKNOWN
10262 && argvars[2].v_type != VAR_UNKNOWN
10263 && get_tv_number_chk(&argvars[2], &error)
10264 && !error)
10265 {
10266 rettv->v_type = VAR_LIST;
10267 rettv->vval.v_list = NULL;
10268 }
10269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 if (*s == '%' || *s == '#' || *s == '<')
10272 {
10273 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010274 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010276 if (rettv->v_type == VAR_LIST)
10277 {
10278 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10279 list_append_string(rettv->vval.v_list, result, -1);
10280 else
10281 vim_free(result);
10282 }
10283 else
10284 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286 else
10287 {
10288 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010289 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 if (argvars[1].v_type != VAR_UNKNOWN
10291 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010292 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 if (!error)
10294 {
10295 ExpandInit(&xpc);
10296 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010297 if (p_wic)
10298 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010299 if (rettv->v_type == VAR_STRING)
10300 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10301 options, WILD_ALL);
10302 else if (rettv_list_alloc(rettv) != FAIL)
10303 {
10304 int i;
10305
10306 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10307 for (i = 0; i < xpc.xp_numfiles; i++)
10308 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10309 ExpandCleanup(&xpc);
10310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 }
10312 else
10313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315}
10316
10317/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010318 * Go over all entries in "d2" and add them to "d1".
10319 * When "action" is "error" then a duplicate key is an error.
10320 * When "action" is "force" then a duplicate key is overwritten.
10321 * Otherwise duplicate keys are ignored ("action" is "keep").
10322 */
10323 void
10324dict_extend(d1, d2, action)
10325 dict_T *d1;
10326 dict_T *d2;
10327 char_u *action;
10328{
10329 dictitem_T *di1;
10330 hashitem_T *hi2;
10331 int todo;
10332
10333 todo = (int)d2->dv_hashtab.ht_used;
10334 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10335 {
10336 if (!HASHITEM_EMPTY(hi2))
10337 {
10338 --todo;
10339 di1 = dict_find(d1, hi2->hi_key, -1);
10340 if (d1->dv_scope != 0)
10341 {
10342 /* Disallow replacing a builtin function in l: and g:.
10343 * Check the key to be valid when adding to any
10344 * scope. */
10345 if (d1->dv_scope == VAR_DEF_SCOPE
10346 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10347 && var_check_func_name(hi2->hi_key,
10348 di1 == NULL))
10349 break;
10350 if (!valid_varname(hi2->hi_key))
10351 break;
10352 }
10353 if (di1 == NULL)
10354 {
10355 di1 = dictitem_copy(HI2DI(hi2));
10356 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10357 dictitem_free(di1);
10358 }
10359 else if (*action == 'e')
10360 {
10361 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10362 break;
10363 }
10364 else if (*action == 'f' && HI2DI(hi2) != di1)
10365 {
10366 clear_tv(&di1->di_tv);
10367 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10368 }
10369 }
10370 }
10371}
10372
10373/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010374 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010375 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010376 */
10377 static void
10378f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010381{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010382 char *arg_errmsg = N_("extend() argument");
10383
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010386 list_T *l1, *l2;
10387 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010389 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010390
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 l1 = argvars[0].vval.v_list;
10392 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010393 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010394 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010395 {
10396 if (argvars[2].v_type != VAR_UNKNOWN)
10397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 before = get_tv_number_chk(&argvars[2], &error);
10399 if (error)
10400 return; /* type error; errmsg already given */
10401
Bram Moolenaar758711c2005-02-02 23:11:38 +000010402 if (before == l1->lv_len)
10403 item = NULL;
10404 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010406 item = list_find(l1, before);
10407 if (item == NULL)
10408 {
10409 EMSGN(_(e_listidx), before);
10410 return;
10411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010412 }
10413 }
10414 else
10415 item = NULL;
10416 list_extend(l1, l2, item);
10417
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 copy_tv(&argvars[0], rettv);
10419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10422 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010423 dict_T *d1, *d2;
10424 char_u *action;
10425 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426
10427 d1 = argvars[0].vval.v_dict;
10428 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010429 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010430 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010431 {
10432 /* Check the third argument. */
10433 if (argvars[2].v_type != VAR_UNKNOWN)
10434 {
10435 static char *(av[]) = {"keep", "force", "error"};
10436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 action = get_tv_string_chk(&argvars[2]);
10438 if (action == NULL)
10439 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010440 for (i = 0; i < 3; ++i)
10441 if (STRCMP(action, av[i]) == 0)
10442 break;
10443 if (i == 3)
10444 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010445 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 return;
10447 }
10448 }
10449 else
10450 action = (char_u *)"force";
10451
Bram Moolenaara9922d62013-05-30 13:01:18 +020010452 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 copy_tv(&argvars[0], rettv);
10455 }
10456 }
10457 else
10458 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010459}
10460
10461/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010462 * "feedkeys()" function
10463 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010464 static void
10465f_feedkeys(argvars, rettv)
10466 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010467 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010468{
10469 int remap = TRUE;
10470 char_u *keys, *flags;
10471 char_u nbuf[NUMBUFLEN];
10472 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010473 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010474
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010475 /* This is not allowed in the sandbox. If the commands would still be
10476 * executed in the sandbox it would be OK, but it probably happens later,
10477 * when "sandbox" is no longer set. */
10478 if (check_secure())
10479 return;
10480
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010481 keys = get_tv_string(&argvars[0]);
10482 if (*keys != NUL)
10483 {
10484 if (argvars[1].v_type != VAR_UNKNOWN)
10485 {
10486 flags = get_tv_string_buf(&argvars[1], nbuf);
10487 for ( ; *flags != NUL; ++flags)
10488 {
10489 switch (*flags)
10490 {
10491 case 'n': remap = FALSE; break;
10492 case 'm': remap = TRUE; break;
10493 case 't': typed = TRUE; break;
10494 }
10495 }
10496 }
10497
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010498 /* Need to escape K_SPECIAL and CSI before putting the string in the
10499 * typeahead buffer. */
10500 keys_esc = vim_strsave_escape_csi(keys);
10501 if (keys_esc != NULL)
10502 {
10503 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010504 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010505 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010506 if (vgetc_busy)
10507 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010508 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010509 }
10510}
10511
10512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 * "filereadable()" function
10514 */
10515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010520 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 char_u *p;
10522 int n;
10523
Bram Moolenaarc236c162008-07-13 17:41:49 +000010524#ifndef O_NONBLOCK
10525# define O_NONBLOCK 0
10526#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010528 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10529 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 {
10531 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010532 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534 else
10535 n = FALSE;
10536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539
10540/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010541 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 * rights to write into.
10543 */
10544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 typval_T *argvars;
10547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010549 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550}
10551
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010552static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010553
10554 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010555findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010558 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010559{
10560#ifdef FEAT_SEARCHPATH
10561 char_u *fname;
10562 char_u *fresult = NULL;
10563 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10564 char_u *p;
10565 char_u pathbuf[NUMBUFLEN];
10566 int count = 1;
10567 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010568 int error = FALSE;
10569#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010570
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010571 rettv->vval.v_string = NULL;
10572 rettv->v_type = VAR_STRING;
10573
10574#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010576
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10580 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010581 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 else
10583 {
10584 if (*p != NUL)
10585 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010588 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010590 }
10591
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010592 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10593 error = TRUE;
10594
10595 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 do
10598 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010599 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010600 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 fresult = find_file_in_path_option(first ? fname : NULL,
10602 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010603 0, first, path,
10604 find_what,
10605 curbuf->b_ffname,
10606 find_what == FINDFILE_DIR
10607 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010609
10610 if (fresult != NULL && rettv->v_type == VAR_LIST)
10611 list_append_string(rettv->vval.v_list, fresult, -1);
10612
10613 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010615
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010616 if (rettv->v_type == VAR_STRING)
10617 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010618#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010619}
10620
Bram Moolenaar33570922005-01-25 22:26:29 +000010621static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10622static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623
10624/*
10625 * Implementation of map() and filter().
10626 */
10627 static void
10628filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010631 int map;
10632{
10633 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 listitem_T *li, *nli;
10636 list_T *l = NULL;
10637 dictitem_T *di;
10638 hashtab_T *ht;
10639 hashitem_T *hi;
10640 dict_T *d = NULL;
10641 typval_T save_val;
10642 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010644 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010645 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10646 char *arg_errmsg = (map ? N_("map() argument")
10647 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010648 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010649 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010653 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010654 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655 return;
10656 }
10657 else if (argvars[0].v_type == VAR_DICT)
10658 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010659 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010660 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010661 return;
10662 }
10663 else
10664 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010665 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 return;
10667 }
10668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 expr = get_tv_string_buf_chk(&argvars[1], buf);
10670 /* On type errors, the preceding call has already displayed an error
10671 * message. Avoid a misleading error message for an empty string that
10672 * was not passed as argument. */
10673 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 prepare_vimvar(VV_VAL, &save_val);
10676 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010677
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010678 /* We reset "did_emsg" to be able to detect whether an error
10679 * occurred during evaluation of the expression. */
10680 save_did_emsg = did_emsg;
10681 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010682
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010683 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010684 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 vimvars[VV_KEY].vv_type = VAR_STRING;
10687
10688 ht = &d->dv_hashtab;
10689 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010690 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 if (!HASHITEM_EMPTY(hi))
10694 {
10695 --todo;
10696 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010697 if (tv_check_lock(di->di_tv.v_lock,
10698 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 break;
10700 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010701 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010702 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 break;
10704 if (!map && rem)
10705 dictitem_remove(d, di);
10706 clear_tv(&vimvars[VV_KEY].vv_tv);
10707 }
10708 }
10709 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 }
10711 else
10712 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010713 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 for (li = l->lv_first; li != NULL; li = nli)
10716 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010717 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010718 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010720 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010721 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010722 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010723 break;
10724 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010726 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010727 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010729
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010730 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010731 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010732
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010733 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735
10736 copy_tv(&argvars[0], rettv);
10737}
10738
10739 static int
10740filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 char_u *expr;
10743 int map;
10744 int *remp;
10745{
Bram Moolenaar33570922005-01-25 22:26:29 +000010746 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010748 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010751 s = expr;
10752 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010753 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010754 if (*s != NUL) /* check for trailing chars after expr */
10755 {
10756 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010757 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 }
10759 if (map)
10760 {
10761 /* map(): replace the list item value */
10762 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010763 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010764 *tv = rettv;
10765 }
10766 else
10767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010768 int error = FALSE;
10769
Bram Moolenaare9a41262005-01-15 22:18:47 +000010770 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 /* On type error, nothing has been removed; return FAIL to stop the
10774 * loop. The error message was given by get_tv_number_chk(). */
10775 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010776 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010778 retval = OK;
10779theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010781 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010782}
10783
10784/*
10785 * "filter()" function
10786 */
10787 static void
10788f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010791{
10792 filter_map(argvars, rettv, FALSE);
10793}
10794
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010795/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010796 * "finddir({fname}[, {path}[, {count}]])" function
10797 */
10798 static void
10799f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010800 typval_T *argvars;
10801 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010803 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804}
10805
10806/*
10807 * "findfile({fname}[, {path}[, {count}]])" function
10808 */
10809 static void
10810f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010814 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010815}
10816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010817#ifdef FEAT_FLOAT
10818/*
10819 * "float2nr({float})" function
10820 */
10821 static void
10822f_float2nr(argvars, rettv)
10823 typval_T *argvars;
10824 typval_T *rettv;
10825{
10826 float_T f;
10827
10828 if (get_float_arg(argvars, &f) == OK)
10829 {
10830 if (f < -0x7fffffff)
10831 rettv->vval.v_number = -0x7fffffff;
10832 else if (f > 0x7fffffff)
10833 rettv->vval.v_number = 0x7fffffff;
10834 else
10835 rettv->vval.v_number = (varnumber_T)f;
10836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010837}
10838
10839/*
10840 * "floor({float})" function
10841 */
10842 static void
10843f_floor(argvars, rettv)
10844 typval_T *argvars;
10845 typval_T *rettv;
10846{
10847 float_T f;
10848
10849 rettv->v_type = VAR_FLOAT;
10850 if (get_float_arg(argvars, &f) == OK)
10851 rettv->vval.v_float = floor(f);
10852 else
10853 rettv->vval.v_float = 0.0;
10854}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010855
10856/*
10857 * "fmod()" function
10858 */
10859 static void
10860f_fmod(argvars, rettv)
10861 typval_T *argvars;
10862 typval_T *rettv;
10863{
10864 float_T fx, fy;
10865
10866 rettv->v_type = VAR_FLOAT;
10867 if (get_float_arg(argvars, &fx) == OK
10868 && get_float_arg(&argvars[1], &fy) == OK)
10869 rettv->vval.v_float = fmod(fx, fy);
10870 else
10871 rettv->vval.v_float = 0.0;
10872}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010873#endif
10874
Bram Moolenaar0d660222005-01-07 21:51:51 +000010875/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010876 * "fnameescape({string})" function
10877 */
10878 static void
10879f_fnameescape(argvars, rettv)
10880 typval_T *argvars;
10881 typval_T *rettv;
10882{
10883 rettv->vval.v_string = vim_strsave_fnameescape(
10884 get_tv_string(&argvars[0]), FALSE);
10885 rettv->v_type = VAR_STRING;
10886}
10887
10888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 * "fnamemodify({fname}, {mods})" function
10890 */
10891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 char_u *fname;
10897 char_u *mods;
10898 int usedlen = 0;
10899 int len;
10900 char_u *fbuf = NULL;
10901 char_u buf[NUMBUFLEN];
10902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 fname = get_tv_string_chk(&argvars[0]);
10904 mods = get_tv_string_buf_chk(&argvars[1], buf);
10905 if (fname == NULL || mods == NULL)
10906 fname = NULL;
10907 else
10908 {
10909 len = (int)STRLEN(fname);
10910 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 vim_free(fbuf);
10919}
10920
Bram Moolenaar33570922005-01-25 22:26:29 +000010921static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922
10923/*
10924 * "foldclosed()" function
10925 */
10926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010928 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010930 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
10932#ifdef FEAT_FOLDING
10933 linenr_T lnum;
10934 linenr_T first, last;
10935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10938 {
10939 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10940 {
10941 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 return;
10946 }
10947 }
10948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950}
10951
10952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010953 * "foldclosed()" function
10954 */
10955 static void
10956f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959{
10960 foldclosed_both(argvars, rettv, FALSE);
10961}
10962
10963/*
10964 * "foldclosedend()" function
10965 */
10966 static void
10967f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970{
10971 foldclosed_both(argvars, rettv, TRUE);
10972}
10973
10974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 * "foldlevel()" function
10976 */
10977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010979 typval_T *argvars UNUSED;
10980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
10982#ifdef FEAT_FOLDING
10983 linenr_T lnum;
10984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989}
10990
10991/*
10992 * "foldtext()" function
10993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010996 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
10999#ifdef FEAT_FOLDING
11000 linenr_T lnum;
11001 char_u *s;
11002 char_u *r;
11003 int len;
11004 char *txt;
11005#endif
11006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 rettv->v_type = VAR_STRING;
11008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11011 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11012 <= curbuf->b_ml.ml_line_count
11013 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 {
11015 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11017 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 {
11019 if (!linewhite(lnum))
11020 break;
11021 ++lnum;
11022 }
11023
11024 /* Find interesting text in this line. */
11025 s = skipwhite(ml_get(lnum));
11026 /* skip C comment-start */
11027 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011030 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011032 {
11033 s = skipwhite(ml_get(lnum + 1));
11034 if (*s == '*')
11035 s = skipwhite(s + 1);
11036 }
11037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 txt = _("+-%s%3ld lines: ");
11039 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 + 20 /* for %3ld */
11042 + STRLEN(s))); /* concatenated */
11043 if (r != NULL)
11044 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11046 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11047 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 len = (int)STRLEN(r);
11049 STRCAT(r, s);
11050 /* remove 'foldmarker' and 'commentstring' */
11051 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054 }
11055#endif
11056}
11057
11058/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011059 * "foldtextresult(lnum)" function
11060 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011065{
11066#ifdef FEAT_FOLDING
11067 linenr_T lnum;
11068 char_u *text;
11069 char_u buf[51];
11070 foldinfo_T foldinfo;
11071 int fold_count;
11072#endif
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_STRING;
11075 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011076#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 /* treat illegal types and illegal string values for {lnum} the same */
11079 if (lnum < 0)
11080 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011081 fold_count = foldedCount(curwin, lnum, &foldinfo);
11082 if (fold_count > 0)
11083 {
11084 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11085 &foldinfo, buf);
11086 if (text == buf)
11087 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011089 }
11090#endif
11091}
11092
11093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 * "foreground()" function
11095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011098 typval_T *argvars UNUSED;
11099 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101#ifdef FEAT_GUI
11102 if (gui.in_use)
11103 gui_mch_set_foreground();
11104#else
11105# ifdef WIN32
11106 win32_set_foreground();
11107# endif
11108#endif
11109}
11110
11111/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011112 * "function()" function
11113 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011118{
11119 char_u *s;
11120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011124 /* Don't check an autoload name for existence here. */
11125 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011126 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011127 else
11128 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011129 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011130 {
11131 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011132 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011133
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011134 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11135 * also be called from another script. Using trans_function_name()
11136 * would also work, but some plugins depend on the name being
11137 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011138 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011139 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011140 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011141 if (rettv->vval.v_string != NULL)
11142 {
11143 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011144 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011145 }
11146 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011147 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011148 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 }
11151}
11152
11153/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011154 * "garbagecollect()" function
11155 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011156 static void
11157f_garbagecollect(argvars, rettv)
11158 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011159 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011160{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011161 /* This is postponed until we are back at the toplevel, because we may be
11162 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11163 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011164
11165 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11166 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011167}
11168
11169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170 * "get()" function
11171 */
11172 static void
11173f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011174 typval_T *argvars;
11175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176{
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 listitem_T *li;
11178 list_T *l;
11179 dictitem_T *di;
11180 dict_T *d;
11181 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011184 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 int error = FALSE;
11188
11189 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11190 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 else if (argvars[0].v_type == VAR_DICT)
11195 {
11196 if ((d = argvars[0].vval.v_dict) != NULL)
11197 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011198 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011199 if (di != NULL)
11200 tv = &di->di_tv;
11201 }
11202 }
11203 else
11204 EMSG2(_(e_listdictarg), "get()");
11205
11206 if (tv == NULL)
11207 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 copy_tv(&argvars[2], rettv);
11210 }
11211 else
11212 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011213}
11214
Bram Moolenaar342337a2005-07-21 21:11:17 +000011215static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011216
11217/*
11218 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011219 * Return a range (from start to end) of lines in rettv from the specified
11220 * buffer.
11221 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011222 */
11223 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011224get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011225 buf_T *buf;
11226 linenr_T start;
11227 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011228 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011229 typval_T *rettv;
11230{
11231 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011232
Bram Moolenaar959a1432013-12-14 12:17:38 +010011233 rettv->v_type = VAR_STRING;
11234 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011235 if (retlist && rettv_list_alloc(rettv) == FAIL)
11236 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011237
11238 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11239 return;
11240
11241 if (!retlist)
11242 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011243 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11244 p = ml_get_buf(buf, start, FALSE);
11245 else
11246 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011247 rettv->vval.v_string = vim_strsave(p);
11248 }
11249 else
11250 {
11251 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011252 return;
11253
11254 if (start < 1)
11255 start = 1;
11256 if (end > buf->b_ml.ml_line_count)
11257 end = buf->b_ml.ml_line_count;
11258 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011259 if (list_append_string(rettv->vval.v_list,
11260 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011261 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011262 }
11263}
11264
11265/*
11266 * "getbufline()" function
11267 */
11268 static void
11269f_getbufline(argvars, rettv)
11270 typval_T *argvars;
11271 typval_T *rettv;
11272{
11273 linenr_T lnum;
11274 linenr_T end;
11275 buf_T *buf;
11276
11277 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11278 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011279 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011280 --emsg_off;
11281
Bram Moolenaar661b1822005-07-28 22:36:45 +000011282 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011283 if (argvars[2].v_type == VAR_UNKNOWN)
11284 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011285 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011286 end = get_tv_lnum_buf(&argvars[2], buf);
11287
Bram Moolenaar342337a2005-07-21 21:11:17 +000011288 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011289}
11290
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291/*
11292 * "getbufvar()" function
11293 */
11294 static void
11295f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298{
11299 buf_T *buf;
11300 buf_T *save_curbuf;
11301 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011302 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011303 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11306 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011308 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011310 rettv->v_type = VAR_STRING;
11311 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011312
11313 if (buf != NULL && varname != NULL)
11314 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011315 /* set curbuf to be our buf, temporarily */
11316 save_curbuf = curbuf;
11317 curbuf = buf;
11318
Bram Moolenaar0d660222005-01-07 21:51:51 +000011319 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011320 {
11321 if (get_option_tv(&varname, rettv, TRUE) == OK)
11322 done = TRUE;
11323 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011324 else if (STRCMP(varname, "changedtick") == 0)
11325 {
11326 rettv->v_type = VAR_NUMBER;
11327 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011328 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011329 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330 else
11331 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011332 /* Look up the variable. */
11333 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11334 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11335 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011339 done = TRUE;
11340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011342
11343 /* restore previous notion of curbuf */
11344 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345 }
11346
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011347 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11348 /* use the default value */
11349 copy_tv(&argvars[2], rettv);
11350
Bram Moolenaar0d660222005-01-07 21:51:51 +000011351 --emsg_off;
11352}
11353
11354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 * "getchar()" function
11356 */
11357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *argvars;
11360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011365 /* Position the cursor. Needed after a message that ends in a space. */
11366 windgoto(msg_row, msg_col);
11367
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 ++no_mapping;
11369 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011370 for (;;)
11371 {
11372 if (argvars[0].v_type == VAR_UNKNOWN)
11373 /* getchar(): blocking wait. */
11374 n = safe_vgetc();
11375 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11376 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011377 n = vpeekc_any();
11378 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011379 /* illegal argument or getchar(0) and no char avail: return zero */
11380 n = 0;
11381 else
11382 /* getchar(0) and char avail: return char */
11383 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011384
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011385 if (n == K_IGNORE)
11386 continue;
11387 break;
11388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 --no_mapping;
11390 --allow_keys;
11391
Bram Moolenaar219b8702006-11-01 14:32:36 +000011392 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11393 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11394 vimvars[VV_MOUSE_COL].vv_nr = 0;
11395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 if (IS_SPECIAL(n) || mod_mask != 0)
11398 {
11399 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11400 int i = 0;
11401
11402 /* Turn a special key into three bytes, plus modifier. */
11403 if (mod_mask != 0)
11404 {
11405 temp[i++] = K_SPECIAL;
11406 temp[i++] = KS_MODIFIER;
11407 temp[i++] = mod_mask;
11408 }
11409 if (IS_SPECIAL(n))
11410 {
11411 temp[i++] = K_SPECIAL;
11412 temp[i++] = K_SECOND(n);
11413 temp[i++] = K_THIRD(n);
11414 }
11415#ifdef FEAT_MBYTE
11416 else if (has_mbyte)
11417 i += (*mb_char2bytes)(n, temp + i);
11418#endif
11419 else
11420 temp[i++] = n;
11421 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->v_type = VAR_STRING;
11423 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011424
11425#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011426 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011427 {
11428 int row = mouse_row;
11429 int col = mouse_col;
11430 win_T *win;
11431 linenr_T lnum;
11432# ifdef FEAT_WINDOWS
11433 win_T *wp;
11434# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011435 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011436
11437 if (row >= 0 && col >= 0)
11438 {
11439 /* Find the window at the mouse coordinates and compute the
11440 * text position. */
11441 win = mouse_find_win(&row, &col);
11442 (void)mouse_comp_pos(win, &row, &col, &lnum);
11443# ifdef FEAT_WINDOWS
11444 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011445 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011446# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011447 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011448 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11449 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11450 }
11451 }
11452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 }
11454}
11455
11456/*
11457 * "getcharmod()" function
11458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * "getcmdline()" function
11469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->v_type = VAR_STRING;
11476 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477}
11478
11479/*
11480 * "getcmdpos()" function
11481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011491 * "getcmdtype()" function
11492 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011493 static void
11494f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011495 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011496 typval_T *rettv;
11497{
11498 rettv->v_type = VAR_STRING;
11499 rettv->vval.v_string = alloc(2);
11500 if (rettv->vval.v_string != NULL)
11501 {
11502 rettv->vval.v_string[0] = get_cmdline_type();
11503 rettv->vval.v_string[1] = NUL;
11504 }
11505}
11506
11507/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011508 * "getcmdwintype()" function
11509 */
11510 static void
11511f_getcmdwintype(argvars, rettv)
11512 typval_T *argvars UNUSED;
11513 typval_T *rettv;
11514{
11515 rettv->v_type = VAR_STRING;
11516 rettv->vval.v_string = NULL;
11517#ifdef FEAT_CMDWIN
11518 rettv->vval.v_string = alloc(2);
11519 if (rettv->vval.v_string != NULL)
11520 {
11521 rettv->vval.v_string[0] = cmdwin_type;
11522 rettv->vval.v_string[1] = NUL;
11523 }
11524#endif
11525}
11526
11527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 * "getcwd()" function
11529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011532 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011535 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011538 rettv->vval.v_string = NULL;
11539 cwd = alloc(MAXPATHL);
11540 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011542 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11543 {
11544 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011546 if (rettv->vval.v_string != NULL)
11547 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011549 }
11550 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 }
11552}
11553
11554/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011555 * "getfontname()" function
11556 */
11557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->v_type = VAR_STRING;
11563 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011564#ifdef FEAT_GUI
11565 if (gui.in_use)
11566 {
11567 GuiFont font;
11568 char_u *name = NULL;
11569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011570 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011571 {
11572 /* Get the "Normal" font. Either the name saved by
11573 * hl_set_font_name() or from the font ID. */
11574 font = gui.norm_font;
11575 name = hl_get_font_name();
11576 }
11577 else
11578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011580 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11581 return;
11582 font = gui_mch_get_font(name, FALSE);
11583 if (font == NOFONT)
11584 return; /* Invalid font name, return empty string. */
11585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011588 gui_mch_free_font(font);
11589 }
11590#endif
11591}
11592
11593/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594 * "getfperm({fname})" function
11595 */
11596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *argvars;
11599 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011600{
11601 char_u *fname;
11602 struct stat st;
11603 char_u *perm = NULL;
11604 char_u flags[] = "rwx";
11605 int i;
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011610 if (mch_stat((char *)fname, &st) >= 0)
11611 {
11612 perm = vim_strsave((char_u *)"---------");
11613 if (perm != NULL)
11614 {
11615 for (i = 0; i < 9; i++)
11616 {
11617 if (st.st_mode & (1 << (8 - i)))
11618 perm[i] = flags[i % 3];
11619 }
11620 }
11621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011623}
11624
11625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 * "getfsize({fname})" function
11627 */
11628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 typval_T *argvars;
11631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632{
11633 char_u *fname;
11634 struct stat st;
11635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
11640 if (mch_stat((char *)fname, &st) >= 0)
11641 {
11642 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011647
11648 /* non-perfect check for overflow */
11649 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11650 rettv->vval.v_number = -2;
11651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 }
11653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655}
11656
11657/*
11658 * "getftime({fname})" function
11659 */
11660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 typval_T *argvars;
11663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
11665 char_u *fname;
11666 struct stat st;
11667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669
11670 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674}
11675
11676/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011677 * "getftype({fname})" function
11678 */
11679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011683{
11684 char_u *fname;
11685 struct stat st;
11686 char_u *type = NULL;
11687 char *t;
11688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011692 if (mch_lstat((char *)fname, &st) >= 0)
11693 {
11694#ifdef S_ISREG
11695 if (S_ISREG(st.st_mode))
11696 t = "file";
11697 else if (S_ISDIR(st.st_mode))
11698 t = "dir";
11699# ifdef S_ISLNK
11700 else if (S_ISLNK(st.st_mode))
11701 t = "link";
11702# endif
11703# ifdef S_ISBLK
11704 else if (S_ISBLK(st.st_mode))
11705 t = "bdev";
11706# endif
11707# ifdef S_ISCHR
11708 else if (S_ISCHR(st.st_mode))
11709 t = "cdev";
11710# endif
11711# ifdef S_ISFIFO
11712 else if (S_ISFIFO(st.st_mode))
11713 t = "fifo";
11714# endif
11715# ifdef S_ISSOCK
11716 else if (S_ISSOCK(st.st_mode))
11717 t = "fifo";
11718# endif
11719 else
11720 t = "other";
11721#else
11722# ifdef S_IFMT
11723 switch (st.st_mode & S_IFMT)
11724 {
11725 case S_IFREG: t = "file"; break;
11726 case S_IFDIR: t = "dir"; break;
11727# ifdef S_IFLNK
11728 case S_IFLNK: t = "link"; break;
11729# endif
11730# ifdef S_IFBLK
11731 case S_IFBLK: t = "bdev"; break;
11732# endif
11733# ifdef S_IFCHR
11734 case S_IFCHR: t = "cdev"; break;
11735# endif
11736# ifdef S_IFIFO
11737 case S_IFIFO: t = "fifo"; break;
11738# endif
11739# ifdef S_IFSOCK
11740 case S_IFSOCK: t = "socket"; break;
11741# endif
11742 default: t = "other";
11743 }
11744# else
11745 if (mch_isdir(fname))
11746 t = "dir";
11747 else
11748 t = "file";
11749# endif
11750#endif
11751 type = vim_strsave((char_u *)t);
11752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011754}
11755
11756/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011757 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011758 */
11759 static void
11760f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011763{
11764 linenr_T lnum;
11765 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011766 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011767
11768 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011769 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011770 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011771 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011772 retlist = FALSE;
11773 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011775 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011776 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011777 retlist = TRUE;
11778 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011779
Bram Moolenaar342337a2005-07-21 21:11:17 +000011780 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781}
11782
11783/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011784 * "getmatches()" function
11785 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011786 static void
11787f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011788 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011789 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011790{
11791#ifdef FEAT_SEARCH_EXTRA
11792 dict_T *dict;
11793 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011794 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011795
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011796 if (rettv_list_alloc(rettv) == OK)
11797 {
11798 while (cur != NULL)
11799 {
11800 dict = dict_alloc();
11801 if (dict == NULL)
11802 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011803 if (cur->match.regprog == NULL)
11804 {
11805 /* match added with matchaddpos() */
11806 for (i = 0; i < MAXPOSMATCH; ++i)
11807 {
11808 llpos_T *llpos;
11809 char buf[6];
11810 list_T *l;
11811
11812 llpos = &cur->pos.pos[i];
11813 if (llpos->lnum == 0)
11814 break;
11815 l = list_alloc();
11816 if (l == NULL)
11817 break;
11818 list_append_number(l, (varnumber_T)llpos->lnum);
11819 if (llpos->col > 0)
11820 {
11821 list_append_number(l, (varnumber_T)llpos->col);
11822 list_append_number(l, (varnumber_T)llpos->len);
11823 }
11824 sprintf(buf, "pos%d", i + 1);
11825 dict_add_list(dict, buf, l);
11826 }
11827 }
11828 else
11829 {
11830 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11831 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011832 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011833 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11834 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11835 list_append_dict(rettv->vval.v_list, dict);
11836 cur = cur->next;
11837 }
11838 }
11839#endif
11840}
11841
11842/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011843 * "getpid()" function
11844 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011845 static void
11846f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011847 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011848 typval_T *rettv;
11849{
11850 rettv->vval.v_number = mch_get_pid();
11851}
11852
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011853static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11854
11855/*
11856 * "getcurpos()" function
11857 */
11858 static void
11859f_getcurpos(argvars, rettv)
11860 typval_T *argvars;
11861 typval_T *rettv;
11862{
11863 getpos_both(argvars, rettv, TRUE);
11864}
11865
Bram Moolenaar18081e32008-02-20 19:11:07 +000011866/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011867 * "getpos(string)" function
11868 */
11869 static void
11870f_getpos(argvars, rettv)
11871 typval_T *argvars;
11872 typval_T *rettv;
11873{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011874 getpos_both(argvars, rettv, FALSE);
11875}
11876
11877 static void
11878getpos_both(argvars, rettv, getcurpos)
11879 typval_T *argvars;
11880 typval_T *rettv;
11881 int getcurpos;
11882{
Bram Moolenaara5525202006-03-02 22:52:09 +000011883 pos_T *fp;
11884 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011885 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011886
11887 if (rettv_list_alloc(rettv) == OK)
11888 {
11889 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011890 if (getcurpos)
11891 fp = &curwin->w_cursor;
11892 else
11893 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011894 if (fnum != -1)
11895 list_append_number(l, (varnumber_T)fnum);
11896 else
11897 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011898 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11899 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011900 list_append_number(l, (fp != NULL)
11901 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011902 : (varnumber_T)0);
11903 list_append_number(l,
11904#ifdef FEAT_VIRTUALEDIT
11905 (fp != NULL) ? (varnumber_T)fp->coladd :
11906#endif
11907 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011908 if (getcurpos)
Bram Moolenaar493c1782014-05-28 14:34:46 +020011909 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011910 }
11911 else
11912 rettv->vval.v_number = FALSE;
11913}
11914
11915/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011916 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011917 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011918 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011919f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011920 typval_T *argvars UNUSED;
11921 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011922{
11923#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011924 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011925#endif
11926
Bram Moolenaar2641f772005-03-25 21:58:17 +000011927#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011928 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011929 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011930 wp = NULL;
11931 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11932 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011933 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011934 if (wp == NULL)
11935 return;
11936 }
11937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011938 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011939 }
11940#endif
11941}
11942
11943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 * "getreg()" function
11945 */
11946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950{
11951 char_u *strregname;
11952 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011953 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011954 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 strregname = get_tv_string_chk(&argvars[0]);
11960 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011961 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011964 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11965 return_list = get_tv_number_chk(&argvars[2], &error);
11966 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011969 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011970
11971 if (error)
11972 return;
11973
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 regname = (strregname == NULL ? '"' : *strregname);
11975 if (regname == 0)
11976 regname = '"';
11977
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011978 if (return_list)
11979 {
11980 rettv->v_type = VAR_LIST;
11981 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11982 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11983 }
11984 else
11985 {
11986 rettv->v_type = VAR_STRING;
11987 rettv->vval.v_string = get_reg_contents(regname,
11988 arg2 ? GREG_EXPR_SRC : 0);
11989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
11993 * "getregtype()" function
11994 */
11995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 typval_T *argvars;
11998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
12000 char_u *strregname;
12001 int regname;
12002 char_u buf[NUMBUFLEN + 2];
12003 long reglen = 0;
12004
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012005 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 {
12007 strregname = get_tv_string_chk(&argvars[0]);
12008 if (strregname == NULL) /* type error; errmsg already given */
12009 {
12010 rettv->v_type = VAR_STRING;
12011 rettv->vval.v_string = NULL;
12012 return;
12013 }
12014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 else
12016 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
12019 regname = (strregname == NULL ? '"' : *strregname);
12020 if (regname == 0)
12021 regname = '"';
12022
12023 buf[0] = NUL;
12024 buf[1] = NUL;
12025 switch (get_reg_type(regname, &reglen))
12026 {
12027 case MLINE: buf[0] = 'V'; break;
12028 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 case MBLOCK:
12030 buf[0] = Ctrl_V;
12031 sprintf((char *)buf + 1, "%ld", reglen + 1);
12032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034 rettv->v_type = VAR_STRING;
12035 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036}
12037
12038/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012039 * "gettabvar()" function
12040 */
12041 static void
12042f_gettabvar(argvars, rettv)
12043 typval_T *argvars;
12044 typval_T *rettv;
12045{
12046 tabpage_T *tp;
12047 dictitem_T *v;
12048 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012049 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012050
12051 rettv->v_type = VAR_STRING;
12052 rettv->vval.v_string = NULL;
12053
12054 varname = get_tv_string_chk(&argvars[1]);
12055 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12056 if (tp != NULL && varname != NULL)
12057 {
12058 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020012059 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012060 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012061 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012062 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012063 done = TRUE;
12064 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012065 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012066
12067 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012068 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012069}
12070
12071/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012072 * "gettabwinvar()" function
12073 */
12074 static void
12075f_gettabwinvar(argvars, rettv)
12076 typval_T *argvars;
12077 typval_T *rettv;
12078{
12079 getwinvar(argvars, rettv, 1);
12080}
12081
12082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 * "getwinposx()" function
12084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef FEAT_GUI
12092 if (gui.in_use)
12093 {
12094 int x, y;
12095
12096 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 }
12099#endif
12100}
12101
12102/*
12103 * "getwinposy()" function
12104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012107 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111#ifdef FEAT_GUI
12112 if (gui.in_use)
12113 {
12114 int x, y;
12115
12116 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
12119#endif
12120}
12121
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012122/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012123 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012124 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012125 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012126find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012127 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012128 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012129{
12130#ifdef FEAT_WINDOWS
12131 win_T *wp;
12132#endif
12133 int nr;
12134
12135 nr = get_tv_number_chk(vp, NULL);
12136
12137#ifdef FEAT_WINDOWS
12138 if (nr < 0)
12139 return NULL;
12140 if (nr == 0)
12141 return curwin;
12142
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012143 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12144 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012145 if (--nr <= 0)
12146 break;
12147 return wp;
12148#else
12149 if (nr == 0 || nr == 1)
12150 return curwin;
12151 return NULL;
12152#endif
12153}
12154
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155/*
12156 * "getwinvar()" function
12157 */
12158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012163 getwinvar(argvars, rettv, 0);
12164}
12165
12166/*
12167 * getwinvar() and gettabwinvar()
12168 */
12169 static void
12170getwinvar(argvars, rettv, off)
12171 typval_T *argvars;
12172 typval_T *rettv;
12173 int off; /* 1 for gettabwinvar() */
12174{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 win_T *win, *oldcurwin;
12176 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012178 tabpage_T *tp = NULL;
12179 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012180 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012182#ifdef FEAT_WINDOWS
12183 if (off == 1)
12184 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12185 else
12186 tp = curtab;
12187#endif
12188 win = find_win_by_nr(&argvars[off], tp);
12189 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012190 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012192 rettv->v_type = VAR_STRING;
12193 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
12195 if (win != NULL && varname != NULL)
12196 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012197 /* Set curwin to be our win, temporarily. Also set the tabpage,
12198 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012199 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012200
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012202 {
12203 if (get_option_tv(&varname, rettv, 1) == OK)
12204 done = TRUE;
12205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 else
12207 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012208 /* Look up the variable. */
12209 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12210 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012213 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012214 done = TRUE;
12215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012217
12218 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012219 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 }
12221
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012222 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12223 /* use the default return value */
12224 copy_tv(&argvars[off + 2], rettv);
12225
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 --emsg_off;
12227}
12228
12229/*
12230 * "glob()" function
12231 */
12232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *argvars;
12235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012237 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012241 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012242 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012244 if (argvars[1].v_type != VAR_UNKNOWN)
12245 {
12246 if (get_tv_number_chk(&argvars[1], &error))
12247 options |= WILD_KEEP_ALL;
12248 if (argvars[2].v_type != VAR_UNKNOWN
12249 && get_tv_number_chk(&argvars[2], &error))
12250 {
12251 rettv->v_type = VAR_LIST;
12252 rettv->vval.v_list = NULL;
12253 }
12254 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012255 if (!error)
12256 {
12257 ExpandInit(&xpc);
12258 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012259 if (p_wic)
12260 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012261 if (rettv->v_type == VAR_STRING)
12262 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012263 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012264 else if (rettv_list_alloc(rettv) != FAIL)
12265 {
12266 int i;
12267
12268 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12269 NULL, options, WILD_ALL_KEEP);
12270 for (i = 0; i < xpc.xp_numfiles; i++)
12271 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12272
12273 ExpandCleanup(&xpc);
12274 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012275 }
12276 else
12277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278}
12279
12280/*
12281 * "globpath()" function
12282 */
12283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012288 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012291 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012292 garray_T ga;
12293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012295 /* When the optional second argument is non-zero, don't remove matches
12296 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012298 if (argvars[2].v_type != VAR_UNKNOWN)
12299 {
12300 if (get_tv_number_chk(&argvars[2], &error))
12301 flags |= WILD_KEEP_ALL;
12302 if (argvars[3].v_type != VAR_UNKNOWN
12303 && get_tv_number_chk(&argvars[3], &error))
12304 {
12305 rettv->v_type = VAR_LIST;
12306 rettv->vval.v_list = NULL;
12307 }
12308 }
12309 if (file != NULL && !error)
12310 {
12311 ga_init2(&ga, (int)sizeof(char_u *), 10);
12312 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12313 if (rettv->v_type == VAR_STRING)
12314 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12315 else if (rettv_list_alloc(rettv) != FAIL)
12316 for (i = 0; i < ga.ga_len; ++i)
12317 list_append_string(rettv->vval.v_list,
12318 ((char_u **)(ga.ga_data))[i], -1);
12319 ga_clear_strings(&ga);
12320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323}
12324
12325/*
12326 * "has()" function
12327 */
12328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *argvars;
12331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332{
12333 int i;
12334 char_u *name;
12335 int n = FALSE;
12336 static char *(has_list[]) =
12337 {
12338#ifdef AMIGA
12339 "amiga",
12340# ifdef FEAT_ARP
12341 "arp",
12342# endif
12343#endif
12344#ifdef __BEOS__
12345 "beos",
12346#endif
12347#ifdef MSDOS
12348# ifdef DJGPP
12349 "dos32",
12350# else
12351 "dos16",
12352# endif
12353#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012354#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 "mac",
12356#endif
12357#if defined(MACOS_X_UNIX)
12358 "macunix",
12359#endif
12360#ifdef OS2
12361 "os2",
12362#endif
12363#ifdef __QNX__
12364 "qnx",
12365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#ifdef UNIX
12367 "unix",
12368#endif
12369#ifdef VMS
12370 "vms",
12371#endif
12372#ifdef WIN16
12373 "win16",
12374#endif
12375#ifdef WIN32
12376 "win32",
12377#endif
12378#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12379 "win32unix",
12380#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012381#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 "win64",
12383#endif
12384#ifdef EBCDIC
12385 "ebcdic",
12386#endif
12387#ifndef CASE_INSENSITIVE_FILENAME
12388 "fname_case",
12389#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012390#ifdef HAVE_ACL
12391 "acl",
12392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393#ifdef FEAT_ARABIC
12394 "arabic",
12395#endif
12396#ifdef FEAT_AUTOCMD
12397 "autocmd",
12398#endif
12399#ifdef FEAT_BEVAL
12400 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012401# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12402 "balloon_multiline",
12403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404#endif
12405#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12406 "builtin_terms",
12407# ifdef ALL_BUILTIN_TCAPS
12408 "all_builtin_terms",
12409# endif
12410#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012411#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12412 || defined(FEAT_GUI_W32) \
12413 || defined(FEAT_GUI_MOTIF))
12414 "browsefilter",
12415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416#ifdef FEAT_BYTEOFF
12417 "byte_offset",
12418#endif
12419#ifdef FEAT_CINDENT
12420 "cindent",
12421#endif
12422#ifdef FEAT_CLIENTSERVER
12423 "clientserver",
12424#endif
12425#ifdef FEAT_CLIPBOARD
12426 "clipboard",
12427#endif
12428#ifdef FEAT_CMDL_COMPL
12429 "cmdline_compl",
12430#endif
12431#ifdef FEAT_CMDHIST
12432 "cmdline_hist",
12433#endif
12434#ifdef FEAT_COMMENTS
12435 "comments",
12436#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012437#ifdef FEAT_CONCEAL
12438 "conceal",
12439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440#ifdef FEAT_CRYPT
12441 "cryptv",
12442#endif
12443#ifdef FEAT_CSCOPE
12444 "cscope",
12445#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012446#ifdef FEAT_CURSORBIND
12447 "cursorbind",
12448#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012449#ifdef CURSOR_SHAPE
12450 "cursorshape",
12451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452#ifdef DEBUG
12453 "debug",
12454#endif
12455#ifdef FEAT_CON_DIALOG
12456 "dialog_con",
12457#endif
12458#ifdef FEAT_GUI_DIALOG
12459 "dialog_gui",
12460#endif
12461#ifdef FEAT_DIFF
12462 "diff",
12463#endif
12464#ifdef FEAT_DIGRAPHS
12465 "digraphs",
12466#endif
12467#ifdef FEAT_DND
12468 "dnd",
12469#endif
12470#ifdef FEAT_EMACS_TAGS
12471 "emacs_tags",
12472#endif
12473 "eval", /* always present, of course! */
12474#ifdef FEAT_EX_EXTRA
12475 "ex_extra",
12476#endif
12477#ifdef FEAT_SEARCH_EXTRA
12478 "extra_search",
12479#endif
12480#ifdef FEAT_FKMAP
12481 "farsi",
12482#endif
12483#ifdef FEAT_SEARCHPATH
12484 "file_in_path",
12485#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012486#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012487 "filterpipe",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_FIND_ID
12490 "find_in_path",
12491#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012492#ifdef FEAT_FLOAT
12493 "float",
12494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495#ifdef FEAT_FOLDING
12496 "folding",
12497#endif
12498#ifdef FEAT_FOOTER
12499 "footer",
12500#endif
12501#if !defined(USE_SYSTEM) && defined(UNIX)
12502 "fork",
12503#endif
12504#ifdef FEAT_GETTEXT
12505 "gettext",
12506#endif
12507#ifdef FEAT_GUI
12508 "gui",
12509#endif
12510#ifdef FEAT_GUI_ATHENA
12511# ifdef FEAT_GUI_NEXTAW
12512 "gui_neXtaw",
12513# else
12514 "gui_athena",
12515# endif
12516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517#ifdef FEAT_GUI_GTK
12518 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012521#ifdef FEAT_GUI_GNOME
12522 "gui_gnome",
12523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524#ifdef FEAT_GUI_MAC
12525 "gui_mac",
12526#endif
12527#ifdef FEAT_GUI_MOTIF
12528 "gui_motif",
12529#endif
12530#ifdef FEAT_GUI_PHOTON
12531 "gui_photon",
12532#endif
12533#ifdef FEAT_GUI_W16
12534 "gui_win16",
12535#endif
12536#ifdef FEAT_GUI_W32
12537 "gui_win32",
12538#endif
12539#ifdef FEAT_HANGULIN
12540 "hangul_input",
12541#endif
12542#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12543 "iconv",
12544#endif
12545#ifdef FEAT_INS_EXPAND
12546 "insert_expand",
12547#endif
12548#ifdef FEAT_JUMPLIST
12549 "jumplist",
12550#endif
12551#ifdef FEAT_KEYMAP
12552 "keymap",
12553#endif
12554#ifdef FEAT_LANGMAP
12555 "langmap",
12556#endif
12557#ifdef FEAT_LIBCALL
12558 "libcall",
12559#endif
12560#ifdef FEAT_LINEBREAK
12561 "linebreak",
12562#endif
12563#ifdef FEAT_LISP
12564 "lispindent",
12565#endif
12566#ifdef FEAT_LISTCMDS
12567 "listcmds",
12568#endif
12569#ifdef FEAT_LOCALMAP
12570 "localmap",
12571#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012572#ifdef FEAT_LUA
12573# ifndef DYNAMIC_LUA
12574 "lua",
12575# endif
12576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577#ifdef FEAT_MENU
12578 "menu",
12579#endif
12580#ifdef FEAT_SESSION
12581 "mksession",
12582#endif
12583#ifdef FEAT_MODIFY_FNAME
12584 "modify_fname",
12585#endif
12586#ifdef FEAT_MOUSE
12587 "mouse",
12588#endif
12589#ifdef FEAT_MOUSESHAPE
12590 "mouseshape",
12591#endif
12592#if defined(UNIX) || defined(VMS)
12593# ifdef FEAT_MOUSE_DEC
12594 "mouse_dec",
12595# endif
12596# ifdef FEAT_MOUSE_GPM
12597 "mouse_gpm",
12598# endif
12599# ifdef FEAT_MOUSE_JSB
12600 "mouse_jsbterm",
12601# endif
12602# ifdef FEAT_MOUSE_NET
12603 "mouse_netterm",
12604# endif
12605# ifdef FEAT_MOUSE_PTERM
12606 "mouse_pterm",
12607# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012608# ifdef FEAT_MOUSE_SGR
12609 "mouse_sgr",
12610# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012611# ifdef FEAT_SYSMOUSE
12612 "mouse_sysmouse",
12613# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012614# ifdef FEAT_MOUSE_URXVT
12615 "mouse_urxvt",
12616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617# ifdef FEAT_MOUSE_XTERM
12618 "mouse_xterm",
12619# endif
12620#endif
12621#ifdef FEAT_MBYTE
12622 "multi_byte",
12623#endif
12624#ifdef FEAT_MBYTE_IME
12625 "multi_byte_ime",
12626#endif
12627#ifdef FEAT_MULTI_LANG
12628 "multi_lang",
12629#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012630#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012631#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012632 "mzscheme",
12633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635#ifdef FEAT_OLE
12636 "ole",
12637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638#ifdef FEAT_PATH_EXTRA
12639 "path_extra",
12640#endif
12641#ifdef FEAT_PERL
12642#ifndef DYNAMIC_PERL
12643 "perl",
12644#endif
12645#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012646#ifdef FEAT_PERSISTENT_UNDO
12647 "persistent_undo",
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef FEAT_PYTHON
12650#ifndef DYNAMIC_PYTHON
12651 "python",
12652#endif
12653#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012654#ifdef FEAT_PYTHON3
12655#ifndef DYNAMIC_PYTHON3
12656 "python3",
12657#endif
12658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659#ifdef FEAT_POSTSCRIPT
12660 "postscript",
12661#endif
12662#ifdef FEAT_PRINTER
12663 "printer",
12664#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012665#ifdef FEAT_PROFILE
12666 "profile",
12667#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012668#ifdef FEAT_RELTIME
12669 "reltime",
12670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671#ifdef FEAT_QUICKFIX
12672 "quickfix",
12673#endif
12674#ifdef FEAT_RIGHTLEFT
12675 "rightleft",
12676#endif
12677#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12678 "ruby",
12679#endif
12680#ifdef FEAT_SCROLLBIND
12681 "scrollbind",
12682#endif
12683#ifdef FEAT_CMDL_INFO
12684 "showcmd",
12685 "cmdline_info",
12686#endif
12687#ifdef FEAT_SIGNS
12688 "signs",
12689#endif
12690#ifdef FEAT_SMARTINDENT
12691 "smartindent",
12692#endif
12693#ifdef FEAT_SNIFF
12694 "sniff",
12695#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012696#ifdef STARTUPTIME
12697 "startuptime",
12698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699#ifdef FEAT_STL_OPT
12700 "statusline",
12701#endif
12702#ifdef FEAT_SUN_WORKSHOP
12703 "sun_workshop",
12704#endif
12705#ifdef FEAT_NETBEANS_INTG
12706 "netbeans_intg",
12707#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012708#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012709 "spell",
12710#endif
12711#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 "syntax",
12713#endif
12714#if defined(USE_SYSTEM) || !defined(UNIX)
12715 "system",
12716#endif
12717#ifdef FEAT_TAG_BINS
12718 "tag_binary",
12719#endif
12720#ifdef FEAT_TAG_OLDSTATIC
12721 "tag_old_static",
12722#endif
12723#ifdef FEAT_TAG_ANYWHITE
12724 "tag_any_white",
12725#endif
12726#ifdef FEAT_TCL
12727# ifndef DYNAMIC_TCL
12728 "tcl",
12729# endif
12730#endif
12731#ifdef TERMINFO
12732 "terminfo",
12733#endif
12734#ifdef FEAT_TERMRESPONSE
12735 "termresponse",
12736#endif
12737#ifdef FEAT_TEXTOBJ
12738 "textobjects",
12739#endif
12740#ifdef HAVE_TGETENT
12741 "tgetent",
12742#endif
12743#ifdef FEAT_TITLE
12744 "title",
12745#endif
12746#ifdef FEAT_TOOLBAR
12747 "toolbar",
12748#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012749#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12750 "unnamedplus",
12751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752#ifdef FEAT_USR_CMDS
12753 "user-commands", /* was accidentally included in 5.4 */
12754 "user_commands",
12755#endif
12756#ifdef FEAT_VIMINFO
12757 "viminfo",
12758#endif
12759#ifdef FEAT_VERTSPLIT
12760 "vertsplit",
12761#endif
12762#ifdef FEAT_VIRTUALEDIT
12763 "virtualedit",
12764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766#ifdef FEAT_VISUALEXTRA
12767 "visualextra",
12768#endif
12769#ifdef FEAT_VREPLACE
12770 "vreplace",
12771#endif
12772#ifdef FEAT_WILDIGN
12773 "wildignore",
12774#endif
12775#ifdef FEAT_WILDMENU
12776 "wildmenu",
12777#endif
12778#ifdef FEAT_WINDOWS
12779 "windows",
12780#endif
12781#ifdef FEAT_WAK
12782 "winaltkeys",
12783#endif
12784#ifdef FEAT_WRITEBACKUP
12785 "writebackup",
12786#endif
12787#ifdef FEAT_XIM
12788 "xim",
12789#endif
12790#ifdef FEAT_XFONTSET
12791 "xfontset",
12792#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012793#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012794 "xpm",
12795 "xpm_w32", /* for backward compatibility */
12796#else
12797# if defined(HAVE_XPM)
12798 "xpm",
12799# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801#ifdef USE_XSMP
12802 "xsmp",
12803#endif
12804#ifdef USE_XSMP_INTERACT
12805 "xsmp_interact",
12806#endif
12807#ifdef FEAT_XCLIPBOARD
12808 "xterm_clipboard",
12809#endif
12810#ifdef FEAT_XTERM_SAVE
12811 "xterm_save",
12812#endif
12813#if defined(UNIX) && defined(FEAT_X11)
12814 "X11",
12815#endif
12816 NULL
12817 };
12818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 for (i = 0; has_list[i] != NULL; ++i)
12821 if (STRICMP(name, has_list[i]) == 0)
12822 {
12823 n = TRUE;
12824 break;
12825 }
12826
12827 if (n == FALSE)
12828 {
12829 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012830 {
12831 if (name[5] == '-'
12832 && STRLEN(name) > 11
12833 && vim_isdigit(name[6])
12834 && vim_isdigit(name[8])
12835 && vim_isdigit(name[10]))
12836 {
12837 int major = atoi((char *)name + 6);
12838 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012839
12840 /* Expect "patch-9.9.01234". */
12841 n = (major < VIM_VERSION_MAJOR
12842 || (major == VIM_VERSION_MAJOR
12843 && (minor < VIM_VERSION_MINOR
12844 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012845 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012846 }
12847 else
12848 n = has_patch(atoi((char *)name + 5));
12849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 else if (STRICMP(name, "vim_starting") == 0)
12851 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012852#ifdef FEAT_MBYTE
12853 else if (STRICMP(name, "multi_byte_encoding") == 0)
12854 n = has_mbyte;
12855#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012856#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12857 else if (STRICMP(name, "balloon_multiline") == 0)
12858 n = multiline_balloon_available();
12859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860#ifdef DYNAMIC_TCL
12861 else if (STRICMP(name, "tcl") == 0)
12862 n = tcl_enabled(FALSE);
12863#endif
12864#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12865 else if (STRICMP(name, "iconv") == 0)
12866 n = iconv_enabled(FALSE);
12867#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012868#ifdef DYNAMIC_LUA
12869 else if (STRICMP(name, "lua") == 0)
12870 n = lua_enabled(FALSE);
12871#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012872#ifdef DYNAMIC_MZSCHEME
12873 else if (STRICMP(name, "mzscheme") == 0)
12874 n = mzscheme_enabled(FALSE);
12875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876#ifdef DYNAMIC_RUBY
12877 else if (STRICMP(name, "ruby") == 0)
12878 n = ruby_enabled(FALSE);
12879#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012880#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881#ifdef DYNAMIC_PYTHON
12882 else if (STRICMP(name, "python") == 0)
12883 n = python_enabled(FALSE);
12884#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012885#endif
12886#ifdef FEAT_PYTHON3
12887#ifdef DYNAMIC_PYTHON3
12888 else if (STRICMP(name, "python3") == 0)
12889 n = python3_enabled(FALSE);
12890#endif
12891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892#ifdef DYNAMIC_PERL
12893 else if (STRICMP(name, "perl") == 0)
12894 n = perl_enabled(FALSE);
12895#endif
12896#ifdef FEAT_GUI
12897 else if (STRICMP(name, "gui_running") == 0)
12898 n = (gui.in_use || gui.starting);
12899# ifdef FEAT_GUI_W32
12900 else if (STRICMP(name, "gui_win32s") == 0)
12901 n = gui_is_win32s();
12902# endif
12903# ifdef FEAT_BROWSE
12904 else if (STRICMP(name, "browse") == 0)
12905 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12906# endif
12907#endif
12908#ifdef FEAT_SYN_HL
12909 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012910 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911#endif
12912#if defined(WIN3264)
12913 else if (STRICMP(name, "win95") == 0)
12914 n = mch_windows95();
12915#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012916#ifdef FEAT_NETBEANS_INTG
12917 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012918 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 }
12921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
12925/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012926 * "has_key()" function
12927 */
12928 static void
12929f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012932{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012933 if (argvars[0].v_type != VAR_DICT)
12934 {
12935 EMSG(_(e_dictreq));
12936 return;
12937 }
12938 if (argvars[0].vval.v_dict == NULL)
12939 return;
12940
12941 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012942 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012943}
12944
12945/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012946 * "haslocaldir()" function
12947 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012948 static void
12949f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012950 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012951 typval_T *rettv;
12952{
12953 rettv->vval.v_number = (curwin->w_localdir != NULL);
12954}
12955
12956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 * "hasmapto()" function
12958 */
12959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 typval_T *argvars;
12962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963{
12964 char_u *name;
12965 char_u *mode;
12966 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012967 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012970 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 mode = (char_u *)"nvo";
12972 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012975 if (argvars[2].v_type != VAR_UNKNOWN)
12976 abbr = get_tv_number(&argvars[2]);
12977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978
Bram Moolenaar2c932302006-03-18 21:42:09 +000012979 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983}
12984
12985/*
12986 * "histadd()" function
12987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992{
12993#ifdef FEAT_CMDHIST
12994 int histype;
12995 char_u *str;
12996 char_u buf[NUMBUFLEN];
12997#endif
12998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 if (check_restricted() || check_secure())
13001 return;
13002#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13004 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 if (histype >= 0)
13006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013007 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 if (*str != NUL)
13009 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013010 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 return;
13014 }
13015 }
13016#endif
13017}
13018
13019/*
13020 * "histdel()" function
13021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013024 typval_T *argvars UNUSED;
13025 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026{
13027#ifdef FEAT_CMDHIST
13028 int n;
13029 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13033 if (str == NULL)
13034 n = 0;
13035 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013038 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 else
13043 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013044 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045 get_tv_string_buf(&argvars[1], buf));
13046 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047#endif
13048}
13049
13050/*
13051 * "histget()" function
13052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013055 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057{
13058#ifdef FEAT_CMDHIST
13059 int type;
13060 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013063 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13064 if (str == NULL)
13065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 {
13068 type = get_histtype(str);
13069 if (argvars[1].v_type == VAR_UNKNOWN)
13070 idx = get_history_idx(type);
13071 else
13072 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13073 /* -1 on type error */
13074 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080}
13081
13082/*
13083 * "histnr()" function
13084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089{
13090 int i;
13091
13092#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 char_u *history = get_tv_string_chk(&argvars[0]);
13094
13095 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 if (i >= HIST_CMD && i < HIST_COUNT)
13097 i = get_history_idx(i);
13098 else
13099#endif
13100 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102}
13103
13104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 * "highlightID(name)" function
13106 */
13107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013109 typval_T *argvars;
13110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013112 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113}
13114
13115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013116 * "highlight_exists()" function
13117 */
13118 static void
13119f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122{
13123 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13124}
13125
13126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 * "hostname()" function
13128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133{
13134 char_u hostname[256];
13135
13136 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 rettv->v_type = VAR_STRING;
13138 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139}
13140
13141/*
13142 * iconv() function
13143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013146 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148{
13149#ifdef FEAT_MBYTE
13150 char_u buf1[NUMBUFLEN];
13151 char_u buf2[NUMBUFLEN];
13152 char_u *from, *to, *str;
13153 vimconv_T vimconv;
13154#endif
13155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->v_type = VAR_STRING;
13157 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
13159#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 str = get_tv_string(&argvars[0]);
13161 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13162 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 vimconv.vc_type = CONV_NONE;
13164 convert_setup(&vimconv, from, to);
13165
13166 /* If the encodings are equal, no conversion needed. */
13167 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
13172 convert_setup(&vimconv, NULL, NULL);
13173 vim_free(from);
13174 vim_free(to);
13175#endif
13176}
13177
13178/*
13179 * "indent()" function
13180 */
13181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *argvars;
13184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185{
13186 linenr_T lnum;
13187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193}
13194
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013195/*
13196 * "index()" function
13197 */
13198 static void
13199f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013202{
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 list_T *l;
13204 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013205 long idx = 0;
13206 int ic = FALSE;
13207
13208 rettv->vval.v_number = -1;
13209 if (argvars[0].v_type != VAR_LIST)
13210 {
13211 EMSG(_(e_listreq));
13212 return;
13213 }
13214 l = argvars[0].vval.v_list;
13215 if (l != NULL)
13216 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013217 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 int error = FALSE;
13221
Bram Moolenaar758711c2005-02-02 23:11:38 +000013222 /* Start at specified item. Use the cached index that list_find()
13223 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013225 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013226 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 ic = get_tv_number_chk(&argvars[3], &error);
13228 if (error)
13229 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013230 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013231
Bram Moolenaar758711c2005-02-02 23:11:38 +000013232 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013233 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234 {
13235 rettv->vval.v_number = idx;
13236 break;
13237 }
13238 }
13239}
13240
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241static int inputsecret_flag = 0;
13242
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013243static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13244
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013246 * This function is used by f_input() and f_inputdialog() functions. The third
13247 * argument to f_input() specifies the type of completion to use at the
13248 * prompt. The third argument to f_inputdialog() specifies the value to return
13249 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 */
13251 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013252get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *argvars;
13254 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013255 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 char_u *p = NULL;
13259 int c;
13260 char_u buf[NUMBUFLEN];
13261 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013263 int xp_type = EXPAND_NOTHING;
13264 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013267 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268
13269#ifdef NO_CONSOLE_INPUT
13270 /* While starting up, there is no place to enter text. */
13271 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273#endif
13274
13275 cmd_silent = FALSE; /* Want to see the prompt. */
13276 if (prompt != NULL)
13277 {
13278 /* Only the part of the message after the last NL is considered as
13279 * prompt for the command line */
13280 p = vim_strrchr(prompt, '\n');
13281 if (p == NULL)
13282 p = prompt;
13283 else
13284 {
13285 ++p;
13286 c = *p;
13287 *p = NUL;
13288 msg_start();
13289 msg_clr_eos();
13290 msg_puts_attr(prompt, echo_attr);
13291 msg_didout = FALSE;
13292 msg_starthere();
13293 *p = c;
13294 }
13295 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 if (argvars[1].v_type != VAR_UNKNOWN)
13298 {
13299 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13300 if (defstr != NULL)
13301 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013303 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013304 {
13305 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013306 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013307 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013308
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013309 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013310 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013311
Bram Moolenaar4463f292005-09-25 22:20:24 +000013312 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13313 if (xp_name == NULL)
13314 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013316 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013317
Bram Moolenaar4463f292005-09-25 22:20:24 +000013318 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13319 &xp_arg) == FAIL)
13320 return;
13321 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013322 }
13323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013325 {
13326# ifdef FEAT_EX_EXTRA
13327 int save_ex_normal_busy = ex_normal_busy;
13328 ex_normal_busy = 0;
13329# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013330 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013331 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13332 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013333# ifdef FEAT_EX_EXTRA
13334 ex_normal_busy = save_ex_normal_busy;
13335# endif
13336 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013337 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013338 && argvars[1].v_type != VAR_UNKNOWN
13339 && argvars[2].v_type != VAR_UNKNOWN)
13340 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13341 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013342
13343 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013345 /* since the user typed this, no need to wait for return */
13346 need_wait_return = FALSE;
13347 msg_didout = FALSE;
13348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 cmd_silent = cmd_silent_save;
13350}
13351
13352/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013353 * "input()" function
13354 * Also handles inputsecret() when inputsecret is set.
13355 */
13356 static void
13357f_input(argvars, rettv)
13358 typval_T *argvars;
13359 typval_T *rettv;
13360{
13361 get_user_input(argvars, rettv, FALSE);
13362}
13363
13364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 * "inputdialog()" function
13366 */
13367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 typval_T *argvars;
13370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372#if defined(FEAT_GUI_TEXTDIALOG)
13373 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13374 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13375 {
13376 char_u *message;
13377 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013380 message = get_tv_string_chk(&argvars[0]);
13381 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013382 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013383 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 else
13385 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 if (message != NULL && defstr != NULL
13387 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013388 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 else
13391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 if (message != NULL && defstr != NULL
13393 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013394 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395 rettv->vval.v_string = vim_strsave(
13396 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 }
13402 else
13403#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013404 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405}
13406
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013407/*
13408 * "inputlist()" function
13409 */
13410 static void
13411f_inputlist(argvars, rettv)
13412 typval_T *argvars;
13413 typval_T *rettv;
13414{
13415 listitem_T *li;
13416 int selected;
13417 int mouse_used;
13418
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013419#ifdef NO_CONSOLE_INPUT
13420 /* While starting up, there is no place to enter text. */
13421 if (no_console_input())
13422 return;
13423#endif
13424 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13425 {
13426 EMSG2(_(e_listarg), "inputlist()");
13427 return;
13428 }
13429
13430 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013431 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013432 lines_left = Rows; /* avoid more prompt */
13433 msg_scroll = TRUE;
13434 msg_clr_eos();
13435
13436 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13437 {
13438 msg_puts(get_tv_string(&li->li_tv));
13439 msg_putchar('\n');
13440 }
13441
13442 /* Ask for choice. */
13443 selected = prompt_for_number(&mouse_used);
13444 if (mouse_used)
13445 selected -= lines_left;
13446
13447 rettv->vval.v_number = selected;
13448}
13449
13450
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13452
13453/*
13454 * "inputrestore()" function
13455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457f_inputrestore(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{
13461 if (ga_userinput.ga_len > 0)
13462 {
13463 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13465 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013466 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 }
13468 else if (p_verbose > 1)
13469 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013470 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013471 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 }
13473}
13474
13475/*
13476 * "inputsave()" function
13477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013480 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013483 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 if (ga_grow(&ga_userinput, 1) == OK)
13485 {
13486 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13487 + ga_userinput.ga_len);
13488 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013489 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 }
13491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493}
13494
13495/*
13496 * "inputsecret()" function
13497 */
13498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
13503 ++cmdline_star;
13504 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 --cmdline_star;
13507 --inputsecret_flag;
13508}
13509
13510/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013511 * "insert()" function
13512 */
13513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013515 typval_T *argvars;
13516 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517{
13518 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 listitem_T *item;
13520 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522
13523 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013524 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013525 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013526 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013527 {
13528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013529 before = get_tv_number_chk(&argvars[2], &error);
13530 if (error)
13531 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532
Bram Moolenaar758711c2005-02-02 23:11:38 +000013533 if (before == l->lv_len)
13534 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535 else
13536 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013537 item = list_find(l, before);
13538 if (item == NULL)
13539 {
13540 EMSGN(_(e_listidx), before);
13541 l = NULL;
13542 }
13543 }
13544 if (l != NULL)
13545 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013546 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013547 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013548 }
13549 }
13550}
13551
13552/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013553 * "invert(expr)" function
13554 */
13555 static void
13556f_invert(argvars, rettv)
13557 typval_T *argvars;
13558 typval_T *rettv;
13559{
13560 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13561}
13562
13563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 * "isdirectory()" function
13565 */
13566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 typval_T *argvars;
13569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572}
13573
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013574/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013575 * "islocked()" function
13576 */
13577 static void
13578f_islocked(argvars, rettv)
13579 typval_T *argvars;
13580 typval_T *rettv;
13581{
13582 lval_T lv;
13583 char_u *end;
13584 dictitem_T *di;
13585
13586 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013587 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13588 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013589 if (end != NULL && lv.ll_name != NULL)
13590 {
13591 if (*end != NUL)
13592 EMSG(_(e_trailing));
13593 else
13594 {
13595 if (lv.ll_tv == NULL)
13596 {
13597 if (check_changedtick(lv.ll_name))
13598 rettv->vval.v_number = 1; /* always locked */
13599 else
13600 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013601 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013602 if (di != NULL)
13603 {
13604 /* Consider a variable locked when:
13605 * 1. the variable itself is locked
13606 * 2. the value of the variable is locked.
13607 * 3. the List or Dict value is locked.
13608 */
13609 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13610 || tv_islocked(&di->di_tv));
13611 }
13612 }
13613 }
13614 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013615 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013616 else if (lv.ll_newkey != NULL)
13617 EMSG2(_(e_dictkey), lv.ll_newkey);
13618 else if (lv.ll_list != NULL)
13619 /* List item. */
13620 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13621 else
13622 /* Dictionary item. */
13623 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13624 }
13625 }
13626
13627 clear_lval(&lv);
13628}
13629
Bram Moolenaar33570922005-01-25 22:26:29 +000013630static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013631
13632/*
13633 * Turn a dict into a list:
13634 * "what" == 0: list of keys
13635 * "what" == 1: list of values
13636 * "what" == 2: list of items
13637 */
13638 static void
13639dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013642 int what;
13643{
Bram Moolenaar33570922005-01-25 22:26:29 +000013644 list_T *l2;
13645 dictitem_T *di;
13646 hashitem_T *hi;
13647 listitem_T *li;
13648 listitem_T *li2;
13649 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013650 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013651
Bram Moolenaar8c711452005-01-14 21:53:12 +000013652 if (argvars[0].v_type != VAR_DICT)
13653 {
13654 EMSG(_(e_dictreq));
13655 return;
13656 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013657 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013658 return;
13659
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013660 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013661 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013662
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013663 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013665 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013666 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013667 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013668 --todo;
13669 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013670
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013671 li = listitem_alloc();
13672 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013673 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013674 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013675
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013676 if (what == 0)
13677 {
13678 /* keys() */
13679 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013680 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013681 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13682 }
13683 else if (what == 1)
13684 {
13685 /* values() */
13686 copy_tv(&di->di_tv, &li->li_tv);
13687 }
13688 else
13689 {
13690 /* items() */
13691 l2 = list_alloc();
13692 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013693 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013694 li->li_tv.vval.v_list = l2;
13695 if (l2 == NULL)
13696 break;
13697 ++l2->lv_refcount;
13698
13699 li2 = listitem_alloc();
13700 if (li2 == NULL)
13701 break;
13702 list_append(l2, li2);
13703 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013704 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013705 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13706
13707 li2 = listitem_alloc();
13708 if (li2 == NULL)
13709 break;
13710 list_append(l2, li2);
13711 copy_tv(&di->di_tv, &li2->li_tv);
13712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013713 }
13714 }
13715}
13716
13717/*
13718 * "items(dict)" function
13719 */
13720 static void
13721f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *argvars;
13723 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013724{
13725 dict_list(argvars, rettv, 2);
13726}
13727
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013729 * "join()" function
13730 */
13731 static void
13732f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *argvars;
13734 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013735{
13736 garray_T ga;
13737 char_u *sep;
13738
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013739 if (argvars[0].v_type != VAR_LIST)
13740 {
13741 EMSG(_(e_listreq));
13742 return;
13743 }
13744 if (argvars[0].vval.v_list == NULL)
13745 return;
13746 if (argvars[1].v_type == VAR_UNKNOWN)
13747 sep = (char_u *)" ";
13748 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013750
13751 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752
13753 if (sep != NULL)
13754 {
13755 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013756 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 ga_append(&ga, NUL);
13758 rettv->vval.v_string = (char_u *)ga.ga_data;
13759 }
13760 else
13761 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013762}
13763
13764/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013765 * "keys()" function
13766 */
13767 static void
13768f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 typval_T *argvars;
13770 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013771{
13772 dict_list(argvars, rettv, 0);
13773}
13774
13775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 * "last_buffer_nr()" function.
13777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782{
13783 int n = 0;
13784 buf_T *buf;
13785
13786 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13787 if (n < buf->b_fnum)
13788 n = buf->b_fnum;
13789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013791}
13792
13793/*
13794 * "len()" function
13795 */
13796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 typval_T *argvars;
13799 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013800{
13801 switch (argvars[0].v_type)
13802 {
13803 case VAR_STRING:
13804 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805 rettv->vval.v_number = (varnumber_T)STRLEN(
13806 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013807 break;
13808 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013810 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013811 case VAR_DICT:
13812 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13813 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013814 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013815 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013816 break;
13817 }
13818}
13819
Bram Moolenaar33570922005-01-25 22:26:29 +000013820static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821
13822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013823libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013824 typval_T *argvars;
13825 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013826 int type;
13827{
13828#ifdef FEAT_LIBCALL
13829 char_u *string_in;
13830 char_u **string_result;
13831 int nr_result;
13832#endif
13833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013835 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013837
13838 if (check_restricted() || check_secure())
13839 return;
13840
13841#ifdef FEAT_LIBCALL
13842 /* The first two args must be strings, otherwise its meaningless */
13843 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13844 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013845 string_in = NULL;
13846 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013847 string_in = argvars[2].vval.v_string;
13848 if (type == VAR_NUMBER)
13849 string_result = NULL;
13850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013852 if (mch_libcall(argvars[0].vval.v_string,
13853 argvars[1].vval.v_string,
13854 string_in,
13855 argvars[2].vval.v_number,
13856 string_result,
13857 &nr_result) == OK
13858 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 }
13861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862}
13863
13864/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 * "libcall()" function
13866 */
13867 static void
13868f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013869 typval_T *argvars;
13870 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871{
13872 libcall_common(argvars, rettv, VAR_STRING);
13873}
13874
13875/*
13876 * "libcallnr()" function
13877 */
13878 static void
13879f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 typval_T *argvars;
13881 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013882{
13883 libcall_common(argvars, rettv, VAR_NUMBER);
13884}
13885
13886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 * "line(string)" function
13888 */
13889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013891 typval_T *argvars;
13892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893{
13894 linenr_T lnum = 0;
13895 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013896 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013898 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 if (fp != NULL)
13900 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902}
13903
13904/*
13905 * "line2byte(lnum)" function
13906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911{
13912#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914#else
13915 linenr_T lnum;
13916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13922 if (rettv->vval.v_number >= 0)
13923 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924#endif
13925}
13926
13927/*
13928 * "lispindent(lnum)" function
13929 */
13930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935#ifdef FEAT_LISP
13936 pos_T pos;
13937 linenr_T lnum;
13938
13939 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13942 {
13943 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 curwin->w_cursor = pos;
13946 }
13947 else
13948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950}
13951
13952/*
13953 * "localtime()" function
13954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961}
13962
Bram Moolenaar33570922005-01-25 22:26:29 +000013963static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964
13965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *argvars;
13968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 int exact;
13970{
13971 char_u *keys;
13972 char_u *which;
13973 char_u buf[NUMBUFLEN];
13974 char_u *keys_buf = NULL;
13975 char_u *rhs;
13976 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013977 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013978 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013979 mapblock_T *mp;
13980 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981
13982 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 rettv->v_type = VAR_STRING;
13984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 if (*keys == NUL)
13988 return;
13989
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013990 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013994 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013995 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013996 if (argvars[3].v_type != VAR_UNKNOWN)
13997 get_dict = get_tv_number(&argvars[3]);
13998 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 else
14001 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 if (which == NULL)
14003 return;
14004
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 mode = get_map_mode(&which, 0);
14006
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014007 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014008 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014010
14011 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014013 /* Return a string. */
14014 if (rhs != NULL)
14015 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
Bram Moolenaarbd743252010-10-20 21:23:33 +020014017 }
14018 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14019 {
14020 /* Return a dictionary. */
14021 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14022 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14023 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024
Bram Moolenaarbd743252010-10-20 21:23:33 +020014025 dict_add_nr_str(dict, "lhs", 0L, lhs);
14026 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14027 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14028 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14029 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14030 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14031 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014032 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014033 dict_add_nr_str(dict, "mode", 0L, mapmode);
14034
14035 vim_free(lhs);
14036 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 }
14038}
14039
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014040#ifdef FEAT_FLOAT
14041/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014042 * "log()" function
14043 */
14044 static void
14045f_log(argvars, rettv)
14046 typval_T *argvars;
14047 typval_T *rettv;
14048{
14049 float_T f;
14050
14051 rettv->v_type = VAR_FLOAT;
14052 if (get_float_arg(argvars, &f) == OK)
14053 rettv->vval.v_float = log(f);
14054 else
14055 rettv->vval.v_float = 0.0;
14056}
14057
14058/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014059 * "log10()" function
14060 */
14061 static void
14062f_log10(argvars, rettv)
14063 typval_T *argvars;
14064 typval_T *rettv;
14065{
14066 float_T f;
14067
14068 rettv->v_type = VAR_FLOAT;
14069 if (get_float_arg(argvars, &f) == OK)
14070 rettv->vval.v_float = log10(f);
14071 else
14072 rettv->vval.v_float = 0.0;
14073}
14074#endif
14075
Bram Moolenaar1dced572012-04-05 16:54:08 +020014076#ifdef FEAT_LUA
14077/*
14078 * "luaeval()" function
14079 */
14080 static void
14081f_luaeval(argvars, rettv)
14082 typval_T *argvars;
14083 typval_T *rettv;
14084{
14085 char_u *str;
14086 char_u buf[NUMBUFLEN];
14087
14088 str = get_tv_string_buf(&argvars[0], buf);
14089 do_luaeval(str, argvars + 1, rettv);
14090}
14091#endif
14092
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014094 * "map()" function
14095 */
14096 static void
14097f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014100{
14101 filter_map(argvars, rettv, TRUE);
14102}
14103
14104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 */
14107 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113}
14114
14115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117 */
14118 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124}
14125
Bram Moolenaar33570922005-01-25 22:26:29 +000014126static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127
14128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
14131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 int type;
14133{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014134 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014135 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014136 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 char_u *pat;
14138 regmatch_T regmatch;
14139 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014140 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 char_u *save_cpo;
14142 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014143 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014144 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014145 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 list_T *l = NULL;
14147 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014148 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014149 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
14151 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14152 save_cpo = p_cpo;
14153 p_cpo = (char_u *)"";
14154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155 rettv->vval.v_number = -1;
14156 if (type == 3)
14157 {
14158 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014159 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014160 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014161 }
14162 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164 rettv->v_type = VAR_STRING;
14165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014168 if (argvars[0].v_type == VAR_LIST)
14169 {
14170 if ((l = argvars[0].vval.v_list) == NULL)
14171 goto theend;
14172 li = l->lv_first;
14173 }
14174 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014175 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014176 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014177 len = (long)STRLEN(str);
14178 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14181 if (pat == NULL)
14182 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 int error = FALSE;
14187
14188 start = get_tv_number_chk(&argvars[2], &error);
14189 if (error)
14190 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014191 if (l != NULL)
14192 {
14193 li = list_find(l, start);
14194 if (li == NULL)
14195 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014196 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197 }
14198 else
14199 {
14200 if (start < 0)
14201 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014202 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014203 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014204 /* When "count" argument is there ignore matches before "start",
14205 * otherwise skip part of the string. Differs when pattern is "^"
14206 * or "\<". */
14207 if (argvars[3].v_type != VAR_UNKNOWN)
14208 startcol = start;
14209 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014210 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014211 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014212 len -= start;
14213 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014214 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014216 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 nth = get_tv_number_chk(&argvars[3], &error);
14218 if (error)
14219 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 }
14221
14222 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14223 if (regmatch.regprog != NULL)
14224 {
14225 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014226
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014227 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014228 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014229 if (l != NULL)
14230 {
14231 if (li == NULL)
14232 {
14233 match = FALSE;
14234 break;
14235 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014236 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014237 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014238 if (str == NULL)
14239 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014240 }
14241
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014242 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014243
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014244 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014245 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014246 if (l == NULL && !match)
14247 break;
14248
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014249 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014250 if (l != NULL)
14251 {
14252 li = li->li_next;
14253 ++idx;
14254 }
14255 else
14256 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014257#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014258 startcol = (colnr_T)(regmatch.startp[0]
14259 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014260#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014261 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014262#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014263 if (startcol > (colnr_T)len
14264 || str + startcol <= regmatch.startp[0])
14265 {
14266 match = FALSE;
14267 break;
14268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014269 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014270 }
14271
14272 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014274 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014276 int i;
14277
14278 /* return list with matched string and submatches */
14279 for (i = 0; i < NSUBEXP; ++i)
14280 {
14281 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014282 {
14283 if (list_append_string(rettv->vval.v_list,
14284 (char_u *)"", 0) == FAIL)
14285 break;
14286 }
14287 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014288 regmatch.startp[i],
14289 (int)(regmatch.endp[i] - regmatch.startp[i]))
14290 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014291 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014292 }
14293 }
14294 else if (type == 2)
14295 {
14296 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014297 if (l != NULL)
14298 copy_tv(&li->li_tv, rettv);
14299 else
14300 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014302 }
14303 else if (l != NULL)
14304 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 else
14306 {
14307 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 (varnumber_T)(regmatch.startp[0] - str);
14310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014313 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 }
14315 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014316 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 }
14318
14319theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014320 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 p_cpo = save_cpo;
14322}
14323
14324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325 * "match()" function
14326 */
14327 static void
14328f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331{
14332 find_some_match(argvars, rettv, 1);
14333}
14334
14335/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014336 * "matchadd()" function
14337 */
14338 static void
14339f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014340 typval_T *argvars UNUSED;
14341 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014342{
14343#ifdef FEAT_SEARCH_EXTRA
14344 char_u buf[NUMBUFLEN];
14345 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14346 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14347 int prio = 10; /* default priority */
14348 int id = -1;
14349 int error = FALSE;
14350
14351 rettv->vval.v_number = -1;
14352
14353 if (grp == NULL || pat == NULL)
14354 return;
14355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014356 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014357 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014358 if (argvars[3].v_type != VAR_UNKNOWN)
14359 id = get_tv_number_chk(&argvars[3], &error);
14360 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014361 if (error == TRUE)
14362 return;
14363 if (id >= 1 && id <= 3)
14364 {
14365 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14366 return;
14367 }
14368
Bram Moolenaarb3414592014-06-17 17:48:32 +020014369 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14370#endif
14371}
14372
14373/*
14374 * "matchaddpos()" function
14375 */
14376 static void
14377f_matchaddpos(argvars, rettv)
14378 typval_T *argvars UNUSED;
14379 typval_T *rettv UNUSED;
14380{
14381#ifdef FEAT_SEARCH_EXTRA
14382 char_u buf[NUMBUFLEN];
14383 char_u *group;
14384 int prio = 10;
14385 int id = -1;
14386 int error = FALSE;
14387 list_T *l;
14388
14389 rettv->vval.v_number = -1;
14390
14391 group = get_tv_string_buf_chk(&argvars[0], buf);
14392 if (group == NULL)
14393 return;
14394
14395 if (argvars[1].v_type != VAR_LIST)
14396 {
14397 EMSG2(_(e_listarg), "matchaddpos()");
14398 return;
14399 }
14400 l = argvars[1].vval.v_list;
14401 if (l == NULL)
14402 return;
14403
14404 if (argvars[2].v_type != VAR_UNKNOWN)
14405 {
14406 prio = get_tv_number_chk(&argvars[2], &error);
14407 if (argvars[3].v_type != VAR_UNKNOWN)
14408 id = get_tv_number_chk(&argvars[3], &error);
14409 }
14410 if (error == TRUE)
14411 return;
14412
14413 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14414 if (id == 1 || id == 2)
14415 {
14416 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14417 return;
14418 }
14419
14420 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014421#endif
14422}
14423
14424/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014425 * "matcharg()" function
14426 */
14427 static void
14428f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014429 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014430 typval_T *rettv;
14431{
14432 if (rettv_list_alloc(rettv) == OK)
14433 {
14434#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014435 int id = get_tv_number(&argvars[0]);
14436 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014437
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014438 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014439 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014440 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14441 {
14442 list_append_string(rettv->vval.v_list,
14443 syn_id2name(m->hlg_id), -1);
14444 list_append_string(rettv->vval.v_list, m->pattern, -1);
14445 }
14446 else
14447 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014448 list_append_string(rettv->vval.v_list, NULL, -1);
14449 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014450 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014451 }
14452#endif
14453 }
14454}
14455
14456/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014457 * "matchdelete()" function
14458 */
14459 static void
14460f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014461 typval_T *argvars UNUSED;
14462 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014463{
14464#ifdef FEAT_SEARCH_EXTRA
14465 rettv->vval.v_number = match_delete(curwin,
14466 (int)get_tv_number(&argvars[0]), TRUE);
14467#endif
14468}
14469
14470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471 * "matchend()" function
14472 */
14473 static void
14474f_matchend(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, 0);
14479}
14480
14481/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014482 * "matchlist()" function
14483 */
14484 static void
14485f_matchlist(argvars, rettv)
14486 typval_T *argvars;
14487 typval_T *rettv;
14488{
14489 find_some_match(argvars, rettv, 3);
14490}
14491
14492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 * "matchstr()" function
14494 */
14495 static void
14496f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014497 typval_T *argvars;
14498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499{
14500 find_some_match(argvars, rettv, 2);
14501}
14502
Bram Moolenaar33570922005-01-25 22:26:29 +000014503static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014504
14505 static void
14506max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014507 typval_T *argvars;
14508 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014509 int domax;
14510{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014511 long n = 0;
14512 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014514
14515 if (argvars[0].v_type == VAR_LIST)
14516 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 list_T *l;
14518 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014519
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014520 l = argvars[0].vval.v_list;
14521 if (l != NULL)
14522 {
14523 li = l->lv_first;
14524 if (li != NULL)
14525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014527 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014528 {
14529 li = li->li_next;
14530 if (li == NULL)
14531 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014533 if (domax ? i > n : i < n)
14534 n = i;
14535 }
14536 }
14537 }
14538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014539 else if (argvars[0].v_type == VAR_DICT)
14540 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014542 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014544 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014545
14546 d = argvars[0].vval.v_dict;
14547 if (d != NULL)
14548 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014549 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014550 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014552 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014554 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014556 if (first)
14557 {
14558 n = i;
14559 first = FALSE;
14560 }
14561 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014562 n = i;
14563 }
14564 }
14565 }
14566 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014567 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014568 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014569 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014570}
14571
14572/*
14573 * "max()" function
14574 */
14575 static void
14576f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 typval_T *argvars;
14578 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014579{
14580 max_min(argvars, rettv, TRUE);
14581}
14582
14583/*
14584 * "min()" function
14585 */
14586 static void
14587f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T *argvars;
14589 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014590{
14591 max_min(argvars, rettv, FALSE);
14592}
14593
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014594static int mkdir_recurse __ARGS((char_u *dir, int prot));
14595
14596/*
14597 * Create the directory in which "dir" is located, and higher levels when
14598 * needed.
14599 */
14600 static int
14601mkdir_recurse(dir, prot)
14602 char_u *dir;
14603 int prot;
14604{
14605 char_u *p;
14606 char_u *updir;
14607 int r = FAIL;
14608
14609 /* Get end of directory name in "dir".
14610 * We're done when it's "/" or "c:/". */
14611 p = gettail_sep(dir);
14612 if (p <= get_past_head(dir))
14613 return OK;
14614
14615 /* If the directory exists we're done. Otherwise: create it.*/
14616 updir = vim_strnsave(dir, (int)(p - dir));
14617 if (updir == NULL)
14618 return FAIL;
14619 if (mch_isdir(updir))
14620 r = OK;
14621 else if (mkdir_recurse(updir, prot) == OK)
14622 r = vim_mkdir_emsg(updir, prot);
14623 vim_free(updir);
14624 return r;
14625}
14626
14627#ifdef vim_mkdir
14628/*
14629 * "mkdir()" function
14630 */
14631 static void
14632f_mkdir(argvars, rettv)
14633 typval_T *argvars;
14634 typval_T *rettv;
14635{
14636 char_u *dir;
14637 char_u buf[NUMBUFLEN];
14638 int prot = 0755;
14639
14640 rettv->vval.v_number = FAIL;
14641 if (check_restricted() || check_secure())
14642 return;
14643
14644 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014645 if (*dir == NUL)
14646 rettv->vval.v_number = FAIL;
14647 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014648 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014649 if (*gettail(dir) == NUL)
14650 /* remove trailing slashes */
14651 *gettail_sep(dir) = NUL;
14652
14653 if (argvars[1].v_type != VAR_UNKNOWN)
14654 {
14655 if (argvars[2].v_type != VAR_UNKNOWN)
14656 prot = get_tv_number_chk(&argvars[2], NULL);
14657 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14658 mkdir_recurse(dir, prot);
14659 }
14660 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014662}
14663#endif
14664
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 * "mode()" function
14667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *argvars;
14671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014673 char_u buf[3];
14674
14675 buf[1] = NUL;
14676 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 if (VIsual_active)
14679 {
14680 if (VIsual_select)
14681 buf[0] = VIsual_mode + 's' - 'v';
14682 else
14683 buf[0] = VIsual_mode;
14684 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014685 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014686 || State == CONFIRM)
14687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014689 if (State == ASKMORE)
14690 buf[1] = 'm';
14691 else if (State == CONFIRM)
14692 buf[1] = '?';
14693 }
14694 else if (State == EXTERNCMD)
14695 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 else if (State & INSERT)
14697 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014698#ifdef FEAT_VREPLACE
14699 if (State & VREPLACE_FLAG)
14700 {
14701 buf[0] = 'R';
14702 buf[1] = 'v';
14703 }
14704 else
14705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 if (State & REPLACE_FLAG)
14707 buf[0] = 'R';
14708 else
14709 buf[0] = 'i';
14710 }
14711 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014714 if (exmode_active)
14715 buf[1] = 'v';
14716 }
14717 else if (exmode_active)
14718 {
14719 buf[0] = 'c';
14720 buf[1] = 'e';
14721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014725 if (finish_op)
14726 buf[1] = 'o';
14727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014729 /* Clear out the minor mode when the argument is not a non-zero number or
14730 * non-empty string. */
14731 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014732 buf[1] = NUL;
14733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014734 rettv->vval.v_string = vim_strsave(buf);
14735 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736}
14737
Bram Moolenaar429fa852013-04-15 12:27:36 +020014738#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014739/*
14740 * "mzeval()" function
14741 */
14742 static void
14743f_mzeval(argvars, rettv)
14744 typval_T *argvars;
14745 typval_T *rettv;
14746{
14747 char_u *str;
14748 char_u buf[NUMBUFLEN];
14749
14750 str = get_tv_string_buf(&argvars[0], buf);
14751 do_mzeval(str, rettv);
14752}
Bram Moolenaar75676462013-01-30 14:55:42 +010014753
14754 void
14755mzscheme_call_vim(name, args, rettv)
14756 char_u *name;
14757 typval_T *args;
14758 typval_T *rettv;
14759{
14760 typval_T argvars[3];
14761
14762 argvars[0].v_type = VAR_STRING;
14763 argvars[0].vval.v_string = name;
14764 copy_tv(args, &argvars[1]);
14765 argvars[2].v_type = VAR_UNKNOWN;
14766 f_call(argvars, rettv);
14767 clear_tv(&argvars[1]);
14768}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014769#endif
14770
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772 * "nextnonblank()" function
14773 */
14774 static void
14775f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 typval_T *argvars;
14777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778{
14779 linenr_T lnum;
14780
14781 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014783 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014784 {
14785 lnum = 0;
14786 break;
14787 }
14788 if (*skipwhite(ml_get(lnum)) != NUL)
14789 break;
14790 }
14791 rettv->vval.v_number = lnum;
14792}
14793
14794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 * "nr2char()" function
14796 */
14797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014798f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014799 typval_T *argvars;
14800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801{
14802 char_u buf[NUMBUFLEN];
14803
14804#ifdef FEAT_MBYTE
14805 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014806 {
14807 int utf8 = 0;
14808
14809 if (argvars[1].v_type != VAR_UNKNOWN)
14810 utf8 = get_tv_number_chk(&argvars[1], NULL);
14811 if (utf8)
14812 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14813 else
14814 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 else
14817#endif
14818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 buf[1] = NUL;
14821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->v_type = VAR_STRING;
14823 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014824}
14825
14826/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014827 * "or(expr, expr)" function
14828 */
14829 static void
14830f_or(argvars, rettv)
14831 typval_T *argvars;
14832 typval_T *rettv;
14833{
14834 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14835 | get_tv_number_chk(&argvars[1], NULL);
14836}
14837
14838/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014839 * "pathshorten()" function
14840 */
14841 static void
14842f_pathshorten(argvars, rettv)
14843 typval_T *argvars;
14844 typval_T *rettv;
14845{
14846 char_u *p;
14847
14848 rettv->v_type = VAR_STRING;
14849 p = get_tv_string_chk(&argvars[0]);
14850 if (p == NULL)
14851 rettv->vval.v_string = NULL;
14852 else
14853 {
14854 p = vim_strsave(p);
14855 rettv->vval.v_string = p;
14856 if (p != NULL)
14857 shorten_dir(p);
14858 }
14859}
14860
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014861#ifdef FEAT_FLOAT
14862/*
14863 * "pow()" function
14864 */
14865 static void
14866f_pow(argvars, rettv)
14867 typval_T *argvars;
14868 typval_T *rettv;
14869{
14870 float_T fx, fy;
14871
14872 rettv->v_type = VAR_FLOAT;
14873 if (get_float_arg(argvars, &fx) == OK
14874 && get_float_arg(&argvars[1], &fy) == OK)
14875 rettv->vval.v_float = pow(fx, fy);
14876 else
14877 rettv->vval.v_float = 0.0;
14878}
14879#endif
14880
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014881/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 * "prevnonblank()" function
14883 */
14884 static void
14885f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 typval_T *argvars;
14887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014888{
14889 linenr_T lnum;
14890
14891 lnum = get_tv_lnum(argvars);
14892 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14893 lnum = 0;
14894 else
14895 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14896 --lnum;
14897 rettv->vval.v_number = lnum;
14898}
14899
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014900#ifdef HAVE_STDARG_H
14901/* This dummy va_list is here because:
14902 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14903 * - locally in the function results in a "used before set" warning
14904 * - using va_start() to initialize it gives "function with fixed args" error */
14905static va_list ap;
14906#endif
14907
Bram Moolenaar8c711452005-01-14 21:53:12 +000014908/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014909 * "printf()" function
14910 */
14911 static void
14912f_printf(argvars, rettv)
14913 typval_T *argvars;
14914 typval_T *rettv;
14915{
14916 rettv->v_type = VAR_STRING;
14917 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014918#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014919 {
14920 char_u buf[NUMBUFLEN];
14921 int len;
14922 char_u *s;
14923 int saved_did_emsg = did_emsg;
14924 char *fmt;
14925
14926 /* Get the required length, allocate the buffer and do it for real. */
14927 did_emsg = FALSE;
14928 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014929 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014930 if (!did_emsg)
14931 {
14932 s = alloc(len + 1);
14933 if (s != NULL)
14934 {
14935 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014936 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014937 }
14938 }
14939 did_emsg |= saved_did_emsg;
14940 }
14941#endif
14942}
14943
14944/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014945 * "pumvisible()" function
14946 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014947 static void
14948f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014949 typval_T *argvars UNUSED;
14950 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014951{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014952#ifdef FEAT_INS_EXPAND
14953 if (pum_visible())
14954 rettv->vval.v_number = 1;
14955#endif
14956}
14957
Bram Moolenaardb913952012-06-29 12:54:53 +020014958#ifdef FEAT_PYTHON3
14959/*
14960 * "py3eval()" function
14961 */
14962 static void
14963f_py3eval(argvars, rettv)
14964 typval_T *argvars;
14965 typval_T *rettv;
14966{
14967 char_u *str;
14968 char_u buf[NUMBUFLEN];
14969
14970 str = get_tv_string_buf(&argvars[0], buf);
14971 do_py3eval(str, rettv);
14972}
14973#endif
14974
14975#ifdef FEAT_PYTHON
14976/*
14977 * "pyeval()" function
14978 */
14979 static void
14980f_pyeval(argvars, rettv)
14981 typval_T *argvars;
14982 typval_T *rettv;
14983{
14984 char_u *str;
14985 char_u buf[NUMBUFLEN];
14986
14987 str = get_tv_string_buf(&argvars[0], buf);
14988 do_pyeval(str, rettv);
14989}
14990#endif
14991
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014992/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993 * "range()" function
14994 */
14995 static void
14996f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 typval_T *argvars;
14998 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014999{
15000 long start;
15001 long end;
15002 long stride = 1;
15003 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015006 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015007 if (argvars[1].v_type == VAR_UNKNOWN)
15008 {
15009 end = start - 1;
15010 start = 0;
15011 }
15012 else
15013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015014 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015017 }
15018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 if (error)
15020 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015021 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015022 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015023 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015024 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015025 else
15026 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015027 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015028 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015029 if (list_append_number(rettv->vval.v_list,
15030 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015031 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015032 }
15033}
15034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035/*
15036 * "readfile()" function
15037 */
15038 static void
15039f_readfile(argvars, rettv)
15040 typval_T *argvars;
15041 typval_T *rettv;
15042{
15043 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015044 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015045 char_u *fname;
15046 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015047 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15048 int io_size = sizeof(buf);
15049 int readlen; /* size of last fread() */
15050 char_u *prev = NULL; /* previously read bytes, if any */
15051 long prevlen = 0; /* length of data in prev */
15052 long prevsize = 0; /* size of prev buffer */
15053 long maxline = MAXLNUM;
15054 long cnt = 0;
15055 char_u *p; /* position in buf */
15056 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015058 if (argvars[1].v_type != VAR_UNKNOWN)
15059 {
15060 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15061 binary = TRUE;
15062 if (argvars[2].v_type != VAR_UNKNOWN)
15063 maxline = get_tv_number(&argvars[2]);
15064 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015066 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015067 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015068
15069 /* Always open the file in binary mode, library functions have a mind of
15070 * their own about CR-LF conversion. */
15071 fname = get_tv_string(&argvars[0]);
15072 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15073 {
15074 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15075 return;
15076 }
15077
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015078 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015079 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015080 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015081
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015082 /* This for loop processes what was read, but is also entered at end
15083 * of file so that either:
15084 * - an incomplete line gets written
15085 * - a "binary" file gets an empty line at the end if it ends in a
15086 * newline. */
15087 for (p = buf, start = buf;
15088 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15089 ++p)
15090 {
15091 if (*p == '\n' || readlen <= 0)
15092 {
15093 listitem_T *li;
15094 char_u *s = NULL;
15095 long_u len = p - start;
15096
15097 /* Finished a line. Remove CRs before NL. */
15098 if (readlen > 0 && !binary)
15099 {
15100 while (len > 0 && start[len - 1] == '\r')
15101 --len;
15102 /* removal may cross back to the "prev" string */
15103 if (len == 0)
15104 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15105 --prevlen;
15106 }
15107 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015108 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015109 else
15110 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015111 /* Change "prev" buffer to be the right size. This way
15112 * the bytes are only copied once, and very long lines are
15113 * allocated only once. */
15114 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015115 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015116 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015117 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015118 prev = NULL; /* the list will own the string */
15119 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015120 }
15121 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015122 if (s == NULL)
15123 {
15124 do_outofmem_msg((long_u) prevlen + len + 1);
15125 failed = TRUE;
15126 break;
15127 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015128
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015129 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015130 {
15131 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015132 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015133 break;
15134 }
15135 li->li_tv.v_type = VAR_STRING;
15136 li->li_tv.v_lock = 0;
15137 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015138 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015139
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015140 start = p + 1; /* step over newline */
15141 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015142 break;
15143 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015144 else if (*p == NUL)
15145 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015146#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015147 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15148 * when finding the BF and check the previous two bytes. */
15149 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015150 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015151 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15152 * + 1, these may be in the "prev" string. */
15153 char_u back1 = p >= buf + 1 ? p[-1]
15154 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15155 char_u back2 = p >= buf + 2 ? p[-2]
15156 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15157 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015158
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015159 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015160 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015161 char_u *dest = p - 2;
15162
15163 /* Usually a BOM is at the beginning of a file, and so at
15164 * the beginning of a line; then we can just step over it.
15165 */
15166 if (start == dest)
15167 start = p + 1;
15168 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015169 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015170 /* have to shuffle buf to close gap */
15171 int adjust_prevlen = 0;
15172
15173 if (dest < buf)
15174 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015175 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015176 dest = buf;
15177 }
15178 if (readlen > p - buf + 1)
15179 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15180 readlen -= 3 - adjust_prevlen;
15181 prevlen -= adjust_prevlen;
15182 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015183 }
15184 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015185 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015186#endif
15187 } /* for */
15188
15189 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15190 break;
15191 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015192 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015193 /* There's part of a line in buf, store it in "prev". */
15194 if (p - start + prevlen >= prevsize)
15195 {
15196 /* need bigger "prev" buffer */
15197 char_u *newprev;
15198
15199 /* A common use case is ordinary text files and "prev" gets a
15200 * fragment of a line, so the first allocation is made
15201 * small, to avoid repeatedly 'allocing' large and
15202 * 'reallocing' small. */
15203 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015204 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015205 else
15206 {
15207 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015208 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015209 prevsize = grow50pc > growmin ? grow50pc : growmin;
15210 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015211 newprev = prev == NULL ? alloc(prevsize)
15212 : vim_realloc(prev, prevsize);
15213 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015214 {
15215 do_outofmem_msg((long_u)prevsize);
15216 failed = TRUE;
15217 break;
15218 }
15219 prev = newprev;
15220 }
15221 /* Add the line part to end of "prev". */
15222 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015223 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015224 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015225 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015226
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015227 /*
15228 * For a negative line count use only the lines at the end of the file,
15229 * free the rest.
15230 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015231 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015232 while (cnt > -maxline)
15233 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015234 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015235 --cnt;
15236 }
15237
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015238 if (failed)
15239 {
15240 list_free(rettv->vval.v_list, TRUE);
15241 /* readfile doc says an empty list is returned on error */
15242 rettv->vval.v_list = list_alloc();
15243 }
15244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015245 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015246 fclose(fd);
15247}
15248
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015249#if defined(FEAT_RELTIME)
15250static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15251
15252/*
15253 * Convert a List to proftime_T.
15254 * Return FAIL when there is something wrong.
15255 */
15256 static int
15257list2proftime(arg, tm)
15258 typval_T *arg;
15259 proftime_T *tm;
15260{
15261 long n1, n2;
15262 int error = FALSE;
15263
15264 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15265 || arg->vval.v_list->lv_len != 2)
15266 return FAIL;
15267 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15268 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15269# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015270 tm->HighPart = n1;
15271 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015272# else
15273 tm->tv_sec = n1;
15274 tm->tv_usec = n2;
15275# endif
15276 return error ? FAIL : OK;
15277}
15278#endif /* FEAT_RELTIME */
15279
15280/*
15281 * "reltime()" function
15282 */
15283 static void
15284f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015285 typval_T *argvars UNUSED;
15286 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015287{
15288#ifdef FEAT_RELTIME
15289 proftime_T res;
15290 proftime_T start;
15291
15292 if (argvars[0].v_type == VAR_UNKNOWN)
15293 {
15294 /* No arguments: get current time. */
15295 profile_start(&res);
15296 }
15297 else if (argvars[1].v_type == VAR_UNKNOWN)
15298 {
15299 if (list2proftime(&argvars[0], &res) == FAIL)
15300 return;
15301 profile_end(&res);
15302 }
15303 else
15304 {
15305 /* Two arguments: compute the difference. */
15306 if (list2proftime(&argvars[0], &start) == FAIL
15307 || list2proftime(&argvars[1], &res) == FAIL)
15308 return;
15309 profile_sub(&res, &start);
15310 }
15311
15312 if (rettv_list_alloc(rettv) == OK)
15313 {
15314 long n1, n2;
15315
15316# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015317 n1 = res.HighPart;
15318 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015319# else
15320 n1 = res.tv_sec;
15321 n2 = res.tv_usec;
15322# endif
15323 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15324 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15325 }
15326#endif
15327}
15328
15329/*
15330 * "reltimestr()" function
15331 */
15332 static void
15333f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015334 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015335 typval_T *rettv;
15336{
15337#ifdef FEAT_RELTIME
15338 proftime_T tm;
15339#endif
15340
15341 rettv->v_type = VAR_STRING;
15342 rettv->vval.v_string = NULL;
15343#ifdef FEAT_RELTIME
15344 if (list2proftime(&argvars[0], &tm) == OK)
15345 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15346#endif
15347}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15350static void make_connection __ARGS((void));
15351static int check_connection __ARGS((void));
15352
15353 static void
15354make_connection()
15355{
15356 if (X_DISPLAY == NULL
15357# ifdef FEAT_GUI
15358 && !gui.in_use
15359# endif
15360 )
15361 {
15362 x_force_connect = TRUE;
15363 setup_term_clip();
15364 x_force_connect = FALSE;
15365 }
15366}
15367
15368 static int
15369check_connection()
15370{
15371 make_connection();
15372 if (X_DISPLAY == NULL)
15373 {
15374 EMSG(_("E240: No connection to Vim server"));
15375 return FAIL;
15376 }
15377 return OK;
15378}
15379#endif
15380
15381#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015382static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015383
15384 static void
15385remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015386 typval_T *argvars;
15387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388 int expr;
15389{
15390 char_u *server_name;
15391 char_u *keys;
15392 char_u *r = NULL;
15393 char_u buf[NUMBUFLEN];
15394# ifdef WIN32
15395 HWND w;
15396# else
15397 Window w;
15398# endif
15399
15400 if (check_restricted() || check_secure())
15401 return;
15402
15403# ifdef FEAT_X11
15404 if (check_connection() == FAIL)
15405 return;
15406# endif
15407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015408 server_name = get_tv_string_chk(&argvars[0]);
15409 if (server_name == NULL)
15410 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015411 keys = get_tv_string_buf(&argvars[1], buf);
15412# ifdef WIN32
15413 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15414# else
15415 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15416 < 0)
15417# endif
15418 {
15419 if (r != NULL)
15420 EMSG(r); /* sending worked but evaluation failed */
15421 else
15422 EMSG2(_("E241: Unable to send to %s"), server_name);
15423 return;
15424 }
15425
15426 rettv->vval.v_string = r;
15427
15428 if (argvars[2].v_type != VAR_UNKNOWN)
15429 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015431 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015433
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015434 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015435 v.di_tv.v_type = VAR_STRING;
15436 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 idvar = get_tv_string_chk(&argvars[2]);
15438 if (idvar != NULL)
15439 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441 }
15442}
15443#endif
15444
15445/*
15446 * "remote_expr()" function
15447 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015448 static void
15449f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015452{
15453 rettv->v_type = VAR_STRING;
15454 rettv->vval.v_string = NULL;
15455#ifdef FEAT_CLIENTSERVER
15456 remote_common(argvars, rettv, TRUE);
15457#endif
15458}
15459
15460/*
15461 * "remote_foreground()" function
15462 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015463 static void
15464f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015465 typval_T *argvars UNUSED;
15466 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015468#ifdef FEAT_CLIENTSERVER
15469# ifdef WIN32
15470 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 {
15472 char_u *server_name = get_tv_string_chk(&argvars[0]);
15473
15474 if (server_name != NULL)
15475 serverForeground(server_name);
15476 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015477# else
15478 /* Send a foreground() expression to the server. */
15479 argvars[1].v_type = VAR_STRING;
15480 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15481 argvars[2].v_type = VAR_UNKNOWN;
15482 remote_common(argvars, rettv, TRUE);
15483 vim_free(argvars[1].vval.v_string);
15484# endif
15485#endif
15486}
15487
Bram Moolenaar0d660222005-01-07 21:51:51 +000015488 static void
15489f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015492{
15493#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015495 char_u *s = NULL;
15496# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015497 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015498# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015499 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015500
15501 if (check_restricted() || check_secure())
15502 {
15503 rettv->vval.v_number = -1;
15504 return;
15505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015506 serverid = get_tv_string_chk(&argvars[0]);
15507 if (serverid == NULL)
15508 {
15509 rettv->vval.v_number = -1;
15510 return; /* type error; errmsg already given */
15511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015513 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015514 if (n == 0)
15515 rettv->vval.v_number = -1;
15516 else
15517 {
15518 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15519 rettv->vval.v_number = (s != NULL);
15520 }
15521# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015522 if (check_connection() == FAIL)
15523 return;
15524
15525 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015526 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527# endif
15528
15529 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 char_u *retvar;
15532
Bram Moolenaar33570922005-01-25 22:26:29 +000015533 v.di_tv.v_type = VAR_STRING;
15534 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015535 retvar = get_tv_string_chk(&argvars[1]);
15536 if (retvar != NULL)
15537 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015538 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015539 }
15540#else
15541 rettv->vval.v_number = -1;
15542#endif
15543}
15544
Bram Moolenaar0d660222005-01-07 21:51:51 +000015545 static void
15546f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549{
15550 char_u *r = NULL;
15551
15552#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015553 char_u *serverid = get_tv_string_chk(&argvars[0]);
15554
15555 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556 {
15557# ifdef WIN32
15558 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015559 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015560
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015561 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 if (n != 0)
15563 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15564 if (r == NULL)
15565# else
15566 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015568# endif
15569 EMSG(_("E277: Unable to read a server reply"));
15570 }
15571#endif
15572 rettv->v_type = VAR_STRING;
15573 rettv->vval.v_string = r;
15574}
15575
15576/*
15577 * "remote_send()" function
15578 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015579 static void
15580f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583{
15584 rettv->v_type = VAR_STRING;
15585 rettv->vval.v_string = NULL;
15586#ifdef FEAT_CLIENTSERVER
15587 remote_common(argvars, rettv, FALSE);
15588#endif
15589}
15590
15591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015592 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015593 */
15594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 typval_T *argvars;
15597 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598{
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 list_T *l;
15600 listitem_T *item, *item2;
15601 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015602 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015603 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015604 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 dict_T *d;
15606 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015607 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015608
Bram Moolenaar8c711452005-01-14 21:53:12 +000015609 if (argvars[0].v_type == VAR_DICT)
15610 {
15611 if (argvars[2].v_type != VAR_UNKNOWN)
15612 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015613 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015614 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 key = get_tv_string_chk(&argvars[1]);
15617 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 di = dict_find(d, key, -1);
15620 if (di == NULL)
15621 EMSG2(_(e_dictkey), key);
15622 else
15623 {
15624 *rettv = di->di_tv;
15625 init_tv(&di->di_tv);
15626 dictitem_remove(d, di);
15627 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015629 }
15630 }
15631 else if (argvars[0].v_type != VAR_LIST)
15632 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015633 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015634 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 int error = FALSE;
15637
15638 idx = get_tv_number_chk(&argvars[1], &error);
15639 if (error)
15640 ; /* type error: do nothing, errmsg already given */
15641 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015642 EMSGN(_(e_listidx), idx);
15643 else
15644 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015645 if (argvars[2].v_type == VAR_UNKNOWN)
15646 {
15647 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015648 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015649 *rettv = item->li_tv;
15650 vim_free(item);
15651 }
15652 else
15653 {
15654 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 end = get_tv_number_chk(&argvars[2], &error);
15656 if (error)
15657 ; /* type error: do nothing */
15658 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015659 EMSGN(_(e_listidx), end);
15660 else
15661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015662 int cnt = 0;
15663
15664 for (li = item; li != NULL; li = li->li_next)
15665 {
15666 ++cnt;
15667 if (li == item2)
15668 break;
15669 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015670 if (li == NULL) /* didn't find "item2" after "item" */
15671 EMSG(_(e_invrange));
15672 else
15673 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015674 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015675 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015676 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015677 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015678 l->lv_first = item;
15679 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015680 item->li_prev = NULL;
15681 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015682 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015683 }
15684 }
15685 }
15686 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015687 }
15688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
15692 * "rename({from}, {to})" function
15693 */
15694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
15699 char_u buf[NUMBUFLEN];
15700
15701 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015704 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15705 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
15708/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015709 * "repeat()" function
15710 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015711 static void
15712f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015713 typval_T *argvars;
15714 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015715{
15716 char_u *p;
15717 int n;
15718 int slen;
15719 int len;
15720 char_u *r;
15721 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015722
15723 n = get_tv_number(&argvars[1]);
15724 if (argvars[0].v_type == VAR_LIST)
15725 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015726 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015727 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015728 if (list_extend(rettv->vval.v_list,
15729 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015730 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015731 }
15732 else
15733 {
15734 p = get_tv_string(&argvars[0]);
15735 rettv->v_type = VAR_STRING;
15736 rettv->vval.v_string = NULL;
15737
15738 slen = (int)STRLEN(p);
15739 len = slen * n;
15740 if (len <= 0)
15741 return;
15742
15743 r = alloc(len + 1);
15744 if (r != NULL)
15745 {
15746 for (i = 0; i < n; i++)
15747 mch_memmove(r + i * slen, p, (size_t)slen);
15748 r[len] = NUL;
15749 }
15750
15751 rettv->vval.v_string = r;
15752 }
15753}
15754
15755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 * "resolve()" function
15757 */
15758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015760 typval_T *argvars;
15761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762{
15763 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015764#ifdef HAVE_READLINK
15765 char_u *buf = NULL;
15766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769#ifdef FEAT_SHORTCUT
15770 {
15771 char_u *v = NULL;
15772
15773 v = mch_resolve_shortcut(p);
15774 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015775 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015777 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 }
15779#else
15780# ifdef HAVE_READLINK
15781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 char_u *cpy;
15783 int len;
15784 char_u *remain = NULL;
15785 char_u *q;
15786 int is_relative_to_current = FALSE;
15787 int has_trailing_pathsep = FALSE;
15788 int limit = 100;
15789
15790 p = vim_strsave(p);
15791
15792 if (p[0] == '.' && (vim_ispathsep(p[1])
15793 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15794 is_relative_to_current = TRUE;
15795
15796 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015797 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015800 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802
15803 q = getnextcomp(p);
15804 if (*q != NUL)
15805 {
15806 /* Separate the first path component in "p", and keep the
15807 * remainder (beginning with the path separator). */
15808 remain = vim_strsave(q - 1);
15809 q[-1] = NUL;
15810 }
15811
Bram Moolenaard9462e32011-04-11 21:35:11 +020015812 buf = alloc(MAXPATHL + 1);
15813 if (buf == NULL)
15814 goto fail;
15815
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 for (;;)
15817 {
15818 for (;;)
15819 {
15820 len = readlink((char *)p, (char *)buf, MAXPATHL);
15821 if (len <= 0)
15822 break;
15823 buf[len] = NUL;
15824
15825 if (limit-- == 0)
15826 {
15827 vim_free(p);
15828 vim_free(remain);
15829 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 goto fail;
15832 }
15833
15834 /* Ensure that the result will have a trailing path separator
15835 * if the argument has one. */
15836 if (remain == NULL && has_trailing_pathsep)
15837 add_pathsep(buf);
15838
15839 /* Separate the first path component in the link value and
15840 * concatenate the remainders. */
15841 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15842 if (*q != NUL)
15843 {
15844 if (remain == NULL)
15845 remain = vim_strsave(q - 1);
15846 else
15847 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015848 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 if (cpy != NULL)
15850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 vim_free(remain);
15852 remain = cpy;
15853 }
15854 }
15855 q[-1] = NUL;
15856 }
15857
15858 q = gettail(p);
15859 if (q > p && *q == NUL)
15860 {
15861 /* Ignore trailing path separator. */
15862 q[-1] = NUL;
15863 q = gettail(p);
15864 }
15865 if (q > p && !mch_isFullName(buf))
15866 {
15867 /* symlink is relative to directory of argument */
15868 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15869 if (cpy != NULL)
15870 {
15871 STRCPY(cpy, p);
15872 STRCPY(gettail(cpy), buf);
15873 vim_free(p);
15874 p = cpy;
15875 }
15876 }
15877 else
15878 {
15879 vim_free(p);
15880 p = vim_strsave(buf);
15881 }
15882 }
15883
15884 if (remain == NULL)
15885 break;
15886
15887 /* Append the first path component of "remain" to "p". */
15888 q = getnextcomp(remain + 1);
15889 len = q - remain - (*q != NUL);
15890 cpy = vim_strnsave(p, STRLEN(p) + len);
15891 if (cpy != NULL)
15892 {
15893 STRNCAT(cpy, remain, len);
15894 vim_free(p);
15895 p = cpy;
15896 }
15897 /* Shorten "remain". */
15898 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015899 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 else
15901 {
15902 vim_free(remain);
15903 remain = NULL;
15904 }
15905 }
15906
15907 /* If the result is a relative path name, make it explicitly relative to
15908 * the current directory if and only if the argument had this form. */
15909 if (!vim_ispathsep(*p))
15910 {
15911 if (is_relative_to_current
15912 && *p != NUL
15913 && !(p[0] == '.'
15914 && (p[1] == NUL
15915 || vim_ispathsep(p[1])
15916 || (p[1] == '.'
15917 && (p[2] == NUL
15918 || vim_ispathsep(p[2]))))))
15919 {
15920 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015921 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 if (cpy != NULL)
15923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 vim_free(p);
15925 p = cpy;
15926 }
15927 }
15928 else if (!is_relative_to_current)
15929 {
15930 /* Strip leading "./". */
15931 q = p;
15932 while (q[0] == '.' && vim_ispathsep(q[1]))
15933 q += 2;
15934 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015935 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 }
15937 }
15938
15939 /* Ensure that the result will have no trailing path separator
15940 * if the argument had none. But keep "/" or "//". */
15941 if (!has_trailing_pathsep)
15942 {
15943 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015944 if (after_pathsep(p, q))
15945 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 }
15947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015948 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 }
15950# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952# endif
15953#endif
15954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015955 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956
15957#ifdef HAVE_READLINK
15958fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015959 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962}
15963
15964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 */
15967 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015968f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015969 typval_T *argvars;
15970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971{
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 list_T *l;
15973 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975 if (argvars[0].v_type != VAR_LIST)
15976 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015977 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015978 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979 {
15980 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015981 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015982 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015983 while (li != NULL)
15984 {
15985 ni = li->li_prev;
15986 list_append(l, li);
15987 li = ni;
15988 }
15989 rettv->vval.v_list = l;
15990 rettv->v_type = VAR_LIST;
15991 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015992 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994}
15995
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015996#define SP_NOMOVE 0x01 /* don't move cursor */
15997#define SP_REPEAT 0x02 /* repeat to find outer pair */
15998#define SP_RETCOUNT 0x04 /* return matchcount */
15999#define SP_SETPCMARK 0x08 /* set previous context mark */
16000#define SP_START 0x10 /* accept match at start position */
16001#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16002#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016003
Bram Moolenaar33570922005-01-25 22:26:29 +000016004static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016005
16006/*
16007 * Get flags for a search function.
16008 * Possibly sets "p_ws".
16009 * Returns BACKWARD, FORWARD or zero (for an error).
16010 */
16011 static int
16012get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016014 int *flagsp;
16015{
16016 int dir = FORWARD;
16017 char_u *flags;
16018 char_u nbuf[NUMBUFLEN];
16019 int mask;
16020
16021 if (varp->v_type != VAR_UNKNOWN)
16022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016023 flags = get_tv_string_buf_chk(varp, nbuf);
16024 if (flags == NULL)
16025 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 while (*flags != NUL)
16027 {
16028 switch (*flags)
16029 {
16030 case 'b': dir = BACKWARD; break;
16031 case 'w': p_ws = TRUE; break;
16032 case 'W': p_ws = FALSE; break;
16033 default: mask = 0;
16034 if (flagsp != NULL)
16035 switch (*flags)
16036 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016037 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016038 case 'e': mask = SP_END; break;
16039 case 'm': mask = SP_RETCOUNT; break;
16040 case 'n': mask = SP_NOMOVE; break;
16041 case 'p': mask = SP_SUBPAT; break;
16042 case 'r': mask = SP_REPEAT; break;
16043 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016044 }
16045 if (mask == 0)
16046 {
16047 EMSG2(_(e_invarg2), flags);
16048 dir = 0;
16049 }
16050 else
16051 *flagsp |= mask;
16052 }
16053 if (dir == 0)
16054 break;
16055 ++flags;
16056 }
16057 }
16058 return dir;
16059}
16060
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016062 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016064 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016065search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016067 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016068 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016070 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 char_u *pat;
16072 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016073 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 int save_p_ws = p_ws;
16075 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016076 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016077 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016078 proftime_T tm;
16079#ifdef FEAT_RELTIME
16080 long time_limit = 0;
16081#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016082 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016083 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016086 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016087 if (dir == 0)
16088 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016089 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016090 if (flags & SP_START)
16091 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016092 if (flags & SP_END)
16093 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094
Bram Moolenaar76929292008-01-06 19:07:36 +000016095 /* Optional arguments: line number to stop searching and timeout. */
16096 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016097 {
16098 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16099 if (lnum_stop < 0)
16100 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016101#ifdef FEAT_RELTIME
16102 if (argvars[3].v_type != VAR_UNKNOWN)
16103 {
16104 time_limit = get_tv_number_chk(&argvars[3], NULL);
16105 if (time_limit < 0)
16106 goto theend;
16107 }
16108#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016109 }
16110
Bram Moolenaar76929292008-01-06 19:07:36 +000016111#ifdef FEAT_RELTIME
16112 /* Set the time limit, if there is one. */
16113 profile_setlimit(time_limit, &tm);
16114#endif
16115
Bram Moolenaar231334e2005-07-25 20:46:57 +000016116 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016117 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016118 * Check to make sure only those flags are set.
16119 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16120 * flags cannot be set. Check for that condition also.
16121 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016122 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016123 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016126 goto theend;
16127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016129 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016130 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016131 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016132 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016134 if (flags & SP_SUBPAT)
16135 retval = subpatnum;
16136 else
16137 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016138 if (flags & SP_SETPCMARK)
16139 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016141 if (match_pos != NULL)
16142 {
16143 /* Store the match cursor position */
16144 match_pos->lnum = pos.lnum;
16145 match_pos->col = pos.col + 1;
16146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 /* "/$" will put the cursor after the end of the line, may need to
16148 * correct that here */
16149 check_cursor();
16150 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016151
16152 /* If 'n' flag is used: restore cursor position. */
16153 if (flags & SP_NOMOVE)
16154 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016155 else
16156 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016157theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016159
16160 return retval;
16161}
16162
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016163#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016164
16165/*
16166 * round() is not in C90, use ceil() or floor() instead.
16167 */
16168 float_T
16169vim_round(f)
16170 float_T f;
16171{
16172 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16173}
16174
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016175/*
16176 * "round({float})" function
16177 */
16178 static void
16179f_round(argvars, rettv)
16180 typval_T *argvars;
16181 typval_T *rettv;
16182{
16183 float_T f;
16184
16185 rettv->v_type = VAR_FLOAT;
16186 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016187 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016188 else
16189 rettv->vval.v_float = 0.0;
16190}
16191#endif
16192
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016193/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016194 * "screenattr()" function
16195 */
16196 static void
16197f_screenattr(argvars, rettv)
16198 typval_T *argvars UNUSED;
16199 typval_T *rettv;
16200{
16201 int row;
16202 int col;
16203 int c;
16204
16205 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16206 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16207 if (row < 0 || row >= screen_Rows
16208 || col < 0 || col >= screen_Columns)
16209 c = -1;
16210 else
16211 c = ScreenAttrs[LineOffset[row] + col];
16212 rettv->vval.v_number = c;
16213}
16214
16215/*
16216 * "screenchar()" function
16217 */
16218 static void
16219f_screenchar(argvars, rettv)
16220 typval_T *argvars UNUSED;
16221 typval_T *rettv;
16222{
16223 int row;
16224 int col;
16225 int off;
16226 int c;
16227
16228 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16229 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16230 if (row < 0 || row >= screen_Rows
16231 || col < 0 || col >= screen_Columns)
16232 c = -1;
16233 else
16234 {
16235 off = LineOffset[row] + col;
16236#ifdef FEAT_MBYTE
16237 if (enc_utf8 && ScreenLinesUC[off] != 0)
16238 c = ScreenLinesUC[off];
16239 else
16240#endif
16241 c = ScreenLines[off];
16242 }
16243 rettv->vval.v_number = c;
16244}
16245
16246/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016247 * "screencol()" function
16248 *
16249 * First column is 1 to be consistent with virtcol().
16250 */
16251 static void
16252f_screencol(argvars, rettv)
16253 typval_T *argvars UNUSED;
16254 typval_T *rettv;
16255{
16256 rettv->vval.v_number = screen_screencol() + 1;
16257}
16258
16259/*
16260 * "screenrow()" function
16261 */
16262 static void
16263f_screenrow(argvars, rettv)
16264 typval_T *argvars UNUSED;
16265 typval_T *rettv;
16266{
16267 rettv->vval.v_number = screen_screenrow() + 1;
16268}
16269
16270/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016271 * "search()" function
16272 */
16273 static void
16274f_search(argvars, rettv)
16275 typval_T *argvars;
16276 typval_T *rettv;
16277{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016278 int flags = 0;
16279
16280 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281}
16282
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016284 * "searchdecl()" function
16285 */
16286 static void
16287f_searchdecl(argvars, rettv)
16288 typval_T *argvars;
16289 typval_T *rettv;
16290{
16291 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016292 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016293 int error = FALSE;
16294 char_u *name;
16295
16296 rettv->vval.v_number = 1; /* default: FAIL */
16297
16298 name = get_tv_string_chk(&argvars[0]);
16299 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016300 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016301 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016302 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16303 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16304 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016305 if (!error && name != NULL)
16306 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016307 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016308}
16309
16310/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016311 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016313 static int
16314searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016315 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016316 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317{
16318 char_u *spat, *mpat, *epat;
16319 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 int dir;
16322 int flags = 0;
16323 char_u nbuf1[NUMBUFLEN];
16324 char_u nbuf2[NUMBUFLEN];
16325 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016326 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016327 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016328 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 spat = get_tv_string_chk(&argvars[0]);
16332 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16333 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16334 if (spat == NULL || mpat == NULL || epat == NULL)
16335 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 /* Handle the optional fourth argument: flags */
16338 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016339 if (dir == 0)
16340 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016341
16342 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016343 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16344 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016345 if ((flags & (SP_END | SP_SUBPAT)) != 0
16346 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016347 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016348 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016349 goto theend;
16350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016352 /* Using 'r' implies 'W', otherwise it doesn't work. */
16353 if (flags & SP_REPEAT)
16354 p_ws = FALSE;
16355
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016356 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016357 if (argvars[3].v_type == VAR_UNKNOWN
16358 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 skip = (char_u *)"";
16360 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016362 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016363 if (argvars[5].v_type != VAR_UNKNOWN)
16364 {
16365 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16366 if (lnum_stop < 0)
16367 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016368#ifdef FEAT_RELTIME
16369 if (argvars[6].v_type != VAR_UNKNOWN)
16370 {
16371 time_limit = get_tv_number_chk(&argvars[6], NULL);
16372 if (time_limit < 0)
16373 goto theend;
16374 }
16375#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016376 }
16377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 if (skip == NULL)
16379 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016381 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016382 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016383
16384theend:
16385 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016386
16387 return retval;
16388}
16389
16390/*
16391 * "searchpair()" function
16392 */
16393 static void
16394f_searchpair(argvars, rettv)
16395 typval_T *argvars;
16396 typval_T *rettv;
16397{
16398 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16399}
16400
16401/*
16402 * "searchpairpos()" function
16403 */
16404 static void
16405f_searchpairpos(argvars, rettv)
16406 typval_T *argvars;
16407 typval_T *rettv;
16408{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016409 pos_T match_pos;
16410 int lnum = 0;
16411 int col = 0;
16412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016414 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016415
16416 if (searchpair_cmn(argvars, &match_pos) > 0)
16417 {
16418 lnum = match_pos.lnum;
16419 col = match_pos.col;
16420 }
16421
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016422 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16423 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016424}
16425
16426/*
16427 * Search for a start/middle/end thing.
16428 * Used by searchpair(), see its documentation for the details.
16429 * Returns 0 or -1 for no match,
16430 */
16431 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016432do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16433 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016434 char_u *spat; /* start pattern */
16435 char_u *mpat; /* middle pattern */
16436 char_u *epat; /* end pattern */
16437 int dir; /* BACKWARD or FORWARD */
16438 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016439 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016440 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016441 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016442 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016443{
16444 char_u *save_cpo;
16445 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16446 long retval = 0;
16447 pos_T pos;
16448 pos_T firstpos;
16449 pos_T foundpos;
16450 pos_T save_cursor;
16451 pos_T save_pos;
16452 int n;
16453 int r;
16454 int nest = 1;
16455 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016456 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016457 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016458
16459 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16460 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016461 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016462
Bram Moolenaar76929292008-01-06 19:07:36 +000016463#ifdef FEAT_RELTIME
16464 /* Set the time limit, if there is one. */
16465 profile_setlimit(time_limit, &tm);
16466#endif
16467
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016468 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16469 * start/middle/end (pat3, for the top pair). */
16470 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16471 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16472 if (pat2 == NULL || pat3 == NULL)
16473 goto theend;
16474 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16475 if (*mpat == NUL)
16476 STRCPY(pat3, pat2);
16477 else
16478 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16479 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016480 if (flags & SP_START)
16481 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016482
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 save_cursor = curwin->w_cursor;
16484 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016485 clearpos(&firstpos);
16486 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 pat = pat3;
16488 for (;;)
16489 {
16490 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016491 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16493 /* didn't find it or found the first match again: FAIL */
16494 break;
16495
16496 if (firstpos.lnum == 0)
16497 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016498 if (equalpos(pos, foundpos))
16499 {
16500 /* Found the same position again. Can happen with a pattern that
16501 * has "\zs" at the end and searching backwards. Advance one
16502 * character and try again. */
16503 if (dir == BACKWARD)
16504 decl(&pos);
16505 else
16506 incl(&pos);
16507 }
16508 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016510 /* clear the start flag to avoid getting stuck here */
16511 options &= ~SEARCH_START;
16512
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 /* If the skip pattern matches, ignore this match. */
16514 if (*skip != NUL)
16515 {
16516 save_pos = curwin->w_cursor;
16517 curwin->w_cursor = pos;
16518 r = eval_to_bool(skip, &err, NULL, FALSE);
16519 curwin->w_cursor = save_pos;
16520 if (err)
16521 {
16522 /* Evaluating {skip} caused an error, break here. */
16523 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016524 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 break;
16526 }
16527 if (r)
16528 continue;
16529 }
16530
16531 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16532 {
16533 /* Found end when searching backwards or start when searching
16534 * forward: nested pair. */
16535 ++nest;
16536 pat = pat2; /* nested, don't search for middle */
16537 }
16538 else
16539 {
16540 /* Found end when searching forward or start when searching
16541 * backward: end of (nested) pair; or found middle in outer pair. */
16542 if (--nest == 1)
16543 pat = pat3; /* outer level, search for middle */
16544 }
16545
16546 if (nest == 0)
16547 {
16548 /* Found the match: return matchcount or line number. */
16549 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016550 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016552 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016553 if (flags & SP_SETPCMARK)
16554 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 curwin->w_cursor = pos;
16556 if (!(flags & SP_REPEAT))
16557 break;
16558 nest = 1; /* search for next unmatched */
16559 }
16560 }
16561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016562 if (match_pos != NULL)
16563 {
16564 /* Store the match cursor position */
16565 match_pos->lnum = curwin->w_cursor.lnum;
16566 match_pos->col = curwin->w_cursor.col + 1;
16567 }
16568
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016570 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 curwin->w_cursor = save_cursor;
16572
16573theend:
16574 vim_free(pat2);
16575 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016576 if (p_cpo == empty_option)
16577 p_cpo = save_cpo;
16578 else
16579 /* Darn, evaluating the {skip} expression changed the value. */
16580 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016581
16582 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583}
16584
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016585/*
16586 * "searchpos()" function
16587 */
16588 static void
16589f_searchpos(argvars, rettv)
16590 typval_T *argvars;
16591 typval_T *rettv;
16592{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016593 pos_T match_pos;
16594 int lnum = 0;
16595 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016596 int n;
16597 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016599 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016600 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016601
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016602 n = search_cmn(argvars, &match_pos, &flags);
16603 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016604 {
16605 lnum = match_pos.lnum;
16606 col = match_pos.col;
16607 }
16608
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016609 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16610 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016611 if (flags & SP_SUBPAT)
16612 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016613}
16614
16615
Bram Moolenaar0d660222005-01-07 21:51:51 +000016616 static void
16617f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621#ifdef FEAT_CLIENTSERVER
16622 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016623 char_u *server = get_tv_string_chk(&argvars[0]);
16624 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 if (server == NULL || reply == NULL)
16628 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629 if (check_restricted() || check_secure())
16630 return;
16631# ifdef FEAT_X11
16632 if (check_connection() == FAIL)
16633 return;
16634# endif
16635
16636 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016638 EMSG(_("E258: Unable to send to client"));
16639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641 rettv->vval.v_number = 0;
16642#else
16643 rettv->vval.v_number = -1;
16644#endif
16645}
16646
Bram Moolenaar0d660222005-01-07 21:51:51 +000016647 static void
16648f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016649 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016650 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651{
16652 char_u *r = NULL;
16653
16654#ifdef FEAT_CLIENTSERVER
16655# ifdef WIN32
16656 r = serverGetVimNames();
16657# else
16658 make_connection();
16659 if (X_DISPLAY != NULL)
16660 r = serverGetVimNames(X_DISPLAY);
16661# endif
16662#endif
16663 rettv->v_type = VAR_STRING;
16664 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665}
16666
16667/*
16668 * "setbufvar()" function
16669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016672 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016673 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674{
16675 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 char_u nbuf[NUMBUFLEN];
16680
16681 if (check_restricted() || check_secure())
16682 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16684 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016685 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 varp = &argvars[2];
16687
16688 if (buf != NULL && varname != NULL && varp != NULL)
16689 {
16690 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692
16693 if (*varname == '&')
16694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016695 long numval;
16696 char_u *strval;
16697 int error = FALSE;
16698
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 numval = get_tv_number_chk(varp, &error);
16701 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016702 if (!error && strval != NULL)
16703 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 }
16705 else
16706 {
16707 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16708 if (bufvarname != NULL)
16709 {
16710 STRCPY(bufvarname, "b:");
16711 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016712 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 vim_free(bufvarname);
16714 }
16715 }
16716
16717 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720}
16721
16722/*
16723 * "setcmdpos()" function
16724 */
16725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016726f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016727 typval_T *argvars;
16728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 int pos = (int)get_tv_number(&argvars[0]) - 1;
16731
16732 if (pos >= 0)
16733 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734}
16735
16736/*
16737 * "setline()" function
16738 */
16739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016741 typval_T *argvars;
16742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743{
16744 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016745 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016746 list_T *l = NULL;
16747 listitem_T *li = NULL;
16748 long added = 0;
16749 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016751 lnum = get_tv_lnum(&argvars[0]);
16752 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016754 l = argvars[1].vval.v_list;
16755 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016757 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016758 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016759
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016760 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016761 for (;;)
16762 {
16763 if (l != NULL)
16764 {
16765 /* list argument, get next string */
16766 if (li == NULL)
16767 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016768 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016769 li = li->li_next;
16770 }
16771
16772 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016773 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016774 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016775
16776 /* When coming here from Insert mode, sync undo, so that this can be
16777 * undone separately from what was previously inserted. */
16778 if (u_sync_once == 2)
16779 {
16780 u_sync_once = 1; /* notify that u_sync() was called */
16781 u_sync(TRUE);
16782 }
16783
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016784 if (lnum <= curbuf->b_ml.ml_line_count)
16785 {
16786 /* existing line, replace it */
16787 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16788 {
16789 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016790 if (lnum == curwin->w_cursor.lnum)
16791 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016792 rettv->vval.v_number = 0; /* OK */
16793 }
16794 }
16795 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16796 {
16797 /* lnum is one past the last line, append the line */
16798 ++added;
16799 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16800 rettv->vval.v_number = 0; /* OK */
16801 }
16802
16803 if (l == NULL) /* only one string argument */
16804 break;
16805 ++lnum;
16806 }
16807
16808 if (added > 0)
16809 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810}
16811
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016812static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16813
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016815 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016816 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016817 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016818set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016819 win_T *wp UNUSED;
16820 typval_T *list_arg UNUSED;
16821 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016822 typval_T *rettv;
16823{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016824#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016825 char_u *act;
16826 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016827#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016828
Bram Moolenaar2641f772005-03-25 21:58:17 +000016829 rettv->vval.v_number = -1;
16830
16831#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016832 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016833 EMSG(_(e_listreq));
16834 else
16835 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016836 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016837
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016838 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016839 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016840 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016841 if (act == NULL)
16842 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016843 if (*act == 'a' || *act == 'r')
16844 action = *act;
16845 }
16846
Bram Moolenaar81484f42012-12-05 15:16:47 +010016847 if (l != NULL && set_errorlist(wp, l, action,
16848 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016849 rettv->vval.v_number = 0;
16850 }
16851#endif
16852}
16853
16854/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016855 * "setloclist()" function
16856 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016857 static void
16858f_setloclist(argvars, rettv)
16859 typval_T *argvars;
16860 typval_T *rettv;
16861{
16862 win_T *win;
16863
16864 rettv->vval.v_number = -1;
16865
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016866 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016867 if (win != NULL)
16868 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16869}
16870
16871/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016872 * "setmatches()" function
16873 */
16874 static void
16875f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016876 typval_T *argvars UNUSED;
16877 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016878{
16879#ifdef FEAT_SEARCH_EXTRA
16880 list_T *l;
16881 listitem_T *li;
16882 dict_T *d;
16883
16884 rettv->vval.v_number = -1;
16885 if (argvars[0].v_type != VAR_LIST)
16886 {
16887 EMSG(_(e_listreq));
16888 return;
16889 }
16890 if ((l = argvars[0].vval.v_list) != NULL)
16891 {
16892
16893 /* To some extent make sure that we are dealing with a list from
16894 * "getmatches()". */
16895 li = l->lv_first;
16896 while (li != NULL)
16897 {
16898 if (li->li_tv.v_type != VAR_DICT
16899 || (d = li->li_tv.vval.v_dict) == NULL)
16900 {
16901 EMSG(_(e_invarg));
16902 return;
16903 }
16904 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16905 && dict_find(d, (char_u *)"pattern", -1) != NULL
16906 && dict_find(d, (char_u *)"priority", -1) != NULL
16907 && dict_find(d, (char_u *)"id", -1) != NULL))
16908 {
16909 EMSG(_(e_invarg));
16910 return;
16911 }
16912 li = li->li_next;
16913 }
16914
16915 clear_matches(curwin);
16916 li = l->lv_first;
16917 while (li != NULL)
16918 {
16919 d = li->li_tv.vval.v_dict;
16920 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16921 get_dict_string(d, (char_u *)"pattern", FALSE),
16922 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020016923 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016924 li = li->li_next;
16925 }
16926 rettv->vval.v_number = 0;
16927 }
16928#endif
16929}
16930
16931/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016932 * "setpos()" function
16933 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016934 static void
16935f_setpos(argvars, rettv)
16936 typval_T *argvars;
16937 typval_T *rettv;
16938{
16939 pos_T pos;
16940 int fnum;
16941 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016942 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016943
Bram Moolenaar08250432008-02-13 11:42:46 +000016944 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016945 name = get_tv_string_chk(argvars);
16946 if (name != NULL)
16947 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016948 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016949 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016950 if (--pos.col < 0)
16951 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016952 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016953 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016954 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016955 if (fnum == curbuf->b_fnum)
16956 {
16957 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016958 if (curswant >= 0)
16959 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016960 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016961 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016962 }
16963 else
16964 EMSG(_(e_invarg));
16965 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016966 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16967 {
16968 /* set mark */
16969 if (setmark_pos(name[1], &pos, fnum) == OK)
16970 rettv->vval.v_number = 0;
16971 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016972 else
16973 EMSG(_(e_invarg));
16974 }
16975 }
16976}
16977
16978/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016979 * "setqflist()" function
16980 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016981 static void
16982f_setqflist(argvars, rettv)
16983 typval_T *argvars;
16984 typval_T *rettv;
16985{
16986 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16987}
16988
16989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 * "setreg()" function
16991 */
16992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016993f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016994 typval_T *argvars;
16995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996{
16997 int regname;
16998 char_u *strregname;
16999 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 int append;
17002 char_u yank_type;
17003 long block_len;
17004
17005 block_len = -1;
17006 yank_type = MAUTO;
17007 append = FALSE;
17008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017010 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 if (strregname == NULL)
17013 return; /* type error; errmsg already given */
17014 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 if (regname == 0 || regname == '@')
17016 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020 stropt = get_tv_string_chk(&argvars[2]);
17021 if (stropt == NULL)
17022 return; /* type error */
17023 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 switch (*stropt)
17025 {
17026 case 'a': case 'A': /* append */
17027 append = TRUE;
17028 break;
17029 case 'v': case 'c': /* character-wise selection */
17030 yank_type = MCHAR;
17031 break;
17032 case 'V': case 'l': /* line-wise selection */
17033 yank_type = MLINE;
17034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 case 'b': case Ctrl_V: /* block-wise selection */
17036 yank_type = MBLOCK;
17037 if (VIM_ISDIGIT(stropt[1]))
17038 {
17039 ++stropt;
17040 block_len = getdigits(&stropt) - 1;
17041 --stropt;
17042 }
17043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
17045 }
17046
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017047 if (argvars[1].v_type == VAR_LIST)
17048 {
17049 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017050 char_u **allocval;
17051 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017052 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017053 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017054 int len = argvars[1].vval.v_list->lv_len;
17055 listitem_T *li;
17056
Bram Moolenaar7d647822014-04-05 21:28:56 +020017057 /* First half: use for pointers to result lines; second half: use for
17058 * pointers to allocated copies. */
17059 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017060 if (lstval == NULL)
17061 return;
17062 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017063 allocval = lstval + len + 2;
17064 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017065
17066 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17067 li = li->li_next)
17068 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017069 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017070 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017071 goto free_lstval;
17072 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017073 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017074 /* Need to make a copy, next get_tv_string_buf_chk() will
17075 * overwrite the string. */
17076 strval = vim_strsave(buf);
17077 if (strval == NULL)
17078 goto free_lstval;
17079 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017080 }
17081 *curval++ = strval;
17082 }
17083 *curval++ = NULL;
17084
17085 write_reg_contents_lst(regname, lstval, -1,
17086 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017087free_lstval:
17088 while (curallocval > allocval)
17089 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017090 vim_free(lstval);
17091 }
17092 else
17093 {
17094 strval = get_tv_string_chk(&argvars[1]);
17095 if (strval == NULL)
17096 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101}
17102
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017103/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017104 * "settabvar()" function
17105 */
17106 static void
17107f_settabvar(argvars, rettv)
17108 typval_T *argvars;
17109 typval_T *rettv;
17110{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017111#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017112 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017113 tabpage_T *tp;
17114#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017115 char_u *varname, *tabvarname;
17116 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017117
17118 rettv->vval.v_number = 0;
17119
17120 if (check_restricted() || check_secure())
17121 return;
17122
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017123#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017124 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017125#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017126 varname = get_tv_string_chk(&argvars[1]);
17127 varp = &argvars[2];
17128
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017129 if (varname != NULL && varp != NULL
17130#ifdef FEAT_WINDOWS
17131 && tp != NULL
17132#endif
17133 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017134 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017135#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017136 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017137 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017138#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017139
17140 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17141 if (tabvarname != NULL)
17142 {
17143 STRCPY(tabvarname, "t:");
17144 STRCPY(tabvarname + 2, varname);
17145 set_var(tabvarname, varp, TRUE);
17146 vim_free(tabvarname);
17147 }
17148
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017149#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017150 /* Restore current tabpage */
17151 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017152 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017153#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017154 }
17155}
17156
17157/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017158 * "settabwinvar()" function
17159 */
17160 static void
17161f_settabwinvar(argvars, rettv)
17162 typval_T *argvars;
17163 typval_T *rettv;
17164{
17165 setwinvar(argvars, rettv, 1);
17166}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
17168/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017169 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017172f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 typval_T *argvars;
17174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017176 setwinvar(argvars, rettv, 0);
17177}
17178
17179/*
17180 * "setwinvar()" and "settabwinvar()" functions
17181 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017182
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017183 static void
17184setwinvar(argvars, rettv, off)
17185 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017186 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017187 int off;
17188{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 win_T *win;
17190#ifdef FEAT_WINDOWS
17191 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017192 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193#endif
17194 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017197 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198
17199 if (check_restricted() || check_secure())
17200 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017201
17202#ifdef FEAT_WINDOWS
17203 if (off == 1)
17204 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17205 else
17206 tp = curtab;
17207#endif
17208 win = find_win_by_nr(&argvars[off], tp);
17209 varname = get_tv_string_chk(&argvars[off + 1]);
17210 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
17212 if (win != NULL && varname != NULL && varp != NULL)
17213 {
17214#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017215 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017216 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217#endif
17218
17219 if (*varname == '&')
17220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017221 long numval;
17222 char_u *strval;
17223 int error = FALSE;
17224
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017226 numval = get_tv_number_chk(varp, &error);
17227 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017228 if (!error && strval != NULL)
17229 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 }
17231 else
17232 {
17233 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17234 if (winvarname != NULL)
17235 {
17236 STRCPY(winvarname, "w:");
17237 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017238 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 vim_free(winvarname);
17240 }
17241 }
17242
17243#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017244 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245#endif
17246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247}
17248
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017249#ifdef FEAT_CRYPT
17250/*
17251 * "sha256({string})" function
17252 */
17253 static void
17254f_sha256(argvars, rettv)
17255 typval_T *argvars;
17256 typval_T *rettv;
17257{
17258 char_u *p;
17259
17260 p = get_tv_string(&argvars[0]);
17261 rettv->vval.v_string = vim_strsave(
17262 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17263 rettv->v_type = VAR_STRING;
17264}
17265#endif /* FEAT_CRYPT */
17266
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017268 * "shellescape({string})" function
17269 */
17270 static void
17271f_shellescape(argvars, rettv)
17272 typval_T *argvars;
17273 typval_T *rettv;
17274{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017275 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017276 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017277 rettv->v_type = VAR_STRING;
17278}
17279
17280/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017281 * shiftwidth() function
17282 */
17283 static void
17284f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017285 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017286 typval_T *rettv;
17287{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017288 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017289}
17290
17291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017292 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 */
17294 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017296 typval_T *argvars;
17297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 p = get_tv_string(&argvars[0]);
17302 rettv->vval.v_string = vim_strsave(p);
17303 simplify_filename(rettv->vval.v_string); /* simplify in place */
17304 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305}
17306
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017307#ifdef FEAT_FLOAT
17308/*
17309 * "sin()" function
17310 */
17311 static void
17312f_sin(argvars, rettv)
17313 typval_T *argvars;
17314 typval_T *rettv;
17315{
17316 float_T f;
17317
17318 rettv->v_type = VAR_FLOAT;
17319 if (get_float_arg(argvars, &f) == OK)
17320 rettv->vval.v_float = sin(f);
17321 else
17322 rettv->vval.v_float = 0.0;
17323}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017324
17325/*
17326 * "sinh()" function
17327 */
17328 static void
17329f_sinh(argvars, rettv)
17330 typval_T *argvars;
17331 typval_T *rettv;
17332{
17333 float_T f;
17334
17335 rettv->v_type = VAR_FLOAT;
17336 if (get_float_arg(argvars, &f) == OK)
17337 rettv->vval.v_float = sinh(f);
17338 else
17339 rettv->vval.v_float = 0.0;
17340}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017341#endif
17342
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343static int
17344#ifdef __BORLANDC__
17345 _RTLENTRYF
17346#endif
17347 item_compare __ARGS((const void *s1, const void *s2));
17348static int
17349#ifdef __BORLANDC__
17350 _RTLENTRYF
17351#endif
17352 item_compare2 __ARGS((const void *s1, const void *s2));
17353
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017354/* struct used in the array that's given to qsort() */
17355typedef struct
17356{
17357 listitem_T *item;
17358 int idx;
17359} sortItem_T;
17360
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017362static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017363static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017364static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017366static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017367static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368#define ITEM_COMPARE_FAIL 999
17369
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017371 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 static int
17374#ifdef __BORLANDC__
17375_RTLENTRYF
17376#endif
17377item_compare(s1, s2)
17378 const void *s1;
17379 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017381 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 char_u *p1, *p2;
17383 char_u *tofree1, *tofree2;
17384 int res;
17385 char_u numbuf1[NUMBUFLEN];
17386 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017388 si1 = (sortItem_T *)s1;
17389 si2 = (sortItem_T *)s2;
17390 p1 = tv2string(&si1->item->li_tv, &tofree1, numbuf1, 0);
17391 p2 = tv2string(&si2->item->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017392 if (p1 == NULL)
17393 p1 = (char_u *)"";
17394 if (p2 == NULL)
17395 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017396 if (!item_compare_numeric)
17397 {
17398 if (item_compare_ic)
17399 res = STRICMP(p1, p2);
17400 else
17401 res = STRCMP(p1, p2);
17402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017404 {
17405 double n1, n2;
17406 n1 = strtod((char *)p1, (char **)&p1);
17407 n2 = strtod((char *)p2, (char **)&p2);
17408 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17409 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017410
17411 /* When the result would be zero, compare the pointers themselves. Makes
17412 * the sort stable. */
17413 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017414 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017415
Bram Moolenaar0d660222005-01-07 21:51:51 +000017416 vim_free(tofree1);
17417 vim_free(tofree2);
17418 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419}
17420
17421 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017422#ifdef __BORLANDC__
17423_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425item_compare2(s1, s2)
17426 const void *s1;
17427 const void *s2;
17428{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017429 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017430 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017432 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017435 /* shortcut after failure in previous call; compare all items equal */
17436 if (item_compare_func_err)
17437 return 0;
17438
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017439 si1 = (sortItem_T *)s1;
17440 si2 = (sortItem_T *)s2;
17441
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017442 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017443 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017444 copy_tv(&si1->item->li_tv, &argv[0]);
17445 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446
17447 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017448 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017449 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17450 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 clear_tv(&argv[0]);
17452 clear_tv(&argv[1]);
17453
17454 if (res == FAIL)
17455 res = ITEM_COMPARE_FAIL;
17456 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017457 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17458 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017459 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017460 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017461
17462 /* When the result would be zero, compare the pointers themselves. Makes
17463 * the sort stable. */
17464 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017465 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017466
Bram Moolenaar0d660222005-01-07 21:51:51 +000017467 return res;
17468}
17469
17470/*
17471 * "sort({list})" function
17472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017474do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 typval_T *argvars;
17476 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017477 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478{
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 list_T *l;
17480 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017481 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017482 long len;
17483 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017486 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 else
17488 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017489 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017490 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017491 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 return;
17493 rettv->vval.v_list = l;
17494 rettv->v_type = VAR_LIST;
17495 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
Bram Moolenaar0d660222005-01-07 21:51:51 +000017497 len = list_len(l);
17498 if (len <= 1)
17499 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017502 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017503 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017504 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017505 if (argvars[1].v_type != VAR_UNKNOWN)
17506 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017507 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510 else
17511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017512 int error = FALSE;
17513
17514 i = get_tv_number_chk(&argvars[1], &error);
17515 if (error)
17516 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017517 if (i == 1)
17518 item_compare_ic = TRUE;
17519 else
17520 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017521 if (item_compare_func != NULL)
17522 {
17523 if (STRCMP(item_compare_func, "n") == 0)
17524 {
17525 item_compare_func = NULL;
17526 item_compare_numeric = TRUE;
17527 }
17528 else if (STRCMP(item_compare_func, "i") == 0)
17529 {
17530 item_compare_func = NULL;
17531 item_compare_ic = TRUE;
17532 }
17533 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017535
17536 if (argvars[2].v_type != VAR_UNKNOWN)
17537 {
17538 /* optional third argument: {dict} */
17539 if (argvars[2].v_type != VAR_DICT)
17540 {
17541 EMSG(_(e_dictreq));
17542 return;
17543 }
17544 item_compare_selfdict = argvars[2].vval.v_dict;
17545 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017549 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 if (ptrs == NULL)
17551 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552
Bram Moolenaar327aa022014-03-25 18:24:23 +010017553 i = 0;
17554 if (sort)
17555 {
17556 /* sort(): ptrs will be the list to sort */
17557 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017558 {
17559 ptrs[i].item = li;
17560 ptrs[i].idx = i;
17561 ++i;
17562 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017563
17564 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017565 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017566 /* test the compare function */
17567 if (item_compare_func != NULL
17568 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017570 EMSG(_("E702: Sort compare function failed"));
17571 else
17572 {
17573 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017574 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017575 item_compare_func == NULL ? item_compare : item_compare2);
17576
17577 if (!item_compare_func_err)
17578 {
17579 /* Clear the List and append the items in sorted order. */
17580 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17581 l->lv_len = 0;
17582 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017583 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017584 }
17585 }
17586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017589 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17590
17591 /* f_uniq(): ptrs will be a stack of items to remove */
17592 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017593 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017594 item_compare_func_ptr = item_compare_func
17595 ? item_compare2 : item_compare;
17596
17597 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17598 li = li->li_next)
17599 {
17600 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17601 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017602 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017603 if (item_compare_func_err)
17604 {
17605 EMSG(_("E882: Uniq compare function failed"));
17606 break;
17607 }
17608 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 if (!item_compare_func_err)
17611 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017612 while (--i >= 0)
17613 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017614 li = ptrs[i].item->li_next;
17615 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017616 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017617 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017618 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017619 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017620 list_fix_watch(l, li);
17621 listitem_free(li);
17622 l->lv_len--;
17623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625 }
17626
17627 vim_free(ptrs);
17628 }
17629}
17630
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017631/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017632 * "sort({list})" function
17633 */
17634 static void
17635f_sort(argvars, rettv)
17636 typval_T *argvars;
17637 typval_T *rettv;
17638{
17639 do_sort_uniq(argvars, rettv, TRUE);
17640}
17641
17642/*
17643 * "uniq({list})" function
17644 */
17645 static void
17646f_uniq(argvars, rettv)
17647 typval_T *argvars;
17648 typval_T *rettv;
17649{
17650 do_sort_uniq(argvars, rettv, FALSE);
17651}
17652
17653/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017654 * "soundfold({word})" function
17655 */
17656 static void
17657f_soundfold(argvars, rettv)
17658 typval_T *argvars;
17659 typval_T *rettv;
17660{
17661 char_u *s;
17662
17663 rettv->v_type = VAR_STRING;
17664 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017665#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017666 rettv->vval.v_string = eval_soundfold(s);
17667#else
17668 rettv->vval.v_string = vim_strsave(s);
17669#endif
17670}
17671
17672/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017673 * "spellbadword()" function
17674 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017675 static void
17676f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017677 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017678 typval_T *rettv;
17679{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017680 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017681 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017682 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017683
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017684 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017685 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017686
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017687#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017688 if (argvars[0].v_type == VAR_UNKNOWN)
17689 {
17690 /* Find the start and length of the badly spelled word. */
17691 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17692 if (len != 0)
17693 word = ml_get_cursor();
17694 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017695 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017696 {
17697 char_u *str = get_tv_string_chk(&argvars[0]);
17698 int capcol = -1;
17699
17700 if (str != NULL)
17701 {
17702 /* Check the argument for spelling. */
17703 while (*str != NUL)
17704 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017705 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017706 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017707 {
17708 word = str;
17709 break;
17710 }
17711 str += len;
17712 }
17713 }
17714 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017715#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017716
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017717 list_append_string(rettv->vval.v_list, word, len);
17718 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017719 attr == HLF_SPB ? "bad" :
17720 attr == HLF_SPR ? "rare" :
17721 attr == HLF_SPL ? "local" :
17722 attr == HLF_SPC ? "caps" :
17723 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017724}
17725
17726/*
17727 * "spellsuggest()" function
17728 */
17729 static void
17730f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017731 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017732 typval_T *rettv;
17733{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017734#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017735 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017736 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017737 int maxcount;
17738 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017739 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017740 listitem_T *li;
17741 int need_capital = FALSE;
17742#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017743
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017744 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017745 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017746
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017747#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017748 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017749 {
17750 str = get_tv_string(&argvars[0]);
17751 if (argvars[1].v_type != VAR_UNKNOWN)
17752 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017753 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017754 if (maxcount <= 0)
17755 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017756 if (argvars[2].v_type != VAR_UNKNOWN)
17757 {
17758 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17759 if (typeerr)
17760 return;
17761 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017762 }
17763 else
17764 maxcount = 25;
17765
Bram Moolenaar4770d092006-01-12 23:22:24 +000017766 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017767
17768 for (i = 0; i < ga.ga_len; ++i)
17769 {
17770 str = ((char_u **)ga.ga_data)[i];
17771
17772 li = listitem_alloc();
17773 if (li == NULL)
17774 vim_free(str);
17775 else
17776 {
17777 li->li_tv.v_type = VAR_STRING;
17778 li->li_tv.v_lock = 0;
17779 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017780 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017781 }
17782 }
17783 ga_clear(&ga);
17784 }
17785#endif
17786}
17787
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017789f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 typval_T *argvars;
17791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792{
17793 char_u *str;
17794 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017795 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796 regmatch_T regmatch;
17797 char_u patbuf[NUMBUFLEN];
17798 char_u *save_cpo;
17799 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017800 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017801 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017802 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017803
17804 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17805 save_cpo = p_cpo;
17806 p_cpo = (char_u *)"";
17807
17808 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017809 if (argvars[1].v_type != VAR_UNKNOWN)
17810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017811 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17812 if (pat == NULL)
17813 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017814 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017815 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017816 }
17817 if (pat == NULL || *pat == NUL)
17818 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017819
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017820 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 if (typeerr)
17823 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17826 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017829 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017830 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017831 if (*str == NUL)
17832 match = FALSE; /* empty item at the end */
17833 else
17834 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017835 if (match)
17836 end = regmatch.startp[0];
17837 else
17838 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017839 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17840 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017841 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017842 if (list_append_string(rettv->vval.v_list, str,
17843 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017845 }
17846 if (!match)
17847 break;
17848 /* Advance to just after the match. */
17849 if (regmatch.endp[0] > str)
17850 col = 0;
17851 else
17852 {
17853 /* Don't get stuck at the same match. */
17854#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017855 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017856#else
17857 col = 1;
17858#endif
17859 }
17860 str = regmatch.endp[0];
17861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaar473de612013-06-08 18:19:48 +020017863 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867}
17868
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017869#ifdef FEAT_FLOAT
17870/*
17871 * "sqrt()" function
17872 */
17873 static void
17874f_sqrt(argvars, rettv)
17875 typval_T *argvars;
17876 typval_T *rettv;
17877{
17878 float_T f;
17879
17880 rettv->v_type = VAR_FLOAT;
17881 if (get_float_arg(argvars, &f) == OK)
17882 rettv->vval.v_float = sqrt(f);
17883 else
17884 rettv->vval.v_float = 0.0;
17885}
17886
17887/*
17888 * "str2float()" function
17889 */
17890 static void
17891f_str2float(argvars, rettv)
17892 typval_T *argvars;
17893 typval_T *rettv;
17894{
17895 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17896
17897 if (*p == '+')
17898 p = skipwhite(p + 1);
17899 (void)string2float(p, &rettv->vval.v_float);
17900 rettv->v_type = VAR_FLOAT;
17901}
17902#endif
17903
Bram Moolenaar2c932302006-03-18 21:42:09 +000017904/*
17905 * "str2nr()" function
17906 */
17907 static void
17908f_str2nr(argvars, rettv)
17909 typval_T *argvars;
17910 typval_T *rettv;
17911{
17912 int base = 10;
17913 char_u *p;
17914 long n;
17915
17916 if (argvars[1].v_type != VAR_UNKNOWN)
17917 {
17918 base = get_tv_number(&argvars[1]);
17919 if (base != 8 && base != 10 && base != 16)
17920 {
17921 EMSG(_(e_invarg));
17922 return;
17923 }
17924 }
17925
17926 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017927 if (*p == '+')
17928 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017929 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17930 rettv->vval.v_number = n;
17931}
17932
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933#ifdef HAVE_STRFTIME
17934/*
17935 * "strftime({format}[, {time}])" function
17936 */
17937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017939 typval_T *argvars;
17940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941{
17942 char_u result_buf[256];
17943 struct tm *curtime;
17944 time_t seconds;
17945 char_u *p;
17946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017950 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 seconds = time(NULL);
17952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 curtime = localtime(&seconds);
17955 /* MSVC returns NULL for an invalid value of seconds. */
17956 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 else
17959 {
17960# ifdef FEAT_MBYTE
17961 vimconv_T conv;
17962 char_u *enc;
17963
17964 conv.vc_type = CONV_NONE;
17965 enc = enc_locale();
17966 convert_setup(&conv, p_enc, enc);
17967 if (conv.vc_type != CONV_NONE)
17968 p = string_convert(&conv, p, NULL);
17969# endif
17970 if (p != NULL)
17971 (void)strftime((char *)result_buf, sizeof(result_buf),
17972 (char *)p, curtime);
17973 else
17974 result_buf[0] = NUL;
17975
17976# ifdef FEAT_MBYTE
17977 if (conv.vc_type != CONV_NONE)
17978 vim_free(p);
17979 convert_setup(&conv, enc, p_enc);
17980 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 else
17983# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985
17986# ifdef FEAT_MBYTE
17987 /* Release conversion descriptors */
17988 convert_setup(&conv, NULL, NULL);
17989 vim_free(enc);
17990# endif
17991 }
17992}
17993#endif
17994
17995/*
17996 * "stridx()" function
17997 */
17998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017999f_stridx(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{
18003 char_u buf[NUMBUFLEN];
18004 char_u *needle;
18005 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018010 needle = get_tv_string_chk(&argvars[1]);
18011 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018012 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018013 if (needle == NULL || haystack == NULL)
18014 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 if (argvars[2].v_type != VAR_UNKNOWN)
18017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018018 int error = FALSE;
18019
18020 start_idx = get_tv_number_chk(&argvars[2], &error);
18021 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018023 if (start_idx >= 0)
18024 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 }
18026
18027 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18028 if (pos != NULL)
18029 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030}
18031
18032/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018033 * "string()" function
18034 */
18035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018036f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 typval_T *argvars;
18038 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018039{
18040 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018041 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018043 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018044 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018045 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018046 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018047 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048}
18049
18050/*
18051 * "strlen()" function
18052 */
18053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *argvars;
18056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058 rettv->vval.v_number = (varnumber_T)(STRLEN(
18059 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060}
18061
18062/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018063 * "strchars()" function
18064 */
18065 static void
18066f_strchars(argvars, rettv)
18067 typval_T *argvars;
18068 typval_T *rettv;
18069{
18070 char_u *s = get_tv_string(&argvars[0]);
18071#ifdef FEAT_MBYTE
18072 varnumber_T len = 0;
18073
18074 while (*s != NUL)
18075 {
18076 mb_cptr2char_adv(&s);
18077 ++len;
18078 }
18079 rettv->vval.v_number = len;
18080#else
18081 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18082#endif
18083}
18084
18085/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018086 * "strdisplaywidth()" function
18087 */
18088 static void
18089f_strdisplaywidth(argvars, rettv)
18090 typval_T *argvars;
18091 typval_T *rettv;
18092{
18093 char_u *s = get_tv_string(&argvars[0]);
18094 int col = 0;
18095
18096 if (argvars[1].v_type != VAR_UNKNOWN)
18097 col = get_tv_number(&argvars[1]);
18098
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018099 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018100}
18101
18102/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018103 * "strwidth()" function
18104 */
18105 static void
18106f_strwidth(argvars, rettv)
18107 typval_T *argvars;
18108 typval_T *rettv;
18109{
18110 char_u *s = get_tv_string(&argvars[0]);
18111
18112 rettv->vval.v_number = (varnumber_T)(
18113#ifdef FEAT_MBYTE
18114 mb_string2cells(s, -1)
18115#else
18116 STRLEN(s)
18117#endif
18118 );
18119}
18120
18121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 * "strpart()" function
18123 */
18124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 typval_T *argvars;
18127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128{
18129 char_u *p;
18130 int n;
18131 int len;
18132 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018133 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018135 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 slen = (int)STRLEN(p);
18137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 n = get_tv_number_chk(&argvars[1], &error);
18139 if (error)
18140 len = 0;
18141 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018142 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 else
18144 len = slen - n; /* default len: all bytes that are available. */
18145
18146 /*
18147 * Only return the overlap between the specified part and the actual
18148 * string.
18149 */
18150 if (n < 0)
18151 {
18152 len += n;
18153 n = 0;
18154 }
18155 else if (n > slen)
18156 n = slen;
18157 if (len < 0)
18158 len = 0;
18159 else if (n + len > slen)
18160 len = slen - n;
18161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 rettv->v_type = VAR_STRING;
18163 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164}
18165
18166/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018167 * "strridx()" function
18168 */
18169 static void
18170f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 typval_T *argvars;
18172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173{
18174 char_u buf[NUMBUFLEN];
18175 char_u *needle;
18176 char_u *haystack;
18177 char_u *rest;
18178 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018179 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018181 needle = get_tv_string_chk(&argvars[1]);
18182 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018183
18184 rettv->vval.v_number = -1;
18185 if (needle == NULL || haystack == NULL)
18186 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018187
18188 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018189 if (argvars[2].v_type != VAR_UNKNOWN)
18190 {
18191 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018192 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018193 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018194 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018195 }
18196 else
18197 end_idx = haystack_len;
18198
Bram Moolenaar0d660222005-01-07 21:51:51 +000018199 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018200 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018202 lastmatch = haystack + end_idx;
18203 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018204 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018205 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018206 for (rest = haystack; *rest != '\0'; ++rest)
18207 {
18208 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018209 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 break;
18211 lastmatch = rest;
18212 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214
18215 if (lastmatch == NULL)
18216 rettv->vval.v_number = -1;
18217 else
18218 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18219}
18220
18221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 * "strtrans()" function
18223 */
18224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018225f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018226 typval_T *argvars;
18227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018229 rettv->v_type = VAR_STRING;
18230 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231}
18232
18233/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018234 * "submatch()" function
18235 */
18236 static void
18237f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018238 typval_T *argvars;
18239 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240{
Bram Moolenaar41571762014-04-02 19:00:58 +020018241 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018242 int no;
18243 int retList = 0;
18244
18245 no = (int)get_tv_number_chk(&argvars[0], &error);
18246 if (error)
18247 return;
18248 error = FALSE;
18249 if (argvars[1].v_type != VAR_UNKNOWN)
18250 retList = get_tv_number_chk(&argvars[1], &error);
18251 if (error)
18252 return;
18253
18254 if (retList == 0)
18255 {
18256 rettv->v_type = VAR_STRING;
18257 rettv->vval.v_string = reg_submatch(no);
18258 }
18259 else
18260 {
18261 rettv->v_type = VAR_LIST;
18262 rettv->vval.v_list = reg_submatch_list(no);
18263 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264}
18265
18266/*
18267 * "substitute()" function
18268 */
18269 static void
18270f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018271 typval_T *argvars;
18272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273{
18274 char_u patbuf[NUMBUFLEN];
18275 char_u subbuf[NUMBUFLEN];
18276 char_u flagsbuf[NUMBUFLEN];
18277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018278 char_u *str = get_tv_string_chk(&argvars[0]);
18279 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18280 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18281 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18282
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018284 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18285 rettv->vval.v_string = NULL;
18286 else
18287 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288}
18289
18290/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018291 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297{
18298 int id = 0;
18299#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018300 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 long col;
18302 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018303 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 lnum = get_tv_lnum(argvars); /* -1 on type error */
18306 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18307 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018309 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018310 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018311 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312#endif
18313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018314 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
18317/*
18318 * "synIDattr(id, what [, mode])" function
18319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018321f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018322 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324{
18325 char_u *p = NULL;
18326#ifdef FEAT_SYN_HL
18327 int id;
18328 char_u *what;
18329 char_u *mode;
18330 char_u modebuf[NUMBUFLEN];
18331 int modec;
18332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333 id = get_tv_number(&argvars[0]);
18334 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018335 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018337 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018339 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 modec = 0; /* replace invalid with current */
18341 }
18342 else
18343 {
18344#ifdef FEAT_GUI
18345 if (gui.in_use)
18346 modec = 'g';
18347 else
18348#endif
18349 if (t_colors > 1)
18350 modec = 'c';
18351 else
18352 modec = 't';
18353 }
18354
18355
18356 switch (TOLOWER_ASC(what[0]))
18357 {
18358 case 'b':
18359 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18360 p = highlight_color(id, what, modec);
18361 else /* bold */
18362 p = highlight_has_attr(id, HL_BOLD, modec);
18363 break;
18364
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018365 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 p = highlight_color(id, what, modec);
18367 break;
18368
18369 case 'i':
18370 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18371 p = highlight_has_attr(id, HL_INVERSE, modec);
18372 else /* italic */
18373 p = highlight_has_attr(id, HL_ITALIC, modec);
18374 break;
18375
18376 case 'n': /* name */
18377 p = get_highlight_name(NULL, id - 1);
18378 break;
18379
18380 case 'r': /* reverse */
18381 p = highlight_has_attr(id, HL_INVERSE, modec);
18382 break;
18383
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018384 case 's':
18385 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18386 p = highlight_color(id, what, modec);
18387 else /* standout */
18388 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 break;
18390
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018391 case 'u':
18392 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18393 /* underline */
18394 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18395 else
18396 /* undercurl */
18397 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 break;
18399 }
18400
18401 if (p != NULL)
18402 p = vim_strsave(p);
18403#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018404 rettv->v_type = VAR_STRING;
18405 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406}
18407
18408/*
18409 * "synIDtrans(id)" function
18410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415{
18416 int id;
18417
18418#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018419 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420
18421 if (id > 0)
18422 id = syn_get_final_id(id);
18423 else
18424#endif
18425 id = 0;
18426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428}
18429
18430/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018431 * "synconcealed(lnum, col)" function
18432 */
18433 static void
18434f_synconcealed(argvars, rettv)
18435 typval_T *argvars UNUSED;
18436 typval_T *rettv;
18437{
18438#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18439 long lnum;
18440 long col;
18441 int syntax_flags = 0;
18442 int cchar;
18443 int matchid = 0;
18444 char_u str[NUMBUFLEN];
18445#endif
18446
18447 rettv->v_type = VAR_LIST;
18448 rettv->vval.v_list = NULL;
18449
18450#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18451 lnum = get_tv_lnum(argvars); /* -1 on type error */
18452 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18453
18454 vim_memset(str, NUL, sizeof(str));
18455
18456 if (rettv_list_alloc(rettv) != FAIL)
18457 {
18458 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18459 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18460 && curwin->w_p_cole > 0)
18461 {
18462 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18463 syntax_flags = get_syntax_info(&matchid);
18464
18465 /* get the conceal character */
18466 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18467 {
18468 cchar = syn_get_sub_char();
18469 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18470 cchar = lcs_conceal;
18471 if (cchar != NUL)
18472 {
18473# ifdef FEAT_MBYTE
18474 if (has_mbyte)
18475 (*mb_char2bytes)(cchar, str);
18476 else
18477# endif
18478 str[0] = cchar;
18479 }
18480 }
18481 }
18482
18483 list_append_number(rettv->vval.v_list,
18484 (syntax_flags & HL_CONCEAL) != 0);
18485 /* -1 to auto-determine strlen */
18486 list_append_string(rettv->vval.v_list, str, -1);
18487 list_append_number(rettv->vval.v_list, matchid);
18488 }
18489#endif
18490}
18491
18492/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018493 * "synstack(lnum, col)" function
18494 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018495 static void
18496f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018497 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018498 typval_T *rettv;
18499{
18500#ifdef FEAT_SYN_HL
18501 long lnum;
18502 long col;
18503 int i;
18504 int id;
18505#endif
18506
18507 rettv->v_type = VAR_LIST;
18508 rettv->vval.v_list = NULL;
18509
18510#ifdef FEAT_SYN_HL
18511 lnum = get_tv_lnum(argvars); /* -1 on type error */
18512 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18513
18514 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018515 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018516 && rettv_list_alloc(rettv) != FAIL)
18517 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018518 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018519 for (i = 0; ; ++i)
18520 {
18521 id = syn_get_stack_item(i);
18522 if (id < 0)
18523 break;
18524 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18525 break;
18526 }
18527 }
18528#endif
18529}
18530
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018532get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018533 typval_T *argvars;
18534 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018535 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018537 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018539 char_u *infile = NULL;
18540 char_u buf[NUMBUFLEN];
18541 int err = FALSE;
18542 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018543 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018545 rettv->v_type = VAR_STRING;
18546 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018547 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018548 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018551 {
18552 /*
18553 * Write the string to a temp file, to be used for input of the shell
18554 * command.
18555 */
18556 if ((infile = vim_tempname('i')) == NULL)
18557 {
18558 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018559 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018560 }
18561
18562 fd = mch_fopen((char *)infile, WRITEBIN);
18563 if (fd == NULL)
18564 {
18565 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018566 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018567 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018568 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018569 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018570 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18571 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018572 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018573 else
18574 {
18575 p = get_tv_string_buf_chk(&argvars[1], buf);
18576 if (p == NULL)
18577 {
18578 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018579 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018580 }
18581 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18582 err = TRUE;
18583 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018584 if (fclose(fd) != 0)
18585 err = TRUE;
18586 if (err)
18587 {
18588 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018589 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018590 }
18591 }
18592
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018593 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018595 int len;
18596 listitem_T *li;
18597 char_u *s = NULL;
18598 char_u *start;
18599 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018600 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018602 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18603 SHELL_SILENT | SHELL_COOKED, &len);
18604 if (res == NULL)
18605 goto errret;
18606
18607 list = list_alloc();
18608 if (list == NULL)
18609 goto errret;
18610
18611 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018613 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018614 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018615 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018616 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018617
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018618 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018619 if (s == NULL)
18620 goto errret;
18621
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018622 for (p = s; start < end; ++p, ++start)
18623 *p = *start == NUL ? NL : *start;
18624 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018625
18626 li = listitem_alloc();
18627 if (li == NULL)
18628 {
18629 vim_free(s);
18630 goto errret;
18631 }
18632 li->li_tv.v_type = VAR_STRING;
18633 li->li_tv.vval.v_string = s;
18634 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018636
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018637 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018638 rettv->v_type = VAR_LIST;
18639 rettv->vval.v_list = list;
18640 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018642 else
18643 {
18644 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18645 SHELL_SILENT | SHELL_COOKED, NULL);
18646#ifdef USE_CR
18647 /* translate <CR> into <NL> */
18648 if (res != NULL)
18649 {
18650 char_u *s;
18651
18652 for (s = res; *s; ++s)
18653 {
18654 if (*s == CAR)
18655 *s = NL;
18656 }
18657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658#else
18659# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018660 /* translate <CR><NL> into <NL> */
18661 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018663 char_u *s, *d;
18664
18665 d = res;
18666 for (s = res; *s; ++s)
18667 {
18668 if (s[0] == CAR && s[1] == NL)
18669 ++s;
18670 *d++ = *s;
18671 }
18672 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674# endif
18675#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018676 rettv->vval.v_string = res;
18677 res = NULL;
18678 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018679
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018680errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018681 if (infile != NULL)
18682 {
18683 mch_remove(infile);
18684 vim_free(infile);
18685 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018686 if (res != NULL)
18687 vim_free(res);
18688 if (list != NULL)
18689 list_free(list, TRUE);
18690}
18691
18692/*
18693 * "system()" function
18694 */
18695 static void
18696f_system(argvars, rettv)
18697 typval_T *argvars;
18698 typval_T *rettv;
18699{
18700 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18701}
18702
18703/*
18704 * "systemlist()" function
18705 */
18706 static void
18707f_systemlist(argvars, rettv)
18708 typval_T *argvars;
18709 typval_T *rettv;
18710{
18711 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712}
18713
18714/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018715 * "tabpagebuflist()" function
18716 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018717 static void
18718f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018719 typval_T *argvars UNUSED;
18720 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018721{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018722#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018723 tabpage_T *tp;
18724 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018725
18726 if (argvars[0].v_type == VAR_UNKNOWN)
18727 wp = firstwin;
18728 else
18729 {
18730 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18731 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018732 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018733 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018734 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018735 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018736 for (; wp != NULL; wp = wp->w_next)
18737 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018738 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018739 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018740 }
18741#endif
18742}
18743
18744
18745/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018746 * "tabpagenr()" function
18747 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018748 static void
18749f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018750 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018751 typval_T *rettv;
18752{
18753 int nr = 1;
18754#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018755 char_u *arg;
18756
18757 if (argvars[0].v_type != VAR_UNKNOWN)
18758 {
18759 arg = get_tv_string_chk(&argvars[0]);
18760 nr = 0;
18761 if (arg != NULL)
18762 {
18763 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018764 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018765 else
18766 EMSG2(_(e_invexpr2), arg);
18767 }
18768 }
18769 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018770 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018771#endif
18772 rettv->vval.v_number = nr;
18773}
18774
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018775
18776#ifdef FEAT_WINDOWS
18777static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18778
18779/*
18780 * Common code for tabpagewinnr() and winnr().
18781 */
18782 static int
18783get_winnr(tp, argvar)
18784 tabpage_T *tp;
18785 typval_T *argvar;
18786{
18787 win_T *twin;
18788 int nr = 1;
18789 win_T *wp;
18790 char_u *arg;
18791
18792 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18793 if (argvar->v_type != VAR_UNKNOWN)
18794 {
18795 arg = get_tv_string_chk(argvar);
18796 if (arg == NULL)
18797 nr = 0; /* type error; errmsg already given */
18798 else if (STRCMP(arg, "$") == 0)
18799 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18800 else if (STRCMP(arg, "#") == 0)
18801 {
18802 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18803 if (twin == NULL)
18804 nr = 0;
18805 }
18806 else
18807 {
18808 EMSG2(_(e_invexpr2), arg);
18809 nr = 0;
18810 }
18811 }
18812
18813 if (nr > 0)
18814 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18815 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018816 {
18817 if (wp == NULL)
18818 {
18819 /* didn't find it in this tabpage */
18820 nr = 0;
18821 break;
18822 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018823 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018824 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018825 return nr;
18826}
18827#endif
18828
18829/*
18830 * "tabpagewinnr()" function
18831 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018832 static void
18833f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018834 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018835 typval_T *rettv;
18836{
18837 int nr = 1;
18838#ifdef FEAT_WINDOWS
18839 tabpage_T *tp;
18840
18841 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18842 if (tp == NULL)
18843 nr = 0;
18844 else
18845 nr = get_winnr(tp, &argvars[1]);
18846#endif
18847 rettv->vval.v_number = nr;
18848}
18849
18850
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018851/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018852 * "tagfiles()" function
18853 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018854 static void
18855f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018856 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018857 typval_T *rettv;
18858{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018859 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018860 tagname_T tn;
18861 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018862
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018863 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018864 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018865 fname = alloc(MAXPATHL);
18866 if (fname == NULL)
18867 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018868
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018869 for (first = TRUE; ; first = FALSE)
18870 if (get_tagfname(&tn, first, fname) == FAIL
18871 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018872 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018873 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018874 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018875}
18876
18877/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018878 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018879 */
18880 static void
18881f_taglist(argvars, rettv)
18882 typval_T *argvars;
18883 typval_T *rettv;
18884{
18885 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018886
18887 tag_pattern = get_tv_string(&argvars[0]);
18888
18889 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018890 if (*tag_pattern == NUL)
18891 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018892
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018893 if (rettv_list_alloc(rettv) == OK)
18894 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018895}
18896
18897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 * "tempname()" function
18899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904{
18905 static int x = 'A';
18906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907 rettv->v_type = VAR_STRING;
18908 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909
18910 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18911 * names. Skip 'I' and 'O', they are used for shell redirection. */
18912 do
18913 {
18914 if (x == 'Z')
18915 x = '0';
18916 else if (x == '9')
18917 x = 'A';
18918 else
18919 {
18920#ifdef EBCDIC
18921 if (x == 'I')
18922 x = 'J';
18923 else if (x == 'R')
18924 x = 'S';
18925 else
18926#endif
18927 ++x;
18928 }
18929 } while (x == 'I' || x == 'O');
18930}
18931
18932/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018933 * "test(list)" function: Just checking the walls...
18934 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018935 static void
18936f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018937 typval_T *argvars UNUSED;
18938 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018939{
18940 /* Used for unit testing. Change the code below to your liking. */
18941#if 0
18942 listitem_T *li;
18943 list_T *l;
18944 char_u *bad, *good;
18945
18946 if (argvars[0].v_type != VAR_LIST)
18947 return;
18948 l = argvars[0].vval.v_list;
18949 if (l == NULL)
18950 return;
18951 li = l->lv_first;
18952 if (li == NULL)
18953 return;
18954 bad = get_tv_string(&li->li_tv);
18955 li = li->li_next;
18956 if (li == NULL)
18957 return;
18958 good = get_tv_string(&li->li_tv);
18959 rettv->vval.v_number = test_edit_score(bad, good);
18960#endif
18961}
18962
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018963#ifdef FEAT_FLOAT
18964/*
18965 * "tan()" function
18966 */
18967 static void
18968f_tan(argvars, rettv)
18969 typval_T *argvars;
18970 typval_T *rettv;
18971{
18972 float_T f;
18973
18974 rettv->v_type = VAR_FLOAT;
18975 if (get_float_arg(argvars, &f) == OK)
18976 rettv->vval.v_float = tan(f);
18977 else
18978 rettv->vval.v_float = 0.0;
18979}
18980
18981/*
18982 * "tanh()" function
18983 */
18984 static void
18985f_tanh(argvars, rettv)
18986 typval_T *argvars;
18987 typval_T *rettv;
18988{
18989 float_T f;
18990
18991 rettv->v_type = VAR_FLOAT;
18992 if (get_float_arg(argvars, &f) == OK)
18993 rettv->vval.v_float = tanh(f);
18994 else
18995 rettv->vval.v_float = 0.0;
18996}
18997#endif
18998
Bram Moolenaard52d9742005-08-21 22:20:28 +000018999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 * "tolower(string)" function
19001 */
19002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 typval_T *argvars;
19005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006{
19007 char_u *p;
19008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019009 p = vim_strsave(get_tv_string(&argvars[0]));
19010 rettv->v_type = VAR_STRING;
19011 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012
19013 if (p != NULL)
19014 while (*p != NUL)
19015 {
19016#ifdef FEAT_MBYTE
19017 int l;
19018
19019 if (enc_utf8)
19020 {
19021 int c, lc;
19022
19023 c = utf_ptr2char(p);
19024 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019025 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 /* TODO: reallocate string when byte count changes. */
19027 if (utf_char2len(lc) == l)
19028 utf_char2bytes(lc, p);
19029 p += l;
19030 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019031 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 p += l; /* skip multi-byte character */
19033 else
19034#endif
19035 {
19036 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19037 ++p;
19038 }
19039 }
19040}
19041
19042/*
19043 * "toupper(string)" function
19044 */
19045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019047 typval_T *argvars;
19048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019051 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052}
19053
19054/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019055 * "tr(string, fromstr, tostr)" function
19056 */
19057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019058f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 typval_T *argvars;
19060 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019061{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019062 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019063 char_u *fromstr;
19064 char_u *tostr;
19065 char_u *p;
19066#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019067 int inlen;
19068 int fromlen;
19069 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019070 int idx;
19071 char_u *cpstr;
19072 int cplen;
19073 int first = TRUE;
19074#endif
19075 char_u buf[NUMBUFLEN];
19076 char_u buf2[NUMBUFLEN];
19077 garray_T ga;
19078
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019079 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019080 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19081 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019082
19083 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084 rettv->v_type = VAR_STRING;
19085 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019086 if (fromstr == NULL || tostr == NULL)
19087 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019088 ga_init2(&ga, (int)sizeof(char), 80);
19089
19090#ifdef FEAT_MBYTE
19091 if (!has_mbyte)
19092#endif
19093 /* not multi-byte: fromstr and tostr must be the same length */
19094 if (STRLEN(fromstr) != STRLEN(tostr))
19095 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019096#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019097error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019098#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019099 EMSG2(_(e_invarg2), fromstr);
19100 ga_clear(&ga);
19101 return;
19102 }
19103
19104 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019105 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019106 {
19107#ifdef FEAT_MBYTE
19108 if (has_mbyte)
19109 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019110 inlen = (*mb_ptr2len)(in_str);
19111 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019112 cplen = inlen;
19113 idx = 0;
19114 for (p = fromstr; *p != NUL; p += fromlen)
19115 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019116 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019117 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019118 {
19119 for (p = tostr; *p != NUL; p += tolen)
19120 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019121 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019122 if (idx-- == 0)
19123 {
19124 cplen = tolen;
19125 cpstr = p;
19126 break;
19127 }
19128 }
19129 if (*p == NUL) /* tostr is shorter than fromstr */
19130 goto error;
19131 break;
19132 }
19133 ++idx;
19134 }
19135
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019136 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019137 {
19138 /* Check that fromstr and tostr have the same number of
19139 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019140 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019141 first = FALSE;
19142 for (p = tostr; *p != NUL; p += tolen)
19143 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019144 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019145 --idx;
19146 }
19147 if (idx != 0)
19148 goto error;
19149 }
19150
19151 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019152 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019153 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019154
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019155 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019156 }
19157 else
19158#endif
19159 {
19160 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019161 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019162 if (p != NULL)
19163 ga_append(&ga, tostr[p - fromstr]);
19164 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019165 ga_append(&ga, *in_str);
19166 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019167 }
19168 }
19169
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019170 /* add a terminating NUL */
19171 ga_grow(&ga, 1);
19172 ga_append(&ga, NUL);
19173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019175}
19176
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019177#ifdef FEAT_FLOAT
19178/*
19179 * "trunc({float})" function
19180 */
19181 static void
19182f_trunc(argvars, rettv)
19183 typval_T *argvars;
19184 typval_T *rettv;
19185{
19186 float_T f;
19187
19188 rettv->v_type = VAR_FLOAT;
19189 if (get_float_arg(argvars, &f) == OK)
19190 /* trunc() is not in C90, use floor() or ceil() instead. */
19191 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19192 else
19193 rettv->vval.v_float = 0.0;
19194}
19195#endif
19196
Bram Moolenaar8299df92004-07-10 09:47:34 +000019197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 * "type(expr)" function
19199 */
19200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019202 typval_T *argvars;
19203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019205 int n;
19206
19207 switch (argvars[0].v_type)
19208 {
19209 case VAR_NUMBER: n = 0; break;
19210 case VAR_STRING: n = 1; break;
19211 case VAR_FUNC: n = 2; break;
19212 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019213 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019214#ifdef FEAT_FLOAT
19215 case VAR_FLOAT: n = 5; break;
19216#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019217 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19218 }
19219 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220}
19221
19222/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019223 * "undofile(name)" function
19224 */
19225 static void
19226f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019227 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019228 typval_T *rettv;
19229{
19230 rettv->v_type = VAR_STRING;
19231#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019232 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019233 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019234
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019235 if (*fname == NUL)
19236 {
19237 /* If there is no file name there will be no undo file. */
19238 rettv->vval.v_string = NULL;
19239 }
19240 else
19241 {
19242 char_u *ffname = FullName_save(fname, FALSE);
19243
19244 if (ffname != NULL)
19245 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19246 vim_free(ffname);
19247 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019248 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019249#else
19250 rettv->vval.v_string = NULL;
19251#endif
19252}
19253
19254/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019255 * "undotree()" function
19256 */
19257 static void
19258f_undotree(argvars, rettv)
19259 typval_T *argvars UNUSED;
19260 typval_T *rettv;
19261{
19262 if (rettv_dict_alloc(rettv) == OK)
19263 {
19264 dict_T *dict = rettv->vval.v_dict;
19265 list_T *list;
19266
Bram Moolenaar730cde92010-06-27 05:18:54 +020019267 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019268 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019269 dict_add_nr_str(dict, "save_last",
19270 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019271 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19272 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019273 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019274
19275 list = list_alloc();
19276 if (list != NULL)
19277 {
19278 u_eval_tree(curbuf->b_u_oldhead, list);
19279 dict_add_list(dict, "entries", list);
19280 }
19281 }
19282}
19283
19284/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019285 * "values(dict)" function
19286 */
19287 static void
19288f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 typval_T *argvars;
19290 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019291{
19292 dict_list(argvars, rettv, 1);
19293}
19294
19295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 * "virtcol(string)" function
19297 */
19298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019299f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019300 typval_T *argvars;
19301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302{
19303 colnr_T vcol = 0;
19304 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019305 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019307 fp = var2fpos(&argvars[0], FALSE, &fnum);
19308 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19309 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 {
19311 getvvcol(curwin, fp, NULL, NULL, &vcol);
19312 ++vcol;
19313 }
19314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019315 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316}
19317
19318/*
19319 * "visualmode()" function
19320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019323 typval_T *argvars UNUSED;
19324 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 char_u str[2];
19327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 str[0] = curbuf->b_visual_mode_eval;
19330 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332
19333 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019334 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336}
19337
19338/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019339 * "wildmenumode()" function
19340 */
19341 static void
19342f_wildmenumode(argvars, rettv)
19343 typval_T *argvars UNUSED;
19344 typval_T *rettv UNUSED;
19345{
19346#ifdef FEAT_WILDMENU
19347 if (wild_menu_showing)
19348 rettv->vval.v_number = 1;
19349#endif
19350}
19351
19352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 * "winbufnr(nr)" function
19354 */
19355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019357 typval_T *argvars;
19358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359{
19360 win_T *wp;
19361
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019362 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367}
19368
19369/*
19370 * "wincol()" function
19371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019373f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019374 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
19377 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
19381/*
19382 * "winheight(nr)" function
19383 */
19384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 typval_T *argvars;
19387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388{
19389 win_T *wp;
19390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019391 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019395 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396}
19397
19398/*
19399 * "winline()" function
19400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019403 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405{
19406 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019407 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408}
19409
19410/*
19411 * "winnr()" function
19412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019415 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417{
19418 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019419
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019421 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424}
19425
19426/*
19427 * "winrestcmd()" function
19428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019431 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433{
19434#ifdef FEAT_WINDOWS
19435 win_T *wp;
19436 int winnr = 1;
19437 garray_T ga;
19438 char_u buf[50];
19439
19440 ga_init2(&ga, (int)sizeof(char), 70);
19441 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19442 {
19443 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19444 ga_concat(&ga, buf);
19445# ifdef FEAT_VERTSPLIT
19446 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19447 ga_concat(&ga, buf);
19448# endif
19449 ++winnr;
19450 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019451 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458}
19459
19460/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019461 * "winrestview()" function
19462 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019463 static void
19464f_winrestview(argvars, rettv)
19465 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019466 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019467{
19468 dict_T *dict;
19469
19470 if (argvars[0].v_type != VAR_DICT
19471 || (dict = argvars[0].vval.v_dict) == NULL)
19472 EMSG(_(e_invarg));
19473 else
19474 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019475 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19476 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19477 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19478 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019480 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19481 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019482#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019483 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19484 {
19485 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19486 curwin->w_set_curswant = FALSE;
19487 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019488
Bram Moolenaar82c25852014-05-28 16:47:16 +020019489 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19490 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019491#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019492 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19493 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019494#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019495 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19496 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19497 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19498 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019499
19500 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019501 win_new_height(curwin, curwin->w_height);
19502# ifdef FEAT_VERTSPLIT
19503 win_new_width(curwin, W_WIDTH(curwin));
19504# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019505 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019506
19507 if (curwin->w_topline == 0)
19508 curwin->w_topline = 1;
19509 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19510 curwin->w_topline = curbuf->b_ml.ml_line_count;
19511#ifdef FEAT_DIFF
19512 check_topfill(curwin, TRUE);
19513#endif
19514 }
19515}
19516
19517/*
19518 * "winsaveview()" function
19519 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019520 static void
19521f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019522 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019523 typval_T *rettv;
19524{
19525 dict_T *dict;
19526
Bram Moolenaara800b422010-06-27 01:15:55 +020019527 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019528 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019529 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019530
19531 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19532 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19533#ifdef FEAT_VIRTUALEDIT
19534 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19535#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019536 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019537 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19538
19539 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19540#ifdef FEAT_DIFF
19541 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19542#endif
19543 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19544 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19545}
19546
19547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 * "winwidth(nr)" function
19549 */
19550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019551f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 typval_T *argvars;
19553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554{
19555 win_T *wp;
19556
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019557 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 else
19561#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019564 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565#endif
19566}
19567
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019569 * Write list of strings to file
19570 */
19571 static int
19572write_list(fd, list, binary)
19573 FILE *fd;
19574 list_T *list;
19575 int binary;
19576{
19577 listitem_T *li;
19578 int c;
19579 int ret = OK;
19580 char_u *s;
19581
19582 for (li = list->lv_first; li != NULL; li = li->li_next)
19583 {
19584 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19585 {
19586 if (*s == '\n')
19587 c = putc(NUL, fd);
19588 else
19589 c = putc(*s, fd);
19590 if (c == EOF)
19591 {
19592 ret = FAIL;
19593 break;
19594 }
19595 }
19596 if (!binary || li->li_next != NULL)
19597 if (putc('\n', fd) == EOF)
19598 {
19599 ret = FAIL;
19600 break;
19601 }
19602 if (ret == FAIL)
19603 {
19604 EMSG(_(e_write));
19605 break;
19606 }
19607 }
19608 return ret;
19609}
19610
19611/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019612 * "writefile()" function
19613 */
19614 static void
19615f_writefile(argvars, rettv)
19616 typval_T *argvars;
19617 typval_T *rettv;
19618{
19619 int binary = FALSE;
19620 char_u *fname;
19621 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019622 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019623
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019624 if (check_restricted() || check_secure())
19625 return;
19626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019627 if (argvars[0].v_type != VAR_LIST)
19628 {
19629 EMSG2(_(e_listarg), "writefile()");
19630 return;
19631 }
19632 if (argvars[0].vval.v_list == NULL)
19633 return;
19634
19635 if (argvars[2].v_type != VAR_UNKNOWN
19636 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19637 binary = TRUE;
19638
19639 /* Always open the file in binary mode, library functions have a mind of
19640 * their own about CR-LF conversion. */
19641 fname = get_tv_string(&argvars[1]);
19642 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19643 {
19644 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19645 ret = -1;
19646 }
19647 else
19648 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019649 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19650 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019651 fclose(fd);
19652 }
19653
19654 rettv->vval.v_number = ret;
19655}
19656
19657/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019658 * "xor(expr, expr)" function
19659 */
19660 static void
19661f_xor(argvars, rettv)
19662 typval_T *argvars;
19663 typval_T *rettv;
19664{
19665 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19666 ^ get_tv_number_chk(&argvars[1], NULL);
19667}
19668
19669
19670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019672 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 */
19674 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019675var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019676 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019677 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019678 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019680 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019682 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683
Bram Moolenaara5525202006-03-02 22:52:09 +000019684 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019685 if (varp->v_type == VAR_LIST)
19686 {
19687 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019688 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019689 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019690 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019691
19692 l = varp->vval.v_list;
19693 if (l == NULL)
19694 return NULL;
19695
19696 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019697 pos.lnum = list_find_nr(l, 0L, &error);
19698 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019699 return NULL; /* invalid line number */
19700
19701 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019702 pos.col = list_find_nr(l, 1L, &error);
19703 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019704 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019705 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019706
19707 /* We accept "$" for the column number: last column. */
19708 li = list_find(l, 1L);
19709 if (li != NULL && li->li_tv.v_type == VAR_STRING
19710 && li->li_tv.vval.v_string != NULL
19711 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19712 pos.col = len + 1;
19713
Bram Moolenaara5525202006-03-02 22:52:09 +000019714 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019715 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019716 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019717 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019718
Bram Moolenaara5525202006-03-02 22:52:09 +000019719#ifdef FEAT_VIRTUALEDIT
19720 /* Get the virtual offset. Defaults to zero. */
19721 pos.coladd = list_find_nr(l, 2L, &error);
19722 if (error)
19723 pos.coladd = 0;
19724#endif
19725
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019726 return &pos;
19727 }
19728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019729 name = get_tv_string_chk(varp);
19730 if (name == NULL)
19731 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019732 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019734 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19735 {
19736 if (VIsual_active)
19737 return &VIsual;
19738 return &curwin->w_cursor;
19739 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019740 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019742 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19744 return NULL;
19745 return pp;
19746 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019747
19748#ifdef FEAT_VIRTUALEDIT
19749 pos.coladd = 0;
19750#endif
19751
Bram Moolenaar477933c2007-07-17 14:32:23 +000019752 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019753 {
19754 pos.col = 0;
19755 if (name[1] == '0') /* "w0": first visible line */
19756 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019757 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019758 pos.lnum = curwin->w_topline;
19759 return &pos;
19760 }
19761 else if (name[1] == '$') /* "w$": last visible line */
19762 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019763 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019764 pos.lnum = curwin->w_botline - 1;
19765 return &pos;
19766 }
19767 }
19768 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019770 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 {
19772 pos.lnum = curbuf->b_ml.ml_line_count;
19773 pos.col = 0;
19774 }
19775 else
19776 {
19777 pos.lnum = curwin->w_cursor.lnum;
19778 pos.col = (colnr_T)STRLEN(ml_get_curline());
19779 }
19780 return &pos;
19781 }
19782 return NULL;
19783}
19784
19785/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019786 * Convert list in "arg" into a position and optional file number.
19787 * When "fnump" is NULL there is no file number, only 3 items.
19788 * Note that the column is passed on as-is, the caller may want to decrement
19789 * it to use 1 for the first column.
19790 * Return FAIL when conversion is not possible, doesn't check the position for
19791 * validity.
19792 */
19793 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019794list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019795 typval_T *arg;
19796 pos_T *posp;
19797 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019798 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019799{
19800 list_T *l = arg->vval.v_list;
19801 long i = 0;
19802 long n;
19803
Bram Moolenaar493c1782014-05-28 14:34:46 +020019804 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19805 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019806 if (arg->v_type != VAR_LIST
19807 || l == NULL
19808 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019809 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019810 return FAIL;
19811
19812 if (fnump != NULL)
19813 {
19814 n = list_find_nr(l, i++, NULL); /* fnum */
19815 if (n < 0)
19816 return FAIL;
19817 if (n == 0)
19818 n = curbuf->b_fnum; /* current buffer */
19819 *fnump = n;
19820 }
19821
19822 n = list_find_nr(l, i++, NULL); /* lnum */
19823 if (n < 0)
19824 return FAIL;
19825 posp->lnum = n;
19826
19827 n = list_find_nr(l, i++, NULL); /* col */
19828 if (n < 0)
19829 return FAIL;
19830 posp->col = n;
19831
19832#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019833 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019834 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019835 posp->coladd = 0;
19836 else
19837 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019838#endif
19839
Bram Moolenaar493c1782014-05-28 14:34:46 +020019840 if (curswantp != NULL)
19841 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19842
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019843 return OK;
19844}
19845
19846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 * Get the length of an environment variable name.
19848 * Advance "arg" to the first character after the name.
19849 * Return 0 for error.
19850 */
19851 static int
19852get_env_len(arg)
19853 char_u **arg;
19854{
19855 char_u *p;
19856 int len;
19857
19858 for (p = *arg; vim_isIDc(*p); ++p)
19859 ;
19860 if (p == *arg) /* no name found */
19861 return 0;
19862
19863 len = (int)(p - *arg);
19864 *arg = p;
19865 return len;
19866}
19867
19868/*
19869 * Get the length of the name of a function or internal variable.
19870 * "arg" is advanced to the first non-white character after the name.
19871 * Return 0 if something is wrong.
19872 */
19873 static int
19874get_id_len(arg)
19875 char_u **arg;
19876{
19877 char_u *p;
19878 int len;
19879
19880 /* Find the end of the name. */
19881 for (p = *arg; eval_isnamec(*p); ++p)
19882 ;
19883 if (p == *arg) /* no name found */
19884 return 0;
19885
19886 len = (int)(p - *arg);
19887 *arg = skipwhite(p);
19888
19889 return len;
19890}
19891
19892/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019893 * Get the length of the name of a variable or function.
19894 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019896 * Return -1 if curly braces expansion failed.
19897 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 * If the name contains 'magic' {}'s, expand them and return the
19899 * expanded name in an allocated string via 'alias' - caller must free.
19900 */
19901 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019902get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 char_u **arg;
19904 char_u **alias;
19905 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019906 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907{
19908 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 char_u *p;
19910 char_u *expr_start;
19911 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912
19913 *alias = NULL; /* default to no alias */
19914
19915 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19916 && (*arg)[2] == (int)KE_SNR)
19917 {
19918 /* hard coded <SNR>, already translated */
19919 *arg += 3;
19920 return get_id_len(arg) + 3;
19921 }
19922 len = eval_fname_script(*arg);
19923 if (len > 0)
19924 {
19925 /* literal "<SID>", "s:" or "<SNR>" */
19926 *arg += len;
19927 }
19928
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019932 p = find_name_end(*arg, &expr_start, &expr_end,
19933 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 if (expr_start != NULL)
19935 {
19936 char_u *temp_string;
19937
19938 if (!evaluate)
19939 {
19940 len += (int)(p - *arg);
19941 *arg = skipwhite(p);
19942 return len;
19943 }
19944
19945 /*
19946 * Include any <SID> etc in the expanded string:
19947 * Thus the -len here.
19948 */
19949 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19950 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019951 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 *alias = temp_string;
19953 *arg = skipwhite(p);
19954 return (int)STRLEN(temp_string);
19955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956
19957 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019958 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 EMSG2(_(e_invexpr2), *arg);
19960
19961 return len;
19962}
19963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964/*
19965 * Find the end of a variable or function name, taking care of magic braces.
19966 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19967 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019968 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 * Return a pointer to just after the name. Equal to "arg" if there is no
19970 * valid name.
19971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019973find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 char_u *arg;
19975 char_u **expr_start;
19976 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019977 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019979 int mb_nest = 0;
19980 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 char_u *p;
19982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 *expr_start = NULL;
19986 *expr_end = NULL;
19987 }
19988
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019989 /* Quick check for valid starting character. */
19990 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19991 return arg;
19992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993 for (p = arg; *p != NUL
19994 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019995 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019996 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019998 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020000 if (*p == '\'')
20001 {
20002 /* skip over 'string' to avoid counting [ and ] inside it. */
20003 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20004 ;
20005 if (*p == NUL)
20006 break;
20007 }
20008 else if (*p == '"')
20009 {
20010 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20011 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20012 if (*p == '\\' && p[1] != NUL)
20013 ++p;
20014 if (*p == NUL)
20015 break;
20016 }
20017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 if (*p == '[')
20021 ++br_nest;
20022 else if (*p == ']')
20023 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020028 if (*p == '{')
20029 {
20030 mb_nest++;
20031 if (expr_start != NULL && *expr_start == NULL)
20032 *expr_start = p;
20033 }
20034 else if (*p == '}')
20035 {
20036 mb_nest--;
20037 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20038 *expr_end = p;
20039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 }
20042
20043 return p;
20044}
20045
20046/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020047 * Expands out the 'magic' {}'s in a variable/function name.
20048 * Note that this can call itself recursively, to deal with
20049 * constructs like foo{bar}{baz}{bam}
20050 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20051 * "in_start" ^
20052 * "expr_start" ^
20053 * "expr_end" ^
20054 * "in_end" ^
20055 *
20056 * Returns a new allocated string, which the caller must free.
20057 * Returns NULL for failure.
20058 */
20059 static char_u *
20060make_expanded_name(in_start, expr_start, expr_end, in_end)
20061 char_u *in_start;
20062 char_u *expr_start;
20063 char_u *expr_end;
20064 char_u *in_end;
20065{
20066 char_u c1;
20067 char_u *retval = NULL;
20068 char_u *temp_result;
20069 char_u *nextcmd = NULL;
20070
20071 if (expr_end == NULL || in_end == NULL)
20072 return NULL;
20073 *expr_start = NUL;
20074 *expr_end = NUL;
20075 c1 = *in_end;
20076 *in_end = NUL;
20077
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020078 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020079 if (temp_result != NULL && nextcmd == NULL)
20080 {
20081 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20082 + (in_end - expr_end) + 1));
20083 if (retval != NULL)
20084 {
20085 STRCPY(retval, in_start);
20086 STRCAT(retval, temp_result);
20087 STRCAT(retval, expr_end + 1);
20088 }
20089 }
20090 vim_free(temp_result);
20091
20092 *in_end = c1; /* put char back for error messages */
20093 *expr_start = '{';
20094 *expr_end = '}';
20095
20096 if (retval != NULL)
20097 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020098 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020099 if (expr_start != NULL)
20100 {
20101 /* Further expansion! */
20102 temp_result = make_expanded_name(retval, expr_start,
20103 expr_end, temp_result);
20104 vim_free(retval);
20105 retval = temp_result;
20106 }
20107 }
20108
20109 return retval;
20110}
20111
20112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020114 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 */
20116 static int
20117eval_isnamec(c)
20118 int c;
20119{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020120 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20121}
20122
20123/*
20124 * Return TRUE if character "c" can be used as the first character in a
20125 * variable or function name (excluding '{' and '}').
20126 */
20127 static int
20128eval_isnamec1(c)
20129 int c;
20130{
20131 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132}
20133
20134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 * Set number v: variable to "val".
20136 */
20137 void
20138set_vim_var_nr(idx, val)
20139 int idx;
20140 long val;
20141{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020142 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143}
20144
20145/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020146 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 */
20148 long
20149get_vim_var_nr(idx)
20150 int idx;
20151{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020152 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153}
20154
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020155/*
20156 * Get string v: variable value. Uses a static buffer, can only be used once.
20157 */
20158 char_u *
20159get_vim_var_str(idx)
20160 int idx;
20161{
20162 return get_tv_string(&vimvars[idx].vv_tv);
20163}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020164
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020166 * Get List v: variable value. Caller must take care of reference count when
20167 * needed.
20168 */
20169 list_T *
20170get_vim_var_list(idx)
20171 int idx;
20172{
20173 return vimvars[idx].vv_list;
20174}
20175
20176/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020177 * Set v:char to character "c".
20178 */
20179 void
20180set_vim_var_char(c)
20181 int c;
20182{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020183 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020184
20185#ifdef FEAT_MBYTE
20186 if (has_mbyte)
20187 buf[(*mb_char2bytes)(c, buf)] = NUL;
20188 else
20189#endif
20190 {
20191 buf[0] = c;
20192 buf[1] = NUL;
20193 }
20194 set_vim_var_string(VV_CHAR, buf, -1);
20195}
20196
20197/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020198 * Set v:count to "count" and v:count1 to "count1".
20199 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 */
20201 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020202set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 long count;
20204 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020205 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020207 if (set_prevcount)
20208 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020209 vimvars[VV_COUNT].vv_nr = count;
20210 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211}
20212
20213/*
20214 * Set string v: variable to a copy of "val".
20215 */
20216 void
20217set_vim_var_string(idx, val, len)
20218 int idx;
20219 char_u *val;
20220 int len; /* length of "val" to use or -1 (whole string) */
20221{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020222 /* Need to do this (at least) once, since we can't initialize a union.
20223 * Will always be invoked when "v:progname" is set. */
20224 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20225
Bram Moolenaare9a41262005-01-15 22:18:47 +000020226 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020228 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020230 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020232 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233}
20234
20235/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020236 * Set List v: variable to "val".
20237 */
20238 void
20239set_vim_var_list(idx, val)
20240 int idx;
20241 list_T *val;
20242{
20243 list_unref(vimvars[idx].vv_list);
20244 vimvars[idx].vv_list = val;
20245 if (val != NULL)
20246 ++val->lv_refcount;
20247}
20248
20249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 * Set v:register if needed.
20251 */
20252 void
20253set_reg_var(c)
20254 int c;
20255{
20256 char_u regname;
20257
20258 if (c == 0 || c == ' ')
20259 regname = '"';
20260 else
20261 regname = c;
20262 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020263 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 set_vim_var_string(VV_REG, &regname, 1);
20265}
20266
20267/*
20268 * Get or set v:exception. If "oldval" == NULL, return the current value.
20269 * Otherwise, restore the value to "oldval" and return NULL.
20270 * Must always be called in pairs to save and restore v:exception! Does not
20271 * take care of memory allocations.
20272 */
20273 char_u *
20274v_exception(oldval)
20275 char_u *oldval;
20276{
20277 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020278 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279
Bram Moolenaare9a41262005-01-15 22:18:47 +000020280 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 return NULL;
20282}
20283
20284/*
20285 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20286 * Otherwise, restore the value to "oldval" and return NULL.
20287 * Must always be called in pairs to save and restore v:throwpoint! Does not
20288 * take care of memory allocations.
20289 */
20290 char_u *
20291v_throwpoint(oldval)
20292 char_u *oldval;
20293{
20294 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020295 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296
Bram Moolenaare9a41262005-01-15 22:18:47 +000020297 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 return NULL;
20299}
20300
20301#if defined(FEAT_AUTOCMD) || defined(PROTO)
20302/*
20303 * Set v:cmdarg.
20304 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20305 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20306 * Must always be called in pairs!
20307 */
20308 char_u *
20309set_cmdarg(eap, oldarg)
20310 exarg_T *eap;
20311 char_u *oldarg;
20312{
20313 char_u *oldval;
20314 char_u *newval;
20315 unsigned len;
20316
Bram Moolenaare9a41262005-01-15 22:18:47 +000020317 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020318 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020320 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020321 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020322 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 }
20324
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020325 if (eap->force_bin == FORCE_BIN)
20326 len = 6;
20327 else if (eap->force_bin == FORCE_NOBIN)
20328 len = 8;
20329 else
20330 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020331
20332 if (eap->read_edit)
20333 len += 7;
20334
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020335 if (eap->force_ff != 0)
20336 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20337# ifdef FEAT_MBYTE
20338 if (eap->force_enc != 0)
20339 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020340 if (eap->bad_char != 0)
20341 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020342# endif
20343
20344 newval = alloc(len + 1);
20345 if (newval == NULL)
20346 return NULL;
20347
20348 if (eap->force_bin == FORCE_BIN)
20349 sprintf((char *)newval, " ++bin");
20350 else if (eap->force_bin == FORCE_NOBIN)
20351 sprintf((char *)newval, " ++nobin");
20352 else
20353 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020354
20355 if (eap->read_edit)
20356 STRCAT(newval, " ++edit");
20357
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020358 if (eap->force_ff != 0)
20359 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20360 eap->cmd + eap->force_ff);
20361# ifdef FEAT_MBYTE
20362 if (eap->force_enc != 0)
20363 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20364 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020365 if (eap->bad_char == BAD_KEEP)
20366 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20367 else if (eap->bad_char == BAD_DROP)
20368 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20369 else if (eap->bad_char != 0)
20370 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020371# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020372 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020373 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374}
20375#endif
20376
20377/*
20378 * Get the value of internal variable "name".
20379 * Return OK or FAIL.
20380 */
20381 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020382get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 char_u *name;
20384 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020386 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020387 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388{
20389 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 typval_T *tv = NULL;
20391 typval_T atv;
20392 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394
20395 /* truncate the name, so that we can use strcmp() */
20396 cc = name[len];
20397 name[len] = NUL;
20398
20399 /*
20400 * Check for "b:changedtick".
20401 */
20402 if (STRCMP(name, "b:changedtick") == 0)
20403 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020404 atv.v_type = VAR_NUMBER;
20405 atv.vval.v_number = curbuf->b_changedtick;
20406 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 }
20408
20409 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 * Check for user-defined variables.
20411 */
20412 else
20413 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020414 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020416 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 }
20418
Bram Moolenaare9a41262005-01-15 22:18:47 +000020419 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020421 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 ret = FAIL;
20424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020426 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427
20428 name[len] = cc;
20429
20430 return ret;
20431}
20432
20433/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020434 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20435 * Also handle function call with Funcref variable: func(expr)
20436 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20437 */
20438 static int
20439handle_subscript(arg, rettv, evaluate, verbose)
20440 char_u **arg;
20441 typval_T *rettv;
20442 int evaluate; /* do more than finding the end */
20443 int verbose; /* give error messages */
20444{
20445 int ret = OK;
20446 dict_T *selfdict = NULL;
20447 char_u *s;
20448 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020449 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020450
20451 while (ret == OK
20452 && (**arg == '['
20453 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020454 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020455 && !vim_iswhite(*(*arg - 1)))
20456 {
20457 if (**arg == '(')
20458 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020459 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020460 if (evaluate)
20461 {
20462 functv = *rettv;
20463 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020464
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020465 /* Invoke the function. Recursive! */
20466 s = functv.vval.v_string;
20467 }
20468 else
20469 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020470 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020471 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20472 &len, evaluate, selfdict);
20473
20474 /* Clear the funcref afterwards, so that deleting it while
20475 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020476 if (evaluate)
20477 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020478
20479 /* Stop the expression evaluation when immediately aborting on
20480 * error, or when an interrupt occurred or an exception was thrown
20481 * but not caught. */
20482 if (aborting())
20483 {
20484 if (ret == OK)
20485 clear_tv(rettv);
20486 ret = FAIL;
20487 }
20488 dict_unref(selfdict);
20489 selfdict = NULL;
20490 }
20491 else /* **arg == '[' || **arg == '.' */
20492 {
20493 dict_unref(selfdict);
20494 if (rettv->v_type == VAR_DICT)
20495 {
20496 selfdict = rettv->vval.v_dict;
20497 if (selfdict != NULL)
20498 ++selfdict->dv_refcount;
20499 }
20500 else
20501 selfdict = NULL;
20502 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20503 {
20504 clear_tv(rettv);
20505 ret = FAIL;
20506 }
20507 }
20508 }
20509 dict_unref(selfdict);
20510 return ret;
20511}
20512
20513/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020514 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020515 * value).
20516 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020517 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020518alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020519{
Bram Moolenaar33570922005-01-25 22:26:29 +000020520 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020521}
20522
20523/*
20524 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 * The string "s" must have been allocated, it is consumed.
20526 * Return NULL for out of memory, the variable otherwise.
20527 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020528 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020529alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 char_u *s;
20531{
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020534 rettv = alloc_tv();
20535 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020537 rettv->v_type = VAR_STRING;
20538 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 }
20540 else
20541 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543}
20544
20545/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020546 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020548 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020549free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551{
20552 if (varp != NULL)
20553 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020554 switch (varp->v_type)
20555 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020556 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020557 func_unref(varp->vval.v_string);
20558 /*FALLTHROUGH*/
20559 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020560 vim_free(varp->vval.v_string);
20561 break;
20562 case VAR_LIST:
20563 list_unref(varp->vval.v_list);
20564 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020565 case VAR_DICT:
20566 dict_unref(varp->vval.v_dict);
20567 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020568 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020569#ifdef FEAT_FLOAT
20570 case VAR_FLOAT:
20571#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020572 case VAR_UNKNOWN:
20573 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020574 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020575 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020576 break;
20577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 vim_free(varp);
20579 }
20580}
20581
20582/*
20583 * Free the memory for a variable value and set the value to NULL or 0.
20584 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020585 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020586clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020587 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588{
20589 if (varp != NULL)
20590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020591 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020593 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020594 func_unref(varp->vval.v_string);
20595 /*FALLTHROUGH*/
20596 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 vim_free(varp->vval.v_string);
20598 varp->vval.v_string = NULL;
20599 break;
20600 case VAR_LIST:
20601 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020602 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020604 case VAR_DICT:
20605 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020606 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020607 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020608 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 varp->vval.v_number = 0;
20610 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020611#ifdef FEAT_FLOAT
20612 case VAR_FLOAT:
20613 varp->vval.v_float = 0.0;
20614 break;
20615#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020616 case VAR_UNKNOWN:
20617 break;
20618 default:
20619 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020621 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 }
20623}
20624
20625/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020626 * Set the value of a variable to NULL without freeing items.
20627 */
20628 static void
20629init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020630 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020631{
20632 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020633 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634}
20635
20636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 * Get the number value of a variable.
20638 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020639 * For incompatible types, return 0.
20640 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20641 * caller of incompatible types: it sets *denote to TRUE if "denote"
20642 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 */
20644 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020645get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020646 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020648 int error = FALSE;
20649
20650 return get_tv_number_chk(varp, &error); /* return 0L on error */
20651}
20652
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020653 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020654get_tv_number_chk(varp, denote)
20655 typval_T *varp;
20656 int *denote;
20657{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020658 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020660 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020662 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020663 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020664#ifdef FEAT_FLOAT
20665 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020666 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020667 break;
20668#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020669 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020670 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020671 break;
20672 case VAR_STRING:
20673 if (varp->vval.v_string != NULL)
20674 vim_str2nr(varp->vval.v_string, NULL, NULL,
20675 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020676 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020677 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020678 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020679 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020680 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020681 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020683 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020684 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020687 if (denote == NULL) /* useful for values that must be unsigned */
20688 n = -1;
20689 else
20690 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020691 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692}
20693
20694/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020695 * Get the lnum from the first argument.
20696 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020697 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698 */
20699 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020700get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020701 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702{
Bram Moolenaar33570922005-01-25 22:26:29 +000020703 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 linenr_T lnum;
20705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020706 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 if (lnum == 0) /* no valid number, try using line() */
20708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020709 rettv.v_type = VAR_NUMBER;
20710 f_line(argvars, &rettv);
20711 lnum = rettv.vval.v_number;
20712 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 }
20714 return lnum;
20715}
20716
20717/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020718 * Get the lnum from the first argument.
20719 * Also accepts "$", then "buf" is used.
20720 * Returns 0 on error.
20721 */
20722 static linenr_T
20723get_tv_lnum_buf(argvars, buf)
20724 typval_T *argvars;
20725 buf_T *buf;
20726{
20727 if (argvars[0].v_type == VAR_STRING
20728 && argvars[0].vval.v_string != NULL
20729 && argvars[0].vval.v_string[0] == '$'
20730 && buf != NULL)
20731 return buf->b_ml.ml_line_count;
20732 return get_tv_number_chk(&argvars[0], NULL);
20733}
20734
20735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 * Get the string value of a variable.
20737 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020738 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20739 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 * If the String variable has never been set, return an empty string.
20741 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020742 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20743 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 */
20745 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020746get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748{
20749 static char_u mybuf[NUMBUFLEN];
20750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020751 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752}
20753
20754 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020755get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020756 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020757 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020759 char_u *res = get_tv_string_buf_chk(varp, buf);
20760
20761 return res != NULL ? res : (char_u *)"";
20762}
20763
Bram Moolenaar7d647822014-04-05 21:28:56 +020020764/*
20765 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20766 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020767 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020768get_tv_string_chk(varp)
20769 typval_T *varp;
20770{
20771 static char_u mybuf[NUMBUFLEN];
20772
20773 return get_tv_string_buf_chk(varp, mybuf);
20774}
20775
20776 static char_u *
20777get_tv_string_buf_chk(varp, buf)
20778 typval_T *varp;
20779 char_u *buf;
20780{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020781 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020783 case VAR_NUMBER:
20784 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20785 return buf;
20786 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020787 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020788 break;
20789 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020790 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020791 break;
20792 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020793 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020794 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020795#ifdef FEAT_FLOAT
20796 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020797 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020798 break;
20799#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020800 case VAR_STRING:
20801 if (varp->vval.v_string != NULL)
20802 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020803 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020804 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020808 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809}
20810
20811/*
20812 * Find variable "name" in the list of variables.
20813 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020814 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020815 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020816 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020818 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020819find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020821 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020822 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826
Bram Moolenaara7043832005-01-21 11:56:39 +000020827 ht = find_var_ht(name, &varname);
20828 if (htp != NULL)
20829 *htp = ht;
20830 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020832 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833}
20834
20835/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020836 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020837 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020840find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020841 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020842 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020843 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020844 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020845{
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 hashitem_T *hi;
20847
20848 if (*varname == NUL)
20849 {
20850 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020851 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020852 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020853 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020854 case 'g': return &globvars_var;
20855 case 'v': return &vimvars_var;
20856 case 'b': return &curbuf->b_bufvar;
20857 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020858#ifdef FEAT_WINDOWS
20859 case 't': return &curtab->tp_winvar;
20860#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020861 case 'l': return current_funccal == NULL
20862 ? NULL : &current_funccal->l_vars_var;
20863 case 'a': return current_funccal == NULL
20864 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 }
20866 return NULL;
20867 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020868
20869 hi = hash_find(ht, varname);
20870 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020871 {
20872 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020873 * worked find the variable again. Don't auto-load a script if it was
20874 * loaded already, otherwise it would be loaded every time when
20875 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020876 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020877 {
20878 /* Note: script_autoload() may make "hi" invalid. It must either
20879 * be obtained again or not used. */
20880 if (!script_autoload(varname, FALSE) || aborting())
20881 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020882 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020883 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020884 if (HASHITEM_EMPTY(hi))
20885 return NULL;
20886 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020888}
20889
20890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020891 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020892 * Set "varname" to the start of name without ':'.
20893 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020894 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020895find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 char_u *name;
20897 char_u **varname;
20898{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020899 hashitem_T *hi;
20900
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 if (name[1] != ':')
20902 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020903 /* The name must not start with a colon or #. */
20904 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 return NULL;
20906 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020907
20908 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020909 hi = hash_find(&compat_hashtab, name);
20910 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020911 return &compat_hashtab;
20912
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020914 return &globvarht; /* global variable */
20915 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 }
20917 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020918 if (*name == 'g') /* global variable */
20919 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020920 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20921 */
20922 if (vim_strchr(name + 2, ':') != NULL
20923 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020924 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020926 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020928 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020929#ifdef FEAT_WINDOWS
20930 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020931 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020932#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020933 if (*name == 'v') /* v: variable */
20934 return &vimvarht;
20935 if (*name == 'a' && current_funccal != NULL) /* function argument */
20936 return &current_funccal->l_avars.dv_hashtab;
20937 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20938 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 if (*name == 's' /* script variable */
20940 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20941 return &SCRIPT_VARS(current_SID);
20942 return NULL;
20943}
20944
20945/*
20946 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020947 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 * Returns NULL when it doesn't exist.
20949 */
20950 char_u *
20951get_var_value(name)
20952 char_u *name;
20953{
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020956 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 if (v == NULL)
20958 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020959 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960}
20961
20962/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020963 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 * sourcing this script and when executing functions defined in the script.
20965 */
20966 void
20967new_script_vars(id)
20968 scid_T id;
20969{
Bram Moolenaara7043832005-01-21 11:56:39 +000020970 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020971 hashtab_T *ht;
20972 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020973
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20975 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020976 /* Re-allocating ga_data means that an ht_array pointing to
20977 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020979 for (i = 1; i <= ga_scripts.ga_len; ++i)
20980 {
20981 ht = &SCRIPT_VARS(i);
20982 if (ht->ht_mask == HT_INIT_SIZE - 1)
20983 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020984 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020985 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020986 }
20987
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 while (ga_scripts.ga_len < id)
20989 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020990 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020991 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020992 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 }
20995 }
20996}
20997
20998/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21000 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 */
21002 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021003init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021004 dict_T *dict;
21005 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021006 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007{
Bram Moolenaar33570922005-01-25 22:26:29 +000021008 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021009 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021010 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021011 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021012 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 dict_var->di_tv.vval.v_dict = dict;
21014 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021015 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21017 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018}
21019
21020/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021021 * Unreference a dictionary initialized by init_var_dict().
21022 */
21023 void
21024unref_var_dict(dict)
21025 dict_T *dict;
21026{
21027 /* Now the dict needs to be freed if no one else is using it, go back to
21028 * normal reference counting. */
21029 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21030 dict_unref(dict);
21031}
21032
21033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021035 * Frees all allocated variables and the value they contain.
21036 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 */
21038 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021039vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021040 hashtab_T *ht;
21041{
21042 vars_clear_ext(ht, TRUE);
21043}
21044
21045/*
21046 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21047 */
21048 static void
21049vars_clear_ext(ht, free_val)
21050 hashtab_T *ht;
21051 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052{
Bram Moolenaara7043832005-01-21 11:56:39 +000021053 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021054 hashitem_T *hi;
21055 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056
Bram Moolenaar33570922005-01-25 22:26:29 +000021057 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021058 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021059 for (hi = ht->ht_array; todo > 0; ++hi)
21060 {
21061 if (!HASHITEM_EMPTY(hi))
21062 {
21063 --todo;
21064
Bram Moolenaar33570922005-01-25 22:26:29 +000021065 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021066 * ht_array might change then. hash_clear() takes care of it
21067 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021068 v = HI2DI(hi);
21069 if (free_val)
21070 clear_tv(&v->di_tv);
21071 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21072 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021073 }
21074 }
21075 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021076 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077}
21078
Bram Moolenaara7043832005-01-21 11:56:39 +000021079/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021080 * Delete a variable from hashtab "ht" at item "hi".
21081 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021084delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 hashtab_T *ht;
21086 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087{
Bram Moolenaar33570922005-01-25 22:26:29 +000021088 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021089
21090 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 clear_tv(&di->di_tv);
21092 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093}
21094
21095/*
21096 * List the value of one internal variable.
21097 */
21098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021099list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021102 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021104 char_u *tofree;
21105 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021106 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021107
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021108 current_copyID += COPYID_INC;
21109 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021110 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021111 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021112 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113}
21114
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021116list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 char_u *prefix;
21118 char_u *name;
21119 int type;
21120 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021121 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122{
Bram Moolenaar31859182007-08-14 20:41:13 +000021123 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21124 msg_start();
21125 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 if (name != NULL) /* "a:" vars don't have a name stored */
21127 msg_puts(name);
21128 msg_putchar(' ');
21129 msg_advance(22);
21130 if (type == VAR_NUMBER)
21131 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021132 else if (type == VAR_FUNC)
21133 msg_putchar('*');
21134 else if (type == VAR_LIST)
21135 {
21136 msg_putchar('[');
21137 if (*string == '[')
21138 ++string;
21139 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021140 else if (type == VAR_DICT)
21141 {
21142 msg_putchar('{');
21143 if (*string == '{')
21144 ++string;
21145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 else
21147 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021150
21151 if (type == VAR_FUNC)
21152 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021153 if (*first)
21154 {
21155 msg_clr_eos();
21156 *first = FALSE;
21157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158}
21159
21160/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021161 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 * If the variable already exists, the value is updated.
21163 * Otherwise the variable is created.
21164 */
21165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170{
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021173 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021175 ht = find_var_ht(name, &varname);
21176 if (ht == NULL || *varname == NUL)
21177 {
21178 EMSG2(_(e_illvar), name);
21179 return;
21180 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021181 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021182
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021183 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21184 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021185
Bram Moolenaar33570922005-01-25 22:26:29 +000021186 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021189 if (var_check_ro(v->di_flags, name)
21190 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021191 return;
21192 if (v->di_tv.v_type != tv->v_type
21193 && !((v->di_tv.v_type == VAR_STRING
21194 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021196 || tv->v_type == VAR_NUMBER))
21197#ifdef FEAT_FLOAT
21198 && !((v->di_tv.v_type == VAR_NUMBER
21199 || v->di_tv.v_type == VAR_FLOAT)
21200 && (tv->v_type == VAR_NUMBER
21201 || tv->v_type == VAR_FLOAT))
21202#endif
21203 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021205 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021206 return;
21207 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021208
21209 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021210 * Handle setting internal v: variables separately: we don't change
21211 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021212 */
21213 if (ht == &vimvarht)
21214 {
21215 if (v->di_tv.v_type == VAR_STRING)
21216 {
21217 vim_free(v->di_tv.vval.v_string);
21218 if (copy || tv->v_type != VAR_STRING)
21219 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21220 else
21221 {
21222 /* Take over the string to avoid an extra alloc/free. */
21223 v->di_tv.vval.v_string = tv->vval.v_string;
21224 tv->vval.v_string = NULL;
21225 }
21226 }
21227 else if (v->di_tv.v_type != VAR_NUMBER)
21228 EMSG2(_(e_intern2), "set_var()");
21229 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021231 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021232 if (STRCMP(varname, "searchforward") == 0)
21233 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021234#ifdef FEAT_SEARCH_EXTRA
21235 else if (STRCMP(varname, "hlsearch") == 0)
21236 {
21237 no_hlsearch = !v->di_tv.vval.v_number;
21238 redraw_all_later(SOME_VALID);
21239 }
21240#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021241 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021242 return;
21243 }
21244
21245 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 }
21247 else /* add a new variable */
21248 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021249 /* Can't add "v:" variable. */
21250 if (ht == &vimvarht)
21251 {
21252 EMSG2(_(e_illvar), name);
21253 return;
21254 }
21255
Bram Moolenaar92124a32005-06-17 22:03:40 +000021256 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021257 if (!valid_varname(varname))
21258 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021259
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021260 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21261 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021262 if (v == NULL)
21263 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021265 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021267 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 return;
21269 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021270 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021272
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021273 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021274 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021275 else
21276 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021277 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021278 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021279 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281}
21282
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021283/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021284 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021285 * Also give an error message.
21286 */
21287 static int
21288var_check_ro(flags, name)
21289 int flags;
21290 char_u *name;
21291{
21292 if (flags & DI_FLAGS_RO)
21293 {
21294 EMSG2(_(e_readonlyvar), name);
21295 return TRUE;
21296 }
21297 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21298 {
21299 EMSG2(_(e_readonlysbx), name);
21300 return TRUE;
21301 }
21302 return FALSE;
21303}
21304
21305/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021306 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21307 * Also give an error message.
21308 */
21309 static int
21310var_check_fixed(flags, name)
21311 int flags;
21312 char_u *name;
21313{
21314 if (flags & DI_FLAGS_FIX)
21315 {
21316 EMSG2(_("E795: Cannot delete variable %s"), name);
21317 return TRUE;
21318 }
21319 return FALSE;
21320}
21321
21322/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021323 * Check if a funcref is assigned to a valid variable name.
21324 * Return TRUE and give an error if not.
21325 */
21326 static int
21327var_check_func_name(name, new_var)
21328 char_u *name; /* points to start of variable name */
21329 int new_var; /* TRUE when creating the variable */
21330{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021331 /* Allow for w: b: s: and t:. */
21332 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021333 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21334 ? name[2] : name[0]))
21335 {
21336 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21337 name);
21338 return TRUE;
21339 }
21340 /* Don't allow hiding a function. When "v" is not NULL we might be
21341 * assigning another function to the same var, the type is checked
21342 * below. */
21343 if (new_var && function_exists(name))
21344 {
21345 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21346 name);
21347 return TRUE;
21348 }
21349 return FALSE;
21350}
21351
21352/*
21353 * Check if a variable name is valid.
21354 * Return FALSE and give an error if not.
21355 */
21356 static int
21357valid_varname(varname)
21358 char_u *varname;
21359{
21360 char_u *p;
21361
21362 for (p = varname; *p != NUL; ++p)
21363 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21364 && *p != AUTOLOAD_CHAR)
21365 {
21366 EMSG2(_(e_illvar), varname);
21367 return FALSE;
21368 }
21369 return TRUE;
21370}
21371
21372/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021373 * Return TRUE if typeval "tv" is set to be locked (immutable).
21374 * Also give an error message, using "name".
21375 */
21376 static int
21377tv_check_lock(lock, name)
21378 int lock;
21379 char_u *name;
21380{
21381 if (lock & VAR_LOCKED)
21382 {
21383 EMSG2(_("E741: Value is locked: %s"),
21384 name == NULL ? (char_u *)_("Unknown") : name);
21385 return TRUE;
21386 }
21387 if (lock & VAR_FIXED)
21388 {
21389 EMSG2(_("E742: Cannot change value of %s"),
21390 name == NULL ? (char_u *)_("Unknown") : name);
21391 return TRUE;
21392 }
21393 return FALSE;
21394}
21395
21396/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021397 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021398 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021399 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021400 * It is OK for "from" and "to" to point to the same item. This is used to
21401 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021402 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021403 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 typval_T *from;
21406 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021408 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021409 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021410 switch (from->v_type)
21411 {
21412 case VAR_NUMBER:
21413 to->vval.v_number = from->vval.v_number;
21414 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021415#ifdef FEAT_FLOAT
21416 case VAR_FLOAT:
21417 to->vval.v_float = from->vval.v_float;
21418 break;
21419#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021420 case VAR_STRING:
21421 case VAR_FUNC:
21422 if (from->vval.v_string == NULL)
21423 to->vval.v_string = NULL;
21424 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021425 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021426 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021427 if (from->v_type == VAR_FUNC)
21428 func_ref(to->vval.v_string);
21429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 break;
21431 case VAR_LIST:
21432 if (from->vval.v_list == NULL)
21433 to->vval.v_list = NULL;
21434 else
21435 {
21436 to->vval.v_list = from->vval.v_list;
21437 ++to->vval.v_list->lv_refcount;
21438 }
21439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021440 case VAR_DICT:
21441 if (from->vval.v_dict == NULL)
21442 to->vval.v_dict = NULL;
21443 else
21444 {
21445 to->vval.v_dict = from->vval.v_dict;
21446 ++to->vval.v_dict->dv_refcount;
21447 }
21448 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021449 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021451 break;
21452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453}
21454
21455/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 * Make a copy of an item.
21457 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021458 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21459 * reference to an already copied list/dict can be used.
21460 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021461 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021462 static int
21463item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021464 typval_T *from;
21465 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021466 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021467 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021468{
21469 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021470 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021471
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021473 {
21474 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021475 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021476 }
21477 ++recurse;
21478
21479 switch (from->v_type)
21480 {
21481 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021482#ifdef FEAT_FLOAT
21483 case VAR_FLOAT:
21484#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021485 case VAR_STRING:
21486 case VAR_FUNC:
21487 copy_tv(from, to);
21488 break;
21489 case VAR_LIST:
21490 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021491 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021492 if (from->vval.v_list == NULL)
21493 to->vval.v_list = NULL;
21494 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21495 {
21496 /* use the copy made earlier */
21497 to->vval.v_list = from->vval.v_list->lv_copylist;
21498 ++to->vval.v_list->lv_refcount;
21499 }
21500 else
21501 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21502 if (to->vval.v_list == NULL)
21503 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021504 break;
21505 case VAR_DICT:
21506 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021507 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021508 if (from->vval.v_dict == NULL)
21509 to->vval.v_dict = NULL;
21510 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21511 {
21512 /* use the copy made earlier */
21513 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21514 ++to->vval.v_dict->dv_refcount;
21515 }
21516 else
21517 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21518 if (to->vval.v_dict == NULL)
21519 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021520 break;
21521 default:
21522 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021523 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021524 }
21525 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021526 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021527}
21528
21529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 * ":echo expr1 ..." print each argument separated with a space, add a
21531 * newline at the end.
21532 * ":echon expr1 ..." print each argument plain.
21533 */
21534 void
21535ex_echo(eap)
21536 exarg_T *eap;
21537{
21538 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021540 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 char_u *p;
21542 int needclr = TRUE;
21543 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545
21546 if (eap->skip)
21547 ++emsg_skip;
21548 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21549 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021550 /* If eval1() causes an error message the text from the command may
21551 * still need to be cleared. E.g., "echo 22,44". */
21552 need_clr_eos = needclr;
21553
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021555 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 {
21557 /*
21558 * Report the invalid expression unless the expression evaluation
21559 * has been cancelled due to an aborting error, an interrupt, or an
21560 * exception.
21561 */
21562 if (!aborting())
21563 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021564 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 break;
21566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021567 need_clr_eos = FALSE;
21568
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 if (!eap->skip)
21570 {
21571 if (atstart)
21572 {
21573 atstart = FALSE;
21574 /* Call msg_start() after eval1(), evaluating the expression
21575 * may cause a message to appear. */
21576 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021577 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021578 /* Mark the saved text as finishing the line, so that what
21579 * follows is displayed on a new line when scrolling back
21580 * at the more prompt. */
21581 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 }
21585 else if (eap->cmdidx == CMD_echo)
21586 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021587 current_copyID += COPYID_INC;
21588 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021589 if (p != NULL)
21590 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021592 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021594 if (*p != TAB && needclr)
21595 {
21596 /* remove any text still there from the command */
21597 msg_clr_eos();
21598 needclr = FALSE;
21599 }
21600 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 }
21602 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021603 {
21604#ifdef FEAT_MBYTE
21605 if (has_mbyte)
21606 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021607 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021608
21609 (void)msg_outtrans_len_attr(p, i, echo_attr);
21610 p += i - 1;
21611 }
21612 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021614 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021617 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 arg = skipwhite(arg);
21621 }
21622 eap->nextcmd = check_nextcmd(arg);
21623
21624 if (eap->skip)
21625 --emsg_skip;
21626 else
21627 {
21628 /* remove text that may still be there from the command */
21629 if (needclr)
21630 msg_clr_eos();
21631 if (eap->cmdidx == CMD_echo)
21632 msg_end();
21633 }
21634}
21635
21636/*
21637 * ":echohl {name}".
21638 */
21639 void
21640ex_echohl(eap)
21641 exarg_T *eap;
21642{
21643 int id;
21644
21645 id = syn_name2id(eap->arg);
21646 if (id == 0)
21647 echo_attr = 0;
21648 else
21649 echo_attr = syn_id2attr(id);
21650}
21651
21652/*
21653 * ":execute expr1 ..." execute the result of an expression.
21654 * ":echomsg expr1 ..." Print a message
21655 * ":echoerr expr1 ..." Print an error
21656 * Each gets spaces around each argument and a newline at the end for
21657 * echo commands
21658 */
21659 void
21660ex_execute(eap)
21661 exarg_T *eap;
21662{
21663 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 int ret = OK;
21666 char_u *p;
21667 garray_T ga;
21668 int len;
21669 int save_did_emsg;
21670
21671 ga_init2(&ga, 1, 80);
21672
21673 if (eap->skip)
21674 ++emsg_skip;
21675 while (*arg != NUL && *arg != '|' && *arg != '\n')
21676 {
21677 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021678 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 {
21680 /*
21681 * Report the invalid expression unless the expression evaluation
21682 * has been cancelled due to an aborting error, an interrupt, or an
21683 * exception.
21684 */
21685 if (!aborting())
21686 EMSG2(_(e_invexpr2), p);
21687 ret = FAIL;
21688 break;
21689 }
21690
21691 if (!eap->skip)
21692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021693 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 len = (int)STRLEN(p);
21695 if (ga_grow(&ga, len + 2) == FAIL)
21696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021697 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 ret = FAIL;
21699 break;
21700 }
21701 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 ga.ga_len += len;
21705 }
21706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 arg = skipwhite(arg);
21709 }
21710
21711 if (ret != FAIL && ga.ga_data != NULL)
21712 {
21713 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021716 out_flush();
21717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 else if (eap->cmdidx == CMD_echoerr)
21719 {
21720 /* We don't want to abort following commands, restore did_emsg. */
21721 save_did_emsg = did_emsg;
21722 EMSG((char_u *)ga.ga_data);
21723 if (!force_abort)
21724 did_emsg = save_did_emsg;
21725 }
21726 else if (eap->cmdidx == CMD_execute)
21727 do_cmdline((char_u *)ga.ga_data,
21728 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21729 }
21730
21731 ga_clear(&ga);
21732
21733 if (eap->skip)
21734 --emsg_skip;
21735
21736 eap->nextcmd = check_nextcmd(arg);
21737}
21738
21739/*
21740 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21741 * "arg" points to the "&" or '+' when called, to "option" when returning.
21742 * Returns NULL when no option name found. Otherwise pointer to the char
21743 * after the option name.
21744 */
21745 static char_u *
21746find_option_end(arg, opt_flags)
21747 char_u **arg;
21748 int *opt_flags;
21749{
21750 char_u *p = *arg;
21751
21752 ++p;
21753 if (*p == 'g' && p[1] == ':')
21754 {
21755 *opt_flags = OPT_GLOBAL;
21756 p += 2;
21757 }
21758 else if (*p == 'l' && p[1] == ':')
21759 {
21760 *opt_flags = OPT_LOCAL;
21761 p += 2;
21762 }
21763 else
21764 *opt_flags = 0;
21765
21766 if (!ASCII_ISALPHA(*p))
21767 return NULL;
21768 *arg = p;
21769
21770 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21771 p += 4; /* termcap option */
21772 else
21773 while (ASCII_ISALPHA(*p))
21774 ++p;
21775 return p;
21776}
21777
21778/*
21779 * ":function"
21780 */
21781 void
21782ex_function(eap)
21783 exarg_T *eap;
21784{
21785 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021786 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 int j;
21788 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021790 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 char_u *name = NULL;
21792 char_u *p;
21793 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021794 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 garray_T newargs;
21796 garray_T newlines;
21797 int varargs = FALSE;
21798 int mustend = FALSE;
21799 int flags = 0;
21800 ufunc_T *fp;
21801 int indent;
21802 int nesting;
21803 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021804 dictitem_T *v;
21805 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021806 static int func_nr = 0; /* number for nameless function */
21807 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021808 hashtab_T *ht;
21809 int todo;
21810 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021811 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812
21813 /*
21814 * ":function" without argument: list functions.
21815 */
21816 if (ends_excmd(*eap->arg))
21817 {
21818 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021819 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021820 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021821 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021822 {
21823 if (!HASHITEM_EMPTY(hi))
21824 {
21825 --todo;
21826 fp = HI2UF(hi);
21827 if (!isdigit(*fp->uf_name))
21828 list_func_head(fp, FALSE);
21829 }
21830 }
21831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 eap->nextcmd = check_nextcmd(eap->arg);
21833 return;
21834 }
21835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021836 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021837 * ":function /pat": list functions matching pattern.
21838 */
21839 if (*eap->arg == '/')
21840 {
21841 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21842 if (!eap->skip)
21843 {
21844 regmatch_T regmatch;
21845
21846 c = *p;
21847 *p = NUL;
21848 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21849 *p = c;
21850 if (regmatch.regprog != NULL)
21851 {
21852 regmatch.rm_ic = p_ic;
21853
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021854 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021855 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21856 {
21857 if (!HASHITEM_EMPTY(hi))
21858 {
21859 --todo;
21860 fp = HI2UF(hi);
21861 if (!isdigit(*fp->uf_name)
21862 && vim_regexec(&regmatch, fp->uf_name, 0))
21863 list_func_head(fp, FALSE);
21864 }
21865 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021866 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021867 }
21868 }
21869 if (*p == '/')
21870 ++p;
21871 eap->nextcmd = check_nextcmd(p);
21872 return;
21873 }
21874
21875 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021876 * Get the function name. There are these situations:
21877 * func normal function name
21878 * "name" == func, "fudi.fd_dict" == NULL
21879 * dict.func new dictionary entry
21880 * "name" == NULL, "fudi.fd_dict" set,
21881 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21882 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021883 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021884 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21885 * dict.func existing dict entry that's not a Funcref
21886 * "name" == NULL, "fudi.fd_dict" set,
21887 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021888 * s:func script-local function name
21889 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021892 name = trans_function_name(&p, eap->skip, 0, &fudi);
21893 paren = (vim_strchr(p, '(') != NULL);
21894 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 {
21896 /*
21897 * Return on an invalid expression in braces, unless the expression
21898 * evaluation has been cancelled due to an aborting error, an
21899 * interrupt, or an exception.
21900 */
21901 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021902 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021903 if (!eap->skip && fudi.fd_newkey != NULL)
21904 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021905 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 else
21909 eap->skip = TRUE;
21910 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021911
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 /* An error in a function call during evaluation of an expression in magic
21913 * braces should not cause the function not to be defined. */
21914 saved_did_emsg = did_emsg;
21915 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916
21917 /*
21918 * ":function func" with only function name: list function.
21919 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021920 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 {
21922 if (!ends_excmd(*skipwhite(p)))
21923 {
21924 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021925 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 }
21927 eap->nextcmd = check_nextcmd(p);
21928 if (eap->nextcmd != NULL)
21929 *p = NUL;
21930 if (!eap->skip && !got_int)
21931 {
21932 fp = find_func(name);
21933 if (fp != NULL)
21934 {
21935 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021936 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021938 if (FUNCLINE(fp, j) == NULL)
21939 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940 msg_putchar('\n');
21941 msg_outnum((long)(j + 1));
21942 if (j < 9)
21943 msg_putchar(' ');
21944 if (j < 99)
21945 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021946 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 out_flush(); /* show a line at a time */
21948 ui_breakcheck();
21949 }
21950 if (!got_int)
21951 {
21952 msg_putchar('\n');
21953 msg_puts((char_u *)" endfunction");
21954 }
21955 }
21956 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021957 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021959 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 }
21961
21962 /*
21963 * ":function name(arg1, arg2)" Define function.
21964 */
21965 p = skipwhite(p);
21966 if (*p != '(')
21967 {
21968 if (!eap->skip)
21969 {
21970 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021971 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 }
21973 /* attempt to continue by skipping some text */
21974 if (vim_strchr(p, '(') != NULL)
21975 p = vim_strchr(p, '(');
21976 }
21977 p = skipwhite(p + 1);
21978
21979 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21980 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21981
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021982 if (!eap->skip)
21983 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021984 /* Check the name of the function. Unless it's a dictionary function
21985 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021986 if (name != NULL)
21987 arg = name;
21988 else
21989 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021990 if (arg != NULL && (fudi.fd_di == NULL
21991 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021992 {
21993 if (*arg == K_SPECIAL)
21994 j = 3;
21995 else
21996 j = 0;
21997 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21998 : eval_isnamec(arg[j])))
21999 ++j;
22000 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022001 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022002 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022003 /* Disallow using the g: dict. */
22004 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22005 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022006 }
22007
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 /*
22009 * Isolate the arguments: "arg1, arg2, ...)"
22010 */
22011 while (*p != ')')
22012 {
22013 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22014 {
22015 varargs = TRUE;
22016 p += 3;
22017 mustend = TRUE;
22018 }
22019 else
22020 {
22021 arg = p;
22022 while (ASCII_ISALNUM(*p) || *p == '_')
22023 ++p;
22024 if (arg == p || isdigit(*arg)
22025 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22026 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22027 {
22028 if (!eap->skip)
22029 EMSG2(_("E125: Illegal argument: %s"), arg);
22030 break;
22031 }
22032 if (ga_grow(&newargs, 1) == FAIL)
22033 goto erret;
22034 c = *p;
22035 *p = NUL;
22036 arg = vim_strsave(arg);
22037 if (arg == NULL)
22038 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022039
22040 /* Check for duplicate argument name. */
22041 for (i = 0; i < newargs.ga_len; ++i)
22042 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22043 {
22044 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022045 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022046 goto erret;
22047 }
22048
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22050 *p = c;
22051 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 if (*p == ',')
22053 ++p;
22054 else
22055 mustend = TRUE;
22056 }
22057 p = skipwhite(p);
22058 if (mustend && *p != ')')
22059 {
22060 if (!eap->skip)
22061 EMSG2(_(e_invarg2), eap->arg);
22062 break;
22063 }
22064 }
22065 ++p; /* skip the ')' */
22066
Bram Moolenaare9a41262005-01-15 22:18:47 +000022067 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 for (;;)
22069 {
22070 p = skipwhite(p);
22071 if (STRNCMP(p, "range", 5) == 0)
22072 {
22073 flags |= FC_RANGE;
22074 p += 5;
22075 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022076 else if (STRNCMP(p, "dict", 4) == 0)
22077 {
22078 flags |= FC_DICT;
22079 p += 4;
22080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 else if (STRNCMP(p, "abort", 5) == 0)
22082 {
22083 flags |= FC_ABORT;
22084 p += 5;
22085 }
22086 else
22087 break;
22088 }
22089
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022090 /* When there is a line break use what follows for the function body.
22091 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22092 if (*p == '\n')
22093 line_arg = p + 1;
22094 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 EMSG(_(e_trailing));
22096
22097 /*
22098 * Read the body of the function, until ":endfunction" is found.
22099 */
22100 if (KeyTyped)
22101 {
22102 /* Check if the function already exists, don't let the user type the
22103 * whole function before telling him it doesn't work! For a script we
22104 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022105 if (!eap->skip && !eap->forceit)
22106 {
22107 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22108 EMSG(_(e_funcdict));
22109 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022110 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022113 if (!eap->skip && did_emsg)
22114 goto erret;
22115
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 msg_putchar('\n'); /* don't overwrite the function name */
22117 cmdline_row = msg_row;
22118 }
22119
22120 indent = 2;
22121 nesting = 0;
22122 for (;;)
22123 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022124 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022125 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022126 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022127 saved_wait_return = FALSE;
22128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022130 sourcing_lnum_off = sourcing_lnum;
22131
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022132 if (line_arg != NULL)
22133 {
22134 /* Use eap->arg, split up in parts by line breaks. */
22135 theline = line_arg;
22136 p = vim_strchr(theline, '\n');
22137 if (p == NULL)
22138 line_arg += STRLEN(line_arg);
22139 else
22140 {
22141 *p = NUL;
22142 line_arg = p + 1;
22143 }
22144 }
22145 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 theline = getcmdline(':', 0L, indent);
22147 else
22148 theline = eap->getline(':', eap->cookie, indent);
22149 if (KeyTyped)
22150 lines_left = Rows - 1;
22151 if (theline == NULL)
22152 {
22153 EMSG(_("E126: Missing :endfunction"));
22154 goto erret;
22155 }
22156
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022157 /* Detect line continuation: sourcing_lnum increased more than one. */
22158 if (sourcing_lnum > sourcing_lnum_off + 1)
22159 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22160 else
22161 sourcing_lnum_off = 0;
22162
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 if (skip_until != NULL)
22164 {
22165 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22166 * don't check for ":endfunc". */
22167 if (STRCMP(theline, skip_until) == 0)
22168 {
22169 vim_free(skip_until);
22170 skip_until = NULL;
22171 }
22172 }
22173 else
22174 {
22175 /* skip ':' and blanks*/
22176 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22177 ;
22178
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022179 /* Check for "endfunction". */
22180 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022182 if (line_arg == NULL)
22183 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 break;
22185 }
22186
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022187 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 * at "end". */
22189 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22190 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022191 else if (STRNCMP(p, "if", 2) == 0
22192 || STRNCMP(p, "wh", 2) == 0
22193 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 || STRNCMP(p, "try", 3) == 0)
22195 indent += 2;
22196
22197 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022198 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022200 if (*p == '!')
22201 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 p += eval_fname_script(p);
22203 if (ASCII_ISALPHA(*p))
22204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022205 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 if (*skipwhite(p) == '(')
22207 {
22208 ++nesting;
22209 indent += 2;
22210 }
22211 }
22212 }
22213
22214 /* Check for ":append" or ":insert". */
22215 p = skip_range(p, NULL);
22216 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22217 || (p[0] == 'i'
22218 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22219 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22220 skip_until = vim_strsave((char_u *)".");
22221
22222 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22223 arg = skipwhite(skiptowhite(p));
22224 if (arg[0] == '<' && arg[1] =='<'
22225 && ((p[0] == 'p' && p[1] == 'y'
22226 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22227 || (p[0] == 'p' && p[1] == 'e'
22228 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22229 || (p[0] == 't' && p[1] == 'c'
22230 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022231 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22232 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22234 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022235 || (p[0] == 'm' && p[1] == 'z'
22236 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 ))
22238 {
22239 /* ":python <<" continues until a dot, like ":append" */
22240 p = skipwhite(arg + 2);
22241 if (*p == NUL)
22242 skip_until = vim_strsave((char_u *)".");
22243 else
22244 skip_until = vim_strsave(p);
22245 }
22246 }
22247
22248 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022249 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022250 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022251 if (line_arg == NULL)
22252 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022254 }
22255
22256 /* Copy the line to newly allocated memory. get_one_sourceline()
22257 * allocates 250 bytes per line, this saves 80% on average. The cost
22258 * is an extra alloc/free. */
22259 p = vim_strsave(theline);
22260 if (p != NULL)
22261 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022262 if (line_arg == NULL)
22263 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022264 theline = p;
22265 }
22266
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022267 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22268
22269 /* Add NULL lines for continuation lines, so that the line count is
22270 * equal to the index in the growarray. */
22271 while (sourcing_lnum_off-- > 0)
22272 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022273
22274 /* Check for end of eap->arg. */
22275 if (line_arg != NULL && *line_arg == NUL)
22276 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 }
22278
22279 /* Don't define the function when skipping commands or when an error was
22280 * detected. */
22281 if (eap->skip || did_emsg)
22282 goto erret;
22283
22284 /*
22285 * If there are no errors, add the function
22286 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022287 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022289 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022291 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022292 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022293 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022294 goto erret;
22295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022297 fp = find_func(name);
22298 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022300 if (!eap->forceit)
22301 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022302 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022303 goto erret;
22304 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022305 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022307 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022308 name);
22309 goto erret;
22310 }
22311 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022312 ga_clear_strings(&(fp->uf_args));
22313 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022314 vim_free(name);
22315 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 }
22318 else
22319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022320 char numbuf[20];
22321
22322 fp = NULL;
22323 if (fudi.fd_newkey == NULL && !eap->forceit)
22324 {
22325 EMSG(_(e_funcdict));
22326 goto erret;
22327 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022328 if (fudi.fd_di == NULL)
22329 {
22330 /* Can't add a function to a locked dictionary */
22331 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22332 goto erret;
22333 }
22334 /* Can't change an existing function if it is locked */
22335 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22336 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022337
22338 /* Give the function a sequential number. Can only be used with a
22339 * Funcref! */
22340 vim_free(name);
22341 sprintf(numbuf, "%d", ++func_nr);
22342 name = vim_strsave((char_u *)numbuf);
22343 if (name == NULL)
22344 goto erret;
22345 }
22346
22347 if (fp == NULL)
22348 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022349 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022350 {
22351 int slen, plen;
22352 char_u *scriptname;
22353
22354 /* Check that the autoload name matches the script name. */
22355 j = FAIL;
22356 if (sourcing_name != NULL)
22357 {
22358 scriptname = autoload_name(name);
22359 if (scriptname != NULL)
22360 {
22361 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022362 plen = (int)STRLEN(p);
22363 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022364 if (slen > plen && fnamecmp(p,
22365 sourcing_name + slen - plen) == 0)
22366 j = OK;
22367 vim_free(scriptname);
22368 }
22369 }
22370 if (j == FAIL)
22371 {
22372 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22373 goto erret;
22374 }
22375 }
22376
22377 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 if (fp == NULL)
22379 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022380
22381 if (fudi.fd_dict != NULL)
22382 {
22383 if (fudi.fd_di == NULL)
22384 {
22385 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022386 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 if (fudi.fd_di == NULL)
22388 {
22389 vim_free(fp);
22390 goto erret;
22391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022392 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22393 {
22394 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022395 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022396 goto erret;
22397 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022398 }
22399 else
22400 /* overwrite existing dict entry */
22401 clear_tv(&fudi.fd_di->di_tv);
22402 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022403 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022404 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022405 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022406
22407 /* behave like "dict" was used */
22408 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 }
22410
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022412 STRCPY(fp->uf_name, name);
22413 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022415 fp->uf_args = newargs;
22416 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022417#ifdef FEAT_PROFILE
22418 fp->uf_tml_count = NULL;
22419 fp->uf_tml_total = NULL;
22420 fp->uf_tml_self = NULL;
22421 fp->uf_profiling = FALSE;
22422 if (prof_def_func())
22423 func_do_profile(fp);
22424#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022425 fp->uf_varargs = varargs;
22426 fp->uf_flags = flags;
22427 fp->uf_calls = 0;
22428 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022429 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430
22431erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 ga_clear_strings(&newargs);
22433 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022434ret_free:
22435 vim_free(skip_until);
22436 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022439 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440}
22441
22442/*
22443 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022444 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022446 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022447 * TFN_INT: internal function name OK
22448 * TFN_QUIET: be quiet
22449 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 * Advances "pp" to just after the function name (if no error).
22451 */
22452 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022453trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 char_u **pp;
22455 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022456 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022459 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 char_u *start;
22461 char_u *end;
22462 int lead;
22463 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466
22467 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022470
22471 /* Check for hard coded <SNR>: already translated function ID (from a user
22472 * command). */
22473 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22474 && (*pp)[2] == (int)KE_SNR)
22475 {
22476 *pp += 3;
22477 len = get_id_len(pp) + 3;
22478 return vim_strnsave(start, len);
22479 }
22480
22481 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22482 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022484 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022486
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022487 /* Note that TFN_ flags use the same values as GLV_ flags. */
22488 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022489 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022490 if (end == start)
22491 {
22492 if (!skip)
22493 EMSG(_("E129: Function name required"));
22494 goto theend;
22495 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022496 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022497 {
22498 /*
22499 * Report an invalid expression in braces, unless the expression
22500 * evaluation has been cancelled due to an aborting error, an
22501 * interrupt, or an exception.
22502 */
22503 if (!aborting())
22504 {
22505 if (end != NULL)
22506 EMSG2(_(e_invarg2), start);
22507 }
22508 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022509 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022510 goto theend;
22511 }
22512
22513 if (lv.ll_tv != NULL)
22514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022515 if (fdp != NULL)
22516 {
22517 fdp->fd_dict = lv.ll_dict;
22518 fdp->fd_newkey = lv.ll_newkey;
22519 lv.ll_newkey = NULL;
22520 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022522 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22523 {
22524 name = vim_strsave(lv.ll_tv->vval.v_string);
22525 *pp = end;
22526 }
22527 else
22528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022529 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22530 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022531 EMSG(_(e_funcref));
22532 else
22533 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022534 name = NULL;
22535 }
22536 goto theend;
22537 }
22538
22539 if (lv.ll_name == NULL)
22540 {
22541 /* Error found, but continue after the function name. */
22542 *pp = end;
22543 goto theend;
22544 }
22545
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022546 /* Check if the name is a Funcref. If so, use the value. */
22547 if (lv.ll_exp_name != NULL)
22548 {
22549 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022550 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022551 if (name == lv.ll_exp_name)
22552 name = NULL;
22553 }
22554 else
22555 {
22556 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022557 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022558 if (name == *pp)
22559 name = NULL;
22560 }
22561 if (name != NULL)
22562 {
22563 name = vim_strsave(name);
22564 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022565 if (STRNCMP(name, "<SNR>", 5) == 0)
22566 {
22567 /* Change "<SNR>" to the byte sequence. */
22568 name[0] = K_SPECIAL;
22569 name[1] = KS_EXTRA;
22570 name[2] = (int)KE_SNR;
22571 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22572 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022573 goto theend;
22574 }
22575
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022576 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022577 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022578 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022579 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22580 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22581 {
22582 /* When there was "s:" already or the name expanded to get a
22583 * leading "s:" then remove it. */
22584 lv.ll_name += 2;
22585 len -= 2;
22586 lead = 2;
22587 }
22588 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022589 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022590 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022591 /* skip over "s:" and "g:" */
22592 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022593 lv.ll_name += 2;
22594 len = (int)(end - lv.ll_name);
22595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022596
22597 /*
22598 * Copy the function name to allocated memory.
22599 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22600 * Accept <SNR>123_name() outside a script.
22601 */
22602 if (skip)
22603 lead = 0; /* do nothing */
22604 else if (lead > 0)
22605 {
22606 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022607 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22608 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022609 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022610 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022611 if (current_SID <= 0)
22612 {
22613 EMSG(_(e_usingsid));
22614 goto theend;
22615 }
22616 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22617 lead += (int)STRLEN(sid_buf);
22618 }
22619 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022620 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022621 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022622 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022623 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022624 goto theend;
22625 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022626 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022627 {
22628 char_u *cp = vim_strchr(lv.ll_name, ':');
22629
22630 if (cp != NULL && cp < end)
22631 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022632 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022633 goto theend;
22634 }
22635 }
22636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022637 name = alloc((unsigned)(len + lead + 1));
22638 if (name != NULL)
22639 {
22640 if (lead > 0)
22641 {
22642 name[0] = K_SPECIAL;
22643 name[1] = KS_EXTRA;
22644 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022645 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022646 STRCPY(name + 3, sid_buf);
22647 }
22648 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022649 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022650 }
22651 *pp = end;
22652
22653theend:
22654 clear_lval(&lv);
22655 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656}
22657
22658/*
22659 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22660 * Return 2 if "p" starts with "s:".
22661 * Return 0 otherwise.
22662 */
22663 static int
22664eval_fname_script(p)
22665 char_u *p;
22666{
22667 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22668 || STRNICMP(p + 1, "SNR>", 4) == 0))
22669 return 5;
22670 if (p[0] == 's' && p[1] == ':')
22671 return 2;
22672 return 0;
22673}
22674
22675/*
22676 * Return TRUE if "p" starts with "<SID>" or "s:".
22677 * Only works if eval_fname_script() returned non-zero for "p"!
22678 */
22679 static int
22680eval_fname_sid(p)
22681 char_u *p;
22682{
22683 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22684}
22685
22686/*
22687 * List the head of the function: "name(arg1, arg2)".
22688 */
22689 static void
22690list_func_head(fp, indent)
22691 ufunc_T *fp;
22692 int indent;
22693{
22694 int j;
22695
22696 msg_start();
22697 if (indent)
22698 MSG_PUTS(" ");
22699 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022700 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 {
22702 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022703 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 }
22705 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022706 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022708 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 {
22710 if (j)
22711 MSG_PUTS(", ");
22712 msg_puts(FUNCARG(fp, j));
22713 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022714 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 {
22716 if (j)
22717 MSG_PUTS(", ");
22718 MSG_PUTS("...");
22719 }
22720 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022721 if (fp->uf_flags & FC_ABORT)
22722 MSG_PUTS(" abort");
22723 if (fp->uf_flags & FC_RANGE)
22724 MSG_PUTS(" range");
22725 if (fp->uf_flags & FC_DICT)
22726 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022727 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022728 if (p_verbose > 0)
22729 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730}
22731
22732/*
22733 * Find a function by name, return pointer to it in ufuncs.
22734 * Return NULL for unknown function.
22735 */
22736 static ufunc_T *
22737find_func(name)
22738 char_u *name;
22739{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022740 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022742 hi = hash_find(&func_hashtab, name);
22743 if (!HASHITEM_EMPTY(hi))
22744 return HI2UF(hi);
22745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746}
22747
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022748#if defined(EXITFREE) || defined(PROTO)
22749 void
22750free_all_functions()
22751{
22752 hashitem_T *hi;
22753
22754 /* Need to start all over every time, because func_free() may change the
22755 * hash table. */
22756 while (func_hashtab.ht_used > 0)
22757 for (hi = func_hashtab.ht_array; ; ++hi)
22758 if (!HASHITEM_EMPTY(hi))
22759 {
22760 func_free(HI2UF(hi));
22761 break;
22762 }
22763}
22764#endif
22765
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022766 int
22767translated_function_exists(name)
22768 char_u *name;
22769{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022770 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022771 return find_internal_func(name) >= 0;
22772 return find_func(name) != NULL;
22773}
22774
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022775/*
22776 * Return TRUE if a function "name" exists.
22777 */
22778 static int
22779function_exists(name)
22780 char_u *name;
22781{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022782 char_u *nm = name;
22783 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022784 int n = FALSE;
22785
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022786 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22787 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022788 nm = skipwhite(nm);
22789
22790 /* Only accept "funcname", "funcname ", "funcname (..." and
22791 * "funcname(...", not "funcname!...". */
22792 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022793 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022794 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795 return n;
22796}
22797
Bram Moolenaara1544c02013-05-30 12:35:52 +020022798 char_u *
22799get_expanded_name(name, check)
22800 char_u *name;
22801 int check;
22802{
22803 char_u *nm = name;
22804 char_u *p;
22805
22806 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22807
22808 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022809 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022810 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022811
Bram Moolenaara1544c02013-05-30 12:35:52 +020022812 vim_free(p);
22813 return NULL;
22814}
22815
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022816/*
22817 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022818 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22819 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022820 */
22821 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022822builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022823 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022824 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022825{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022826 char_u *p;
22827
22828 if (!ASCII_ISLOWER(name[0]))
22829 return FALSE;
22830 p = vim_strchr(name, AUTOLOAD_CHAR);
22831 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022832}
22833
Bram Moolenaar05159a02005-02-26 23:04:13 +000022834#if defined(FEAT_PROFILE) || defined(PROTO)
22835/*
22836 * Start profiling function "fp".
22837 */
22838 static void
22839func_do_profile(fp)
22840 ufunc_T *fp;
22841{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022842 int len = fp->uf_lines.ga_len;
22843
22844 if (len == 0)
22845 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022846 fp->uf_tm_count = 0;
22847 profile_zero(&fp->uf_tm_self);
22848 profile_zero(&fp->uf_tm_total);
22849 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022850 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022851 if (fp->uf_tml_total == NULL)
22852 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022853 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022854 if (fp->uf_tml_self == NULL)
22855 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022856 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022857 fp->uf_tml_idx = -1;
22858 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22859 || fp->uf_tml_self == NULL)
22860 return; /* out of memory */
22861
22862 fp->uf_profiling = TRUE;
22863}
22864
22865/*
22866 * Dump the profiling results for all functions in file "fd".
22867 */
22868 void
22869func_dump_profile(fd)
22870 FILE *fd;
22871{
22872 hashitem_T *hi;
22873 int todo;
22874 ufunc_T *fp;
22875 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022876 ufunc_T **sorttab;
22877 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022879 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022880 if (todo == 0)
22881 return; /* nothing to dump */
22882
Bram Moolenaar73830342005-02-28 22:48:19 +000022883 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22884
Bram Moolenaar05159a02005-02-26 23:04:13 +000022885 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22886 {
22887 if (!HASHITEM_EMPTY(hi))
22888 {
22889 --todo;
22890 fp = HI2UF(hi);
22891 if (fp->uf_profiling)
22892 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022893 if (sorttab != NULL)
22894 sorttab[st_len++] = fp;
22895
Bram Moolenaar05159a02005-02-26 23:04:13 +000022896 if (fp->uf_name[0] == K_SPECIAL)
22897 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22898 else
22899 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22900 if (fp->uf_tm_count == 1)
22901 fprintf(fd, "Called 1 time\n");
22902 else
22903 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22904 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22905 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22906 fprintf(fd, "\n");
22907 fprintf(fd, "count total (s) self (s)\n");
22908
22909 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22910 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022911 if (FUNCLINE(fp, i) == NULL)
22912 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022913 prof_func_line(fd, fp->uf_tml_count[i],
22914 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22916 }
22917 fprintf(fd, "\n");
22918 }
22919 }
22920 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022921
22922 if (sorttab != NULL && st_len > 0)
22923 {
22924 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22925 prof_total_cmp);
22926 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22927 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22928 prof_self_cmp);
22929 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22930 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022931
22932 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022933}
Bram Moolenaar73830342005-02-28 22:48:19 +000022934
22935 static void
22936prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22937 FILE *fd;
22938 ufunc_T **sorttab;
22939 int st_len;
22940 char *title;
22941 int prefer_self; /* when equal print only self time */
22942{
22943 int i;
22944 ufunc_T *fp;
22945
22946 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22947 fprintf(fd, "count total (s) self (s) function\n");
22948 for (i = 0; i < 20 && i < st_len; ++i)
22949 {
22950 fp = sorttab[i];
22951 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22952 prefer_self);
22953 if (fp->uf_name[0] == K_SPECIAL)
22954 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22955 else
22956 fprintf(fd, " %s()\n", fp->uf_name);
22957 }
22958 fprintf(fd, "\n");
22959}
22960
22961/*
22962 * Print the count and times for one function or function line.
22963 */
22964 static void
22965prof_func_line(fd, count, total, self, prefer_self)
22966 FILE *fd;
22967 int count;
22968 proftime_T *total;
22969 proftime_T *self;
22970 int prefer_self; /* when equal print only self time */
22971{
22972 if (count > 0)
22973 {
22974 fprintf(fd, "%5d ", count);
22975 if (prefer_self && profile_equal(total, self))
22976 fprintf(fd, " ");
22977 else
22978 fprintf(fd, "%s ", profile_msg(total));
22979 if (!prefer_self && profile_equal(total, self))
22980 fprintf(fd, " ");
22981 else
22982 fprintf(fd, "%s ", profile_msg(self));
22983 }
22984 else
22985 fprintf(fd, " ");
22986}
22987
22988/*
22989 * Compare function for total time sorting.
22990 */
22991 static int
22992#ifdef __BORLANDC__
22993_RTLENTRYF
22994#endif
22995prof_total_cmp(s1, s2)
22996 const void *s1;
22997 const void *s2;
22998{
22999 ufunc_T *p1, *p2;
23000
23001 p1 = *(ufunc_T **)s1;
23002 p2 = *(ufunc_T **)s2;
23003 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23004}
23005
23006/*
23007 * Compare function for self time sorting.
23008 */
23009 static int
23010#ifdef __BORLANDC__
23011_RTLENTRYF
23012#endif
23013prof_self_cmp(s1, s2)
23014 const void *s1;
23015 const void *s2;
23016{
23017 ufunc_T *p1, *p2;
23018
23019 p1 = *(ufunc_T **)s1;
23020 p2 = *(ufunc_T **)s2;
23021 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23022}
23023
Bram Moolenaar05159a02005-02-26 23:04:13 +000023024#endif
23025
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023026/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023027 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023028 * Return TRUE if a package was loaded.
23029 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023030 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023031script_autoload(name, reload)
23032 char_u *name;
23033 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023034{
23035 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023036 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023037 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023038 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023039
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023040 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023041 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023042 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023043 return FALSE;
23044
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023045 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023046
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023047 /* Find the name in the list of previously loaded package names. Skip
23048 * "autoload/", it's always the same. */
23049 for (i = 0; i < ga_loaded.ga_len; ++i)
23050 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23051 break;
23052 if (!reload && i < ga_loaded.ga_len)
23053 ret = FALSE; /* was loaded already */
23054 else
23055 {
23056 /* Remember the name if it wasn't loaded already. */
23057 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23058 {
23059 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23060 tofree = NULL;
23061 }
23062
23063 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023064 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023065 ret = TRUE;
23066 }
23067
23068 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023069 return ret;
23070}
23071
23072/*
23073 * Return the autoload script name for a function or variable name.
23074 * Returns NULL when out of memory.
23075 */
23076 static char_u *
23077autoload_name(name)
23078 char_u *name;
23079{
23080 char_u *p;
23081 char_u *scriptname;
23082
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023083 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023084 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23085 if (scriptname == NULL)
23086 return FALSE;
23087 STRCPY(scriptname, "autoload/");
23088 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023089 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023090 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023091 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023092 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023093 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023094}
23095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23097
23098/*
23099 * Function given to ExpandGeneric() to obtain the list of user defined
23100 * function names.
23101 */
23102 char_u *
23103get_user_func_name(xp, idx)
23104 expand_T *xp;
23105 int idx;
23106{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023107 static long_u done;
23108 static hashitem_T *hi;
23109 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110
23111 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023113 done = 0;
23114 hi = func_hashtab.ht_array;
23115 }
23116 if (done < func_hashtab.ht_used)
23117 {
23118 if (done++ > 0)
23119 ++hi;
23120 while (HASHITEM_EMPTY(hi))
23121 ++hi;
23122 fp = HI2UF(hi);
23123
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023124 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023125 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023126
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023127 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23128 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129
23130 cat_func_name(IObuff, fp);
23131 if (xp->xp_context != EXPAND_USER_FUNC)
23132 {
23133 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023134 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 STRCAT(IObuff, ")");
23136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 return IObuff;
23138 }
23139 return NULL;
23140}
23141
23142#endif /* FEAT_CMDL_COMPL */
23143
23144/*
23145 * Copy the function name of "fp" to buffer "buf".
23146 * "buf" must be able to hold the function name plus three bytes.
23147 * Takes care of script-local function names.
23148 */
23149 static void
23150cat_func_name(buf, fp)
23151 char_u *buf;
23152 ufunc_T *fp;
23153{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023154 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 {
23156 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023157 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 }
23159 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023160 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161}
23162
23163/*
23164 * ":delfunction {name}"
23165 */
23166 void
23167ex_delfunction(eap)
23168 exarg_T *eap;
23169{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023170 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 char_u *p;
23172 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023173 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174
23175 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023176 name = trans_function_name(&p, eap->skip, 0, &fudi);
23177 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023179 {
23180 if (fudi.fd_dict != NULL && !eap->skip)
23181 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 if (!ends_excmd(*skipwhite(p)))
23185 {
23186 vim_free(name);
23187 EMSG(_(e_trailing));
23188 return;
23189 }
23190 eap->nextcmd = check_nextcmd(p);
23191 if (eap->nextcmd != NULL)
23192 *p = NUL;
23193
23194 if (!eap->skip)
23195 fp = find_func(name);
23196 vim_free(name);
23197
23198 if (!eap->skip)
23199 {
23200 if (fp == NULL)
23201 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023202 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 return;
23204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023205 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 {
23207 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23208 return;
23209 }
23210
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023211 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023213 /* Delete the dict item that refers to the function, it will
23214 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023215 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 else
23218 func_free(fp);
23219 }
23220}
23221
23222/*
23223 * Free a function and remove it from the list of functions.
23224 */
23225 static void
23226func_free(fp)
23227 ufunc_T *fp;
23228{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023229 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023230
23231 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023232 ga_clear_strings(&(fp->uf_args));
23233 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023234#ifdef FEAT_PROFILE
23235 vim_free(fp->uf_tml_count);
23236 vim_free(fp->uf_tml_total);
23237 vim_free(fp->uf_tml_self);
23238#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023240 /* remove the function from the function hashtable */
23241 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23242 if (HASHITEM_EMPTY(hi))
23243 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023244 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023245 hash_remove(&func_hashtab, hi);
23246
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 vim_free(fp);
23248}
23249
23250/*
23251 * Unreference a Function: decrement the reference count and free it when it
23252 * becomes zero. Only for numbered functions.
23253 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023254 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023255func_unref(name)
23256 char_u *name;
23257{
23258 ufunc_T *fp;
23259
23260 if (name != NULL && isdigit(*name))
23261 {
23262 fp = find_func(name);
23263 if (fp == NULL)
23264 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023265 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023266 {
23267 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023268 * when "uf_calls" becomes zero. */
23269 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270 func_free(fp);
23271 }
23272 }
23273}
23274
23275/*
23276 * Count a reference to a Function.
23277 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023278 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023279func_ref(name)
23280 char_u *name;
23281{
23282 ufunc_T *fp;
23283
23284 if (name != NULL && isdigit(*name))
23285 {
23286 fp = find_func(name);
23287 if (fp == NULL)
23288 EMSG2(_(e_intern2), "func_ref()");
23289 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023290 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 }
23292}
23293
23294/*
23295 * Call a user function.
23296 */
23297 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023298call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 ufunc_T *fp; /* pointer to function */
23300 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 typval_T *argvars; /* arguments */
23302 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 linenr_T firstline; /* first line of range */
23304 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023305 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306{
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 char_u *save_sourcing_name;
23308 linenr_T save_sourcing_lnum;
23309 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023310 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 int save_did_emsg;
23312 static int depth = 0;
23313 dictitem_T *v;
23314 int fixvar_idx = 0; /* index in fixvar[] */
23315 int i;
23316 int ai;
23317 char_u numbuf[NUMBUFLEN];
23318 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023319#ifdef FEAT_PROFILE
23320 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023321 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323
23324 /* If depth of calling is getting too high, don't execute the function */
23325 if (depth >= p_mfd)
23326 {
23327 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023328 rettv->v_type = VAR_NUMBER;
23329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 return;
23331 }
23332 ++depth;
23333
23334 line_breakcheck(); /* check for CTRL-C hit */
23335
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023336 fc = (funccall_T *)alloc(sizeof(funccall_T));
23337 fc->caller = current_funccal;
23338 current_funccal = fc;
23339 fc->func = fp;
23340 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023341 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023342 fc->linenr = 0;
23343 fc->returned = FALSE;
23344 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023346 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23347 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348
Bram Moolenaar33570922005-01-25 22:26:29 +000023349 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023350 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023351 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23352 * each argument variable and saves a lot of time.
23353 */
23354 /*
23355 * Init l: variables.
23356 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023357 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023358 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023359 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023360 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23361 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023362 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023363 name = v->di_key;
23364 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023365 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023366 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023367 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023368 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023369 v->di_tv.vval.v_dict = selfdict;
23370 ++selfdict->dv_refcount;
23371 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023372
Bram Moolenaar33570922005-01-25 22:26:29 +000023373 /*
23374 * Init a: variables.
23375 * Set a:0 to "argcount".
23376 * Set a:000 to a list with room for the "..." arguments.
23377 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023378 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023379 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023380 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023381 /* Use "name" to avoid a warning from some compiler that checks the
23382 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023383 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023384 name = v->di_key;
23385 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023386 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023387 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023388 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023389 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023390 v->di_tv.vval.v_list = &fc->l_varlist;
23391 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23392 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23393 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023394
23395 /*
23396 * Set a:firstline to "firstline" and a:lastline to "lastline".
23397 * Set a:name to named arguments.
23398 * Set a:N to the "..." arguments.
23399 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023400 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023401 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023402 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023403 (varnumber_T)lastline);
23404 for (i = 0; i < argcount; ++i)
23405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023406 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023407 if (ai < 0)
23408 /* named argument a:name */
23409 name = FUNCARG(fp, i);
23410 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023412 /* "..." argument a:1, a:2, etc. */
23413 sprintf((char *)numbuf, "%d", ai + 1);
23414 name = numbuf;
23415 }
23416 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23417 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023418 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023419 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23420 }
23421 else
23422 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023423 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23424 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023425 if (v == NULL)
23426 break;
23427 v->di_flags = DI_FLAGS_RO;
23428 }
23429 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023430 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023431
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023432 /* Note: the values are copied directly to avoid alloc/free.
23433 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023435 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023436
23437 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23438 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023439 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23440 fc->l_listitems[ai].li_tv = argvars[i];
23441 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023442 }
23443 }
23444
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 /* Don't redraw while executing the function. */
23446 ++RedrawingDisabled;
23447 save_sourcing_name = sourcing_name;
23448 save_sourcing_lnum = sourcing_lnum;
23449 sourcing_lnum = 1;
23450 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023451 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 if (sourcing_name != NULL)
23453 {
23454 if (save_sourcing_name != NULL
23455 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23456 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23457 else
23458 STRCPY(sourcing_name, "function ");
23459 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23460
23461 if (p_verbose >= 12)
23462 {
23463 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023464 verbose_enter_scroll();
23465
Bram Moolenaar555b2802005-05-19 21:08:39 +000023466 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 if (p_verbose >= 14)
23468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023470 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023471 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023472 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473
23474 msg_puts((char_u *)"(");
23475 for (i = 0; i < argcount; ++i)
23476 {
23477 if (i > 0)
23478 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023479 if (argvars[i].v_type == VAR_NUMBER)
23480 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 else
23482 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023483 /* Do not want errors such as E724 here. */
23484 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023485 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023486 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023487 if (s != NULL)
23488 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023489 if (vim_strsize(s) > MSG_BUF_CLEN)
23490 {
23491 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23492 s = buf;
23493 }
23494 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023495 vim_free(tofree);
23496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 }
23498 }
23499 msg_puts((char_u *)")");
23500 }
23501 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023502
23503 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 --no_wait_return;
23505 }
23506 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023507#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023508 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023509 {
23510 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23511 func_do_profile(fp);
23512 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023513 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023514 {
23515 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023516 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517 profile_zero(&fp->uf_tm_children);
23518 }
23519 script_prof_save(&wait_start);
23520 }
23521#endif
23522
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023524 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 save_did_emsg = did_emsg;
23526 did_emsg = FALSE;
23527
23528 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023529 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23531
23532 --RedrawingDisabled;
23533
23534 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023537 clear_tv(rettv);
23538 rettv->v_type = VAR_NUMBER;
23539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 }
23541
Bram Moolenaar05159a02005-02-26 23:04:13 +000023542#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023543 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023544 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023545 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023546 profile_end(&call_start);
23547 profile_sub_wait(&wait_start, &call_start);
23548 profile_add(&fp->uf_tm_total, &call_start);
23549 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023550 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023551 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023552 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23553 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023554 }
23555 }
23556#endif
23557
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 /* when being verbose, mention the return value */
23559 if (p_verbose >= 12)
23560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023562 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023565 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023566 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023567 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023568 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023571 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023572 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023573 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023574 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023575
Bram Moolenaar555b2802005-05-19 21:08:39 +000023576 /* The value may be very long. Skip the middle part, so that we
23577 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023578 * truncate it at the end. Don't want errors such as E724 here. */
23579 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023580 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023581 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023582 if (s != NULL)
23583 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023584 if (vim_strsize(s) > MSG_BUF_CLEN)
23585 {
23586 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23587 s = buf;
23588 }
23589 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023590 vim_free(tofree);
23591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 }
23593 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023594
23595 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 --no_wait_return;
23597 }
23598
23599 vim_free(sourcing_name);
23600 sourcing_name = save_sourcing_name;
23601 sourcing_lnum = save_sourcing_lnum;
23602 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023604 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023605 script_prof_restore(&wait_start);
23606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607
23608 if (p_verbose >= 12 && sourcing_name != NULL)
23609 {
23610 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023611 verbose_enter_scroll();
23612
Bram Moolenaar555b2802005-05-19 21:08:39 +000023613 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023615
23616 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 --no_wait_return;
23618 }
23619
23620 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023621 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023623
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023624 /* If the a:000 list and the l: and a: dicts are not referenced we can
23625 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023626 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23627 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23628 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23629 {
23630 free_funccal(fc, FALSE);
23631 }
23632 else
23633 {
23634 hashitem_T *hi;
23635 listitem_T *li;
23636 int todo;
23637
23638 /* "fc" is still in use. This can happen when returning "a:000" or
23639 * assigning "l:" to a global variable.
23640 * Link "fc" in the list for garbage collection later. */
23641 fc->caller = previous_funccal;
23642 previous_funccal = fc;
23643
23644 /* Make a copy of the a: variables, since we didn't do that above. */
23645 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23646 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23647 {
23648 if (!HASHITEM_EMPTY(hi))
23649 {
23650 --todo;
23651 v = HI2DI(hi);
23652 copy_tv(&v->di_tv, &v->di_tv);
23653 }
23654 }
23655
23656 /* Make a copy of the a:000 items, since we didn't do that above. */
23657 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23658 copy_tv(&li->li_tv, &li->li_tv);
23659 }
23660}
23661
23662/*
23663 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023664 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023665 */
23666 static int
23667can_free_funccal(fc, copyID)
23668 funccall_T *fc;
23669 int copyID;
23670{
23671 return (fc->l_varlist.lv_copyID != copyID
23672 && fc->l_vars.dv_copyID != copyID
23673 && fc->l_avars.dv_copyID != copyID);
23674}
23675
23676/*
23677 * Free "fc" and what it contains.
23678 */
23679 static void
23680free_funccal(fc, free_val)
23681 funccall_T *fc;
23682 int free_val; /* a: vars were allocated */
23683{
23684 listitem_T *li;
23685
23686 /* The a: variables typevals may not have been allocated, only free the
23687 * allocated variables. */
23688 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23689
23690 /* free all l: variables */
23691 vars_clear(&fc->l_vars.dv_hashtab);
23692
23693 /* Free the a:000 variables if they were allocated. */
23694 if (free_val)
23695 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23696 clear_tv(&li->li_tv);
23697
23698 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699}
23700
23701/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023702 * Add a number variable "name" to dict "dp" with value "nr".
23703 */
23704 static void
23705add_nr_var(dp, v, name, nr)
23706 dict_T *dp;
23707 dictitem_T *v;
23708 char *name;
23709 varnumber_T nr;
23710{
23711 STRCPY(v->di_key, name);
23712 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23713 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23714 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023715 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023716 v->di_tv.vval.v_number = nr;
23717}
23718
23719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 * ":return [expr]"
23721 */
23722 void
23723ex_return(eap)
23724 exarg_T *eap;
23725{
23726 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023727 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 int returning = FALSE;
23729
23730 if (current_funccal == NULL)
23731 {
23732 EMSG(_("E133: :return not inside a function"));
23733 return;
23734 }
23735
23736 if (eap->skip)
23737 ++emsg_skip;
23738
23739 eap->nextcmd = NULL;
23740 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023741 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 {
23743 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023744 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 }
23748 /* It's safer to return also on error. */
23749 else if (!eap->skip)
23750 {
23751 /*
23752 * Return unless the expression evaluation has been cancelled due to an
23753 * aborting error, an interrupt, or an exception.
23754 */
23755 if (!aborting())
23756 returning = do_return(eap, FALSE, TRUE, NULL);
23757 }
23758
23759 /* When skipping or the return gets pending, advance to the next command
23760 * in this line (!returning). Otherwise, ignore the rest of the line.
23761 * Following lines will be ignored by get_func_line(). */
23762 if (returning)
23763 eap->nextcmd = NULL;
23764 else if (eap->nextcmd == NULL) /* no argument */
23765 eap->nextcmd = check_nextcmd(arg);
23766
23767 if (eap->skip)
23768 --emsg_skip;
23769}
23770
23771/*
23772 * Return from a function. Possibly makes the return pending. Also called
23773 * for a pending return at the ":endtry" or after returning from an extra
23774 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023775 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023776 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 * FALSE when the return gets pending.
23778 */
23779 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023780do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 exarg_T *eap;
23782 int reanimate;
23783 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023784 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785{
23786 int idx;
23787 struct condstack *cstack = eap->cstack;
23788
23789 if (reanimate)
23790 /* Undo the return. */
23791 current_funccal->returned = FALSE;
23792
23793 /*
23794 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23795 * not in its finally clause (which then is to be executed next) is found.
23796 * In this case, make the ":return" pending for execution at the ":endtry".
23797 * Otherwise, return normally.
23798 */
23799 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23800 if (idx >= 0)
23801 {
23802 cstack->cs_pending[idx] = CSTP_RETURN;
23803
23804 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023805 /* A pending return again gets pending. "rettv" points to an
23806 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023808 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 else
23810 {
23811 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023812 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023814 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023816 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 {
23818 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023819 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023820 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 else
23822 EMSG(_(e_outofmem));
23823 }
23824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023825 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826
23827 if (reanimate)
23828 {
23829 /* The pending return value could be overwritten by a ":return"
23830 * without argument in a finally clause; reset the default
23831 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023832 current_funccal->rettv->v_type = VAR_NUMBER;
23833 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 }
23835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023836 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 }
23838 else
23839 {
23840 current_funccal->returned = TRUE;
23841
23842 /* If the return is carried out now, store the return value. For
23843 * a return immediately after reanimation, the value is already
23844 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023845 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023847 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023848 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023850 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 }
23852 }
23853
23854 return idx < 0;
23855}
23856
23857/*
23858 * Free the variable with a pending return value.
23859 */
23860 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023861discard_pending_return(rettv)
23862 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863{
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865}
23866
23867/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023868 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 * is an allocated string. Used by report_pending() for verbose messages.
23870 */
23871 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023872get_return_cmd(rettv)
23873 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023875 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023876 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023877 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023879 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023880 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023881 if (s == NULL)
23882 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023883
23884 STRCPY(IObuff, ":return ");
23885 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23886 if (STRLEN(s) + 8 >= IOSIZE)
23887 STRCPY(IObuff + IOSIZE - 4, "...");
23888 vim_free(tofree);
23889 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890}
23891
23892/*
23893 * Get next function line.
23894 * Called by do_cmdline() to get the next line.
23895 * Returns allocated string, or NULL for end of function.
23896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 char_u *
23898get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023899 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023901 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902{
Bram Moolenaar33570922005-01-25 22:26:29 +000023903 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023904 ufunc_T *fp = fcp->func;
23905 char_u *retval;
23906 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907
23908 /* If breakpoints have been added/deleted need to check for it. */
23909 if (fcp->dbg_tick != debug_tick)
23910 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023911 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 sourcing_lnum);
23913 fcp->dbg_tick = debug_tick;
23914 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023915#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023916 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023917 func_line_end(cookie);
23918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919
Bram Moolenaar05159a02005-02-26 23:04:13 +000023920 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023921 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23922 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 retval = NULL;
23924 else
23925 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023926 /* Skip NULL lines (continuation lines). */
23927 while (fcp->linenr < gap->ga_len
23928 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23929 ++fcp->linenr;
23930 if (fcp->linenr >= gap->ga_len)
23931 retval = NULL;
23932 else
23933 {
23934 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23935 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023936#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023937 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023938 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 }
23942
23943 /* Did we encounter a breakpoint? */
23944 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23945 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023946 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023948 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 sourcing_lnum);
23950 fcp->dbg_tick = debug_tick;
23951 }
23952
23953 return retval;
23954}
23955
Bram Moolenaar05159a02005-02-26 23:04:13 +000023956#if defined(FEAT_PROFILE) || defined(PROTO)
23957/*
23958 * Called when starting to read a function line.
23959 * "sourcing_lnum" must be correct!
23960 * When skipping lines it may not actually be executed, but we won't find out
23961 * until later and we need to store the time now.
23962 */
23963 void
23964func_line_start(cookie)
23965 void *cookie;
23966{
23967 funccall_T *fcp = (funccall_T *)cookie;
23968 ufunc_T *fp = fcp->func;
23969
23970 if (fp->uf_profiling && sourcing_lnum >= 1
23971 && sourcing_lnum <= fp->uf_lines.ga_len)
23972 {
23973 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023974 /* Skip continuation lines. */
23975 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23976 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977 fp->uf_tml_execed = FALSE;
23978 profile_start(&fp->uf_tml_start);
23979 profile_zero(&fp->uf_tml_children);
23980 profile_get_wait(&fp->uf_tml_wait);
23981 }
23982}
23983
23984/*
23985 * Called when actually executing a function line.
23986 */
23987 void
23988func_line_exec(cookie)
23989 void *cookie;
23990{
23991 funccall_T *fcp = (funccall_T *)cookie;
23992 ufunc_T *fp = fcp->func;
23993
23994 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23995 fp->uf_tml_execed = TRUE;
23996}
23997
23998/*
23999 * Called when done with a function line.
24000 */
24001 void
24002func_line_end(cookie)
24003 void *cookie;
24004{
24005 funccall_T *fcp = (funccall_T *)cookie;
24006 ufunc_T *fp = fcp->func;
24007
24008 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24009 {
24010 if (fp->uf_tml_execed)
24011 {
24012 ++fp->uf_tml_count[fp->uf_tml_idx];
24013 profile_end(&fp->uf_tml_start);
24014 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024015 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024016 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24017 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024018 }
24019 fp->uf_tml_idx = -1;
24020 }
24021}
24022#endif
24023
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024/*
24025 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024026 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 */
24028 int
24029func_has_ended(cookie)
24030 void *cookie;
24031{
Bram Moolenaar33570922005-01-25 22:26:29 +000024032 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033
24034 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24035 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024036 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 || fcp->returned);
24038}
24039
24040/*
24041 * return TRUE if cookie indicates a function which "abort"s on errors.
24042 */
24043 int
24044func_has_abort(cookie)
24045 void *cookie;
24046{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048}
24049
24050#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24051typedef enum
24052{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024053 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24054 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24055 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056} var_flavour_T;
24057
24058static var_flavour_T var_flavour __ARGS((char_u *varname));
24059
24060 static var_flavour_T
24061var_flavour(varname)
24062 char_u *varname;
24063{
24064 char_u *p = varname;
24065
24066 if (ASCII_ISUPPER(*p))
24067 {
24068 while (*(++p))
24069 if (ASCII_ISLOWER(*p))
24070 return VAR_FLAVOUR_SESSION;
24071 return VAR_FLAVOUR_VIMINFO;
24072 }
24073 else
24074 return VAR_FLAVOUR_DEFAULT;
24075}
24076#endif
24077
24078#if defined(FEAT_VIMINFO) || defined(PROTO)
24079/*
24080 * Restore global vars that start with a capital from the viminfo file
24081 */
24082 int
24083read_viminfo_varlist(virp, writing)
24084 vir_T *virp;
24085 int writing;
24086{
24087 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024088 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090
24091 if (!writing && (find_viminfo_parameter('!') != NULL))
24092 {
24093 tab = vim_strchr(virp->vir_line + 1, '\t');
24094 if (tab != NULL)
24095 {
24096 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024097 switch (*tab)
24098 {
24099 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024100#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024101 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024102#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024103 case 'D': type = VAR_DICT; break;
24104 case 'L': type = VAR_LIST; break;
24105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106
24107 tab = vim_strchr(tab, '\t');
24108 if (tab != NULL)
24109 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024110 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024111 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024112 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024114#ifdef FEAT_FLOAT
24115 else if (type == VAR_FLOAT)
24116 (void)string2float(tab + 1, &tv.vval.v_float);
24117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024119 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024120 if (type == VAR_DICT || type == VAR_LIST)
24121 {
24122 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24123
24124 if (etv == NULL)
24125 /* Failed to parse back the dict or list, use it as a
24126 * string. */
24127 tv.v_type = VAR_STRING;
24128 else
24129 {
24130 vim_free(tv.vval.v_string);
24131 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024132 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024133 }
24134 }
24135
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024136 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024137
24138 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024139 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024140 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 }
24143 }
24144 }
24145
24146 return viminfo_readline(virp);
24147}
24148
24149/*
24150 * Write global vars that start with a capital to the viminfo file
24151 */
24152 void
24153write_viminfo_varlist(fp)
24154 FILE *fp;
24155{
Bram Moolenaar33570922005-01-25 22:26:29 +000024156 hashitem_T *hi;
24157 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024158 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024159 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024160 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024162 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163
24164 if (find_viminfo_parameter('!') == NULL)
24165 return;
24166
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024167 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024168
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024169 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024174 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024175 this_var = HI2DI(hi);
24176 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024179 {
24180 case VAR_STRING: s = "STR"; break;
24181 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024182#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024183 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024184#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024185 case VAR_DICT: s = "DIC"; break;
24186 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024187 default: continue;
24188 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024189 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024190 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024191 if (p != NULL)
24192 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024193 vim_free(tofree);
24194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 }
24196 }
24197}
24198#endif
24199
24200#if defined(FEAT_SESSION) || defined(PROTO)
24201 int
24202store_session_globals(fd)
24203 FILE *fd;
24204{
Bram Moolenaar33570922005-01-25 22:26:29 +000024205 hashitem_T *hi;
24206 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024207 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 char_u *p, *t;
24209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024210 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024211 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024213 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024215 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024216 this_var = HI2DI(hi);
24217 if ((this_var->di_tv.v_type == VAR_NUMBER
24218 || this_var->di_tv.v_type == VAR_STRING)
24219 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024220 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024221 /* Escape special characters with a backslash. Turn a LF and
24222 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024224 (char_u *)"\\\"\n\r");
24225 if (p == NULL) /* out of memory */
24226 break;
24227 for (t = p; *t != NUL; ++t)
24228 if (*t == '\n')
24229 *t = 'n';
24230 else if (*t == '\r')
24231 *t = 'r';
24232 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024233 this_var->di_key,
24234 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24235 : ' ',
24236 p,
24237 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24238 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024239 || put_eol(fd) == FAIL)
24240 {
24241 vim_free(p);
24242 return FAIL;
24243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 vim_free(p);
24245 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024246#ifdef FEAT_FLOAT
24247 else if (this_var->di_tv.v_type == VAR_FLOAT
24248 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24249 {
24250 float_T f = this_var->di_tv.vval.v_float;
24251 int sign = ' ';
24252
24253 if (f < 0)
24254 {
24255 f = -f;
24256 sign = '-';
24257 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024258 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024259 this_var->di_key, sign, f) < 0)
24260 || put_eol(fd) == FAIL)
24261 return FAIL;
24262 }
24263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 }
24265 }
24266 return OK;
24267}
24268#endif
24269
Bram Moolenaar661b1822005-07-28 22:36:45 +000024270/*
24271 * Display script name where an item was last set.
24272 * Should only be invoked when 'verbose' is non-zero.
24273 */
24274 void
24275last_set_msg(scriptID)
24276 scid_T scriptID;
24277{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024278 char_u *p;
24279
Bram Moolenaar661b1822005-07-28 22:36:45 +000024280 if (scriptID != 0)
24281 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024282 p = home_replace_save(NULL, get_scriptname(scriptID));
24283 if (p != NULL)
24284 {
24285 verbose_enter();
24286 MSG_PUTS(_("\n\tLast set from "));
24287 MSG_PUTS(p);
24288 vim_free(p);
24289 verbose_leave();
24290 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024291 }
24292}
24293
Bram Moolenaard812df62008-11-09 12:46:09 +000024294/*
24295 * List v:oldfiles in a nice way.
24296 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024297 void
24298ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024299 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024300{
24301 list_T *l = vimvars[VV_OLDFILES].vv_list;
24302 listitem_T *li;
24303 int nr = 0;
24304
24305 if (l == NULL)
24306 msg((char_u *)_("No old files"));
24307 else
24308 {
24309 msg_start();
24310 msg_scroll = TRUE;
24311 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24312 {
24313 msg_outnum((long)++nr);
24314 MSG_PUTS(": ");
24315 msg_outtrans(get_tv_string(&li->li_tv));
24316 msg_putchar('\n');
24317 out_flush(); /* output one line at a time */
24318 ui_breakcheck();
24319 }
24320 /* Assume "got_int" was set to truncate the listing. */
24321 got_int = FALSE;
24322
24323#ifdef FEAT_BROWSE_CMD
24324 if (cmdmod.browse)
24325 {
24326 quit_more = FALSE;
24327 nr = prompt_for_number(FALSE);
24328 msg_starthere();
24329 if (nr > 0)
24330 {
24331 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24332 (long)nr);
24333
24334 if (p != NULL)
24335 {
24336 p = expand_env_save(p);
24337 eap->arg = p;
24338 eap->cmdidx = CMD_edit;
24339 cmdmod.browse = FALSE;
24340 do_exedit(eap, NULL);
24341 vim_free(p);
24342 }
24343 }
24344 }
24345#endif
24346 }
24347}
24348
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349#endif /* FEAT_EVAL */
24350
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024352#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353
24354#ifdef WIN3264
24355/*
24356 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24357 */
24358static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24359static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24360static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24361
24362/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024363 * Get the short path (8.3) for the filename in "fnamep".
24364 * Only works for a valid file name.
24365 * When the path gets longer "fnamep" is changed and the allocated buffer
24366 * is put in "bufp".
24367 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24368 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 */
24370 static int
24371get_short_pathname(fnamep, bufp, fnamelen)
24372 char_u **fnamep;
24373 char_u **bufp;
24374 int *fnamelen;
24375{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024376 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 char_u *newbuf;
24378
24379 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 l = GetShortPathName(*fnamep, *fnamep, len);
24381 if (l > len - 1)
24382 {
24383 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024384 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 newbuf = vim_strnsave(*fnamep, l);
24386 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388
24389 vim_free(*bufp);
24390 *fnamep = *bufp = newbuf;
24391
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024392 /* Really should always succeed, as the buffer is big enough. */
24393 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 }
24395
24396 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024397 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398}
24399
24400/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024401 * Get the short path (8.3) for the filename in "fname". The converted
24402 * path is returned in "bufp".
24403 *
24404 * Some of the directories specified in "fname" may not exist. This function
24405 * will shorten the existing directories at the beginning of the path and then
24406 * append the remaining non-existing path.
24407 *
24408 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024409 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024410 * bufp - Pointer to an allocated buffer for the filename.
24411 * fnamelen - Length of the filename pointed to by fname
24412 *
24413 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 */
24415 static int
24416shortpath_for_invalid_fname(fname, bufp, fnamelen)
24417 char_u **fname;
24418 char_u **bufp;
24419 int *fnamelen;
24420{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024421 char_u *short_fname, *save_fname, *pbuf_unused;
24422 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024424 int old_len, len;
24425 int new_len, sfx_len;
24426 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427
24428 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024429 old_len = *fnamelen;
24430 save_fname = vim_strnsave(*fname, old_len);
24431 pbuf_unused = NULL;
24432 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024434 endp = save_fname + old_len - 1; /* Find the end of the copy */
24435 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024437 /*
24438 * Try shortening the supplied path till it succeeds by removing one
24439 * directory at a time from the tail of the path.
24440 */
24441 len = 0;
24442 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024444 /* go back one path-separator */
24445 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24446 --endp;
24447 if (endp <= save_fname)
24448 break; /* processed the complete path */
24449
24450 /*
24451 * Replace the path separator with a NUL and try to shorten the
24452 * resulting path.
24453 */
24454 ch = *endp;
24455 *endp = 0;
24456 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024457 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024458 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24459 {
24460 retval = FAIL;
24461 goto theend;
24462 }
24463 *endp = ch; /* preserve the string */
24464
24465 if (len > 0)
24466 break; /* successfully shortened the path */
24467
24468 /* failed to shorten the path. Skip the path separator */
24469 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 }
24471
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024472 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024474 /*
24475 * Succeeded in shortening the path. Now concatenate the shortened
24476 * path with the remaining path at the tail.
24477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024479 /* Compute the length of the new path. */
24480 sfx_len = (int)(save_endp - endp) + 1;
24481 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024483 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024485 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024487 /* There is not enough space in the currently allocated string,
24488 * copy it to a buffer big enough. */
24489 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024491 {
24492 retval = FAIL;
24493 goto theend;
24494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 }
24496 else
24497 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024498 /* Transfer short_fname to the main buffer (it's big enough),
24499 * unless get_short_pathname() did its work in-place. */
24500 *fname = *bufp = save_fname;
24501 if (short_fname != save_fname)
24502 vim_strncpy(save_fname, short_fname, len);
24503 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024505
24506 /* concat the not-shortened part of the path */
24507 vim_strncpy(*fname + len, endp, sfx_len);
24508 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024510
24511theend:
24512 vim_free(pbuf_unused);
24513 vim_free(save_fname);
24514
24515 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516}
24517
24518/*
24519 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024520 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 */
24522 static int
24523shortpath_for_partial(fnamep, bufp, fnamelen)
24524 char_u **fnamep;
24525 char_u **bufp;
24526 int *fnamelen;
24527{
24528 int sepcount, len, tflen;
24529 char_u *p;
24530 char_u *pbuf, *tfname;
24531 int hasTilde;
24532
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024533 /* Count up the path separators from the RHS.. so we know which part
24534 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024536 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 if (vim_ispathsep(*p))
24538 ++sepcount;
24539
24540 /* Need full path first (use expand_env() to remove a "~/") */
24541 hasTilde = (**fnamep == '~');
24542 if (hasTilde)
24543 pbuf = tfname = expand_env_save(*fnamep);
24544 else
24545 pbuf = tfname = FullName_save(*fnamep, FALSE);
24546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024547 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024549 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551
24552 if (len == 0)
24553 {
24554 /* Don't have a valid filename, so shorten the rest of the
24555 * path if we can. This CAN give us invalid 8.3 filenames, but
24556 * there's not a lot of point in guessing what it might be.
24557 */
24558 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024559 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24560 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 }
24562
24563 /* Count the paths backward to find the beginning of the desired string. */
24564 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024565 {
24566#ifdef FEAT_MBYTE
24567 if (has_mbyte)
24568 p -= mb_head_off(tfname, p);
24569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 if (vim_ispathsep(*p))
24571 {
24572 if (sepcount == 0 || (hasTilde && sepcount == 1))
24573 break;
24574 else
24575 sepcount --;
24576 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 if (hasTilde)
24579 {
24580 --p;
24581 if (p >= tfname)
24582 *p = '~';
24583 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 }
24586 else
24587 ++p;
24588
24589 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24590 vim_free(*bufp);
24591 *fnamelen = (int)STRLEN(p);
24592 *bufp = pbuf;
24593 *fnamep = p;
24594
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024595 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596}
24597#endif /* WIN3264 */
24598
24599/*
24600 * Adjust a filename, according to a string of modifiers.
24601 * *fnamep must be NUL terminated when called. When returning, the length is
24602 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024603 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 * When there is an error, *fnamep is set to NULL.
24605 */
24606 int
24607modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24608 char_u *src; /* string with modifiers */
24609 int *usedlen; /* characters after src that are used */
24610 char_u **fnamep; /* file name so far */
24611 char_u **bufp; /* buffer for allocated file name or NULL */
24612 int *fnamelen; /* length of fnamep */
24613{
24614 int valid = 0;
24615 char_u *tail;
24616 char_u *s, *p, *pbuf;
24617 char_u dirname[MAXPATHL];
24618 int c;
24619 int has_fullname = 0;
24620#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024621 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 int has_shortname = 0;
24623#endif
24624
24625repeat:
24626 /* ":p" - full path/file_name */
24627 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24628 {
24629 has_fullname = 1;
24630
24631 valid |= VALID_PATH;
24632 *usedlen += 2;
24633
24634 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24635 if ((*fnamep)[0] == '~'
24636#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24637 && ((*fnamep)[1] == '/'
24638# ifdef BACKSLASH_IN_FILENAME
24639 || (*fnamep)[1] == '\\'
24640# endif
24641 || (*fnamep)[1] == NUL)
24642
24643#endif
24644 )
24645 {
24646 *fnamep = expand_env_save(*fnamep);
24647 vim_free(*bufp); /* free any allocated file name */
24648 *bufp = *fnamep;
24649 if (*fnamep == NULL)
24650 return -1;
24651 }
24652
24653 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024654 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 {
24656 if (vim_ispathsep(*p)
24657 && p[1] == '.'
24658 && (p[2] == NUL
24659 || vim_ispathsep(p[2])
24660 || (p[2] == '.'
24661 && (p[3] == NUL || vim_ispathsep(p[3])))))
24662 break;
24663 }
24664
24665 /* FullName_save() is slow, don't use it when not needed. */
24666 if (*p != NUL || !vim_isAbsName(*fnamep))
24667 {
24668 *fnamep = FullName_save(*fnamep, *p != NUL);
24669 vim_free(*bufp); /* free any allocated file name */
24670 *bufp = *fnamep;
24671 if (*fnamep == NULL)
24672 return -1;
24673 }
24674
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024675#ifdef WIN3264
24676# if _WIN32_WINNT >= 0x0500
24677 if (vim_strchr(*fnamep, '~') != NULL)
24678 {
24679 /* Expand 8.3 filename to full path. Needed to make sure the same
24680 * file does not have two different names.
24681 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24682 p = alloc(_MAX_PATH + 1);
24683 if (p != NULL)
24684 {
24685 if (GetLongPathName(*fnamep, p, MAXPATHL))
24686 {
24687 vim_free(*bufp);
24688 *bufp = *fnamep = p;
24689 }
24690 else
24691 vim_free(p);
24692 }
24693 }
24694# endif
24695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 /* Append a path separator to a directory. */
24697 if (mch_isdir(*fnamep))
24698 {
24699 /* Make room for one or two extra characters. */
24700 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24701 vim_free(*bufp); /* free any allocated file name */
24702 *bufp = *fnamep;
24703 if (*fnamep == NULL)
24704 return -1;
24705 add_pathsep(*fnamep);
24706 }
24707 }
24708
24709 /* ":." - path relative to the current directory */
24710 /* ":~" - path relative to the home directory */
24711 /* ":8" - shortname path - postponed till after */
24712 while (src[*usedlen] == ':'
24713 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24714 {
24715 *usedlen += 2;
24716 if (c == '8')
24717 {
24718#ifdef WIN3264
24719 has_shortname = 1; /* Postpone this. */
24720#endif
24721 continue;
24722 }
24723 pbuf = NULL;
24724 /* Need full path first (use expand_env() to remove a "~/") */
24725 if (!has_fullname)
24726 {
24727 if (c == '.' && **fnamep == '~')
24728 p = pbuf = expand_env_save(*fnamep);
24729 else
24730 p = pbuf = FullName_save(*fnamep, FALSE);
24731 }
24732 else
24733 p = *fnamep;
24734
24735 has_fullname = 0;
24736
24737 if (p != NULL)
24738 {
24739 if (c == '.')
24740 {
24741 mch_dirname(dirname, MAXPATHL);
24742 s = shorten_fname(p, dirname);
24743 if (s != NULL)
24744 {
24745 *fnamep = s;
24746 if (pbuf != NULL)
24747 {
24748 vim_free(*bufp); /* free any allocated file name */
24749 *bufp = pbuf;
24750 pbuf = NULL;
24751 }
24752 }
24753 }
24754 else
24755 {
24756 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24757 /* Only replace it when it starts with '~' */
24758 if (*dirname == '~')
24759 {
24760 s = vim_strsave(dirname);
24761 if (s != NULL)
24762 {
24763 *fnamep = s;
24764 vim_free(*bufp);
24765 *bufp = s;
24766 }
24767 }
24768 }
24769 vim_free(pbuf);
24770 }
24771 }
24772
24773 tail = gettail(*fnamep);
24774 *fnamelen = (int)STRLEN(*fnamep);
24775
24776 /* ":h" - head, remove "/file_name", can be repeated */
24777 /* Don't remove the first "/" or "c:\" */
24778 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24779 {
24780 valid |= VALID_HEAD;
24781 *usedlen += 2;
24782 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024783 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024784 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 *fnamelen = (int)(tail - *fnamep);
24786#ifdef VMS
24787 if (*fnamelen > 0)
24788 *fnamelen += 1; /* the path separator is part of the path */
24789#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024790 if (*fnamelen == 0)
24791 {
24792 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24793 p = vim_strsave((char_u *)".");
24794 if (p == NULL)
24795 return -1;
24796 vim_free(*bufp);
24797 *bufp = *fnamep = tail = p;
24798 *fnamelen = 1;
24799 }
24800 else
24801 {
24802 while (tail > s && !after_pathsep(s, tail))
24803 mb_ptr_back(*fnamep, tail);
24804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805 }
24806
24807 /* ":8" - shortname */
24808 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24809 {
24810 *usedlen += 2;
24811#ifdef WIN3264
24812 has_shortname = 1;
24813#endif
24814 }
24815
24816#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024817 /*
24818 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 */
24820 if (has_shortname)
24821 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024822 /* Copy the string if it is shortened by :h and when it wasn't copied
24823 * yet, because we are going to change it in place. Avoids changing
24824 * the buffer name for "%:8". */
24825 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826 {
24827 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024828 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829 return -1;
24830 vim_free(*bufp);
24831 *bufp = *fnamep = p;
24832 }
24833
24834 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024835 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 if (!has_fullname && !vim_isAbsName(*fnamep))
24837 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024838 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 return -1;
24840 }
24841 else
24842 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024843 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844
Bram Moolenaardc935552011-08-17 15:23:23 +020024845 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024847 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 return -1;
24849
24850 if (l == 0)
24851 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024852 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024854 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855 return -1;
24856 }
24857 *fnamelen = l;
24858 }
24859 }
24860#endif /* WIN3264 */
24861
24862 /* ":t" - tail, just the basename */
24863 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24864 {
24865 *usedlen += 2;
24866 *fnamelen -= (int)(tail - *fnamep);
24867 *fnamep = tail;
24868 }
24869
24870 /* ":e" - extension, can be repeated */
24871 /* ":r" - root, without extension, can be repeated */
24872 while (src[*usedlen] == ':'
24873 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24874 {
24875 /* find a '.' in the tail:
24876 * - for second :e: before the current fname
24877 * - otherwise: The last '.'
24878 */
24879 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24880 s = *fnamep - 2;
24881 else
24882 s = *fnamep + *fnamelen - 1;
24883 for ( ; s > tail; --s)
24884 if (s[0] == '.')
24885 break;
24886 if (src[*usedlen + 1] == 'e') /* :e */
24887 {
24888 if (s > tail)
24889 {
24890 *fnamelen += (int)(*fnamep - (s + 1));
24891 *fnamep = s + 1;
24892#ifdef VMS
24893 /* cut version from the extension */
24894 s = *fnamep + *fnamelen - 1;
24895 for ( ; s > *fnamep; --s)
24896 if (s[0] == ';')
24897 break;
24898 if (s > *fnamep)
24899 *fnamelen = s - *fnamep;
24900#endif
24901 }
24902 else if (*fnamep <= tail)
24903 *fnamelen = 0;
24904 }
24905 else /* :r */
24906 {
24907 if (s > tail) /* remove one extension */
24908 *fnamelen = (int)(s - *fnamep);
24909 }
24910 *usedlen += 2;
24911 }
24912
24913 /* ":s?pat?foo?" - substitute */
24914 /* ":gs?pat?foo?" - global substitute */
24915 if (src[*usedlen] == ':'
24916 && (src[*usedlen + 1] == 's'
24917 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24918 {
24919 char_u *str;
24920 char_u *pat;
24921 char_u *sub;
24922 int sep;
24923 char_u *flags;
24924 int didit = FALSE;
24925
24926 flags = (char_u *)"";
24927 s = src + *usedlen + 2;
24928 if (src[*usedlen + 1] == 'g')
24929 {
24930 flags = (char_u *)"g";
24931 ++s;
24932 }
24933
24934 sep = *s++;
24935 if (sep)
24936 {
24937 /* find end of pattern */
24938 p = vim_strchr(s, sep);
24939 if (p != NULL)
24940 {
24941 pat = vim_strnsave(s, (int)(p - s));
24942 if (pat != NULL)
24943 {
24944 s = p + 1;
24945 /* find end of substitution */
24946 p = vim_strchr(s, sep);
24947 if (p != NULL)
24948 {
24949 sub = vim_strnsave(s, (int)(p - s));
24950 str = vim_strnsave(*fnamep, *fnamelen);
24951 if (sub != NULL && str != NULL)
24952 {
24953 *usedlen = (int)(p + 1 - src);
24954 s = do_string_sub(str, pat, sub, flags);
24955 if (s != NULL)
24956 {
24957 *fnamep = s;
24958 *fnamelen = (int)STRLEN(s);
24959 vim_free(*bufp);
24960 *bufp = s;
24961 didit = TRUE;
24962 }
24963 }
24964 vim_free(sub);
24965 vim_free(str);
24966 }
24967 vim_free(pat);
24968 }
24969 }
24970 /* after using ":s", repeat all the modifiers */
24971 if (didit)
24972 goto repeat;
24973 }
24974 }
24975
Bram Moolenaar26df0922014-02-23 23:39:13 +010024976 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24977 {
24978 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24979 if (p == NULL)
24980 return -1;
24981 vim_free(*bufp);
24982 *bufp = *fnamep = p;
24983 *fnamelen = (int)STRLEN(p);
24984 *usedlen += 2;
24985 }
24986
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 return valid;
24988}
24989
24990/*
24991 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24992 * "flags" can be "g" to do a global substitute.
24993 * Returns an allocated string, NULL for error.
24994 */
24995 char_u *
24996do_string_sub(str, pat, sub, flags)
24997 char_u *str;
24998 char_u *pat;
24999 char_u *sub;
25000 char_u *flags;
25001{
25002 int sublen;
25003 regmatch_T regmatch;
25004 int i;
25005 int do_all;
25006 char_u *tail;
25007 garray_T ga;
25008 char_u *ret;
25009 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025010 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011
25012 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25013 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025014 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015
25016 ga_init2(&ga, 1, 200);
25017
25018 do_all = (flags[0] == 'g');
25019
25020 regmatch.rm_ic = p_ic;
25021 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25022 if (regmatch.regprog != NULL)
25023 {
25024 tail = str;
25025 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25026 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025027 /* Skip empty match except for first match. */
25028 if (regmatch.startp[0] == regmatch.endp[0])
25029 {
25030 if (zero_width == regmatch.startp[0])
25031 {
25032 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025033 i = MB_PTR2LEN(tail);
25034 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25035 (size_t)i);
25036 ga.ga_len += i;
25037 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025038 continue;
25039 }
25040 zero_width = regmatch.startp[0];
25041 }
25042
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 /*
25044 * Get some space for a temporary buffer to do the substitution
25045 * into. It will contain:
25046 * - The text up to where the match is.
25047 * - The substituted text.
25048 * - The text after the match.
25049 */
25050 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
25051 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
25052 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25053 {
25054 ga_clear(&ga);
25055 break;
25056 }
25057
25058 /* copy the text up to where the match is */
25059 i = (int)(regmatch.startp[0] - tail);
25060 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25061 /* add the substituted text */
25062 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25063 + ga.ga_len + i, TRUE, TRUE, FALSE);
25064 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025065 tail = regmatch.endp[0];
25066 if (*tail == NUL)
25067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 if (!do_all)
25069 break;
25070 }
25071
25072 if (ga.ga_data != NULL)
25073 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25074
Bram Moolenaar473de612013-06-08 18:19:48 +020025075 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 }
25077
25078 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25079 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025080 if (p_cpo == empty_option)
25081 p_cpo = save_cpo;
25082 else
25083 /* Darn, evaluating {sub} expression changed the value. */
25084 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085
25086 return ret;
25087}
25088
25089#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */